$PATH = "/";

$(document).ready(function() {
	
	$("#info_and_price_placeholder").append($("#product_info_price_stock"));
	
	$("#stars_div IMG").click(function() {
		if (rateProduct) rateProduct($(this).prevAll().andSelf().length);
	});

	$("#stars_div IMG").mouseenter(function() {
		if (!$("#stars_roller").length)
			$("#stars_div").append('<div id="stars_roller" style="position: absolute; left:90px; top:1px; width: 700px; color: #ff6600;">Acordati nota: <b id="stars_nota"></b></div>');
		$(this).prevAll("IMG").andSelf().addClass('stars-over').removeClass('stars-over-off');
		$(this).nextAll("IMG").addClass('stars-over-off').removeClass('stars-over');
		$("#stars_nota").html($(this).prevAll().andSelf().length);
	});
	
	$("#stars_div").mouseleave(function() {
		$("#stars_div IMG").removeClass('stars-over').removeClass('stars-over-off');
		$("#stars_roller").remove();
	});	
	
	$("#shipping_info_more").click(function(){
		$("#shipping_info_more_div").load($(this).attr("href")+"&ajax");
		$(this).remove();
		return false;
	});
});


$(document).ready(function() {
	
	if ($(".carousel_btn").length)
	{	
		$carousel_interval = false;
		$carousel_interval_allowed = true;
		
		carouselBtnClicker = function(elem)
		{
			$("#carousel .carousel").each(function() { $(this).css('z-index', 50); });
			$("#carousel .carousel").stop(true).fadeOut('slow');
			$("#carousel_"+$(elem).html()).css('z-index', 100).stop(true).fadeTo('slow', 1, function() { $carousel_interval_allowed = true; } ).show();
			$("#carousel_nav span.carousel_item").removeClass('carousel_item_selected');
			$("#carousel_btn_"+$(elem).html()).addClass('carousel_item_selected');
		}
		
		$(".carousel_btn").click(function() {
			if ($carousel_interval) { clearInterval($carousel_interval); $carousel_interval = false; }
			carouselBtnClicker(this);
			$(this).blur();
			return false;
		});

		$(window).load(function() {
			nextCarousel = function() {
				if (!$carousel_interval_allowed) return;
				$carousel_interval_allowed = false;
				var $c = $("#carousel_nav").children(".carousel_item_selected").next().children("A");
				if (!$c.length) $c = $("#carousel_nav").children(":first-child").children("A");			
				carouselBtnClicker($c);
			}
			$carousel_interval = setInterval(nextCarousel, 3000);
		});
	}

	if ($('#additional_gallery a').lightBox)
		$('#additional_gallery a').lightBox({fixedNavigation:false
				, txtImage : 'Imaginea'
				, txtOf : 'din'
				, imageBtnPrev: $PATH+'resources/jquery/images/lightbox-btn-prev.gif'
				, imageBtnNext: $PATH+'resources/jquery/images/lightbox-btn-next.gif'
				, imageLoading:	$PATH+'resources/jquery/images/lightbox-ico-loading.gif'
				, imageBtnClose:$PATH+'resources/jquery/images/lightbox-btn-close.gif'
				, imageBlank:	$PATH+'resources/jquery/images/lightbox-blank.gif'
		});	
	
	var s = 'Cosul este gol';
	if (cart_content.quantity > 2) s = cart_content.quantity + " produse";
	else if (cart_content.quantity == 1) s = "un produs";
	$("#cart_nr_prd").html(s);	
	$("#cart_total").html((0+cart_content.total).toFixed(2) + " RON");

	$("#adv_search_form").submit(function() {
		var $ok = ($(this).find('#fld_search').val().length > 0) || ($(this).find('#fld_group').val().length > 0) || ($(this).find('#fld_editor').val().length > 0);
		if (!$ok) alert("Va rugam sa selectati cel putin un criteriu principal");
		return $ok;
	});
		
});


