$(document).ready(function(){

	// external links
	$("a[@rel='external']").click(function() {
		return !window.open($(this).attr("href"));
	});

	// fade in messages
	$('.successMsg').hide();
	$('.successMsg').fadeIn(1000);
	$('.errorMsg div').hide();
	$('.errorMsg div').fadeIn(1000);
	$('.warningMsg').hide();
	$('.warningMsg').fadeIn(1000);

	// captcha
	$("#captcha-block #reload").click( function() {
		return false;
	});
	$('.hide-with-js').hide();

	// fade Images
	$("ul#img-menu li a img, ul#catalog-listing li a img").fadeTo(100, 0.6);
	$("ul#img-menu li a img, ul#catalog-listing li a img").hover(
		function() {
			$(this).fadeTo(100, 1);
		},
		function() {
			$(this).fadeTo(100, 0.6);
		}
	);

	// Header inputs
	$("#form-search #keywords").focus(function(){
		if ($(this).val()=="Enter keywords here") {
			$(this).val("");
		}
	});
	$("#form-search #keywords").blur(function(){
		if ($(this).val()=="") {
			$(this).val("Enter keywords here");
		}
	});
	
	$("#form-newsletter #email_newsletter").focus(function(){
		if ($(this).val()=="Enter your e-mail here") {
			$(this).val("");
		}
	});
	$("#form-newsletter #email_newsletter").blur(function(){
		if ($(this).val()=="") {
			$(this).val("Enter your e-mail here");
		}
	});

	$("#form-login #header-username").focus(function(){
		if ($(this).val()=="username") {
			$(this).val("");
		}
	});
	$("#form-login #header-username").blur(function(){
		if ($(this).val()=="") {
			$(this).val("username");
		}
	});

	$("#form-login #header-password").focus(function(){
		if ($(this).val()=="password") {
			$(this).val("");
		}
	});
	$("#form-login #header-password").blur(function(){
		if ($(this).val()=="") {
			$(this).val("password");
		}
	});


	// PRODUCT LISTING HOVER IMAGE
	$("ul.product-listing li").hover(
		function(){
			$(this).prepend(hoverAppend);
			var url = $("h2 a", this).attr("href");
			$(hoverRemove+" a", this).attr("href", url);
		},
		function(){
			$(hoverRemove).remove();
		}
	);


	// ADD ANOTHER RECIPIENT
	$("#add-recipient").click(function(){
		var nr = $("#nr").val();
		var newNr = parseInt(nr) + 1;
		var recipientHtml = '\
			<br\>\
			<div class="ctrlHolder clear clearfix other'+newNr+'">\
				<label for="grams-friend-name'+newNr+'">Friend`s name: <em>*</em></label>\
				<input type="text" size="20" id="grams-friend-name'+newNr+'" name="grams[friend-name][]" value="" class="textInput" style="width: 120px;" />\
			</div>\
			\
			<div class="ctrlHolder clearfix other'+newNr+'">\
				<label for="grams-friend-email'+newNr+'">Friend`s email: <em>*</em></label>\
				<input type="text" size="20" id="grams-friend-email'+newNr+'" name="grams[friend-email][]" value="" class="textInput" style="width: 120px;" />\
				<a href="#" class="remove-recipient" rel="other'+newNr+'" onclick="removeRecipient(\'other'+newNr+'\'); return false;">Remove</a>\
			</div>';

		$("#grams-text-content").before(recipientHtml);
		$("#nr").val(newNr);

		//alert(newNr);
		return false;
	});

	$(".remove-recipient").click(function(){
		var elem = $(this).attr("rel");
		removeRecipient(elem);
		return false;
	});


	// ADD ANOTHER RECIPIENT FOR WISHLIST SEND TO FRIEND
	$("#add-recipient").click(function(){
		var nr = $("#nr").val();
		var newNr = parseInt(nr) + 1;
		var recipientHtml = '\
			<br\>\
			<div class="ctrlHolder clear clearfix other'+newNr+'">\
				<label for="wishlist-friend-name'+newNr+'">Friend`s name: <em>*</em></label>\
				<input type="text" size="20" id="wishlist-friend-name'+newNr+'" name="wishlist[friend-name][]" value="" class="textInput" style="width: 120px;" />\
			</div>\
			\
			<div class="ctrlHolder clearfix other'+newNr+'">\
				<label for="wishlist-friend-email'+newNr+'">Friend`s email: <em>*</em></label>\
				<input type="text" size="20" id="wishlist-friend-email'+newNr+'" name="wishlist[friend-email][]" value="" class="textInput" style="width: 120px;" />\
				<a href="#" class="remove-recipient" rel="other'+newNr+'" onclick="removeRecipient(\'other'+newNr+'\'); return false;">Remove</a>\
			</div>';

		$("#wishlist-text-content").before(recipientHtml);
		$("#nr").val(newNr);

		//alert(newNr);
		return false;
	});


	// GIFT WRAPPINGS IN CART
	$("#gift-wrapping").click(function(){
		var gift_wrap_catp = $("#gift_wrap_catp").val();
		var gift_wrap_cat = $("#gift_wrap_cat").val();
		var gift_wrap_product = $("#gift_wrap_product").val();
		var gift_wrap_min_order = $("#gift_wrap_min_order").val();
		var gift_add = $("#gift-wrapping").val();

		var total = $("#total-cart-price").text();
		total = total.replace("$ ", "");
		total = parseInt(total.replace('.', ''));
		//alert(total);
		var gift_price = $(this).parent().parent().parent().find(".price-item").text();
		gift_price = gift_price.replace("$ ", "");
		gift_price = parseInt(gift_price.replace('.', ''));
		//alert(gift_price)

		var new_total = String(total + gift_price);
		new_total = _setFloat(new_total, 2);

		$(this).parent().hide();
		$("#preloader").show();

		$.ajax({
			method: "post",
			url: "add-to-cart-gift-wrap.php",
			data: "id_catp="+gift_wrap_catp+"&id_cat="+gift_wrap_cat+"&id_product="+gift_wrap_product+"&quantity="+gift_wrap_min_order+"&add_gift="+gift_add,

			//beforeSend: function(){ $(target + " .ajax-loader").show(200); $(target + " .locatie-info-holder").hide(); }, //show loading just when event triggered
			//complete: function(){ $(target + " .ajax-loader").hide(200); }, //stop showing loading when the process is complete
			success: function(html){ //so, if data is retrieved, store it in html
				$("#preloader").hide();
				$("#gift-wrapper-row").html(html);
				$("#total-cart-price").text(new_total);
				//$(target + " .locatie-info-holder").html(html); //show the html inside .content div
				//$(target + " .locatie-info-holder").fadeIn(300); //animation
			} //
		}); //end $.ajax
	});
	
	
	
	// CHARITY
	$("#charity-names li").click(function(){
			var aa= $("input[type=radio]:checked").attr('id'); 
			
			var bb = [];
			var id = 0;	
			var cc;
			
			$("input[type=checkbox]:checked").each(function(){
				bb[id] =  $(this).attr('id'); 
				id++;
			});
			
			if (bb.length <1){
				cc = '';
			}
			else{
				cc = bb.join(';'); 
			}
			//alert('aa');
			//alert(cc);
			//alert(cc);
			$.ajax({
				type: "POST",
				url: "ajax-charity.php",
				data: {
					id1 : aa,
					id2: cc
					},
				success: function(data){
					$("#bla").html(data);
				}
			});			
	});
	
	$("#test .charity").click(function(){
		var aa= $("input[type=radio]:checked").attr('id'); 
		
		var bb = [];
			var id = 0;	
			var cc;
			
			$("input[type=checkbox]:checked").each(function(){
				bb[id] =  $(this).attr('id'); 
				id++;
				//$(this).attr('checked');
			});
			
			if (bb.length <1){
				cc = '';
			}
			else{
				cc = bb.join(';'); 
			}
			//alert(aa);
			//alert('cc');
			//alert(cc);
			$.ajax({
				type: "POST",
				url: "ajax-charity.php",
				data: {
					id1 : aa,
					id2: cc
					},
				success: function(data){
					$("#bla").html(data);
				}
			});			
		//alert(aa);
	});
	
	
	
	
});


function removeRecipient(itemClass) {
	$("."+itemClass).remove();

	var nr = $("#nr").val();
	var newNr = parseInt(nr) - 1;
	$("#nr").val(newNr);
	//alert(newNr);
}

// gets a String and an Integer, and creates a Float with the Integer number of decimals
function _setFloat(stringValue, nrDecimals) {
	var parteIntreaga = stringValue.substr(0, stringValue.length-nrDecimals);
	var parteZecimala = stringValue.substr(parteIntreaga.length, stringValue.length);

	floatValue = String(parteIntreaga) + '.' + String(parteZecimala);

	return floatValue;
}
