var adrMsg = "adresa de e-mail";
function focusNewsletterE( f )
{
	if( f.value == adrMsg ) f.value = "";
}
function blurNewsletterE( f )
{
	if( trimAll(f.value) == "" ) f.value = adrMsg;
}
function changeSortType(x)
{
	//var x = document.getElementById("selectSortType");
	var unde = x.options[x.selectedIndex].value;
	window.location = xcart_web_dir + "/" + unde;
}
function trimAll(sString)
{
	while (sString.substring(0,1) == ' ')                              sString = sString.substring(1, sString.length);
	while (sString.substring(sString.length-1, sString.length) == ' ') sString = sString.substring(0,sString.length-1);

	return sString;
}
function addSelectElem(select, elem)
{
	try
	{
		 select.add(elem,null); // standards compliant; doesn't work in IE
	}
	catch (ex)
	{
		select.add(elem); // IE
	}
	//alert( "am adaugat "+  elem.value + " " + elem.text );
}
function clearAllElem(select)
{
	select.options.length = 0;
	//var i;
	//for( i = 0; i <= select.length; i++) select.remove( select.options[i] );
}
function focusCupon( f )
{
	if( f.value == cuponMsg ) f.value = "";
}
function blurCupon( f )
{
	if( trimAll(f.value) == "" ) f.value = cuponMsg;
}
function submitCupon()
{
	if( trimAll(document.couponform.coupon.value) != '' && document.couponform.coupon.value != cuponMsg ) document.couponform.submit();
	else alert("Introduceti codul cuponului");
}
function changeSort(q, sort)
{
	self.location="_search.php?q=" + q + "&sort="+sort;
	//self.location= xcart_web_dir + "/cauta-haine/" + q + "?sort=" + sort;
}

function advanced_submit(){

	if (document.smart_search_advanced.all_the_words.value == '' && document.smart_search_advanced.exact_phrase.value == '' && document.smart_search_advanced.at_least.value == '' && document.smart_search_advanced.without_the_words.value == ''){
		alert ('You must fill in at least one text field.');
		}

	else if (document.smart_search_advanced.all_the_words.value == '' && document.smart_search_advanced.exact_phrase.value == '' && document.smart_search_advanced.at_least.value == '' && document.smart_search_advanced.without_the_words.value != ''){
		alert ('You must fill in at least one other text field.');
		}
	else document.smart_search_advanced.submit();
	}

var isOnCat1 ;
var isOnCat2;
var isOnCat3;
var isOnCat4;
var isOnCat5;

$(document).ready(function(){


});

function checkCupon(thisform){
alert("merge");
return false;
}

