ai_slider={
	GROW_SLIDER_AfterGrow:null,
	totalPanels:null,
	regWidth:null,
	regImgWidth:null,
	regTitleSize:null,
	regParSize:null,
	
	movingDistance:130,
	curWidth:150,
	curImgWidth:70,
	curTitleSize:"14px",
	curParSize:"14px",
	
	$panels:null,
	$container:null,
	curPanelEl:null,

	returnToNormal:function(element) {
		$(element)
			.animate({ width: this.regWidth ,top:0})
			.find("img")
			.animate({ width: this.regImgWidth })
		    .end()
			.find("h2")
			.animate({ fontSize: this.regTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: this.regParSize });
	},
	growBigger:function(element) {
		$(element)
			.animate({ width: this.curWidth ,top:-20})
			.find("img")
			.animate({ width: this.curImgWidth })
		    .end()
			.find("h2")
			.animate({ fontSize: this.curTitleSize })
			.end()
			//.find("p")
			//.animate({ fontSize: this.curParSize })
			.addClass('panel-selected');
	},
	//direction true = right, false = left
	change:function(direction){
		var _this=this;
	    //if not at the first or last panel
		if((direction && !(this.curPanel < this.totalPanels)) || (!direction && (this.curPanel <= 1))) { return false; }	
        
        //if not currently moving
        if (($("#slider").data("currentlyMoving") == false)) {
            
			$("#slider").data("currentlyMoving", true);
			
			var next         = direction ? this.curPanel + 1 : this.curPanel - 1;
			var leftValue    = $(".scrollContainer").css("left");
			var movement	 = direction ? parseFloat(leftValue, 10) - this.movingDistance : parseFloat(leftValue, 10) + this.movingDistance;
		
			$(".scrollContainer")
				.stop()
				.animate({
					"left": movement
				}, function() {
					$("#slider").data("currentlyMoving", false);
				});
			
			this.returnToNormal("#panel_"+this.curPanel);
			this.growBigger("#panel_"+next);
			
			if(typeof this.GROW_SLIDER_AfterGrow=='function') this.GROW_SLIDER_AfterGrow("#panel_"+next);
			
			this.curPanel = next;
			
			//remove all previous bound functions
			$("#panel_"+(this.curPanel+1)).unbind();	
			
			//go forward
			$("#panel_"+(this.curPanel+1)).click(function(){ _this.change(true); });
			
            //remove all previous bound functions															
			$("#panel_"+(this.curPanel-1)).unbind();
			
			//go back
			$("#panel_"+(this.curPanel-1)).click(function(){ _this.change(false); }); 
			
			//remove all previous bound functions
			$("#panel_"+this.curPanel).unbind();
		}
	},
	init:function(params){
		var _this = this;
		if(params) $.extend(this, params);

		this.totalPanels	= $(".scrollContainer").children().size();
		this.regWidth		= $(".panel").css("width");
		this.regImgWidth	= $(".panel img").css("width");
		this.regTitleSize	= $(".panel h2").css("font-size");
		this.regParSize		= $(".panel p").css("font-size");
		this.$panels		= $('#slider .scrollContainer > div');
		this.$container		= $('#slider .scrollContainer');

		this.$panels.css({'float' : 'left','position' : 'relative'});
		$("#slider").data("currentlyMoving", false);
		this.$container.css('width', (this.$panels[0].offsetWidth * this.$panels.length) + 100 )
		    
		this.curPanelEl = $(".panel-selected");
		if(this.curPanelEl.length>0&&parseInt(this.curPanelEl.attr("id").replace("panel_",""))==1)
			this.$container.css('left', "260px");
		else
			this.$container.css('left', "260px");

		var scroll = $('#slider .scroll').css('overflow', 'hidden');
		// Set up "Current" panel and next and prev
		this.curPanelEl = $(".panel-selected");
		this.curPanel = (this.curPanelEl.length>0)?parseInt(this.curPanelEl.attr("id").replace("panel_","")):1;
		this.growBigger("#panel_"+this.curPanel);	


		$("#panel_"+(this.curPanel+1)).click(function(){ _this.change(true); return false; });
		$("#panel_"+(this.curPanel-1)).click(function(){ _this.change(false); return false; });
        
        var rEl = $("#slider .right");
        var lEl = $("#slider .left");
        
        var tcount = $(".scrollContainer .panel").length;
		var defaultIndex=Math.ceil(tcount/2)-1;
		var doMove = function(){
		    if(defaultIndex>0){
		        _this.change(true);
		        defaultIndex--;
		        setTimeout(function(){
		            doMove();
		        },500);
		    }
		};
		doMove();
		//when the left/right arrows are clicked
		rEl.click(function(){ _this.change(true); return false; });	
		lEl.click(function(){ _this.change(false); return false; });
		$(window).keydown(function(event){
			switch (event.keyCode) {
				case 13: //enter
					rEl.click();
					break;
				case 32: //space
					rEl.click();
					break;
				case 37: //left arrow
					lEl.click();
					break;
				case 39: //right arrow
					rEl.click();
					break;
			}
		});
	}

};

