
/* CORE */
function add_target_blank(){
	$('a.target-blank').attr('target', '_blank');	
}
function init_frontend(){
	add_target_blank();
}
$(init_frontend);

/* PROJECT */
// pagrindinio menu veikimas

var sub_hover = false;
var sub_arrow_hover = false;
var iter = 0;
var timeout_id = false;

function hide_menu() {
	timeout_id = setTimeout(
		function () {
			if (!sub_hover && !sub_arrow_hover) {
				$("#main_menu .sub").hide();
			}
		},
		200
	);
}

$(function(){
	$("#main_menu a.level0").hover(
		function(){
			sub_id = "sub_"+this.id.replace(/^menu_link_/,'');
			// alert (sub_id);
			// $("#help_box").html('.down in'+'<br>'+$("#help_box").html());
			sub_arrow_hover = true;
			$("#main_menu .sub").each(function(){
				if (this.id!=sub_id) {
					$(this).hide();
				}
			});
			if (timeout_id) {
				clearTimeout(timeout_id);
			}
			$("#"+sub_id).show();
			
		},
		function(){
			sub_arrow_hover = false;
			hide_menu();
		}
	);
	$("#main_menu .sub").hover(
		function(){
			sub_hover = true;
		},
		function(){
			sub_hover = false;
			hide_menu();
		}
	);
});

/*
// gamintoju menu centravimas
$(function() {
	w = $('#manufacturer_menu li').length*120;
	margin = (Math.round(($('#footer').width()-w)/2));
	$('#manufacturer_menu').css('margin-left', margin+'px');
});*/

// krepselio kiekio keitimas
$(function() {
	$("a.cart").click(function(){
		amount_input = $(this).parent().children('input.amount');
		if (amount_input.length) {
			amount = amount_input.val();
			if (!amount.match(/^\d+$/)) {
				alert('Kiekio laukelyje turi būti įvestas skaičius.');
				amount_input.focus();
				return false;
			} else {
				this.href = this.href.replace(/^(.+\?.*amount=)(\d+)(.*)$/, '$1'+amount+'$3');
			}
		}
	});
});