$(function()
{

	checkavailImgbutt();
	attachDetImgButton();
	attachThumbImgButton();
	checkavailLivImgbutt();

	$(".linkblue").css({"text-decoration":"underline"});

	 $("table input:submit").css({"font-weight":"bold",background:"#F17DB1",border:"0px none", color:"#FFF",cursor:"pointer"})
	 $("table input:button").css({"font-weight":"bold",background:"#F17DB1",border:"0px none", color:"#FFF",cursor:"pointer"})

	$(".star-rating a").mouseover(function(){
		$(".curentstar").css({display:"none"});
	});
	$(".star-rating a").mouseout(function(){
		$(".curentstar").css({display:"block"});
	});
	$(".star-rating a").click(function(){
		var xx  =  parseInt($(this).attr("rel"));
		$(".curentstar").css({width: (xx*22)+"px"});
		$("#review_ratting").val(xx);
		return false;
	});
	$(".rwreset").click(function(){
		$("#review_ratting").val(0);
		$(".curentstar").css({width: "0px"});
		document.forms['reviewform'].reset();
		$('input:text').blur();
		$('textarea').blur();
		return false;
	});

	$('a[@href^="#"]').each(function(i){
			$(this).click(function(){
				$.scrollTo( $(this).attr("href"), 1000 );
				return false;
			});
		});
	$("#custom_friend").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 100,'frameWidth':550,'frameHeight':350, 'overlayShow': false });

	$(".stelute").stelute();
	$('.tool').tooltip();
	$('input:text').hint();
	$('textarea').hint();
	$("#product2wishlist").click(function(){

	$("#orderForm").attr("action", "cart.php?mode=add2wl").submit();;
	return false;

	});


	$("#rprod").boxCumparate();

	$(".filtruselection").change(function(){

		location.href=$(this).val();

	});

	$(".categorybox").hover(
		function(){
			$(this).css({cursor:"pointer"});
			$(".categoryt h2 a", this).addClass("activ");


		},
		function(){
			$(this).css({cursor:"default"});
			$(".categoryt h2 a", this).removeClass("activ");
		}
	);
	$(".categorybox").click(
		function(){
			location.href=$(".categoryt h2 a", this).attr("href");
	});



	//alert($("#lista1").height());
	$("#lista1 #zoom_iframe").css({height: $("#lista1").height()+"px" });

	//alert($("#lista2").height());
	$("#lista2 #zoom_iframe").css({height: $("#lista2").height()+"px" });

	//alert($("#lista3").height());
	$("#lista3 #zoom_iframe").css({height: $("#lista3").height()+"px" });

	//alert($("#lista4").height())
	$("#lista4 #zoom_iframe").css({height: $("#lista4").height()+"px" });

	//alert($("#lista5").height());
	$("#lista5 #zoom_iframe").css({height: $("#lista5").height()+"px" });

	$("#prodlist1").mouseover(function(){
		var t = jQuery(this);
		if( t.hasClass("h") ){
			isOnCat1 = 1;
			//hideMyCats(1);
		}
		else{
			showMyCats(1);
		}
		return false;
	});
	$("#prodlist2").mouseover(function(){
		var t = jQuery(this);
		if( t.hasClass("h") ){
			isOnCat2 = 1;
			//hideMyCats(2);
		}
		else{
			showMyCats(2);
		}
		return false;
	});
	$("#prodlist1").click(function(){return false;});
	$("#prodlist2").click(function(){return false;});
	$("#prodlist3").click(function(){return false;});
	$("#prodlist4").click(function(){return false;});
	$("#prodlist5").click(function(){return false;});

	$("#prodlist3").mouseover(function(){
		var t = jQuery(this);
		if( t.hasClass("h") ){
			isOnCat3 = 1;
			//hideMyCats(3);
		}
		else{
			showMyCats(3);
		}
		return false;
	});
	$("#prodlist4").mouseover(function(){
		var t = jQuery(this);
		if( t.hasClass("h") ){
			isOnCat4 = 1;
			//hideMyCats(4);
		}
		else{
			showMyCats(4);
		}
		return false;
	});
	$("#prodlist5").mouseover(function(){
		var t = jQuery(this);
		if( t.hasClass("h") ){
			isOnCat5 = 1;
			//hideMyCats(5);
		}
		else{
			showMyCats(5);
		}
		return false;
	});
	$("#prodlist1").mouseout(function(){
			setTimeout("hideMyCats(1)", 300);
		}
	);
	$("#prodlist2").mouseout(function(){
			setTimeout("hideMyCats(2)", 300);
		}
	);
	$("#prodlist3").mouseout(function(){
			setTimeout("hideMyCats(3)", 300);
		}
	);
	$("#prodlist4").mouseout(function(){
			//alert("iese");
			setTimeout("hideMyCats(4)", 300);
		}
	);
	$("#prodlist5").mouseout(function(){
			setTimeout("hideMyCats(5)", 300);
		}
	);
	//$("#lista").css("opacity", "0");
	$("#lista1").mouseout(function(){
			isOnCat1 = 0;
			hideMyCatsT();
			setTimeout("hideMyCats(1)", 300);
		}
	);
	$("#lista2").mouseout(function(){
			isOnCat2 = 0;
			hideMyCatsT();
			setTimeout("hideMyCats(2)", 300);
		}
	);
	$("#lista3").mouseout(function(){
			isOnCat3 = 0;
			hideMyCatsT();
			setTimeout("hideMyCats(3)", 300);
		}
	);
	$("#lista4").mouseout(function(){
			isOnCat4 = 0;
			hideMyCatsT();
			setTimeout("hideMyCats(4)", 300);
		}
	);
	$("#lista5").mouseout(function(){
			isOnCat5 = 0;
			hideMyCatsT();
			setTimeout("hideMyCats(5)", 300);
		}
	);
	$("#lista1").mouseover(function(){
			isOnCat1 = 1;
		}
	);
	$("#lista2").mouseover(function(){
			isOnCat2 = 1;
		}
	);
	$("#lista3").mouseover(function(){
			isOnCat3 = 1;
		}
	);
	$("#lista4").mouseover(function(){
			isOnCat4 = 1;
		}
	);
	$("#lista5").mouseover(function(){
			isOnCat5 = 1;
		}
	);


});

