var timeout         = 500;
var closetimer		= 100;
var ddmenuitem      = 0;

function jsddm_open() {
	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('.drop-down').eq(0).css('visibility', 'visible');
	ddmenuitem.css('display', 'block');
}
function jsddm_close() {
	if(ddmenuitem) {
		ddmenuitem.css('visibility', 'hidden');
		ddmenuitem.css('display', 'none');
	}
}

function jsddm_timer() {
	//closetimer = window.setTimeout(jsddm_close, timeout);
	jsddm_close();
}

function jsddm_canceltimer() {
	if(closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

document.onclick = jsddm_close;


$(document).ready(function(){
	
	// 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;
	});
	
	
	
	// DROP-DOWN
	$('#main-menu ul li.main-menu-item').bind('mouseover', jsddm_open);
	$('#main-menu .drop-down').bind('mouseout',  jsddm_timer);
	
	
	
	// DRAG & DROP
	$('a.draggable').draggable({
		containement: "#wrapper",
		scroll: false,
		stack: { group: 'a.draggable', min: 100 },
		opacity: 0.9,
		helper: 'clone',
		revert: 'invalid',
		cursorAt: { cursor: 'move', top: 20, left: 20 }
	});
	
	$("#shopping-bag").droppable({
		accept: "a.draggable",
		//hoverClass: 'ui-state-hover',
		activeClass: 'ui-state-active',
		drop: function(event, ui) {
			
			// get the ID and TABLE set on the draggable element
			var id_product = $(ui.draggable).attr("rev");
			var ids = $(ui.draggable).attr("rel");
			// hide the content, display the preloader
			$('#shopping-bag-content').html("<span>Loading</span>");
			// disable adding while loading
			$('a.draggable').draggable('disable');
			
			$.ajax({ 
				method: "post",
				url: "add-to-cart-drag.php",
				data: "id_product="+id_product+"&ids="+ids, 
				
				success: function(html){ //so, if data is retrieved, store it in html 
					//alert(html);
					$("#shopping-bag-content").html(html); //show the html inside .content div 
				} //
			}); //end $.ajax
			
			
			// re-enable after all is done
			$('a.draggable').draggable('enable');
		}
	});
	
	
	// INPUTS
	$("#form-newsletter input, #form-search 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);
			}
		});
	});
	
	$("#form-press button").hide();
	
	// JUMP MENU
	$("select.jump-menu").change(function(x){
		var url = $("option:selected", this).attr("title");
		if (url.length) {
			window.location.href = url;
		}
	});
	
	
	// PRESS AJAX
	$("#press-list li a.tooltip").click(function(){
		var year = $("#year").val();
		var id_cat_press = $("#category").val();
		var id_press = $(this).attr("rel");
		
		$.ajax({ 
			method: "get",
			url: "inc-pag/press/press-details.php",
			data: "ajax=true"+"&year="+year+"&id_cat_press="+id_cat_press+"&id_press="+id_press, 
			
			beforeSend: function(){ 
				$(".ajax-loader").show(200);
				$("#press #press-details").hide();
			}, //show loading just when event triggered
			complete: function(){ 
				$(" .ajax-loader").hide(200);
				$("#press #press-details").show();
			}, //stop showing loading when the process is complete 
			
			success: function(html){ //so, if data is retrieved, store it in html 
				//alert(html);
				$("#press #press-details").html(html); //show the html inside .content div 
			} //
		}); //end $.ajax
		
		return false;
	});
	
	
	//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>"); 
		}
	}
	
	$("#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(); 
		}
	});
	
	
	
});
