var goods_loc = "/ajax.php?mod=catalog_goods&action=";
var novinka = id = num = 0;
function showMustGoOn(obj){	
	$("#pane ul li").removeClass("active"); 
	//alert($(obj).parrent().parrent().html());
	$(obj).parent().parent().addClass("active"); 
	loaderHandler();
	id = $(obj).find("img").attr("picid");
	num = parseInt($(obj).find("img").attr("num"));
	$('#basic-modal-content').modal({onOpen: function (dialog) {
		dialog.overlay.fadeIn('slow', function () {
			dialog.data.hide();
			$.post(goods_loc + "show_details", {
				id: id
			}, function(xml) {
				dialog.container.fadeIn('slow', function () {
					dialog.data.slideDown('slow');
				});
				if($("message",xml).attr("result") == 1){
					novinka = 0;
					$("#view").html($("message",xml).text());
					
				} else {
					novinka = 1;
					$.modal.close();
					$('#reg').modal();
				}
				strelki();
				rebindgoods();
				loaderHandler();
			});	
		});
	}});	
}

function rebindgoods(){
	
	$('#freg').submit(function(){
		var count = 0;
		$('.fcheck').each(function(){
			if($(this).val() == "") {
				alert("Заполнены не все обязательные поля!");
				count = 1;
				$(this).focus();
				return false;
			}
		});
		return count == 0;
	});
	
	
	$(".color_hol").unbind("click").bind("click", function(){
		var obj = $(this);
		var cid = obj.attr("cid");
		loaderHandler();
		$.post(goods_loc + "show_details", {
			cid: cid
		}, function(xml) {
			if($("message",xml).attr("result") == 1){
				$("#view").html($("message",xml).text());
				rebindgoods();
			}
			loaderHandler();
		});	
		return false;
	});
	
	$(".btn_add").unbind("click").bind("click", function(){
		var obj = $(this);			
		var id = obj.attr("gid");
		var cid = obj.attr("cid");
		loaderHandler();
		$.post(goods_loc + "add_in_basket", {
			id: id,
			cid: cid
		}, function(xml) {
			if($("message",xml).attr("result") == 1){
				if(obj.attr("zoom") == 1) {
					$(".btn_add", window.opener.document).unbind("click").html("в корзине").removeClass("btn_add").addClass("btn_bask");
				}
				obj.unbind("click").html("в корзине").removeClass("btn_add").addClass("btn_bask");
				rebindgoods();
				loaderHandler();
			}
		});	
		return false;
	});
	
	$(".btn_bask").unbind("click").bind("click", function(){ return false; });
	
	$(".picture-slides-previous-image").unbind("click").bind("click", function(){
		num = num-1;
		id = $("#num_"+num).attr("picid");
		loaderHandler();
		$("#pane ul li").removeClass("active");
		$("#num_"+num).parent().parent().parent().addClass("active"); 
		$.post(goods_loc + "show_details", {
			id: id
		}, function(xml) {
			if($("message",xml).attr("result") == 1){
					if(novinka){						
						$.modal.close();
						$('#basic-modal-content').modal();;
					}
					novinka = 0;
				$("#view").html($("message",xml).text());
				if(num == 1) $(".picture-slides-previous-image").hide();
				else $(".picture-slides-previous-image").show();
				if(num == $("#goods_count").val()) $(".picture-slides-next-image").hide();
				else $(".picture-slides-next-image").show();				
			} else {
					novinka = 1;
				$.modal.close();
				$('#reg').modal();
				strelki();
			}
			rebindgoods();
			loaderHandler();
		});	
		
		return false;
	});
	
	$(".picture-slides-next-image").unbind("click").bind("click", function(){
		num = num+1;
		id = $("#num_"+num).attr("picid");
		loaderHandler();
		$("#pane ul li").removeClass("active"); 
		$("#num_"+num).parent().parent().parent().addClass("active"); 
		$.post(goods_loc + "show_details", {
			id: id
		}, function(xml) {
			if($("message",xml).attr("result") == 1){
					if(novinka){						
						$.modal.close();
						$('#basic-modal-content').modal();;
					}
					novinka = 0;
				$("#view").html($("message",xml).text());
				if(num == 1) $(".picture-slides-previous-image").hide();
				else $(".picture-slides-previous-image").show();
				if(num == $("#goods_count").val()) $(".picture-slides-next-image").hide();
				else $(".picture-slides-next-image").show();			
			} else {
					novinka = 1;
				$.modal.close();
				$('#reg').modal();
				strelki();
			}
			rebindgoods();
			loaderHandler();
		});	
		
		return false;
	});
	
	$(".simplemodal-close").unbind("click").bind("click", function(){
		$.modal.close();
		return false;
	});
		
	$(".btn_info").hover(function () {
		$(".info").fadeIn("fast");
		$(this).addClass("act_info");
	},function(){
  		$(".info").fadeOut("fast");
		$(this).removeClass("act_info");
	});	
		
	$(".color_hol").hover(function () {		
		$(this).addClass("actived");
	},function(){  		
		$(this).removeClass("actived");
	});
	
	$(".show_zoom").unbind("click").bind("click", openZoom);
}