function hideMyCatsT(){
}

function hideMyCats(cat){

	if (cat == '1')
	{
		if( isOnCat1 == 1 ) return ;
	}
	if (cat == '2')
	{
		if( isOnCat2 == 1 ) return ;
	}
	if (cat == '3')
	{
		if( isOnCat3 == 1 ) return ;
	}
	if (cat == '4')
	{
		if( isOnCat4 == 1 ) return ;
	}
	if (cat == '5')
	{
		if( isOnCat5 == 1 ) return ;
	}

	hideC(cat);




	isOnCat1 = 0;
	isOnCat2 = 0;
	isOnCat3 = 0;
	isOnCat4 = 0;
	isOnCat5 = 0;
}
function hideC(cat){
if ($.browser.msie)
		{
			if ($.browser.version.number > 6)
				{
					$("#lista"+cat).stop().animate({"opacity": "0"}, 300, function(){  $("#lista"+cat).css("display", "none"); });
				}
				else
				{
					$("#lista"+cat).css("display", "none");
				}

		}
		else {$("#lista"+cat).stop().animate({"opacity": "0"}, 300, function(){  $("#lista"+cat).css("display", "none"); });}
	$("#prodlist"+cat).removeClass("h");

}
function showMyCats(cat){
	for(i=1;i<=5;i++)
	{
		if (cat != i )
		{
			hideC(i);
		}

	}


	if ($.browser.msie)
		{
			if ($.browser.version.number > 6)
				{
					$("#lista"+cat).stop().css("display", "block").animate({"opacity": 1}, 300);
				}
				else
				{
					$("#lista"+cat).css("display", "block");
				}
		}
		else {$("#lista"+cat).stop().css("display", "block").animate({"opacity": 1}, 300);}




	$("#prodlist"+cat).addClass("h");
}


jQuery.fn.hint = function () {
  return this.each(function (){
    /* get jQuery version of 'this'*/
    var t = jQuery(this);
    /* get it once since it won't change*/
    var title = t.attr('alt');
    /* only apply logic if the element has the attribute*/
    if (title) {
      /* on blur, set value to title attr if text is blank*/
      t.blur(function (){
        if ($.trim(t.val()) == '') {
          t.val(title);
        }
      });
      /* on focus, set value to blank if current value */
      /* matches title attr*/
      t.focus(function (){
        if (t.val() == title) {
          t.val('');
        }
      });

      /* clear the pre-defined text when form is submitted*/
      t.parents('form:first()').submit(function(){
          if (t.val() == title) {
              t.val('');
          }
      });

      /* now change all inputs to title */
      t.blur();
    }
  });
}



jQuery.fn.stelute = function(){
	return this.each(function(){
		var t = jQuery(this);

		var h = $(t).children(".h");
		var h_w = h.css("width");
		var _h_w = h_w; /* - variabila asta e READ ONLY - folosita pentru reset de formular */
		var hasInput = $(h).find('input:first');
		if( hasInput.length == 0 ){
			hasInput = 0;
		}
		var isBusy = 0;
		function voteReceived(data, note){
			var status = data.getElementsByTagName('status')[0].childNodes[0].nodeValue;
			var mesaj =  data.getElementsByTagName('mesaj')[0].childNodes[0].nodeValue;
			isBusy = 0;
			if( status == "error" ){
				alert(mesaj);
			}
			else if( status == "success" ){
				h_w =  data.getElementsByTagName('new_rank')[0].childNodes[0].nodeValue;
				note = parseInt(h_w);
				h_w = (h_w*22) + "px";
				h.css("width", h_w);
				//$(t).children(".s").css("display", "none");
				var n = parseInt(data.getElementsByTagName('voturi')[0].childNodes[0].nodeValue);
				$("#nr_voturi").html(n);
				$(".voturi").css('display', '');
				alert(mesaj);
			}
		}

		var $aa = $(t).find(".s a").each(function(){
			var a = jQuery(this);
			var note = a.attr("class");
			note = parseInt(note.substring(2));
			a.mouseover(function(){
				h.css("width", (note*22)+"px");
				//console.log(h.css("width") + " " + (note*20) + " " + hasInput);
			});
			a.mouseout(function(){
				h.css("width", h_w);
			});
			if( hasInput ){
				a.click(function(event){
					h_w = (note*22)+"px";
					hasInput.val(note);
					h.css("width", h_w);
					return false;
				});
			}
			else{
				a.click(function(){
					if( isBusy ){
						return false;
					}
					isBusy = 1;
					var id = $("h1.pTtl").attr("id");
					id = id.substring(8);
					$.get(xcart_web_dir+"/placeVote.php", {
						"vote": note,
						"productid": id
						},
						function(msg){
							voteReceived(msg, note);
						},
						"xml"
					);
					return false;
				});
			}
		});
	});
}

