function findPos(obj) {
	var curleft = curtop = 0;
	if ( obj && obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

var nr_ie = 0;
var c_width = 0;
var c_height = 0;
var isOnCat = 0;
var isOnCatSub = 0;



$(function()
{

	$("#searchForm1").submit(function(){
			var t = $("#searchText1");
			if( t.val() == "" ){
				alert("Introduceti cuvantul cautat");
				t.focus();
				return false;
			};
			window.location = $("#searchForm1").attr("action") + "?s=" + t.val();
			return false;
		});

	$(".subcatsel").mouseover(function(){
		var t = jQuery(this);

		var curentid = t.attr("id");
		curentid = curentid.substr(6,curentid.length);
		if( t.hasClass("h") ){
			isOnCatSub = 0;
			hideMyCatsNou(curentid);
		}
		else{
			showMyCatsNou(curentid);
		}
		return false;
	});

	$(".subcatsel").hover(
		function(){
			//do nothing...
			var t = jQuery(this);

		var curentid = t.attr("id");
		curentid = curentid.substr(6,curentid.length);
		if( t.hasClass("h") ){
			isOnCatSub = 0;
			console.log("nu arat nimic,sunt pe "+curentid);
			hideMyCatsNou(curentid);
		}
		else{
			showMyCatsNou(curentid);
			console.log("arat "+curentid);
		}
		return false;
		},
		function(){
			var t = jQuery(this);
			var curentid = t.attr("id");
			curentid = curentid.substr(6,curentid.length);
			console.log(curentid);
			console.log(t.attr("id"));
			//alert(curentid);
			//setTimeout("hideMyCats(curentid)", 500);
			florinTimeout(curentid);
		}
	);
	$(".lista2subcat").hover(
		function(){
			isOnCatSub = 1;
		},
		function(){
			var t = jQuery(this);
			var curentid = t.attr("id");
			curentid = curentid.substr(13,curentid.length);
			console.log(curentid);
			isOnCatSub = 0;
			hideMyCatsT();
			//setTimeout("hideMyCats(curentid)", 500);
			florinTimeout(curentid);
		}
	);

	$("#prodlist1").click(function(){
		var t = jQuery(this);
		if( t.hasClass("h") ){
			isOnCat = 0;
			hideMyCats(1);
		}
		else{
			showMyCats(1);
		}
		return false;
	});
	$("#prodlist2").click(function(){
		var t = jQuery(this);
		if( t.hasClass("h") ){
			isOnCat = 0;
			hideMyCats(2);
		}
		else{
			showMyCats(2);
		}
		return false;
	});
	$("#prodlist3").click(function(){
		var t = jQuery(this);
		if( t.hasClass("h") ){
			isOnCat = 0;
			hideMyCats(3);
		}
		else{
			showMyCats(3);
		}
		return false;
	});
	$("#prodlist1").hover(
		function(){
			//do nothing...
		},
		function(){
			setTimeout("hideMyCats(1)", 500);
		}
	);
	$("#prodlist2").hover(
		function(){
			//do nothing...
		},
		function(){
			setTimeout("hideMyCats(2)", 500);
		}
	);
	$("#prodlist3").hover(
		function(){
			//do nothing...
		},
		function(){
			setTimeout("hideMyCats(3)", 500);
		}
	);
	//$("#lista").css("opacity", "0");
	$("#lista1").hover(
		function(){
			isOnCat = 1;
		},
		function(){
			isOnCat = 0;
			hideMyCatsT();
			setTimeout("hideMyCats(1)", 500);
		}
	);
	$("#lista2").hover(
		function(){
			isOnCat = 1;
		},
		function(){
			isOnCat = 0;
			hideMyCatsT();
			setTimeout("hideMyCats(2)", 500);
		}
	);
	$("#lista3").hover(
		function(){
			isOnCat = 1;
		},
		function(){
			isOnCat = 0;
			hideMyCatsT();
			setTimeout("hideMyCats(3)", 500);
		}
	);


	//jqueryslidemenu.buildmenu("myslidemenu", arrowimages);
});

function hideMyCatsT(){
}

function florinTimeout(cat) {

setInterval(function () {hideMyCatsNou(cat)}, 500);

}

function hideMyCats(cat){
	if( isOnCat == 1 ) return ;

	if ($.browser.msie)
		{
			if ($.browser.version.number > 6)
				{
					$("#lista"+cat).stop().animate({"opacity": "0"}, 500, function(){  $("#lista"+cat).css("display", "none"); });
				}
				else
				{
					$("#lista"+cat).css("display", "none");
				}
		}
		else {$("#lista"+cat).stop().animate({"opacity": "0"}, 500, function(){  $("#lista"+cat).css("display", "none"); });}
	$("#prodlist"+cat).removeClass("h");
	isOnCat = 0;
}
function hideMyCatsNou(cat){
	if( isOnCatSub == 1 ) return ;

	if ($.browser.msie)
		{
			if ($.browser.version.number > 6)
				{
					$("#contentsubcat"+cat).stop().animate({"opacity": "0"}, 500, function(){  $("#contentsubcat"+cat).css("display", "none"); });
				}
				else
				{
					$("#contentsubcat"+cat).css("display", "none");
				}
		}
		else {$("#contentsubcat"+cat).stop().animate({"opacity": "0"}, 500, function(){  $("#contentsubcat"+cat).css("display", "none"); });}
	$("#subcat"+cat).removeClass("h");
	isOnCatSub = 0;
}
function showMyCatsNou(cat){

	if ($.browser.msie)
		{
			if ($.browser.version.number > 6)
				{
					$("#contentsubcat"+cat).stop().css("display", "block").animate({"opacity": 1}, 500);
				}
				else
				{
					$("#contentsubcat"+cat).css("display", "block");
				}
		}
		else {$("#contentsubcat"+cat).stop().css("display", "block").animate({"opacity": 1}, 500);}
	console.log("adauga #subcat" + cat);
	$("#subcat"+cat).addClass("h");
}
function showMyCats(cat){

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




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

jQuery.fn.newsletter = function(){
return this.each(function (){
	 var t = jQuery(this);
	 t.submit(function(){
		 if( checkEmailAddress( document.getElementById( $("#"+t.attr("id")+ " input[name='newsemail']:first" ).attr("id") ), 'Y' ) == false ){
			 return false;
		 }
	 });
});
}

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();
    }
  });
}

