$(document).ready(function(){
	
	// **************************************************************************************************************************
	// GENERAL ACTIONS
	
	// external links
	$("a[rel='external']").click(function() {
		return !window.open($(this).attr("href"));
	});
	$('.hide-with-js').hide(); // use this class to hide the message "please open this link in new window"
	
	// fade in messages
	$('.successMsg').hide();
	$('.successMsg').fadeIn(1000);
	$('.errorMsg div').hide();
	$('.errorMsg div').fadeIn(1000);
	$('.warningMsg').hide();
	$('.warningMsg').fadeIn(1000);
	
	// non-clickable links
	$(".return-false").click( function() {
		return false;
	});
	
	
	// JUMP MENU
	$("select.jump-menu").change(function(x){
		var url = $("option:selected", this).attr("title");
		if (url.length) {
			window.location.href = url;
		}
	});
	
	
	// clear inputs
	$("#homepage-newsletter input").each(function(i){
		var inputValue = $(this).val();
		
		$(this).focus(function(){ 
			if ($(this).val()==inputValue) {
				$(this).val("");
			}
		});
		$(this).blur(function(){ 
			if ($(this).val()=="") {
				$(this).val(inputValue);
			}
		});
	});
	
	
	// Header error message
	$(".errorMsg-overlay, .errorMsg-after, .successMsg-after").hide();
	$(".errorMsg-overlay, .errorMsg-after, .successMsg-after").slideDown(300, function(){ 
		var elem = $(this);
		setTimeout(function(){
			$(elem).slideUp(300);
		}, 1200);
	});
	

	
	
	
	// END GENERAL ACTIONS
	// **************************************************************************************************************************
	
	
	// Products Submit Height
	var quantitiesHeight = $("#quantities").outerHeight();
	var replenishmentHeight = $("#replenishment").outerHeight();
	var newHeight = String(quantitiesHeight + replenishmentHeight + 5) + "px";
	var cssObj = {
		'line-height' : newHeight,
		'height' : newHeight
	}
	$("#form-cart .submit").css(cssObj);
	$("#form-cart .out-of-stock").css(cssObj);
	
	
	// Where to buy Submenu
	$("#locations a[rel='main-item']").hover(
		function(){
			$("#locations ul").hide();
			$(this).parent().find("ul").show();
		},
		function(){
		}
	);
	$("#locations").hover(
		function(){
		},
		function(){
			$("#locations ul").hide();
		}
	);
	
	// Equal heights on category page
	setTimeout(calculateHeights, 500);
	
	// IMAGES NAVIGATION
	$("#img-large .numbers li a").click(function(){
		
		$(this).parent().parent().find("a").removeClass("active");
		var elem = $(this).attr("class");
		$(this).addClass("active");
		//alert(elem);
		//var elem1 = $(this).parent().parent().parent().parent().find(".image span").attr("id");
		//alert(elem1);
		
		$(this).parent().parent().parent().parent().find(".image span").removeClass("active");
		$("#"+elem).addClass("active");
		return false;
	});
	
	
	//$("#description p").hide();
	//$("#description p#descr1").show();
	
	//Description- Ingredients
	/*$("#description a").click(function(){
		
		var elem = $(this).attr("rel");
		
		if (elem=="descr"){
			$("#description p#descr1").hide();
			$("#description p#descr2").show();
			$("#description a").attr("rel","ingr");
			$("#description a").html("Description");
		}
		else{
			$("#description p#descr1").show();
			$("#description p#descr2").hide();
			$("#description a").attr("rel","descr");
			$("#description a").html("List of Ingredients");
		}
	
	});
	*/
	
	/*$("#scent-name").change(function{
		var val = $("#scent-name").value();
		alert(val);
	});*/
	
	
	
	//hide zip code *
	//$("label[for=billing_zip_code] em").show(); 
	val_country = $("#billing_country").val();
	if (val_country != 224 &&  val_country != 38){
		//$("label[for=billing_zip_code] em").hide(); 
		$("label[for=billing_zip_code] ").html("Zip /Postal Code"); 
	}
	else{
		if (val_country == 224 ){
			$("label[for=billing_zip_code] ").html("Zip Code <em>*</em>"); 
		}
		else{
			$("label[for=billing_zip_code] ").html("Postal Code <em>*</em>"); 
		}
	}
	
	val_shipping_country = $("#shipping_country").val();
	
	if (val_shipping_country == 224 ){
		$("label[for=shipping_zip_code] ").html("Zip Code <em>*</em>"); 
	}
	else{
		$("label[for=shipping_zip_code] ").html("Postal Code <em>*</em>"); 
	}
	
	
	$("#billing_country").change(function(){
		val_country = $("#billing_country").val();
		if (val_country != 224 &&  val_country != 38){
			$("label[for=billing_zip_code] ").html("Zip /Postal Code"); 
			//$("label[for=billing_zip_code] em").hide(); 
		}
		else{
			if (val_country == 224 ){
				$("label[for=billing_zip_code] ").html("Zip Code <em>*</em>"); 
			}
			else{
				$("label[for=billing_zip_code] ").html("Postal Code <em>*</em>"); 
			}
			//$("label[for=billing_zip_code] em").show(); 
		}
	});
	
	
	$("#shipping_country").change(function(){
		val_shipping_country = $("#shipping_country").val();
		
		if (val_shipping_country == 224 ){
			$("label[for=shipping_zip_code] ").html("Zip Code <em>*</em>"); 
		}
		else{
			$("label[for=shipping_zip_code] ").html("Postal Code <em>*</em>"); 
		}
			
		
	});
	
	//where to buy
	$("#locations li a ").click(function(){
		 var elem = $(this).attr("class");
		 var id = elem.substr(4);
		 
		 //alert(id);
		 $.ajax({
			type: "POST",
			url: "ajax_get_location.php",
			data: {
				id: id
			},
			
			success: function(data){
				$('#selected_location').html(data);
			}
		});
	});
	
	
	// Fading links
	$("#main-menu ul a").fadeTo(100, 0.80);
	$("#main-menu ul a").hover(
		function(){
			$(this).fadeTo(50, 1);
		},
		function(){
			$(this).fadeTo(50, 0.80);
		}
	);
	
	
	// Banners Homepage
	/*var nrBanner = 1;*/
	nrBannerMax = $("#homepage-banners li").size();
	timer = setTimeout(getNextItem, interval);
	
	$("#banners-nav a").click(function(){
		
		clearTimeout(timer);
		
		var item = $(this).attr("rel");
		currentBanner = parseInt(item);
		
		changeImage();
		timer = setTimeout(getNextItem, interval);
		
		return false;
	});
	
	
	$("#table-press .enlarge").hide();
	$("#table-press .img-press").hover(
		function() {
			$(".enlarge", this).show();
		},
		function() {
			$(".enlarge", this).hide();
		}
	);
	
});


function calculateHeights() {
	var heightLeft = $("#content #left").height() - 46;
	var heightRight = $("#content #right").height();

	
	if ((heightLeft) >= heightRight){
		
		$("#content #right").height(heightLeft);
		$("#content #right .left #img-large").height(heightLeft-28);
		
	}
}



// Homepage Banner Slideshow
var currentBanner = 1;
var nrBannerMax;
var timer;
var interval = 7000;
function changeImage() {
	$("#homepage-banners li").hide();
	$("#banner"+currentBanner).fadeIn(300);
	
	$("#banners-nav a").removeClass("active");
	$("#banners-nav a[rel='"+currentBanner+"']").addClass("active");
}
function getNextItem() {
	var next;
	if (parseInt(currentBanner)==parseInt(nrBannerMax)) {
		next = 1;
	} else {
		next = currentBanner + 1;
	}
	currentBanner = next;
	changeImage();
	clearTimeout(timer);
	timer = setTimeout(getNextItem, interval);
}

