function Slider(params) {
	if (!$(params.viewbox).length) {
		return false;
	}

	this.init = function(){
		// slider objects/controls
		this.btnPrev = params.btnPrev ? params.btnPrev : false;
		this.btnNext = params.btnNext ? params.btnNext : false;
		this.btnGoTo = params.btnGoTo ? params.btnGoTo : false;
		this.viewbox = params.viewbox;
		this.panes = params.panes;

		// create the slide, and move the panes into it
		$(this.viewbox).append("<div class=\"slide\"></div>");
		this.slide = this.viewbox+" .slide";
		$(this.slide).append($(this.panes));
		
		// set required CSS values to allow the animation
		$(this.viewbox).css({"position":"relative"});
		$(this.slide).css({
			"position":"relative",
			"width":"999999px"
		});

		// calculate initial states/attributes
		this.curPos = $(this.slide).position().left;					// the current x co-ordinate/CSS left value of the slider
		this.startPos = this.curPos;									// allow an initial offset other than 0
		this.curGroup = params.group ? params.group : 1;				// the current group being viewed
		this.interval = params.interval ? params.interval : 2;		// the time (in seconds) for the duration of the animation
		this.groups = $(this.panes).length;								// the number of groups or panes to slide through
		this.viewBoxWidth = $(this.viewbox).width();
		
		// BIND EVENTS
		var slider = this;
		$(this.btnPrev).mousedown(function(){slider.retreat();});
		$(this.btnNext).mousedown(function(){slider.advance();});
		$(this.btnGoTo.btn).mousedown(function(){slider.goTo($(this).find(slider.btnGoTo.paneID).val());});
		
		var btns = [];
		if (this.btnPrev) {btns.push(this.btnPrev);}
		if (this.btnNext) {btns.push(this.btnNext);}
		if (this.btnGoTo && this.btnGoTo.btn) {btns.push(this.btnGoTo.btn);}
		$(btns.join(", ")).click(function(){return false;});

		if (this.groups > 1) {
			$(this.btnNext).addClass("active");
		}
	};

	this.resetSlider = function() {
		this.curPos = this.startPos;
		this.curGroup = 1;
		this.groups = $(params.pane).length;

		// BIND EVENTS
		var slider = this;
		$(this.btnPrev).mousedown(function(){slider.retreat();});
		$(this.btnNext).mousedown(function(){slider.advance();});
		$(this.btnGoTo.btn).mousedown(function(){slider.goTo($(this).find(slider.btnGoTo.paneID).val());});
		
		var btns = [];
		if (this.btnPrev) {btns.push(this.btnPrev);}
		if (this.btnNext) {btns.push(this.btnNext);}
		if (this.btnGoTo && this.btnGoTo.btn) {btns.push(this.btnGoTo.btn);}
		$(btns.join(", ")).click(function(){return false;});

		$(this.btnPrev+", "+this.btnNext).removeClass("active");
		if(this.groups > 1){
			$(this.btnNext).addClass("active");
		}
	};

	// Move the slider forward one pane
	this.advance = function() {
		if (this.curGroup >= this.groups) {
			return;
		}
		$(this.slide).stop(true,true);
		
		if(this.callbackBeforeAdvance) {
			this.callbackBeforeAdvance();
		}
		
		this.curGroup++;
		var slider = this;
		$(this.slide).animate({"left":this.curPos-this.viewBoxWidth},this.interval,function() {
			slider.curPos -= slider.viewBoxWidth;
			if (slider.curGroup == slider.groups) {
				$(slider.btnNext).removeClass("active");
			}
			if (slider.groups > 1) {
				$(slider.btnPrev).addClass("active");
			}
			
			if (slider.callbackAfterAdvance) {
				slider.callbackAfterAdvance();
			}
		});
	};

	// Move the slider backward one pane
	this.retreat = function() {
		if (this.curGroup <= 1) {
			return;
		}
		$(this.slide).stop(true,true);
		
		if (this.callbackBeforeRetreat) {
			this.callbackBeforeRetreat();
		}
		
		this.curGroup--;
		var slider = this;
		$(this.slide).animate({"left":this.curPos+this.viewBoxWidth},this.interval,function() {
			slider.curPos += slider.viewBoxWidth;
			if (slider.curGroup == 1) {
				$(slider.btnPrev).removeClass("active");
			}
			if (slider.groups > 1) {
				$(slider.btnNext).addClass("active");
			}
			
			if (slider.callbackAfterRetreat) {
				slider.callbackAfterRetreat();
			}
		});
	};
	
	// Move the slider to the selected pane
	this.goTo = function(paneID) {
		if (this.curGroup == paneID) {
			return;
		}

		var destination = (this.viewBoxWidth * (paneID - 1)) * -1;

		$(this.slide).stop(true,true);

		if (this.callbackBeforeGoTo) {
			this.callbackBeforeGoTo();
		}

		this.curGroup = paneID;
		var slider = this;
		$(this.slide).animate({"left":destination},this.interval,function() {
			slider.curPos = destination;

			// put the active class on the selected button
			$(slider.btnGoTo.btn).removeClass("active").eq(paneID - 1).addClass("active");

			if (slider.callbackAfterGoTo) {
				slider.callbackAfterGoTo();
			}
		});
	};

	// Callback events
	this.beforeAdvance = function(f) {this.callbackBeforeAdvance = f;};
	this.afterAdvance = function(f) {this.callbackAfterAdvance = f;};
	this.beforeRetreat = function(f) {this.callbackBeforeRetreat = f;};
	this.afterRetreat = function(f) {this.callbackAfterRetreat = f;};
	this.beforeGoTo = function(f) {this.callbackBeforeGoTo = f;};
	this.afterGoTo = function(f) {this.callbackAfterGoTo = f;};

	// put the slider object in play
	this.init();
}

var homePageBanners = {
	slider:false,
	timer:false,
	init:function() {
		// make sure the object exists before assigning events
		if (!$("#banner1 .viewbox").length) {
			return false;
		}

		// Show controls that are hidden for users without javascript
		$("#banner1 .paging").show();

		homePageBanners.slider = new Slider({
			btnGoTo:{
				btn:"#banner1 .paging li a",
				paneID:"[name=\"paneID\"]"
			},
			panes:"#banner1 .viewbox .pane",
			viewbox:"#banner1 .viewbox"
		});
		homePageBanners.slider.beforeGoTo(homePageBanners.change);
		
		homePageBanners.timer = window.setInterval(homePageBanners.autoChange,9000);
//		$("#banner").mouseover(function() {
//			clearInterval(homePageBanners.timer);
//		}).mouseout(function() {
//			homePageBanners.timer = window.setInterval(homePageBanners.autoChange,5000);
//		});
	},
	change:function() {
		// reset autochange timer
		clearInterval(homePageBanners.timer);
		homePageBanners.timer = window.setInterval(homePageBanners.autoChange,9000);
		
		// swap text in control box
		$("#banner1 .control .pane").stop(true,true);
		$("#banner1 .control .pane:visible").fadeOut(100,function() {
			$("#banner1 .control .pane:eq("+(homePageBanners.slider.curGroup - 1)+")").fadeIn(200);
		});
	},
	autoChange:function(paneID) {
		// automatically go to the next banner
		var next = parseInt(homePageBanners.slider.curGroup,10) + 1;
		   if (next > homePageBanners.slider.groups / 2) {
            next = 1;
        }
		homePageBanners.slider.goTo(next);
	}
};
homePageBanners.init();



