/*
 * jQuery Address Plugin v1.1
 * http://www.asual.com/jquery/address/
 *
 * Copyright (c) 2009 Rostislav Hristov
 * Dual licensed under the MIT and GPL licenses.
 * http://docs.jquery.com/License
 *
 * Date: 2009-12-23 14:27:00 +0200 (Wed, 23 Dec 2009)
 */
(function(a){a.address=(function(){var c=function(i){a(this).trigger(a.extend(a.Event(i),(function(){var ah={value:this.value(),path:this.path(),pathNames:this.pathNames(),parameterNames:this.parameterNames(),parameters:{},queryString:this.queryString()};for(var ag=0,p=ah.parameterNames.length;ag<p;ag++){ah.parameters[ah.parameterNames[ag]]=this.parameter(ah.parameterNames[ag])}return ah}).call(this)))};var V=function(){c.call(a.address,"init")};var o=function(){c.call(a.address,"change")};var P=function(){var i=U.href.indexOf("#");return i!=-1?ae(q(U.href.substr(i+1))):""};var h=function(){try{return top.document!=undefined?top:window}catch(i){return window}};var F=function(p,i){if(C.strict){p=i?(p.substr(0,1)!="/"?"/"+p:p):(p==""?"/":p)}return p};var r=function(i,p){return(j&&U.protocol=="file:")?(p?Q.replace(/\?/,"%3F"):Q.replace(/%253F/,"?")):i};var af=function(ai){for(var ag=0,p=ai.childNodes.length,ah;ag<p;ag++){if(ai.childNodes[ag].src){l=String(ai.childNodes[ag].src)}if(ah=af(ai.childNodes[ag])){return ah}}};var X=function(){if(!t){var p=P();var i=!(Q==p);if(u&&s<523){if(E!=Y.length){E=Y.length;if(typeof z[E-1]!=G){Q=z[E-1]}T(false)}}else{if(j&&s<7&&i){U.reload()}else{if(i){Q=p;T(false)}}}}};var T=function(i){o();if(i){c.call(a.address,"internalChange")}else{c.call(a.address,"externalChange")}n(w,10)};var w=function(){var p=(U.pathname+(/\/$/.test(U.pathname)?"":"/")+D.value()).replace(/\/\//,"/").replace(/^\/$/,"");var i=window[C.tracker];if(typeof i==g){i(p)}else{if(typeof pageTracker!=G&&typeof pageTracker._trackPageview==g){pageTracker._trackPageview(p)}else{if(typeof urchinTracker==g){urchinTracker(p)}}}};var e=function(){var i=f.contentWindow.document;i.open();i.write("<html><head><title>"+ac.title+"</title><script>var "+x+' = "'+P()+'";<\/script></head></html>');i.close()};var N=function(){if(!S){S=B;if(j&&s<8){var i=ac.getElementsByTagName("frameset")[0];f=ac.createElement((i?"":"i")+"frame");if(i){i.insertAdjacentElement("beforeEnd",f);i[i.cols?"cols":"rows"]+=",0";f.src="javascript:false";f.noResize=true;f.frameBorder=f.frameSpacing=0}else{f.src="javascript:false";f.style.display="none";ac.body.insertAdjacentElement("afterBegin",f)}n(function(){a(f).bind("load",function(){var p=f.contentWindow;var ag=p.location.href;Q=(typeof p[x]!=G?p[x]:"");if(Q!=P()){T(false);U.hash=r(Q,B)}});if(typeof f.contentWindow[x]==G){e()}},50)}else{if(u){if(s<418){a(ac.body).append('<form id="'+x+'" style="position:absolute;top:-9999px;" method="get"></form>');J=ac.getElementById(x)}if(typeof U[x]==G){U[x]={}}if(typeof U[x][U.pathname]!=G){z=U[x][U.pathname].split(",")}}}n(function(){V();T(false)},1);if(j&&s>=8){ac.body.onhashchange=X}else{v(X,50)}a("a[rel*=address:]").address()}};var D={baseURL:function(){var i=U.href;if(i.indexOf("#")!=-1){i=i.substr(0,i.indexOf("#"))}if(i.substr(i.length-1)=="/"){i=i.substr(0,i.length-1)}return i},strict:function(){return C.strict},history:function(){return C.history},tracker:function(){return C.tracker},title:function(){return ac.title},value:function(){if(!ab){return null}return q(F(r(Q,Z),Z))},path:function(){var i=this.value();return(i.indexOf("?")!=-1)?i.split("?")[0]:i},pathNames:function(){var p=this.path();var i=p.split("/");if(p.substr(0,1)=="/"||p.length==0){i.splice(0,1)}if(p.substr(p.length-1,1)=="/"){i.splice(i.length-1,1)}return i},queryString:function(){var p=this.value();var i=p.indexOf("?");if(i!=-1&&i<p.length){return p.substr(i+1)}},parameter:function(am){var aj=this.value();var ag=aj.indexOf("?");if(ag!=-1){aj=aj.substr(ag+1);var al=aj.split("&");var ak,ah=al.length,ai=[];while(ah--){ak=al[ah].split("=");if(ak[0]==am){ai.push(ak[1])}}if(ai.length!=0){return ai.length!=1?ai:ai[0]}}},parameterNames:function(){var ah=this.value();var p=ah.indexOf("?");var ai=[];if(p!=-1){ah=ah.substr(p+1);if(ah!=""&&ah.indexOf("=")!=-1){var aj=ah.split("&");var ag=0;while(ag<aj.length){ai.push(aj[ag].split("=")[0]);ag++}}}return ai}};var H={strict:function(i){C.strict=i},history:function(i){C.history=i},tracker:function(i){C.tracker=i},title:function(i){i=q(i);n(function(){I=ac.title=i;if(K&&f&&f.contentWindow&&f.contentWindow.document){f.contentWindow.document.title=i;K=Z}if(!M&&aa){U.replace(U.href.indexOf("#")!=-1?U.href:U.href+"#")}M=Z},50)},value:function(ag){ag=ae(q(F(ag,B)));if(ag=="/"){ag=""}if(Q==ag){return}M=B;Q=ag;t=B;T(true);z[Y.length]=Q;if(u){if(C.history){U[x][U.pathname]=z.toString();E=Y.length+1;if(s<418){if(U.search==""){J.action="#"+Q;J.submit()}}else{if(s<523||Q==""){var i=ac.createEvent("MouseEvents");i.initEvent("click",B,B);var p=ac.createElement("a");p.href="#"+Q;p.dispatchEvent(i)}else{U.hash="#"+Q}}}else{U.replace("#"+Q)}}else{if(Q!=P()){if(C.history){U.hash="#"+r(Q,B)}else{U.replace("#"+Q)}}}if((j&&s<8)&&C.history){n(e,50)}if(u){n(function(){t=Z},1)}else{t=Z}}};var x="jQueryAddress",g="function",G="undefined",B=true,Z=false,A=a.browser,s=parseFloat(a.browser.version),aa=A.mozilla,j=A.msie,L=A.opera,u=A.safari,ab=Z,O=h(),ac=O.document,Y=O.history,U=O.location,v=setInterval,n=setTimeout,q=decodeURI,ae=encodeURI,ad=navigator.userAgent,f,J,l,I=ac.title,E=Y.length,t=Z,S=Z,M=B,K=B,z=[],y={},Q=P(),k={},C={history:B,strict:B};if(j){s=parseFloat(ad.substr(ad.indexOf("MSIE")+4));if(ac.documentMode&&ac.documentMode!=s){s=ac.documentMode!=8?7:8}}ab=(aa&&s>=1)||(j&&s>=6)||(L&&s>=9.5)||(u&&s>=312);if(ab){for(var W=1;W<E;W++){z.push("")}z.push(P());if(j&&U.hash!=P()){U.hash="#"+r(P(),B)}if(L){history.navigationMode="compatible"}af(document);var b=l.indexOf("?");if(l&&b>-1){var m,d=l.substr(b+1).split("&");for(var W=0,R;R=d[W];W++){m=R.split("=");if(/^(history|strict)$/.test(m[0])){C[m[0]]=(isNaN(m[1])?/^(true|yes)$/i.test(m[1]):(parseInt(m[1])!=0))}if(/^tracker$/.test(m[0])){C[m[0]]=m[1]}}}a(N)}else{if((!ab&&U.href.indexOf("#")!=-1)||(u&&s<418&&U.href.indexOf("#")!=-1&&U.search!="")){ac.open();ac.write('<html><head><meta http-equiv="refresh" content="0;url='+U.href.substr(0,U.href.indexOf("#"))+'" /></head></html>');ac.close()}else{w()}}a.each(("init,change,internalChange,externalChange").split(","),function(ag,p){k[p]=function(ah,i){a(a.address).bind(p,i||ah,i&&ah);return this}});a.each(("strict,history,tracker,title,value").split(","),function(ag,p){k[p]=function(i){if(typeof i!="undefined"){if(ab){H[p](i)}return a.address}else{return D[p]()}}});a.each(("baseURL,path,pathNames,queryString,parameter,parameterNames").split(","),function(ag,p){k[p]=function(i){return D[p](i)}});return k})();a.fn.address=function(b){a(this).click(function(){var c=b?b.call(this):/address:/.test(a(this).attr("rel"))?a(this).attr("rel").split("address:")[1].split(" ")[0]:a(this).attr("href").replace(/^#/,"");a.address.value(c);return false})}}(jQuery));/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * TERMS OF USE - jQuery Easing
 * 
 * Open source under the BSD License. 
 * 
 * Copyright © 2008 George McGinley Smith
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
*/

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158; 
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});

/*
 *
 * TERMS OF USE - EASING EQUATIONS
 * 
 * Open source under the BSD License. 
 * 
 * Copyright © 2001 Robert Penner
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
 */// Carousel
$(document).ready(function(){

	// Show image thumbnails, user without javascript don't get to see the other images
	$(".product-gallery-wrapper").addClass("giveHeight");
	$(".gallery-thumbnails-wrapper").removeClass("hide");

	var totalImages = $(".gallery-thumbnails").children().size(); 
	var totalImageGroups = Math.ceil(totalImages / 5);
	var currentImageGroup = 1;
			
	// Next and Prev Buttons
	function clickPrevGroup(){
		currentImageGroup --;
		//If previous from last group
		if (currentImageGroup==(totalImageGroups-1)) {
			var lastPrevMove = (totalImages - ((totalImageGroups-1)*5))*104;	
			$(".gallery-thumbnails").animate({'right': '-='+lastPrevMove+'px'}, 600, 'easeInOutQuint');
		} else {
			$(".gallery-thumbnails").animate({'right': '-=520px'}, 600, 'easeInOutQuint');
		}	
		checkButtonStates();
		return false;
	}
	
	function clickNextGroup(){
		currentImageGroup ++;
		//If next from second last group
		if (totalImageGroups==currentImageGroup) {
			var lastNextMove = (totalImages - ((totalImageGroups-1)*5))*104;
			$(".gallery-thumbnails").animate({'right': '+='+lastNextMove+'px'}, 600, 'easeInOutQuint');
		} else {
			$(".gallery-thumbnails").animate({'right': '+=520px'}, 600, 'easeInOutQuint');
		}
		checkButtonStates();
		return false;
	}
	
	//Disable Buttons
	$(".gallery-prev").unbind('click', clickPrevGroup)
    $(".gallery-next").unbind('click', clickNextGroup)
		
	// Check and Change Button Statest
	var checkButtonStates = function () {
		
		// First Image Group
		if (currentImageGroup == 1) {
			$(".gallery-prev").addClass("disabled-left");
			$(".gallery-prev").unbind('click', clickPrevGroup);
		};	
		
		// Not the First Image Group
		if (currentImageGroup > 1) {
			$(".gallery-prev").removeClass("disabled-left");
     		$(".gallery-prev").bind('click', clickPrevGroup);
		};
		
		// Not the Last Image Group
		if (currentImageGroup < totalImageGroups) {
			$(".gallery-next").removeClass("disabled-right");
     		$(".gallery-next").bind('click', clickNextGroup);
		};
		
		// Last Image Group
		if (totalImageGroups == currentImageGroup) {
			$(".gallery-next").addClass("disabled-right");
			$(".gallery-next").unbind('click', clickNextGroup);
		};
			
	};
	
	// Is there more than 1 image Group
	if (totalImageGroups >= 2) {
		$(".gallery-next").removeClass("disabled-right");
 		$(".gallery-next").bind('click', clickNextGroup);
 		checkButtonStates();
	};
	
	var clickOnThumbnail = function(){
		var imageURL = document.location.toString();
		 // Click the link with the anchor
		var thisImageNO = 'product-image-' + imageURL.split('#/image=')[1];
	
		// Find the first image with the same class and get the attributes
		thisImageSRC=$("a[class='"+thisImageNO+"']").attr("href");
		thisImageWIDTH=$("a[class='"+thisImageNO+"']").find("img").attr("width");
		thisImageHEIGHT=$("a[class='"+thisImageNO+"']").find("img").attr("height");
		thisImageALT=$("a[class='"+thisImageNO+"']").find("img").attr("alt");
		
		// Swap the image
		var img = new Image();
		$(img).load(function () {
			// Hide the image  
			$(this).hide();
			// Insert new image
			$('.large-image-wrapper').html(this);
			// Show image
			$(this).show();
		})
		
		// Error
		.error(function () {
			// Error Message
		})
		
		// Set the Attributes of the new image
		.css('display', 'inline')
		.attr('height', thisImageHEIGHT)
		.attr('width', thisImageWIDTH)
		.addClass('large-image')
		.attr('alt', thisImageALT)
		.attr('src', thisImageSRC)
		;

	};
			
	logoHeight = $('.large-image').attr("height");
	
	// URL Change from location bar, back, forward
	$.address.externalChange(function() {
		 currentURL = document.location.toString();
		 // Check for hash tag
		 if (currentURL.match('#')) {
		 	clickOnThumbnail();
		 };
	});
	
	// URL Change from clicking on thumbnail
	$('.gallery-thumbnails a').click(function() {
		clickOnThumbnail();
	});	
			
});