$(function()
{
	$('input:text').hint();

	$("#newsletteremail").submit(function(){
		var t = $("#searchText1");
		if( t.val() == "" ){
			alert("Introduceti cuvantul cautat");
			t.focus();
			return false;
		};
		window.location = $("#searchForm1").attr("action") + "?q=" + t.val();
		return false;
	});
	$("#searchForm2").submit(function(){
		var t = $("#searchText2");
		if( t.val() == "" ){
			alert("Introduceti cuvantul cautat");
			t.focus();
			return false;
		};
		window.location = $("#searchForm2").attr("action") + "?q=" + t.val();
		return false;
	});
	$("#arataToate").click(function(){
		var t = jQuery(this);
		if( t.hasClass("h") ){
			isOnCat = 0;
			hideMyCats();
		}
		else{
			showMyCats();
		}
		return false;
	});
	$("#arataToate").hover(
		function(){
			//do nothing...
		},
		function(){
			setTimeout("hideMyCats()", 500);
		}
	);
	if ( $.browser.msie ){
		$('input:checkbox').css('border', 'none');
		$('input:radio').css('border', 'none');
	}
	/*
	 var optiuni = {
        resizeLgImages:     true,
        displayNav:         true,
        handleUnsupported:  'remove',
        keysClose:          ['c', 27] // c or esc
    };
	*/

	 if (!document.getElementById('pzdm5')) {
		$('#imgDetHolder a').lightBox();
	}else{
		$('#imgDetHolder .dtscrollcontentall a').lightBox();
	}


	$('#imgprodFl').click(function(){

			//alert($(this).children().attr('rel'));
			if (!$(this).children().attr('rel'))
			{
				//alert('nu este setat');
				if (!document.getElementById('pzdm5')) {
					$('#imgDetHolder a:first').click();
				}else{
					$('#imgDetHolder .dtscrollcontentall a:first').click();
				}

			}
			else{
				//alert($(this).children().attr('rel'));

				$('#imgTFl'+$(this).children().attr('rel')).click();

			}
			return false;

		});
	pozeDetaliateProdus();
});
function pozeDetaliateProdus() {
	if (!document.getElementById('pzdm5')) {
		return false;
	}
	var currentIndex = 1;
	var totalItems = 0;
	var running = 0;

	$('#pzdm5 .arr a').click(function() {
		var a = jQuery(this);

		if (totalItems < 1) {
			totalItems = $('#pzdm5 .dtscrollcontentall a').length;
		}

		if (a.hasClass('i')) {
			return false;
		}

		var directie = 1;
		if (a.parent(':first').hasClass('arrL')) {
			directie = -1;
		}

		if (running) {
			if (running == directie) {
				return false;
			}
		}

		running = directie;


		currentIndex += 4 * directie;
		if (directie == 1) {
			if (currentIndex + 4 > totalItems) {
				//console.log(currentIndex-totalItems-1);
				currentIndex = currentIndex - (4 + (currentIndex-totalItems-1));
				a.addClass('i');
			}
			$('#pzdm5 #arrL a').removeClass('i');
		}
		else {
			if (currentIndex <= 1) {
				currentIndex = 1;
				a.addClass('i');
			}
			$('#pzdm5 #arrR a').removeClass('i');
		}

		$("#pzdm5 .dtscrollcontentall").animate({
				left: (currentIndex-1) * -72 + 'px'
			}, 1000, 'linear', function (){animationStopped();} );

		return false;
	});
	function animationStopped() {
		running = 0;
	}
}


function changeSortType(x)
{
//var x = document.getElementById("selectSortType");
var unde = x.options[x.selectedIndex].value;
window.location = xcart_web_dir + "/" + unde;
}

