(function($){
	var HomeBox = function(ele, opts) {
		this.ele = $(ele);
		this.config = $.extend({
			intervalTime: 12000,
			animateTime: 1000
		}, opts, this.ele.ParseClass());
		
		this.thumbLIs = this.ele.find("div.thumbNav li:not(.arrow)")
		.bind("switchTo", $.proxy(this._onThumbLISwitchTo, this))
		.click($.proxy(this._onThumbLIClick, this));
		
		this.bgImages = this.ele.find("div.bgImage");
		
		this.promoBoxes = this.ele.find("div.promoText").each(function() {
			$(this).css("height", $(this).height());
		});
		
		this.interval = setInterval($.proxy(function() {
			var i = $.inArray(this.thumbLIs.filter(".selected")[0], this.thumbLIs) + 1;
			if (i == this.thumbLIs.length) {
				i=0;
			}
			$(this.thumbLIs[i]).trigger("switchTo");
		}, this), this.config.intervalTime);
		
		this.collapsibleBoxes = this.ele.find("div.collapsible")
		.bind("expand", $.proxy(this._onCollapsibleExpand, this));
		this.collapsibleBoxes.find("div.header").click($.proxy(this._onCollapsibleHeaderClick, this));

		var cookieVal = this._fnReadCookie("HomeBoxExpanded");
		if (cookieVal) {
			this.collapsibleBoxes.find("div.content").each(function(i) {
				if (this.id == cookieVal) {
					$(this).trigger("expand");
				}
			});
			if (this._finishExpandAnimation) {
				this._finishExpandAnimation();
			}
		}
	};
	$.extend(HomeBox.prototype, {
		_onThumbLIClick: function(e) {
			clearInterval(this.interval);
			$(e.currentTarget).trigger("switchTo");
		},
		_onThumbLISwitchTo: function(e) {
			var ele = $(e.currentTarget);
			if (ele.hasClass("selected")) {
				return;
			}
			if (this._finishAnimation) {
				this._finishAnimation();
			}
			
			var i = $.inArray(e.currentTarget, this.thumbLIs);
			
			this.thumbLIs.removeClass("selected");
			ele.addClass("selected");
			
			var newBgImg = $(this.bgImages[i]),
			oldBgImg = this.bgImages.filter(":visible");
			oldBgImg.css("zIndex", "1");
			newBgImg.css("display", "");
			this._finishAnimation = $.proxy(function() {
				oldBgImg.stop().css("zIndex", "").css("width", "").css("display", "none");
				oldPromoBox.stop().css("zIndex", "").css("opacity", "").css("display", "none");
				this._finishAnimation = false;
			}, this);
			oldBgImg.animate({width: 0}, this.config.animateTime, this._finishAnimation);
			
			var newPromoBox = $(this.promoBoxes[i]),
			oldPromoBox = this.promoBoxes.filter(":visible");
			oldPromoBox.css("zIndex", "1");
			newPromoBox.css("display", "");
			oldPromoBox.animate({opacity: 0}, this.config.animateTime);
		},
		_onCollapsibleHeaderClick: function(e) {
			var ele = $(e.currentTarget);
			ele.parents("div.collapsible:first").trigger("expand");
		},
		_onCollapsibleExpand: function(e) {
			var ele = $(e.currentTarget);
			if (!ele.hasClass("closed")) {
				return;
			}
			if (this._finishExpandAnimation) {
				this._finishExpandAnimation();
			}
			var oldPromoBox = this.collapsibleBoxes.filter(":not(.closed)").addClass("closed").find("div.content"),
			newPromoBox = $(e.currentTarget).removeClass("closed").find("div.content");
			
			this._finishExpandAnimation = $.proxy(function() {
				oldPromoBox.stop().css("height", "").css("display", "none");
				newPromoBox.stop().css("height", "");
				newPromoBox.stop().css("overflow", "");
				this._finishExpandAnimation = false;
			}, this);
			
			var height = newPromoBox.css("display", "").height();
			
			oldPromoBox.animate({height: 0}, this.config.animateTime, this._finishExpandAnimation);
			newPromoBox.css({"height": 0}).animate({height: height}, this.config.animateTime);

			this._fnCreateCookie("HomeBoxExpanded", newPromoBox[0].id, 60*60*24*30);
		},
		_fnCreateCookie: function(sName,sValue,iSecs){
			var date=new Date();
			date.setTime(date.getTime()+(iSecs*1000));
			sName+="_"+window.location.pathname.replace(/[\/:]/g,"").toLowerCase();
			document.cookie=sName+"="+encodeURIComponent(sValue)+";expires="+date.toGMTString()+"; path=/"
		},
		_fnReadCookie: function(sName){
			var sNameEQ=sName+"_"+window.location.pathname.replace(/[\/:]/g,"").toLowerCase()+"=";
			var sCookieContents=document.cookie.split(";");
			for(var i=0;i<sCookieContents.length; i++){
				var c=sCookieContents[i];
				while(c.charAt(0)==" "){
					c=c.substring(1,c.length)
				}
				if(c.indexOf(sNameEQ)===0){
					return decodeURIComponent(c.substring(sNameEQ.length,c.length))
				}
			}
			return null
		}
	});
	
	$.fn.extend({
		HomeBox: function(opts) {
			var $this = this.not(function(i) {
				return $.data(this, "HomeBoxObj");
			}).each(function() {
				$.data(this, "HomeBoxObj", new HomeBox(this, opts));
			});
			return this;
		}
	});
	$.HomeBox = {
		init: function(sel) {
			$(sel || "div.box.home").HomeBox();
		}
	};
	$(function() {
		$.HomeBox.init();
	});
})(jQuery);
