var bActionProcess = false;
jQuery(document).ready(function () {

	/*$(".popup .close").click(function () { return fClosePopup(); });
	$("#popup_bg").click(function () { return fClosePopup(); });
	$(".imgs a").click(function () {
	fShowPopupImgGallery('popup_for_img', $("img", $(this)));
	return false;
	});*/

	$(".popup .lnk a").click(function () {
		var new_li = globalCurPagerLink.parent("li").prev("li");
		if ($(this).hasClass("next")) new_li = globalCurPagerLink.parent("li").next("li");

		if (new_li.length) {
			$("li", new_li.parent(".pager")).removeClass("select");
			new_li.addClass("select");
			var new_a = new_li.children("a");
			globalCurPagerLink = new_a;

			var img_new = $("img", new_a.get(0).tolnk);
			fShowPopupImg($(this).parents(".popup").attr("id"), img_new);
		}
		return false;
	});
    // Copyright
    $("p.copy").click(function () {
        $("span.toggle").toggle(300);
    });
	// Worksheet validate
	var error = "<span id='errval'>Поля должны быть заполнены</span>";
	var iteration = 0;
	var validators =
	[
		function () { return $("div.item.val input:checkbox").is(":checked") || $("div.item.val input:text").val().length > 0; },
		function () { return $("div.item.val input:radio").is(":checked") || $("div.item.val input:text").val().length > 0; },
		function () { return $("div.item.val input:radio:checked").length == 7; },
		function () { return $("div.item.val textarea").val().length > 0; },
		function () { return $("div.item.val input:radio").is(":checked"); },
		function () { return $("div.item.val input:radio").is(":checked"); },
		function () { return $("div.item.val input:radio").is(":checked") || $("div.item.val input:text").val().length > 0; },
		function () { return $("div.item.val input:radio").is(":checked"); },
		function () { return $("div.item.val input:radio").is(":checked"); },
		function () { return $("div.item.val input:radio").is(":checked"); },
		function () { return true; },
		function () { return $("div.item.val input:radio").is(":checked"); },
		function () { return true; },
		function () { return $("div.item.val input:radio").is(":checked"); },
		function () { return $("div.item.val input:radio").is(":checked") || $("div.item.val input:text").val().length > 0; },
		function () { return true; /*var notEmpty = true; $("div.item.val .line:not(.hide) input, div.item.val .line:not(.hide) select").val(function (index, value) { if (value == 0) notEmpty = false; return value; }); return notEmpty;*/ },
		function () { return true; },
		function () { return true; },
		function () { return true; },
		function () { return true; },
		function () { return true; }
	];
	function validateAll(iter, cur) {
		cur.addClass("val");
		var res = validators[iter]();
		cur.removeClass("val");
		if (res && iter == 0) return true;
		else if (!res) return false;
		else return validateAll(iter - 1, cur.prev());
	}
	// Anketa
	$("#progress_total").html($(".anketa .item").length);
	$("#anketa_form").submit(function () {
		$("#errval").remove();
		var cur = $(".anketa .item.cur");
		var next = cur.next(".item");
		if (!validateAll(iteration, cur)) {
			$("div.item.cur").append(error);
			return false;
		}
		if (next.length) {
			cur.removeClass("cur").addClass("show");
			next.addClass("cur");
			//$("input", cur).attr("disabled", "disabled");
			//$("textarea", cur).attr("disabled", "disabled");
			//$("select", cur).attr("disabled", "disabled");

			var cnum = parseInt($("#progress_num").html());
			var total = $(".anketa .item").length;
			var proc = Math.round(cnum / (total / 100));
			$("#progress_num").html(cnum + 1);
			$(".progress .ready").css("width", proc + "%");

			if (!next.next(".item").length) {
			    // $(".anketa .line.btn input").attr("value", "Отправить");
			    $(".anketa .line.btn input").addClass("disabled");
			}
			iteration++;
			return false;
		} else {
			//$("input").removeAttr("disabled", "false");
			//$("textarea").removeAttr("disabled", "false");
			//$("select").removeAttr("disabled", "false");
			return true;
		}
		return false;
	});

	// Children
	$("#anketa_form .item .dinadd").click(function () {
		var cur = $(".anketa .item .line.cur");
		var next = cur.next(".line.hide");
		if (next.length) {
			cur.removeClass("cur");
			next.removeClass("hide").addClass("cur");

			if (!next.next(".line.hide").length) {
				$(this).hide();
			}
		}
		return false;
	});
	// Jornal Gallery top
	$("#jb_total_num").html($(".jornal_big .gallery .galcnt a").length);
	$(".jornal_big .gallery .galcnt a:first").addClass("c");

	$(".jornal_big .gallery .next").click(function () {
		var cnt_obj = $(".galcnt", $(this).parents(".gallery"));
		var wide_obj = $(".wide", cnt_obj);
		var actual_obj = $(".actual", cnt_obj);
		var count = $("a", cnt_obj).length;
		var width = actual_obj.width();

		var item_width = 282;

		var lft = parseInt(wide_obj.css("marginLeft"));

		if (lft >= cnt_obj.width() - actual_obj.width() + item_width && !bActionProcess) {
			//wide_obj.css("marginLeft", (lft - item_width)+"px");
			bActionProcess = true;
			wide_obj.animate({ "marginLeft": (lft - item_width) + "px" }, function () { bActionProcess = false; });
			$("#jb_cur_num").html(parseInt($("#jb_cur_num").html()) + 1);
			var c = $(".jornal_big .gallery .galcnt a.c");
			c.next("a").addClass("c");
			$("#jb_title").html($("img", c.next("a")).attr("alt"));
			c.removeClass("c");
		}
		return false;
	});
	$(".jornal_big .gallery .prev").click(function () {
		var cnt_obj = $(".galcnt", $(this).parents(".gallery"));
		var wide_obj = $(".wide", cnt_obj);
		var actual_obj = $(".actual", cnt_obj);
		var item_width = 282;
		var lft = parseInt(wide_obj.css("marginLeft"));
		if (lft < 0 && !bActionProcess) {
			//wide_obj.css("marginLeft", (lft + item_width)+"px");
			bActionProcess = true;
			wide_obj.animate({ "marginLeft": (lft + item_width) + "px" }, function () { bActionProcess = false; });
			$("#jb_cur_num").html(parseInt($("#jb_cur_num").html()) - 1);
			var c = $(".jornal_big .gallery .galcnt a.c");
			c.prev("a").addClass("c");
			$("#jb_title").html($("img", c.prev("a")).attr("alt"));
			c.removeClass("c");
		}
		return false;
	});

	// Jornal Gallery list

	$(".gallery.jornal_list .next").click(function () {
		var cnt_obj = $(".galcnt", $(this).parents(".gallery"));
		var wide_obj = $(".wide", cnt_obj);
		var actual_obj = $(".actual", cnt_obj);
		var item_width = actual_obj.width() / $(".item", cnt_obj).length;
		var lft = parseInt(wide_obj.css("marginLeft"));
		if (lft > cnt_obj.width() - actual_obj.width() && !bActionProcess) {
			//wide_obj.css("marginLeft", (lft - item_width)+"px");
			bActionProcess = true;
			wide_obj.animate({ "marginLeft": (lft - item_width) + "px" }, function () { bActionProcess = false; });
		}
		return false;
	});
	$(".gallery.jornal_list .prev").click(function () {
		var cnt_obj = $(".galcnt", $(this).parents(".gallery"));
		var wide_obj = $(".wide", cnt_obj);
		var actual_obj = $(".actual", cnt_obj);
		var item_width = actual_obj.width() / $(".item", cnt_obj).length;
		var lft = parseInt(wide_obj.css("marginLeft"));
		if (lft < 0 && !bActionProcess) {
			//wide_obj.css("marginLeft", (lft + item_width)+"px");
			bActionProcess = true;
			wide_obj.animate({ "marginLeft": (lft + item_width) + "px" }, function () { bActionProcess = false; });
		}
		return false;
	});


	// Sobery Gallery
	$(".sobery_gallery .next").click(function () {
		var cnt_obj = $(".galcnt", $(this).parents(".sobery_gallery"));
		var wide_obj = $(".wide", cnt_obj);
		var actual_obj = $(".actual", cnt_obj);
		var item_width = actual_obj.width() / $("a", cnt_obj).length;
		var lft = parseInt(wide_obj.css("marginLeft"));
		if (lft > cnt_obj.width() - actual_obj.width() && !bActionProcess) {
			//wide_obj.css("marginLeft", (lft - item_width)+"px");
			bActionProcess = true;
			wide_obj.animate({ "marginLeft": (lft - item_width) + "px" }, function () { bActionProcess = false; });
			var c = $(".sobery_gallery .galcnt a.c");
			c.next("a").addClass("c");
			var id = c.next("a").attr("href");
			$(".sobery_txts div.c").removeClass("c");
			$(id).addClass("c");
			c.removeClass("c");
		}
		return false;
	});
	$(".sobery_gallery .prev").click(function () {
		var cnt_obj = $(".galcnt", $(this).parents(".sobery_gallery"));
		var wide_obj = $(".wide", cnt_obj);
		var actual_obj = $(".actual", cnt_obj);
		var item_width = actual_obj.width() / $("a", cnt_obj).length;
		var lft = parseInt(wide_obj.css("marginLeft"));
		if (lft < 0 && !bActionProcess) {
			//wide_obj.css("marginLeft", (lft + item_width)+"px");
			bActionProcess = true;
			wide_obj.animate({ "marginLeft": (lft + item_width) + "px" }, function () { bActionProcess = false; });
			var c = $(".sobery_gallery .galcnt a.c");
			c.prev("a").addClass("c");
			var id = c.prev("a").attr("href");
			$(".sobery_txts div.c").removeClass("c");
			$(id).addClass("c");
			c.removeClass("c");
		}
		return false;
	});

});


