var GT = {title: 'General Things Stuff' };

GT.Tools = {};
GT.Tools.FitInside = function (object, container, scale) {
	var obj = jQuery(object);
	var con = jQuery(container);
	var arObj = obj.width() / obj.height();
	var arCon = con.width() / con.height();
	if (arObj > arCon) {
		if (obj.width() > con.width() || scale) 
		{
			obj.width(con.width());
			obj.height(con.width() / arObj);
		}
	}
	else if (obj.height() > con.height() || scale) {
		obj.width(con.height() *arObj);
		obj.height(con.height());
	}
}

GT.Animations = {};
GT.Animations.init = function(me, id, options) {
	me.doOptions(options);
	me.container = jQuery(id);
	me.container.css('overflow', 'hidden');
	if (!me.container.css('position'))
		me.container.css('position', 'relative');
	if (!me.container) return false;
	me.loadedImages = new Array;
	me.currentImage = -1;
}

GT.Animations.loadImage = function(url, options) {
	if( typeof(options) != 'object') options = {};
	var image = jQuery('<img src="' + url + '" />');
	image.css('position', options.position ? options.position : "relative");
	if (options.width)
		image.width(options.width);
	if (options.height)
		image.height(options.height);
	image.css('top', options.top ? options.top : "0px");
	image.css('left', options.left ? options.left : "0px");
	image.css('display', options.display ? options.display : 'block');
	image.css('opacity', typeof(options.opacity) == 'number' ? options.opacity : 1);
	image.css('z-index', typeof(options.zIndex) == 'number' ? options.zIndex : 1);
	return image;
}

GT.Animations.loadImageURLs = function (me, options) {
	if (!me.options.url) return false;
	
	jQuery.ajax({'url': me.options.url,
			'dataType': 'text',
			'complete': function(res) {
					list = res.responseText.split(',');
					for(var x in list) {
						var image = GT.Animations.loadImage(list[x].replace(/\s*/, ''), options);
						image.bind('load', {obj: me}, function(event) { 
							GT.Tools.FitInside(event.target, event.data.obj.container, true);
							event.data.obj.loadedImages.push(jQuery(event.target));
						});
						me.container.append(image);
					}
				}
		});
	
	return true;
}

GT.SlidingBox = function(id) {
	GT.Animations.init(this, id, arguments[1]);
	
	if (GT.Animations.loadImageURLs(this, {'position': 'absolute', 'left': (this.container.width() + 5) + 'px'}))
		GT.SlidingBox.doSwitch(this);
};

GT.SlidingBox.prototype.doOptions = function(options) {
	this.options = {};
	this.options.url = false;
	this.options.interval = 5000;
	this.options.speed = 1000;
	
	if (typeof(options) == 'object') for (var x in options) this.options[x] = options[x];
}

GT.SlidingBox.doSwitch = function(obj) {
	if (!obj.loadedImages.length || (obj.currentImage != -1 && obj.loadedImages.length == 1)) {
		setTimeout(function() { GT.SlidingBox.doSwitch(obj) }, 100);
		return;
	}
	var idx = obj.currentImage;
	while(idx == obj.currentImage) idx = Math.floor(Math.random()*obj.loadedImages.length);
	
	if (obj.currentImage != -1) {
		obj.loadedImages[obj.currentImage].animate( {left: '-' + (obj.container.width() + 5) + 'px'},
									{duration: obj.options.speed, easing: 'easeInOutCubic', complete: function() { jQuery(this).css('left', (obj.container.width() + 5) +'px')}});				
		obj.loadedImages[idx].animate( {left: '0px'},
									{duration: obj.options.speed, easing: 'easeInOutCubic', complete: function() { obj.currentImage = idx; }});
	} else {
		obj.loadedImages[idx].css('left', '0px');
		obj.currentImage = idx;
	}
									
	setTimeout(function() { GT.SlidingBox.doSwitch(obj) }, obj.options.interval);
}

GT.FadingBox = function(id) {
	GT.Animations.init(this, id, arguments[1]);
	
	if (GT.Animations.loadImageURLs(this, {'position': 'absolute', 'opacity': '0'}))
		GT.FadingBox.doSwitch(this);
};

