$().ready(function(){
	
	$("#menu li").hover(function() {
		if($(this).children('ul').length == 0) {
		} else {
			$(this).addClass('hover');
		}
	}, function() {
		$(this).removeClass('hover');
	});
	
});

function changePager() {
	if ($(this).attr("id") == "plist1") {
		$("#pager1Wrap").show();
		$("#pager2Wrap").hide();
	}
	else {
		$("#pager2Wrap").show();
		$("#pager1Wrap").hide();
	};
	changeShowing();
}

function changeShowing() {
	var idx = $(".pline:visible").attr("id");
	
	var total = $("#"+idx+" .product_list_line .product_box").length;
	var from = parseInt($("#"+idx+" .product_list_line:visible").attr('id').substring(3)) * 6 + 1;
	var showing = $("#"+idx+" .product_list_line:visible .product_box").length;
	
	var idp = idx == 'plist1' ? 'pager1Showing' : 'pager2Showing'; 
	
	var msg = '';
	msg = sh.replace("%from%", from);
	msg = msg.replace("%to%", from+showing-1);
	msg = msg.replace("%total%", total);
	$("#"+idp).html(msg);
	
	idx = null;
	idp = null;
	total = null;
	from = null;
	showing = null;
	msg = null;
}


function compare(product_id) {
	$("#bigImgWrap").effect("transfer", { to: "#compare" }, 600, function(){
		
	});
	$.get(baseUrl+'/compare/add/id/'+product_id, function(data) {
		$("#compare").html(data);
	});
}

function clearCompare() {
	$.get(baseUrl+'/compare/clear/', function(data) {
		$("#compare").html(data);
	});
}

function showLargeImg(img, title) {
	var url = baseUrl + '/img/products_images/' + img;
	$("#img-"+img_selected).trigger('click');
}


function changeImage(img, title, num) {
	img_selected = num;
	$("#bigImgWrap").attr("src", baseUrl + '/img/products_thumbs5/' + img);
	$("#bigImgWrap").removeAttr('onclick');
	$("#bigImgWrap").unbind('click')
		.bind('click', function(){
			showLargeImg(img, title);
		});
}

function ajaxError() {
	removeLoadCursor();	
	$("#diag2").remove();
	$('<div id="diag2"></div>').appendTo("body");
	$("#diag2").html('<p class="errors">error processing request...</p>');
	$("#diag2").dialog({
		draggable:true,	
		autoOpen: true, 
		resizable:true,
		title: 'Ajax error',
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		},
		modal: true,
		closeOnEscape:true, 
		width: 450
	});
}

function addToCart(id, num) {
	var qty = $("#p_qty").val();
	if (!qty) {
		qty = 1;
	} 
	$(".transfer"+num+"-"+id).effect("transfer", { to: "#shopping_cart" }, 800, function(){});
	
	$.ajax({
		method: 'post',
		url: baseUrl+'/'+ language +'/cart/add',
		data: {id:id, qty:qty},
		success: function(data){
			if (data.info)
				$("#shopping_cart").html(data.info);
			if (data.cart_diag) {
				openCartDiag(data.cart_diag);
			}
		},
		error: ajaxError,
		dataType: 'json'
	});
}

function openCartDiag(data) {
	$("#diag1").remove();
	$('<div id="diag1"></div>').appendTo("body");
	$("#diag1").html(data.content);
	$("#diag1").dialog({
		draggable:true,	
		autoOpen: true, 
		resizable:true,
		title: data.title,
		modal: true,
		closeOnEscape:true, 
		width:600
	});
}


function closeDiag1() {
	$('#diag1').dialog('close'); 
}


function checkSearch() {
	if (!$("#search_input").val()) {
		return false;
	}
	else {
		return true;
	}
}

function qtyUp(id) {
	var inp = $(".cartQty"+id);
	var qty = parseInt(inp.val(), 10) + 1;
	if (qty < 1 || isNaN(qty)) {
		qty = 1;
	}
	inp.val(qty);
}

function qtyDown(id) {
	var inp = $(".cartQty"+id);
	var qty = parseInt(inp.val(), 10) - 1;
	if (qty < 1 || isNaN(qty)) {
		qty = 1;
	}
	inp.val(qty);
}

//show loading cursor 
function showLoadCursor() {
//	$.cursorMessage('<img src="'+baseUrl+'/images/public/global/loading4.gif" alt="loading"/>', {offsetX:10, offsetY:15});
	$("body").css("cursor", "none");
	$.cursorMessage('<img src="'+baseUrl+'/cursors/loading1.gif" alt="loading"/>', {offsetX:-12, offsetY:-6});
}

function removeLoadCursor() {
	$('#cursorMessageDiv').fadeOut('slow');
	$("body").css("cursor", "auto");
}

function removeFromCart(id) {
	showLoadCursor();
	$.ajax({
		method: 'post',
		url: baseUrl+'/'+ language +'/cart/remove',
		data: {id:id, ajax:1},
		success: function(data){
			if (data.error == 0) {
				$(".cart_item"+id).fadeOut("slow", function() {
					$("#shopping_cart").html(data.info);
					$(".cart_item"+id).remove();
					$("#subtotal").html(data.sum);
				});
			}
			else if (data.error == 1) {
				$("#shopping_cart").html(data.info);
				$("#full_width").remove();
				$(".error").fadeIn("slow");
			}
				
			removeLoadCursor();	
		},
		error: ajaxError,
		dataType: 'json'
	});
}

function changeCartQty(id) {
	showLoadCursor();
	
	var qty = parseInt($("#cart_qty"+id).val(), 10);
	if (qty < 1 || isNaN(qty) || qty > 999) {
		qty = 1;
	}
	$("#cart_qty"+id).val(qty);
	
	$.ajax({
		method: 'post',
		url: baseUrl+'/'+ language +'/cart/qty',
		data: {id:id, qty:qty},
		success: function(data){
			if (data.errors == 0) {
				$("#cart_qty"+id).val(data.qty);
				$("#shopping_cart").html(data.info);
				$("#price_total"+id).html(data.total);
				$("#subtotal").html(data.sum);
			}
				
			removeLoadCursor();	
		},
		error: ajaxError,
		dataType: 'json'
	});
}

//check n populate state/region select
function checkState(inp, country, classBlock) {
	if (cr.hasOwnProperty(country)) {
		//populating select
		$(inp).html('');
		$(inp).append('<option value=""></option>');
		$.each(r[country], function () {
			$(inp).append('<option value="'+ this.value +'">'+ this.name +'</option>');
	    });

		
		//showing block if needed
		if (classBlock)
			classBlock.show();
	}
	else {
		if (classBlock)
			classBlock.hide();
	}
}
