// jQuery and Prototype library used in store section colliding on '$'. 
jQuery.noConflict();					// ovverride default use of '$'
jQuery(document).ready(function($) {	// wrap all contents in document.ready and pass '$' so code doesn't have to be rewritten.

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).prepend("<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 : 300;		// the time (in milliseconds) 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");
			}
			
			// put the active class on the selected button
			$(slider.btnGoTo.btn).removeClass("active").eq(slider.curGroup - 1).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");
			}
			
			// put the active class on the selected button
			$(slider.btnGoTo.btn).removeClass("active").eq(slider.curGroup - 1).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 highlights = {
	slider:false,
	timer:false,
	init:function() {
		// Define the slider object
		highlights.slider = new Slider({
			btnGoTo:{
				btn:"#highlights .paging li a",
				paneID:"[name=\"paneID\"]"
			},
			btnPrev:"#highlights .left",
			btnNext:"#highlights .right",
			panes:"#highlights .viewbox .pane",
			viewbox:"#highlights .viewbox"
		});
		
		// Set the initial link for the mask
		var href = $("#highlights .viewbox .pane").eq(0).attr("href");
		$("#highlights .viewbox .mask").attr("href",href);
		
		// Define callbacks to change link on mask every time a banner is changed
		highlights.slider.callbackAfterRetreat = highlights.changeMaskHref;
		highlights.slider.callbackAfterAdvance = highlights.changeMaskHref;
		highlights.slider.callbackAfterGoTo = highlights.changeMaskHref;
		
		// Define the timer to auto change the banners
		highlights.timer = window.setInterval(highlights.autoChange,5000);
		$("#highlights").mouseover(function() {
			clearInterval(highlights.timer);
		}).mouseout(function() {
			highlights.timer = window.setInterval(highlights.autoChange,5000);
		});
	},
	autoChange:function() {
		// automatically go to the next banner
		var next = parseInt(highlights.slider.curGroup,10) + 1;
		if (next > highlights.slider.groups) {
			next = 1;
		}
		highlights.slider.goTo(next);
	},
	changeMaskHref:function() {
		// take the href value from the pane and apply it to the mask
		var href = $("#highlights .viewbox .pane").eq(highlights.slider.curGroup - 1).attr("href");
		$("#highlights .viewbox .mask").attr("href",href);
	}
};
highlights.init();

/*
var homePageBanners = {
	slider:false,
	timer:false,
	init:function() {
		homePageBanners.slider = new Slider({
			btnGoTo:{
				btn:"#banner .control .paging li a",
				paneID:"[name=\"paneID\"]"
			},
			panes:"#banner .viewbox .pane",
			viewbox:"#banner .viewbox"
		});
		homePageBanners.slider.beforeGoTo(homePageBanners.change);
		
		homePageBanners.timer = window.setInterval(homePageBanners.autoChange,9999);
		$("#banner").mouseover(function() {
			clearInterval(homePageBanners.timer);
		}).mouseout(function() {
			homePageBanners.timer = window.setInterval(homePageBanners.autoChange,9999);
		});
	},
	change:function() {
		// swap text in control box
		$("#banner .control .pane").stop(true,true);
		$("#banner .control .pane:visible").fadeOut(100,function() {
			$("#banner .control .pane:eq("+(homePageBanners.slider.curGroup - 1)+")").fadeIn(200);
		});
	},
	autoChange:function(paneID) {
		// automatically go to the next banner
		var next = parseInt(homePageBanners.slider.curGroup) + 1;
		if (next > homePageBanners.slider.groups) {
			next = 1;
		}
		homePageBanners.slider.goTo(next);
	}
};
homePageBanners.init();

var feaPhotosVideos = {
	slider:false,
	init:function() {
		feaPhotosVideos.slider = new Slider({
			btnPrev:"#home-feas .fea.photos-video .larr",
			btnNext:"#home-feas .fea.photos-video .rarr",
			panes:"#home-feas .fea.photos-video .slider .viewbox .pane",
			viewbox:"#home-feas .fea.photos-video .slider .viewbox"
		});
		feaPhotosVideos.slider.afterAdvance(feaPhotosVideos.changeNext);
		feaPhotosVideos.slider.afterRetreat(feaPhotosVideos.changePrev);
	},
	changePrev:function() {
		// swap text/description under thumbnail
	},
	changeNext:function() {
		// swap text/description under thumbnail
	}
};
feaPhotosVideos.init();
*/

// Show controls that are hidden for users without javascript
$("#highlights .left,#highlights .right").css({"visibility":"visible"});
$("#highlights .paging").show();



});		// end wrapping all code in document.ready for compatibility with Prototype library.