GT.FadingBox.prototype.doOptions = function(options) {
	this.options = {};
	this.options.url = false;
	this.options.interval = 5000;
	this.options.speed = 1000;
	
	if (typeof(options) == 'object') for (var x in options) this.options[x] = options[x];
}

GT.FadingBox.doSwitch = function(obj) {
	if (!obj.loadedImages.length || (obj.currentImage != -1 && obj.loadedImages.length == 1)) {
		setTimeout(function() { GT.FadingBox.doSwitch(obj) }, 100);
		return;
	}
	var idx = obj.currentImage;
	while(idx == obj.currentImage) idx = Math.floor(Math.random()*obj.loadedImages.length);
	
	if (obj.currentImage != -1) {
		obj.loadedImages[obj.currentImage].animate( {'opacity': 0},
									{duration: obj.options.speed});				
		obj.loadedImages[idx].animate( {'opacity': 1},
									{duration: obj.options.speed, complete: function() { obj.currentImage = idx; }});
	} else {
		obj.loadedImages[idx].css('opacity', 1);
		obj.currentImage = idx;
	}
									
	setTimeout(function() { GT.FadingBox.doSwitch(obj) }, obj.options.interval);
}

GT.ImageChooseBox = function(con) {	
	if (!jQuery) return;
	this.con = jQuery(con);
	if (!this.con.css('position') || this.con.css('position') == 'static')
		this.con.css('position', 'relative');
		
	this.left = jQuery('<DIV class="gt-imagechoosebox-left"></DIV>');
	this.right = jQuery('<DIV class="gt-imagechoosebox-right"></DIV>');
	this.left.css('position', 'absolute');
	this.right.css('position', 'absolute');
	
	this.current = -1;
	
	if (arguments.length > 1) {
		this.options = arguments[1];
		if (arguments[1].json)
			this.doJSON(arguments[1].json);
		else
			this.finishInit();
	}
	
}

GT.ImageChooseBox.prototype.doJSON = function(url) {
	var me = this;
	jQuery.getJSON(url, {}, function(json) { me.finishInit(json); });
}

