/*

Title:		jShowOff: a jQuery Content Rotator Plugin
Author:		Erik Kallevig
Version:	0.1.2
Website:	http://ekallevig.com/jshowoff
License: 	Dual licensed under the MIT and GPL licenses.

jShowOff Options

animatePause :		whether to use 'Pause' animation text when pausing [boolean, defaults to true]
autoPlay :			whether to start playing immediately [boolean, defaults to true]
changeSpeed :		speed of transition [integer, milliseconds, defaults to 600]
controls :			whether to create & display controls (Play/Pause, Previous, Next) [boolean, defaults to true]
controlText :		custom text for controls [object, 'play', 'pause', 'previous' and 'next' properties]
cssClass :			custom class to add to .jshowoff wrapper [string]
effect :			transition effect [string: 'fade', 'slideLeft' or 'none', defaults to 'fade']
hoverPause :		whether to pause on hover [boolean, defaults to true]
links :				whether to create & display numeric links to each slide [boolean, defaults to true]
speed :				time each slide is shown [integer, milliseconds, defaults to 3000]

*/

(function($) {


	$.fn.jshowoff = function(settings) {

		// default global vars
		var config = {
			animatePause : true,
			autoPlay : true,
			changeSpeed : 600,
			controls : true,
			controlText : {
				play :		'Play',
				pause :		'Pause',
				next :		'Next',
				previous :	'Previous'
			},
			effect : 'fade',
			hoverPause : true,
			links : true,
			speed : 3000
		};
		
		// merge default global variables with custom variables, modifying 'config'
		if (settings) $.extend(true, config, settings);

		// make sure speed is at least 20ms longer than changeSpeed
		if (config.speed < (config.changeSpeed+20)) {
			alert('jShowOff: Make speed at least 20ms longer than changeSpeed; the fades aren\'t always right on time.');
			return this;
		};
		
		// create slideshow for each matching element invoked by .jshowoff()
		this.each(function(i) {
			
			// declare instance variables
			var $cont = $(this);
			var gallery = $(this).children().remove();
			var timer = '';
			var counter = 0;
			var preloadedImg = [];
			var howManyInstances = $('.jshowoff').length+1;
			var uniqueClass = 'jshowoff-'+howManyInstances;
			var cssClass = config.cssClass != undefined ? config.cssClass : '';
			var resumePlaying = 0;
			
			// set up wrapper
			$cont.css('position','relative').wrap('<div class="jshowoff '+uniqueClass+'" />');
			var $wrap = $('.'+uniqueClass);
			$wrap.css('position','relative').addClass(cssClass);
			
			// add first slide to wrapper
			$(gallery[0]).clone().appendTo($cont);
			
			// preload slide images into memory
			preloadImg();
			
			// add controls
			if(config.controls){
				addControls();
				if(config.autoPlay==false){
					$('.'+uniqueClass+'-play').addClass(uniqueClass+'-paused jshowoff-paused').text(config.controlText.play);
				};
			};
			
			// add slide links
			if(config.links){
				addSlideLinks();
				$('.'+uniqueClass+'-slidelinks a').eq(0).addClass(uniqueClass+'-active jshowoff-active');
			};
			
			// pause slide rotation on hover
			if(config.hoverPause){ $cont.hover(
				function(){ if(isPlaying()) pause('hover'); },
				function(){ if(isPlaying()){
					resumePlaying = setTimeout(function(){play('hover')}, 100)
				}}
			);};
			
			// determine autoPlay
			if(config.autoPlay && gallery.length>1) {
				timer = setInterval( function(){ play(); }, config.speed );
			};
			
			// display error message if no slides present
			if(gallery.length<1){
				$('.'+uniqueClass).append('<p>For jShowOff to work, the container element must have child elements.</p>');
			};

			
			// utility for loading slides
			function transitionTo(gallery,index) {
				
				var oldCounter = counter;
				if((counter >= gallery.length) || (index >= gallery.length)) { counter = 0; var e2b = true; }
				else if((counter < 0) || (index < 0)) { counter = gallery.length-1; var b2e = true; }
				else { counter = index; }


				if(config.effect=='slideLeft'){
					var newSlideDir, oldSlideDir;
					function slideDir(dir) {
						newSlideDir = dir=='right' ? 'left' : 'right';
						oldSlideDir = dir=='left' ? 'left' : 'right';					
					};
					

					counter >= oldCounter ? slideDir('left') : slideDir('right') ;

					$(gallery[counter]).clone().appendTo($cont).slideIt({direction:newSlideDir,changeSpeed:config.changeSpeed});
					if($cont.children().length>1){
						$cont.children().eq(0).css('position','absolute').slideIt({direction:oldSlideDir,showHide:'hide',changeSpeed:config.changeSpeed},function(){$(this).remove();});
					};
				} else if (config.effect=='fade') {
					$(gallery[counter]).clone().appendTo($cont).hide().fadeIn(config.changeSpeed,function(){if($.browser.msie)this.style.removeAttribute('filter');});
					if($cont.children().length>1){
						$cont.children().eq(0).css('position','absolute').fadeOut(config.changeSpeed,function(){$(this).remove();});
					};
				} else if (config.effect=='none') {
					$(gallery[counter]).clone().appendTo($cont);
					if($cont.children().length>1){
						$cont.children().eq(0).css('position','absolute').remove();
					};
				};
				
				// update active class on slide link
				if(config.links){
					$('.'+uniqueClass+'-active').removeClass(uniqueClass+'-active jshowoff-active');
					$('.'+uniqueClass+'-slidelinks a').eq(counter).addClass(uniqueClass+'-active jshowoff-active');
				};
			};
			
			// is the rotator currently in 'play' mode
			function isPlaying(){
				return $('.'+uniqueClass+'-play').hasClass('jshowoff-paused') ? false : true;
			};
			
			// start slide rotation on specified interval
			function play(src) {
				if(!isBusy()){
					counter++;
					transitionTo(gallery,counter);
					if(src=='hover' || !isPlaying()) {
						timer = setInterval(function(){ play(); },config.speed);
					}
					if(!isPlaying()){
						$('.'+uniqueClass+'-play').text(config.controlText.pause).removeClass('jshowoff-paused '+uniqueClass+'-paused');
					}
				};
			};
			
			// stop slide rotation
			function pause(src) {
				clearInterval(timer);
			};
			
			// load the next slide
			function next() {
				goToAndPause(counter+1);
			};
		
			// load the previous slide
			function previous() {
				goToAndPause(counter-1);
			};
			
			// is the rotator in mid-transition
			function isBusy() {
				return $cont.children().length>1 ? true : false;
			};
			
			// load a specific slide
			function goToAndPause(index) {
				$cont.children().stop(true,true);
				if((counter != index) || ((counter == index) && isBusy())){
					if(isBusy()) $cont.children().eq(0).remove();
					transitionTo(gallery,index);
					pause();
				};
			};	

			// load images into memory
			function preloadImg() {
				$(gallery).each(function(i){
					$(this).find('img').each(function(i){
						preloadedImg[i] = $('<img>').attr('src',$(this).attr('src'));
					});
				});
			};
				
			// generate and add slide links
			function addSlideLinks() {
				$wrap.append('<p class="jshowoff-slidelinks '+uniqueClass+'-slidelinks"></p>');
				$.each(gallery, function(i, val) {
					var linktext = $(this).attr('title') != '' ? $(this).attr('title') : i+1;
					$('<a class="jshowoff-slidelink-'+i+' '+uniqueClass+'-slidelink-'+i+'" href="#null">'+linktext+'</a>').bind('click', {index:i}, function(e){ goToAndPause(e.data.index); return false; }).appendTo('.'+uniqueClass+'-slidelinks');
				});
			};	
			
				if(config.hoverPause){ $(".jshowoff-slidelinks").hover(
					function(){ if(isPlaying()){
						clearTimeout(resumePlaying);
						pause('hover'); 
					} },
					function(){}
				);};	
	
	
		// end .each
		});
	
		return this;

	// end .jshowoff
	};

// end closure
})(jQuery);


