//set up window true false to control transitions in inactive tabs
//the active false here controls all image changes
//resetting active to true is handled per page in wordpress template files
var active = true;

jQuery(window).blur(function(){
	active = false;
});

//general image switcher
var current = 1;
function switcher(container, image){
	var imgcount = jQuery(container).size();
	if(imgcount != 1 && active == true){
		jQuery(image + current).fadeOut(750);
		if(current != imgcount){
			jQuery(image + (++current)).fadeIn(750);
		}else{
			current = 1;
			jQuery(image + current).fadeIn(750);
		}
		setTimeout(function(){ switcher(container, image); }, 5000);
	}
}            

//image switcher for product page
function productSwitcher(container, image, nav){
	var imgcount = jQuery(container).size();
	if(imgcount != 1 && active == true){
		jQuery(image + current).fadeOut(750);
		jQuery(nav + current).css({"background-position" : "-14px"});
		if(current != imgcount){
			current++;
			jQuery(image + (current)).fadeIn(750);
			jQuery(nav + (current)).css({"background-position" : "0px"});
		}else{
			current = 1;
			jQuery(image + current).fadeIn(750);
			jQuery(nav + (current)).css({"background-position" : "0px"});
		}
		setTimeout(function(){ productSwitcher(container, image, nav); }, 5000);
	}
}

//nav image switcher for product page
function changeImage(container, item, imageContainer, imageID){
	var clickedClass = jQuery(item).attr("class");
	var clickedIndex = jQuery(container).index(item) + 1;
	if(clickedIndex != current){
		jQuery("." + clickedClass).css({"background-position" : "-14px"});
		jQuery(item).css({"background-position" : "0px"});
		jQuery("img", imageContainer).fadeOut(750);
		jQuery(imageID + clickedIndex).fadeIn(750);
		current = clickedIndex;
	}
}

//pop up video
function popUp(video){
	if(jQuery("#blanket-1").css("display") == "none"){
		jQuery("#blanket-1").fadeIn(250);
		jQuery("#blanket-content-1 #video-holder").html(video);
		setTimeout(function(){
			jQuery("#blanket-content-1").fadeIn(250);
		}, 350);
	}else{
		jQuery("#blanket-1").fadeOut(250);
		jQuery("#blanket-content-1").fadeOut(250);
		setTimeout(function(){
			jQuery("#blanket-content-1 #video-holder").html("");
		}, 350);
	}
}

//document load
jQuery(document).ready(function(){
	
	//social media share icons 
	jQuery(".sm-link").hover(
		function(){
			jQuery(".sm-icon-grey", this).stop(true, true).fadeOut(300);
			jQuery(".sm-icon-color", this).stop(true, true).fadeIn(200);
		},
		function(){
			jQuery(".sm-icon-color", this).stop(true, true).fadeOut(300);
			jQuery(".sm-icon-grey", this).stop(true, true).fadeIn(200);
		}
	);
	
	//product page price list pop up
	jQuery(".price-popup").click(function(e){
		e.preventDefault();
		if(jQuery("#price-list-container").css("display") == "none"){
			jQuery("#price-list-container").fadeIn(250);
		}else{
			jQuery("#price-list-container").fadeOut(250);
		}
	});
	
	//roll over states for product links (only one line shows at any one time)
	jQuery(".product-link").hover(
		function(){
			jQuery(".product-link").css({"border-top" : "1px solid #cccccc"});
			jQuery(this).css({"border-top" : "1px solid #82CCD1"});
		},
		function(){
			jQuery(".product-link").css({"border-top" : "1px solid #cccccc"});
			jQuery(".active-product").css({"border-top" : "1px solid #82CCD1"});
		}
	);
	
	// auto scroll back to top for blogs
	jQuery("#back-to-top").click(function(e){
		e.preventDefault();
		jQuery('html, body').animate({scrollTop:0}, 500);
	});
	
	jQuery(".back-button").click(function(e){
		e.preventDefault();
		 parent.history.back();
		 return false;
	});

});