GT.ImageChooseBox.prototype.finishInit = function() {
	// Load up all the options.

	if (arguments[0])
		for(var i in arguments[0])
			this.options[i] = arguments[0][i];
	
	var options = this.options;
	this.options = { 'width': this.con.width(), 'height': this.con.height(), 'left': .35};
	for(var i in options) this.options[i] = options[i];
	if (typeof(this.options.data) != 'object') return;
	
	this.data = new Array();
	for (var i in this.options.data)
		if (this.options.data[i].text)
			this.data.push(this.options.data[i]);

	if (this.data.length == 0) return;
	
	this.left.css({'left': '0px', 'width': (this.options.left * this.options.width) + 'px'});
	this.right.css({'left': (this.options.left * this.options.width) + 'px', 
					'width': ((1-this.options.left) * this.options.width) + 'px'});

				
	// These are the indicators. They do things, like indicate which row we are on.
	this.indicators = false;
	if (typeof(this.options.indicator) == 'string') {
		this.indicators = {'start': GT.Animations.loadImage(this.options.indicator, {'opacity': 0, 'position': 'absolute', "zIndex": 15}), 
		'finish': GT.Animations.loadImage(this.options.indicator, {'opacity': 0, 'position': 'absolute', "zIndex": 15})};
		this.indicators.start.attr('class', 'gt-imagechoosebox-indicator');
		if(navigator.userAgent.match(/msie/i))
			this.indicators.start.css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader");
		this.right.append(this.indicators.start);
		this.right.append(this.indicators.finish);
	}
		
	// FIrst, we will load up all the text so we can get an idea to the height of the bounding container.
	for (var i = 0; i < this.data.length; i++) {
		// First, set the text up to load.
		var txt = jQuery('<DIV></DIV>');
		txt.html(this.data[i].text);
		this.data[i].text = txt;
		txt.css('cursor', 'pointer');
		if (this.data[i].url) {
			txt.bind('click', {'url': this.data[i].url}, function(event){ window.location = event.data.url; });
		}
		txt.bind('mouseover', {me: this, choice: i}, GT.ImageChooseBox.doSwitch);
		this.left.append(txt);
	}
	this.con.append(this.left);
	this.con.append(this.right);
	
	this.con.height(this.left.height() > this.right.height() ? this.left.height() : this.right.height());
	this.right.height("100%");
	if (!this.right.css('height'))
		this.right.height(this.left.height());
	
	for (var i = 0; i < this.data.length; i++) {
		// Now set the image to load. We do this after all the text
		if (this.data[i].image) {
			var img = GT.Animations.loadImage(this.data[i].image, {'opacity': 0, 'position': 'absolute', "zIndex": 5});
			this.data[i].image = img;
			// Scale the image to fit as best as we can.
			img.bind('load', {me: this, id: i}, function(event) { GT.Tools.FitInside(jQuery(this), event.data.me.right, 1); event.data.me.data[event.data.id].imgloaded = true; });
			this.right.append(img);
		}	
	}
	
	this.con.find('.gt-imagechoosebox-left div:first').addClass('gt-imagechoosebox-text-first');
	this.con.find('.gt-imagechoosebox-left div:last').addClass('gt-imagechoosebox-text-last');
	
	
	this.right.css('cursor', 'pointer');
	this.right.bind('click', {'me': this}, 
		function(event){ 
			var me = event.data.me;
			if (me.data[me.current])
				if (me.data[me.current].url)
					window.location = me.data[me.current].url;
		});


	var me = this;
	
	me.data[0].text.addClass('gt-imagechoosebox-text-selected');
	me.data[0].image.css({'opacity': 1});
	me.current = 0;
	if (me.indicators) {
		var top = (me.data[0].text.height() - me.indicators.start.height()) / 2;
		var position = me.data[0].text.position();
		me.indicators.start.css({'left': '0px', 'top': (position.top + top) + 'px', 'opacity': 1});
	}		
	
	if (me.data.length > 1) {
		var event = {data: {me: this, choice: 1}};
		this.timeout = setTimeout(function(event) { return function() { GT.ImageChooseBox.doSwitch(event); }}(event), 5000);
	}
}


GT.ImageChooseBox.doSwitch = function (event) {
	var me = event.data.me;
	var choice = event.data.choice;
	if (choice == me.current) return;
	if (choice >= me.data.length || choice < 0) return;
	
	clearTimeout(me.timeout);
	
	// Don't worry about indicators.
	me.data[choice].text.addClass('gt-imagechoosebox-text-selected');
	if (me.data[me.current]) 
		me.data[me.current].text.removeClass('gt-imagechoosebox-text-selected');
		
	if (me.data[choice].imgloaded) {		if (me.indicators) {
			var top = (me.data[choice].text.height() - me.indicators.start.height()) / 2;
			var position = me.data[choice].text.position();
			//me.indicators.start.css({'left': '0px', 'top': (position.top + top) + 'px'}).animate({'opacity': 1}, {duration: 1000, queue: false});
			me.indicators.start.css({'left': '0px', 'top': (position.top + top) + 'px', 'opacity': 1});
		}
		me.data[choice].image.animate({'opacity': 1}, {duration: 1000, queue: false});

	} 	
	if (me.data[me.current] && me.data[me.current].image) {
		me.data[me.current].image.animate({'opacity': 0}, {duration: 1000, queue: false});
		if (me.indicators) {
			//me.indicators.finish.css({'opacity': 1, 'left': '0px', 'top': me.indicators.start.css('top')});
			//me.indicators.start.css('opacity', 0);
			//me.indicators.finish.animate({'opacity': 0}, {duration: 1000, queue: false});
		}
	}

	me.current = choice;
	
	var e = { data: { me: me, choice: choice+1}};
	if (e.data.choice >= me.data.length)
		e.data.choice = 0;
		
	me.timeout = setTimeout(function(event) { return function() { GT.ImageChooseBox.doSwitch(event); }}(e), 5000);
}