var gMouseWheelSpeed = 50; // кол-во пикселей, на которое сдвигается за один шаг колеса мыши
var gClickBtnSpeed = 2.0; // За клик передвигается на длину картинки (w), gClickBtnSpeed*w = время в миллисекундах за которое произсходит этот сдвиг


var globalIntervalId = 0;
var globalBMoveGal = false;
var globalFirstVisImg = null;
var globalAnimateActive = false;

jQuery(document).ready(function() {
	/* Gallery */
	if( $("#gallery").length ) {
		$("#gallery .next").mousedown(function(){ globalBMoveGal = true; fMoveGalleryR(); });
		$("#gallery .next").mouseup(function(){ globalBMoveGal = false; });
		$("#gallery .next").click(function(){ return false; });
		$("#gallery .prev").mousedown(function(){ globalBMoveGal = true; fMoveGalleryL(); });
		$("#gallery .prev").mouseup(function(){ globalBMoveGal = false; });
		$("#gallery .prev").click(function(){ return false; });
		$("#gallery .actual").width( $("#gallery .actual").width() );
		$("#gallery").mousewheel(function(e, delta){
			var ac = $("#gallery .actual");
			var predel = $("#gallery .cnt").width() - ac.width() + 2;
			var ml = parseInt(ac.css("marginLeft"));
			var ml_to = (delta > 0) ? ml + gMouseWheelSpeed : ml - gMouseWheelSpeed;
			if(ml_to >= 0) ml_to = 0;
			if(ml_to <= predel) ml_to = predel;
			ac.css("marginLeft", ml_to+"px");
			return false;
		});
		$("#gallery .actual a").click(function(){
			$("#gallery .actual a").removeClass("select");
			$(this).addClass("select");
			$(".img img", $(this).parents(".photo")).attr( "src", $(this).attr("href") );
			$(".img img", $(this).parents(".photo")).attr( "class", $(this).attr("title") );
			return false;
		});
	}
	
	$("#popup_bg").click(function(){fClosePopup();});
	$(".popup a.close").click(function(){fClosePopup(); return false; });
	
	/* Compare */
	if( $("table.compare").length ) {
		//$("table.compare th.tab li.all").click(function(){ $(this).parents("table.compare").removeClass("unique"); return false; });
		//$("table.compare th.tab li.un").click(function(){ $(this).parents("table.compare").addClass("unique"); return false; });
	}
	
	/* Cart */
	if( $("table.cart").length ) {
		$("table.cart td.check input").click(function(){
			if ($(this).is(":checked")) $(this).parents("tr").addClass("checked");
			else $(this).parents("tr").removeClass("checked");
		});
	}
	
	/* tabs */
	if( $("ul.tabs").length ) {
		//$("ul.tabs").tabs({ fx: { opacity: 'toggle' }, selected: 0 });
		$("ul.tabs").tabs({ selected: 0 });
	}
	
	/*if( $("input:text")) {
		$("input:text").each(function(){
			if($(this).val().length > 0) {
				$(this).focus(function(){
					if(!$(this).get(0).oldValue) $(this).get(0).oldValue = $(this).val();
					if($(this).get(0).oldValue == $(this).val()) $(this).val("");
				});
				$(this).blur(function(){
					if($(this).val() == "") $(this).val($(this).get(0).oldValue);
				});
			}
		});
		
	}*/
	
});

var fMoveGalleryR = function(){
	if( globalBMoveGal && !globalAnimateActive ) {
		globalAnimateActive = true;
		fSearchFirstVisImg(1);
		var ac = $("#gallery .actual");
		
		var predel = $("#gallery .cnt").width() - ac.width() + 2;
		var ml = parseInt(ac.css("marginLeft"));
		var ml_to = ac.offset().left - globalFirstVisImg.offset().left + 1;
		if( ml_to < predel ) ml_to = predel;
		var moneW = ml - ml_to;
		var moveTime = moneW * gClickBtnSpeed;
		
		if(ml_to > predel) ac.animate({"marginLeft": ml_to+"px"}, moveTime, 'linear', function(){ globalAnimateActive = false; fMoveGalleryR(false); });
		else {
			ac.animate({"marginLeft": ml_to+"px"}, moveTime, 'linear', function(){ globalAnimateActive = false; });
		}
	}
};
var fMoveGalleryL = function(){
	if( globalBMoveGal&& globalFirstVisImg && !globalAnimateActive ) {
		globalAnimateActive = true;
		fSearchFirstVisImg(-1);
		var ac = $("#gallery .actual");
		
		var ml = parseInt(ac.css("marginLeft"));
		var ml_to = ac.offset().left - globalFirstVisImg.offset().left + 1;
		if(ml_to > 0) ml_to = 0;
		var moneW = ml_to - ml;
		var moveTime = moneW * gClickBtnSpeed;
		
		if(ml_to < 0) ac.animate({"marginLeft": ml_to+"px"}, moveTime, 'linear', function(){ globalAnimateActive = false; fMoveGalleryL(false); });
		else {
			ac.animate({"marginLeft": ml_to+"px"}, moveTime, 'linear', function(){ globalAnimateActive = false; });
			globalFirstVisImg = $("#gallery .cnt img:first");
		}
	}
};

var fSearchFirstVisImg = function(direct) {
	$("#gallery .actual").get(0).nTmpML = -direct * 9999999;
	$("#gallery .actual a").each(function(){
		var ac = $(this).parents(".actual");
		var ml = parseInt(ac.css("marginLeft"));
		var ml_to = ac.offset().left - $(this).offset().left;
		
		if (direct > 0) {
			if( ml > ml_to && ml_to > ac.get(0).nTmpML) {
				ac.get(0).nTmpML = ml_to;
				globalFirstVisImg = $("img", $(this));
			}
		} else {
			if( ml < ml_to && ml_to < ac.get(0).nTmpML) {
				ac.get(0).nTmpML = ml_to;
				globalFirstVisImg = $("img", $(this));
			}
		}
	});
}


/******************* POPOUP ***********************/

var fShowPopup = function(id, w) {
	if (w) $("#"+id).width(w+"px"); 
	var t = Math.max($(window).height() / 2 - $("#"+id).height() / 2, 0);// + $(window).scrollTop() - $("#content").offset().top;
	var l = Math.max($(window).width() / 2 - $("#"+id).width() / 2, 0);
	$("#"+id).css({ "top": t+"px" }).css({ "left": l+"px" });
    $("#"+id+"_bg").show();
    $("#"+id).show();
    return false;
}
var fClosePopup = function() {
    $(".popup_bg").fadeOut(500);
    $(".popup").fadeOut(500);
    return false;
}

var fShowPopupImg = function(id, img) {
	$("#"+id+" .popcont").empty();
	$("#"+id+" .popcont").append(img.parent().html());
	fShowPopup(id);
}