jQuery.fn.tooltip = function(){
	this.each(function(){
		if ($('#toolbox').length > 0)
		{
			//var toolbox = document.getElementById('toolbox');
		}
		else
		{
			//alert("nu exista .Il crezz");
			var div = document.createElement('div');
			$(div).attr('id', 'toolbox').appendTo('body');
		}

		var hint = jQuery(this);
		var toolbox = document.getElementById('toolbox');

		var rel  = hint.attr("rel");
		var hintcontent = $("#hint"+rel).html();
		var pos = hint.offset({ border: true, padding: true });
		var hinttop = pos["top"];
		var hintleft = pos["left"];
		var hintheight = $("#hint"+rel).height();
		//var hintheight = $("#hint"+rel).innerHeight();
		hint.mouseover(function(){
			$(toolbox).css({top:(hinttop-hintheight)+"px",left:(hintleft-17)+"px",height:hintheight+"px"}).html(hintcontent);
			$(toolbox).fadeIn("fast");
		});
		hint.mouseout(function(){
			$(toolbox).html("");
			$(toolbox).fadeOut("fast");

		});
		hint.click(function(){
			return false;
		});
	});
}
jQuery.fn.boxCumparate = function(){
	return this.each(function(){
		var t =jQuery(this);
		var adaug = $(t).find('a.adaug:first');
		adaug.click(function(){
			var checked = $(t).find(".checkMe input.c:checked");
			if( checked.length == 0 ){
				return false;
			}
			var ids = "";
			id = "";
			checked.each(function(){
				ids = ids + "," + jQuery(this).val();
			});
			ids = ids.substring(1);
			window.location = xcart_web_dir + "/cart.php?mode=multipleAdd&ids=" + ids;
			return false;
		});
	});
}



function attachDetImgButton()
{

	var number = $(".pimagesblock .pimages").size();
	//alert(number);


	$("#pimgnext").click(function(){
		var rel = $(this).attr("rel");
		var relnext =parseInt(rel) + 1;
		//alert(relnext);
		if (number > relnext )
		{
			$(".pimagesblock").animate({ left: ((-rel)*357) }, "normal" );
			$("#pimgnext").attr({rel: relnext});
		}
		else
		{
			if (number == relnext )
			{
				$(".pimagesblock").animate({ left: ((-rel)*357) }, "normal" );
				$("#pimgnext").attr({rel: relnext});
				$(this).fadeOut("normal");
			}
		}
		$("#pimgprev").attr({rel: rel});
		checkavailImgbutt();
		return false;
	});
	$("#pimgprev").click(function(){
		var rel = $(this).attr("rel");
		var relnext =parseInt(rel) - 1;
		//alert(relnext);
		if (relnext > 0 )
		{
			$(".pimagesblock").animate({ left: ((-relnext)*357) }, "normal" );
			$("#pimgprev").attr({rel: relnext});
			//checkavailImgbutt();
		}
		else
		{
			if (0 == relnext )
			{
				$(".pimagesblock").animate({ left: ((-relnext)*357) }, "normal" );
				$("#pimgprev").attr({rel: relnext});
				$(this).fadeOut("normal");
			}
		}
		$("#pimgnext").attr({rel: parseInt(rel)});
		checkavailImgbutt();
		return false;
	});

}

