$(document).ready(function(){
	if (typeof(showTab) != "undefined") {
		activateTab(showTab);
	}
	else {
		activateTab(1);
	}
	$(".mmi").click(function(){
		var id = $(this).attr('id').substring(16);
		activateTab(id);
	});
	
	
});

function activateTab(id) {
	$(".middle_menu_item_active").addClass("middle_menu_item");
	$(".middle_menu_item_active").removeClass("middle_menu_item_active");
	$("#middle_menu_item"+id).addClass("middle_menu_item_active");
	$("#middle_menu_item"+id).removeClass("middle_menu_item");
	
//	$(".middle_menu_item").css('z-index', 1);
//	$("#middle_menu_item"+id).css('z-index', 999);
//	$(".tabs_bg_box").css('z-index', -1);
	
	
	$(".tabs_bg_box_active").addClass('tabs_bg_box');
	$(".tabs_bg_box_active").removeClass('tabs_bg_box_active');
	$("#tab"+id).addClass('tabs_bg_box_active');
	$("#tab"+id).removeClass('tabs_bg_box');
	
	
//	$(".tabs_bg_box").addClass('tabs_bg_box_active');
//	$("#tab"+id).css('z-index', 2);
}

function printPage() {
	$("#page").printArea();
}


function showLargeImg(img, title) {
	var url = baseUrl + '/img/products_images/' + img;
	$.fn.colorbox({href:url, open:true, opacity: 0.5});
}

function changeImage(img, title) {
	$("#bigImgWrap").html('<a href="' + baseUrl + '/img/products_images/' + img + '" class="big_img"  title="' + title + '"><img src="' + baseUrl + '/img/products_thumbs3/' + img + '" title="' + title + '"  alt="' + title + '" /></a>');
	$(".zoomin").attr('href', 'javascript:showLargeImg("' + img + '", "' + title + '")');
	$(".big_img").jqzoom(zoomOptions);
}

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() {
	var qty = $("#p_qty").val();
	if (!qty) {
		qty = 1;
	} 
	if (property_selected_id == 0 || property_selected_id) {
		$("#bigImgWrap").effect("transfer", { to: "#bag1" }, 600, function(){
			
		});
		$.getJSON(baseUrl+'/cart/add/id/'+product_id+'/property/'+property_selected_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 {
		$("#search_form").submit();
	}
}

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);
}