function resizeContainer() {
	var bigContainer = $(".img_holder").width();
	var colorPicker = $(".choose_color").width();
	var imgContainer = parseInt(bigContainer) - parseInt(colorPicker);
	alert(bigContainer+" "+colorPicker);
	$(".img_holder div").css("width", imgContainer);
}

function loaderHandler(){
	if($("#loading").css("display") == "none") {
		var picWidth = parseInt($(".img_holder").width());
		var ml = 0;
		
		if($.browser.msie){
			if(parseInt($.browser.version)=='6') {
				if(picWidth>500) ml = picWidth-875;
				else if(picWidth<=500 && picWidth>0) ml = -325;
				else ml = 0;
			}
			
			else {
				if(picWidth>500) ml = picWidth-800;
				else if(picWidth<=500 && picWidth>0) ml = -150;
				else ml = -50;
			}
		}
		else {
			if(picWidth>500) ml = picWidth-800;
			if(picWidth==500) ml = -150;
		}
		$("#loading").css("margin-left", ml);
		$("#loading").css("display", "block");
		rightArrowPosition();
	} else {
		setTimeout('$("#loading").css("display", "none")', 1000);
		setTimeout('rightArrowPosition()', 1000);
	}
	closeButtonPosition();
}

var openZoom = function(){
	var url = $(this).attr("href");
	var name = new Date();
	var newWin = window.open(url,name.getTime(),'height=550,width=800,toolbar=no,resizable=yes'); 
	if (window.focus) {newWin.focus()};
	return false;
}

function rightArrowPosition() {	
	if(!$.browser.msie){
		if(novinka){			
			var dw = parseInt($("#freg").css("width"));
			if(isNaN(dw)) dw = 500;
			var imW = dw + 120;
			$(".navigation-controls").css("width", imW);
			$(".navigation-controls").css("margin-left", "-85px");	
		} else {
			var dw = parseInt($(".img_holder").css("width"));
			if(isNaN(dw)) dw = 500;
			var imW = dw + 150;
			$(".navigation-controls").css("width", imW);
			$(".navigation-controls").css("margin-left", "-35px");
		}
	}else{
		if(novinka) {
			$(".iecontrols").width(820);
			if(parseInt($.browser.version)=='6') $(".iecontrols").css("margin-left", "-730px");
			else $(".iecontrols").css("margin-left", "-435px");
		
		} else {
			if($(".img_holder").width() == null || $(".img_holder").width() == 0) $(".navigation-controls").width(600);
			else $(".navigation-controls").width(parseInt($(".img_holder").width()) + 150); 
			$(".navigation-controls").css("margin-left", "-35px");
		}
	}
}

function closeButtonPosition() {
	var image = $(".img_holder div img").attr("src");

	if(image==undefined) { 
		if(!$.browser.msie){
			var marginLeft = parseInt($("#simplemodal-container").css("width")) - 75;
			$(".simplemodal-close").css("margin-left", marginLeft);
		}else{
			if($("#simplemodal-container").width() > 0){
				if(parseInt($.browser.version)=='6') var marginLeft = parseInt($("#simplemodal-container").width()) - 450;
				else var marginLeft = parseInt($("#simplemodal-container").width()) - 250;
				$(".simplemodal-close").css("margin-left", marginLeft);
			}
		}	
	}
}

function strelki() {
	if(num < 2) $(".picture-slides-previous-image").hide();
	else $(".picture-slides-previous-image").show();
	if(num == $("#goods_count").val()) $(".picture-slides-next-image").hide();
	else $(".picture-slides-next-image").show();
}