/******************* POPOUP ***********************/
/*
var fShowPopup = function(id, w) {
	fClosePopup();
	if (w) {
		$("#"+id).css("width", w+"px");
	}
	var t = Math.max($(window).height() / 2 - $("#"+id).height() / 2, 0) + $(window).scrollTop();
	var l = Math.max($(window).width() / 2 - $("#"+id).width() / 2, 0);
	$("#"+id).css({ "top": t+"px" }).css({ "left": l+"px" });
	$("#popup_bg").show();
	$("#"+id).show();
	return false;
}
var fClosePopup = function() {
	$("#popup_bg").hide();
	$(".popup").hide();
	return false;
}

var fShowPopupImg = function(id, img) {
	var cachImg = new Image();
	cachImg.popupID = id;
	cachImg.onerror = function(){ };
	cachImg.onload = function(){
		$(".cnt img", $("#"+this.popupID)).attr("src", this.src);
		fShowPopup(this.popupID, this.width+ 20);
	};
	cachImg.src = img.attr("longdesc");
}

var globalCurPagerLink = null;
var fShowPopupImgGallery = function(id, img) {
	var pop = $("#"+id);
	var ul = $(".pager", pop);
	$("li", ul).remove();
	$("a", img.parents(".imgs")).each(function(){
		ul.append("<li><a href=''></a></li>");
		$("li:last a", ul).get(0).tolnk = $(this).get(0);
		if ( $("img", $(this)).get(0) == img.get(0) ) {
			globalCurPagerLink = $("li:last a", ul);
			$("li:last", ul).addClass("select");
		}
	});
	$("li a", ul).click(function(){
		$("li.select", $(this).parents(".pager")).removeClass("select")
		$(this).parent("li").addClass("select");
		globalCurPagerLink = $(this);
		
		var img_new = $("img", $(this).get(0).tolnk);
		fShowPopupImg($(this).parents(".popup").attr("id"), img_new);
		return false;
	});
	
	fShowPopupImg(id, img);
}*/

