$(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;
	});
	
	
	// JUMP MENU
	$("select.jump-menu").change(function(x){
		var url = $("option:selected", this).attr("title");
		if (url.length) {
			window.location.href = url;
		}
	});
	
	
	// clear inputs
	$("#form-login input, #form-search input, #form-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
	$("#form-search .errorMsg-overlay, #form-login .errorMsg-overlay").hide();
	$("#form-search .errorMsg-overlay, #form-login .errorMsg-overlay").fadeIn(300, function(){ 
		var elem = $(this);
		setTimeout(function(){
			$(elem).fadeOut(300);
		}, 1200);
	});
	
	
	
	// PRODUCT LISTING
	// show the button on Hover
	$("#cat-listing li a.img").hover(
		function(){
			var elem = $(this).parent();
			$(".sneak-peek").hide();
			$(".sneak-peek", elem).show();
		},
		function(){
		}
	);
	// hide the button when leaving the UL
	$("#cat-listing").hover(
		function(){
		},
		function(){
			var elem = $(this).parent();
			$(".sneak-peek", elem).hide();
		}
	);
	
	
	$(".sneak-peek").click(function(){
		
		var offSet = $(this).offset();
		var offSetWrapper = $("#wrapper").offset();
		var offSetContent = $("#content").offset();
		
		$(".popup-sneak-peek").hide();
		
		var cssObj = {
			'top' : (offSet.top - offSetContent.top - 1)+"px", // scadem offsetul contentului si 1 (borderul popupului)
			'left' : (offSet.left - offSetWrapper.left - 368)+"px" // scadem offsetul wrapperului si latimea popupului
		}
		$(".popup-sneak-peek").css(cssObj);
		//alert(offSet.top + ' - ' + offSet.left)
		
		$(".popup-sneak-peek").fadeIn(200);
		
		var ids = $(this).attr("rel");
		loadSneakPeek(ids);
		
		return false;
	});
	
	$(".popup-sneak-peek .close, .popup-sneak-peek .sneak-peek-faded").click(function(){
		$(".popup-sneak-peek").fadeOut(200);
		return false;
	});
	
	
	
	// PRODUCT DETAILS
	/*$("#products-table tr").hover(
		function(){
			var elem = $(this);
			$(".product-image img").hide();
			$(".product-image img", this).show();
			
			//$("#products-table tr").css("background","transparent");
			//$(this).css("background","url(images/products/hover-product.png) no-repeat right 50%");
			$("#products-table tr").removeClass("active-product");
			$(this).addClass("active-product");
			$("#products-table tr.special").removeClass("active-product");
		},
		function(){
			
		}
	);*/
	
	// TABS
	var firstTabElement = $("ul.tabs li a.link-tabs:first");
	var firstTabElementName = $(firstTabElement).attr("id");
	$(firstTabElement).addClass("link-tabs-active");
	$("div.panel").hide();
	$("#panel-" + firstTabElementName).fadeIn(500);
	
	$("ul.tabs li a.link-tabs").click(function(){
		$("ul.tabs li a.link-tabs").removeClass("link-tabs-active");
		$(this).addClass("link-tabs-active");
		
		var thisElementName = $(this).attr("id");
		$("div.panel").hide();
		$("#panel-" + thisElementName).fadeIn(500);
		
		return false;
	});
	
	
	// SECRETE DE FRUMUSETE
	$("#secrete-de-frumusete #faq-list li a").click( function() {
		$(this).parent().find("div").slideToggle("300");
		return false;
	});
	var toggle_all = "closed";
	$("#secrete-de-frumusete #toggle-all").click( function() {
		if (toggle_all=="closed") {
			toggle_all = "opened";
			$("#secrete-de-frumusete #faq-list li div").slideDown(300);
		} else {
			toggle_all = "closed";
			$("#secrete-de-frumusete #faq-list li div").slideUp(300);
		}
		return false;
	});
	
	
	// INTREBARI FRECVENTE
	$("#intrebari-frecvente #faq-list li a").hover( function() {
		var answer = $(this).parent().find("div").html();
		$("#intrebari-frecvente #content .right").show();
		$("#intrebari-frecvente #content .right #answer").html(answer);
		return false;
	});
	
	
	// ADD ANOTHER RECIPIENT
	$("#add-recipient").click(function(){
		var nr = $("#nr").val();
		var newNr = parseInt(nr) + 1;
		var recipientHtml = '\
			\
			<div class="ctrlHolder clearfix other'+newNr+'">\
				<label for="refer-friend-email'+newNr+'">'+ text_friends_email +': <em>*</em></label>\
				<input type="text" size="20" id="refer-friend-email'+newNr+'" name="refer[friend-email][]" value="" class="textInput" style="width: 120px;" />\
				&nbsp;&nbsp;&nbsp;<a href="#" class="remove-recipient" rel="other'+newNr+'" onclick="removeRecipient(\'other'+newNr+'\'); return false;">'+text_remove +'</a>\
			</div>';
		
		$("#before-submit").before(recipientHtml);
		$("#nr").val(newNr);
		
		//alert(newNr);
		return false;
	});
	//$text_recomanda[$selected_lang][friend_email]
	
	$(".remove-recipient").click(function(){
		var elem = $(this).attr("rel");
		removeRecipient(elem);
		return false;
	});
	
	
	// Added to cart
	$("#added-to-cart").hide();
	setTimeout(function(){
		$("#added-to-cart").fadeIn(1000);
	}, 300);
	setTimeout(function(){
		$("#added-to-cart").fadeOut(1000);
	}, 3000);
	
});

function loadSneakPeek(id) {
	
	$(".popup-sneak-peek .ajax-data").html("");
	$(".popup-sneak-peek .preloader").fadeIn(100);
	
	$.ajax({ 
		method: "post",
		url: "load-sneak-peek-data.php",
		data: "ids="+id+"&lang="+selected_lang,
		
		success: function(html){ //so, if data is retrieved, store it in html 
			$(".popup-sneak-peek .preloader").hide();
			$(".popup-sneak-peek .ajax-data").html(html);
		} //
	}); //end $.ajax
}


function removeRecipient(itemClass) {
	$("."+itemClass).remove();
	
	var nr = $("#nr").val();
	var newNr = parseInt(nr) - 1;
	$("#nr").val(newNr);
	//alert(newNr);
}



