$(document).ready(function(){
	$("#cart_box").mouseover(function(){
		var emp = $("#cart_det").html();
		fade = false;
		emp = jQuery.trim(emp);
		fading = false;
		if (!c_visible && emp != '') {
			c_visible = true;
			$("#bag1").removeClass('bag_inactive');
			$("#bag1").addClass('bag_active');
			$("#cart_det").fadeIn();
		}
	});
	$("#cart_box").mouseout(function(){
		fading = true;
		fade = true;
		setTimeout('fadeCart()', 1500);
	});
});

var c_visible = false;
var fading = false;
var fade = false;

function fadeCart() {
	if (fade) {
		$("#bag1").removeClass('bag_active');
		$("#bag1").addClass('bag_inactive');
		$("#cart_det").fadeOut(function(){
			fade = false;
			fading = false;
			c_visible = false;
		});
	}
}

function printPage() {
	$("#page").printArea();
}

/*function fixLayout() {
	var ch = $("#content_body").height();
	var lh = $("#left").height();
	var rh = $("#ads").height();
	var hh = Math.max(ch, lh, rh);
	
	$("#content_body").height(hh);
	$("#left").height(hh);
	$("#ads").height(hh);
}*/

function fixLayout() {
	var ch = $("#content_body").height();
	var lh = $("#left").height();
//	var rh = $("#ads").height();
	
	if (lh < ch) {
		$("#left").height(ch);
//		$("#ads").height(ch);
	}
}

function showLargeImg(img, title) {
	var url = baseUrl + '/img/products_images/' + img;
	$.fn.colorbox({href:url, open:true, opacity: 0.1});
}


function changeImage(img, title) {
	$("#bigImgWrap").attr("src", baseUrl + '/img/products_thumbs2/' + img);
	$("#bigImgWrap").removeAttr('onclick');
	$("#bigImgWrap").unbind('click')
		.bind('click', function(){
			showLargeImg(img, title);
		});
}

function selectColor(sel) {
	color_selected_id = $(sel).val();
	color_selected_name = $("#color_sl option:selected").html();
	
	selectPrice();
}

function selectSize(id, name) {
	size_selected_id = id;
	size_selected_name = name;
	
	$(".size_sp_selected").addClass('size_sp');
	$(".size_sp_selected").removeClass('size_sp_selected');
	$("#sizep"+id).removeClass('size_sp');
	$("#sizep"+id).addClass('size_sp_selected');
	if (color_selected_id != 0) {
		$("#color_sl").html('');
		jQuery.each(colors_options[size_selected_id], function(i, val) {
			$("#color_sl").append('<option value="'+ val[0] +'">'+ val[1] +'</option>');				
		});
	    color_selected_id = $("#color_sl").val();
		color_selected_name = $("#color_sl option:selected").html();
	}
	selectPrice();
}

function selectPrice() {
	if (color_selected_id != 0 && size_selected_id == 0 && color_selected_id != null) {
		property_selected_id = prop_det[color_selected_id][3];
		$("#price_box").html(prop_det[color_selected_id][2]);
	}
	else if (color_selected_id == 0 && size_selected_id != 0 && size_selected_id != null) {
		property_selected_id = prop_det[size_selected_id][3];
		$("#price_box").html(prop_det[size_selected_id][2]);
	}
	else if (color_selected_id != 0 && size_selected_id != 0 && size_selected_id != null) {
		property_selected_id = prop_det[size_selected_id][color_selected_id][3];
		$("#price_box").html(prop_det[size_selected_id][color_selected_id][2]);
	}
}

function addToCart(product_id) {
	var qty = $("#p_qty").val();
	if (!qty) {
		qty = 1;
	} 
//	if (property_selected_id == 0 || property_selected_id) {
		$("#bigImgWrap").effect("transfer", { to: "#cart" }, 600, function(){
			
		});
		$.getJSON(baseUrl+'/cart/add/id/'+product_id+'/qty/'+qty, function(data) {
			$("#cart_msg").html(data.cart_info);
			$("#cart_det").html(data.cart_dinfo);
			if (data.cart_diag) {
				openCartDiag(data.cart_diag);
			}
		});
//	}
//	else {
//		remindSelectProp();
//	}
}

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 remindSelectProp() {
	$("#remind_box").html('<div id="remind_msg">'+ remind_msg +'</div>');
	setTimeout(function(){
		$("#remind_msg").fadeOut("slow");
	}, 1500);
}

function closeDiag1() {
	$('#diag1').dialog('close'); 
}

function checkJuridinis() {
	if ($("#juridinis").attr("checked")) {
		$("input.hidden").parents("tr.zf_tr").show();
	}
	else {
		$("input.hidden").parents("tr.zf_tr").hide();
	}
}

function newAdress(title) {
	$("#diag1").remove();
	$('<div id="diag1"></div>').appendTo("body");
	$("#diag1").addClass("loading1");
	$("#diag1").load(baseUrl + '/clients/newadress/', function(){
		$("#diag1").removeClass("loading1");
		$("#diag1").dialog({
			draggable:true,	
			autoOpen: true, 
			resizable:true, 
			title: title,
			modal: true,
			closeOnEscape:true,
			width: 400
		});
	});
	
}


function changeAdress() {
	var id = $("#adress").val();
	$.post(baseUrl + '/checkout/changeadress/', {id : id});
}

function processDiagForm(div) {
	$(div).ajaxSubmit({success:processDiagResponse}); 
	return false;
}

function processDiagResponse(data) {
	if (data == 'go') {
		document.location = baseUrl + '/' + moduleName;
	}
	else {
		$("#diag1").html(data);
	}
}

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