function checkavailImgbutt()
{

	//alert ("verific");

	var rel = $("#pimgnext").attr("rel");

	rel = parseInt(rel)+1 ;
	//alert("attr next = "+rel);
	if ($("#pimg"+rel).length > 0)
		{
			//alert("next");
			$("#pimgnext").fadeIn("normal");
		}
		else
		{
			$("#pimgnext").fadeOut("normal");
		}


	var rel = $("#pimgprev").attr("rel");
	if ($("#pimg"+rel).length > 0)
		{

			$("#pimgprev").fadeIn("normal");
		}
		else
		{
			$("#pimgprev").fadeOut("normal");
		}

}
function attachThumbImgButton()
{

	$(".thumblkink").click(function(){
		var rel = $(this).attr("rel");
		var relreal = parseInt(rel) - 1 ;
		$(".pimagesblock").animate({ left: (-relreal*357) }, "normal" );
		$("#pimgnext").attr({rel: rel});
		$("#pimgprev").attr({rel: relreal});
		checkavailImgbutt();
		return false;
	});

}
function attachLivImgButton()
{
	var number = $(".pimagesblock .pimages").size();
	//alert(number);
	$("#pimgnext").click(function(){
		var rel = $(this).attr("rel");
		var relnext =parseInt(rel) + 1;
		//alert(relnext);
		if (number > relnext )
		{
			$(".pimagesblock").animate({ left: ((-rel)*357) }, "normal" );
			$("#pimgnext").attr({rel: relnext});
		}
		else
		{
			if (number == relnext )
			{
				$(".pimagesblock").animate({ left: ((-rel)*357) }, "normal" );
				$("#pimgnext").attr({rel: relnext});
				$(this).fadeOut("normal");
			}
		}
		$("#pimgprev").attr({rel: rel});
		checkavailImgbutt();
		return false;
	});
	$("#pimgprev").click(function(){
		var rel = $(this).attr("rel");
		var relnext =parseInt(rel) - 1;
		//alert(relnext);
		if (relnext > 0 )
		{
			$(".pimagesblock").animate({ left: ((-relnext)*357) }, "normal" );
			$("#pimgprev").attr({rel: relnext});
			//checkavailImgbutt();
		}
		else
		{
			if (0 == relnext )
			{
				$(".pimagesblock").animate({ left: ((-relnext)*357) }, "normal" );
				$("#pimgprev").attr({rel: relnext});
				$(this).fadeOut("normal");
			}
		}
		$("#pimgnext").attr({rel: parseInt(rel)});
		checkavailImgbutt();
		return false;
	});
}
var stepLiv = 0;
function checkavailLivImgbutt()
{

	var limitstep = $(".cartLivrareGratuita .prod").size();
	$(".cartLivrareGratuita .next").click(function(){
		if (limitstep >= (stepLiv+6))
		{
			stepLiv=parseInt(stepLiv)+1;
			$(".cartLivrareGratuita .scroll").animate({ left: ((-stepLiv)*125) }, "normal" );
		}
		return false;
	});
	$(".cartLivrareGratuita .prev").click(function(){
		if (stepLiv > 0)
		{
			stepLiv=parseInt(stepLiv)-1;
			$(".cartLivrareGratuita .scroll").animate({ left: ((-stepLiv)*125) }, "normal" );
		}
		return false;
	});
}

function updatePuncteCadou(price) {
	if (!document.getElementById('puncteCadou') || typeof(puncteCadou) != "object") {
		return false;
	}
	if (puncteCadou.rateType == "$") {
		var puncte = parseInt(price * puncteCadou.rate);
	}
	else if (puncteCadou.rateType == "%") {
		var puncte = parseInt((price * puncteCadou.rate) / 100);
	}
	else {
		var puncte = 0;
	}
	if (puncte > 0) {
		$("#puncteCadou").html(puncte);
		if (puncte ==1) {
			$("#puncteCadouE").html("");
		}
		else {
			$("#puncteCadouE").html("e");
		}
		$("#pctWrp").removeClass("hide");
	}
	else {
		$("#pctWrp").addClass("hide");
	}
	//console.log(price + " " + typeof(puncteCadou) + " " + puncte);
}