$(document).ready(function(){
	
	/* Foxy Cart*/
	$(".addtofoxycart").bind("click", function(){
		targetid = $(this).attr("href")
		if($(this).hasClass("disabled")) {return false;}
		$(targetid+' input[type="text"]').each(function(i,qel){
			if($(qel).val().match(/^([1-9]|[1-9]\d|100)$/) ==  null){
				$(qel).val("0");
			}
		})
		$(targetid).trigger("submit");
		return false;
	})
	
	$(".cartinline").bind("click", function(){
		$(this).parents("form").trigger("submit");
		return false;
	})
	
	/* Store Finder Action*/
	$("#findastore").bind("click", function(){
		$("#findastoreform").trigger("submit");
		return false
	})
	
	/* Store Finder Action*/
	$("#listastore").bind("click", function(){
		$("#listastoreform").trigger("submit");
		return false
	})
	
	/* Tabs */

	// $(".tabs a").bind("click", function(){
	// 	$(".tabcontent").hide();
	// 	$($(this).attr("href")).show();
	// 	$(".tabs .active").removeClass("active")
	// 	$(this).parent().addClass("active")
	// 	return false;
	// });
	// $('a[href="'+window.location.hash+'"]').trigger("click")
	// if(window.location.hash == '#gift-sets'){
	// 	var targetOffset = $('ul.tabs:first').offset();
	// 	$("html,body").animate({scrollTop: targetOffset.top-20+"px"}, 400);
	// }

	
	/* Captcha Reload*/
	$("#reloadcaptcha").bind("click", function(){
		$("#captchaimg").attr("src", "/libs/securimage/securimage_show.php?"+Math.random());
		return false;
	});
	
	if($(".standardform").length > 0){
		$('.standardform .arrowbutton').bind("click", function(){
			$(this).parents('.standardform').trigger("submit");
			return false;
		});
		
		$.getScript("/javascript/jquery.rsv.min.js", function(){
			$("#sharestory").RSV({
			//	onCompleteHandler: myOnComplete,
				errorTextIntro: "",
				displayType: "display-html",
				errorFieldClass: "errorField",
				rules: [
				"required,customer_name,Please enter your name.",
				"required,email_address,Please enter your email address.",
				"valid_email,email_address,Please enter a valid email address.",
				"required,city,Please enter your city.",
				"required,state,Please enter your state.",
				"required,comment,Please enter a comment.",
				"required,captcharesponse,Please enter the authentication phrase."
				]
			});
		});
	}
	
	/* Expand Reveal Items*/
	$(".viewall").bind("click", function(){
		var me = $(this)
		if(me.text() == "View All" || me.text() == "Read More"){
			$($(this).attr("href")).slideDown();
			if(me.text() == "View All"){
				me.text("View Fewer").toggleClass("uparrowlink");
			} else {
				me.text("Read Less").toggleClass("uparrowlink");
			}
			
		} else if (me.text() == "View Fewer" || me.text() == "Read Less") {
			$($(this).attr("href")).slideUp();
			if(me.text()=="Read Less"){
				me.text("Read More").toggleClass("uparrowlink");
			} else{
				me.text("View All").toggleClass("uparrowlink");
			}
			var targetOffset = $($(this).attr("href").replace("more-","")).offset();
			$("html,body").animate({scrollTop: targetOffset.top+"px"}, 400);
		}
		return false;
	});
	
	$(".scrollto").bind("click", function(){
		var targetOffset = $($(this).attr("href")).offset();
		$("html,body").animate({scrollTop: targetOffset.top+"px"}, 400);
		return false;
	})
	$("#homepagerotator").jshowoff({animatePause:false, controls:false, hoverPause:true, speed:5000});
	$(".leadrotator").jshowoff({animatePause:false,links:false, controls:false, hoverPause:false, speed:3000});
	
	$(".reveallink").each(function(){
		$(this).bind("click", function(){
			if($.browser.msie){
				$($(this).attr("href")).toggle();
			} else {
				$($(this).attr("href")).slideToggle();
			}
			return false;
		})
	})
	
	$(".fcheader").bind("click", function(){
		calcFcBoxHeight();
		return false;
	})
	
	if(typeof $.colorbox != "undefined"){
		$(".colorbox .attachment-thumbnail").after('<span class="enlargeicon">enlarge</span>');
		$('a.enlarge, a.colorbox').colorbox({onOpen:function(){
			$("#colorbox").addClass("image");
		}});
	}
	
	$(".gallery-row:first-child .gallery-item:first-child").addClass("first-child");
	
	
	
	
	
	/* FIx 24bit pngs in IE6 */
	var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	if (badBrowser) {
		$('img.png24bit').each(function() {
			if (!this.complete) {
				this.onload = function() { fixPng(this) };
			} else {
				fixPng(this);
			}
		});
	}
 	
});
function closeOverlay(){
	$("#cboxClose").click();
}
var blank = new Image();
blank.src = '/images/interface/blank.gif';
function fixPng(png) {
	// get src
	var src = png.src;
	// set width and height
	if (!png.style.width) { png.style.width = $(png).width(); }
	if (!png.style.height) { png.style.height = $(png).height(); }
	// replace by blank image
	png.onload = function() { };
	png.src = blank.src;
	// set filter (display original image)
	png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}
function calcFcBoxHeight(){
		if(FC.json.products.length < 1) {
			colorbox_height = 420;
		} else {
			colorbox_height = 68 * FC.json.products.length + 340;
		}
}