function gabi_repair_bug_variant_avail()
{
	//alert("intru in functie: " + parseInt(document.getElementById('product_avail_txt').innerHTML) );
	if( document.getElementById('product_avail') )
	{
		if( parseInt(document.getElementById('product_avail').options[0].value) >= min_avail ) doIsAvailable();
		else doNotAvailable();
	}
}
function checkAvail()
{
	gabi_repair_bug_variant_avail();
}
function doNotAvailable(variantid)
{
	//alert("Nu e disponibil");
	var notVisible = "notVisible";

	if (variantid) {
		var h = xcart_web_dir + "/stock_notify.php?productid=" + $(".aiciid").attr("id") + "&variantid=" + variantid;
	}
	else{
		var h = xcart_web_dir + "/stock_notify.php?productid=" + $(".aiciid").attr("id");
	}

	if ($(".paddtocos").hasClass("nostockk"))
	{

	}
	else{
		$(".paddtocos").html("Cere alerta stoc");
		$(".paddtocos").attr("href",h);
		$(".paddtocos").addClass("nostockk");

	}



	//document.getElementById('txt_produs_avail').setAttribute("class", "txt_produs_avail_not_ok");
	//document.getElementById('txt_produs_avail').setAttribute("className", "txt_produs_avail_not_ok"); // for ie

	$("#product_avvail").removeClass("ch_avail");
	$("#product_avvail").addClass("ch_unavail");
	$("#product_avvail").html(txt_out_of_stock);
	//functions defined in myScripts.js
	clearAllElem(document.getElementById("product_avail"));
	var elem = document.createElement('option');
	elem.value = "0";
	elem.text = txt_out_of_stock;//defined by xcart
	addSelectElem(document.getElementById("product_avail"), elem);


	return true;
}
function doIsAvailable(variantid)
{
	//alert("E disponibil");
	var notVisible = "notVisible";
	if (variantid) {
		var h = xcart_web_dir + "/stock_notify.php?productid=" + $(".aiciid").attr("id") + "&variantid=" + variantid;
	}
	else{
		var h = xcart_web_dir + "/stock_notify.php?productid=" + $(".aiciid").attr("id");
	}
	h="javascript: if(FormValidation()) document.orderform.submit();"
	$(".paddtocos").html("Adauga produsul in cos");
	$(".paddtocos").attr("href",h);
	$(".paddtocos").removeClass("nostockk");


	$("#product_avvail").removeClass("ch_unavail");
	$("#product_avvail").addClass("ch_avail");
	$("#product_avvail").html(is_in_stoc);
	return true;
}
function clearAllElem(select)
{
select.options.length = 0;
}
function addSelectElem(select, elem)
{
try
{
select.add(elem,null);
}
catch (ex)
{
select.add(elem);
}
}
var arrowimages={down:['downarrowclass', 'down.gif', 23], right:['rightarrowclass', 'right.gif']}
var jqueryslidemenu={
animateduration: {over: 50, out: 100}, //duration of slide in/ out animation, in milliseconds

buildmenu:function(menuid, arrowsvar){
	jQuery(document).ready(function($){
		var $mainmenu=$("#"+menuid+">ul")
		var $headers=$mainmenu.find("ul").parent()
		$headers.each(function(i){
			var $curobj=$(this)
			var $subul=$(this).find('ul:eq(0)')
			this._dimensions={w:this.offsetWidth, h:this.offsetHeight, subulw:$subul.outerWidth(), subulh:$subul.outerHeight()}
			this.istopheader=$curobj.parents("ul").length==1? true : false
			$subul.css({top:this.istopheader? this._dimensions.h+"px" : 0})
			/*
			$curobj.children("a:eq(0)").css(this.istopheader? {paddingRight: arrowsvar.down[2]} : {}).append(
				'<img src="'+ (this.istopheader? arrowsvar.down[1] : arrowsvar.right[1])
				+'" class="' + (this.istopheader? arrowsvar.down[0] : arrowsvar.right[0])
				+ '" style="border:0;" />'
			)
			*/
			$curobj.hover(
				function(e){
					var $targetul=$(this).children("ul:eq(0)")
					this._offsets={left:$(this).offset().left, top:$(this).offset().top}
					var menuleft=this.istopheader? 0 : this._dimensions.w
					menuleft=(this._offsets.left+menuleft+this._dimensions.subulw>$(window).width())? (this.istopheader? -this._dimensions.subulw+this._dimensions.w : -this._dimensions.w) : menuleft
					if ($targetul.queue().length<=1) //if 1 or less queued animations
						$targetul.css({left:menuleft+"px", width:this._dimensions.subulw+'px'}).slideDown(jqueryslidemenu.animateduration.over)
				},
				function(e){
					var $targetul=$(this).children("ul:eq(0)")
					$targetul.slideUp(jqueryslidemenu.animateduration.out)
				}
			) //end hover
		}) //end $headers.each()
		$mainmenu.find("ul").css({display:'none', visibility:'visible'})
	}) //end document.ready
}
}