$(document).ready(function() {
	$(".img_fader").mouseenter(function(){
		$(this).find('IMG').fadeTo('normal', 0.1);
	});
	
	$(".img_fader").mouseleave(function(){
		$(this).find('IMG').fadeTo('normal', 1);
	});
});

$(document).ready(function() {
	
	$cauta_text = 'Cauta o carte sau un autor';	
	var $input = $("#search_field");	
	if ($input.val() == '') { $input.blur(); $input.val($cauta_text); }	
	$input.focus(function(){ if ($(this).val() == $cauta_text) $(this).val(''); else $(this).select(); });	
	$input.blur(function(){ if ($(this).val() == '') $(this).val($cauta_text); });
	$("#search_form").submit(function() { var v = $("#search_field").val(); return (v != '') && (v != $cauta_text); });
	
	var $results = $(document.createElement("ul"));
	$results.addClass("search_ajax").appendTo('#search_field_container');
	var $text_content = "";
	var $current_search = "";
	var $query_active = false;
	var theInterval = false;
	
	$input.attr("autocomplete", "off");

	if ($.browser.mozilla)
		$input.keypress(processSpecialKey);
	else
		$input.keydown(processSpecialKey);

	$input.keyup(processKey);
	
	$('BODY').click(function() { closeWindow(); });
	
	function processSpecialKey(e) 
	{
		var letItGo = false;

		switch(e.keyCode)
		{		
			case 38: /* up */     prevResult(); break;		
			case 40: /* down */	  nextResult(); break;		
			case 27: /*	escape */ closeWindow(); break;				
			case 13: /* return */ letItGo = !selectCurrentResult(); break;				
			default: letItGo = true;
		}
		
		if (!letItGo)
		{
	        if (e.preventDefault) e.preventDefault();
			if (e.stopPropagation) e.stopPropagation();	
			e.cancelBubble = true;
			e.returnValue = false;
		}
	}
	
	function processKey(e) 
	{
		if ($input.val() != $text_content)
		{
			$text_content = $input.val();
			if (theInterval) clearTimeout(theInterval);
			theInterval = setTimeout(performSearch, 200);
		}
	}
	
	function performSearch()
	{
		if (theInterval) { clearTimeout(theInterval); theInterval = false; }
		if ($query_active) return;
		if ($current_search == $text_content) return;
		$current_search = $text_content;
		if ($current_search.length == 0) { $results.html(""); closeWindow(); return; }
		$query_active = true;
		$.ajax({
		  url: $PATH+"search_ajax.php",
		  data: {search: $current_search},
		  cache: false,
		  success: responseFromDb
		});
	}

	function responseFromDb(html)
	{
		$query_active = false;
		$results.html(html);
		if (html.length > 0)
		{
			$results.children('li').mouseover(function() {
					$results.children('li').removeClass("selected");
					$(this).addClass("selected"); });
			openWindow();
		}
		else
			closeWindow(); 

		if (!theInterval) performSearch();
	}
	
	function closeWindow()
	{
		$results.hide();
	}
	
	function openWindow()
	{
		if ($results.html() == "") return;
		$results.show();
	}
	
	function getCurrentResult()
	{			
		if (!$results.is(':visible')) return false;			
		var $currentResult = $results.children('li.selected');				
		if (!$currentResult.length) $currentResult = false;					
		return $currentResult;
	}
			
	function selectCurrentResult()
	{	
		$currentResult = getCurrentResult();	
		if ($currentResult)
		{
			location.href = $currentResult.find("A").attr('href');
			return true;
		}
		return false;
	}

	function nextResult()
	{	
		$currentResult = getCurrentResult();	
		if ($currentResult)
			$currentResult.removeClass("selected").next().addClass("selected");
		else
		{
			openWindow();
			$results.children('li:first-child').addClass("selected");
		}	
	}
			
	function prevResult()
	{			
		$currentResult = getCurrentResult();	
		if ($currentResult)
			$currentResult.removeClass('selected').prev().addClass('selected');
		else
			$results.children('li:last-child').addClass('selected');	
	}		
		
});

