var iDOMLoaded = false;
var bDTrackInit = false;
var ocformAnc = false;

jQuery(document).ready(function() {
	iDOMLoaded = true;
	setTimeout("init()", 100);

	/* TrackBar */
	if ($("#DoubleTrack-2")[0]) {
		if ($("#DoubleTrack-2").parents(".cnt").is(":visible")) {
			dt_start();
			bDTrackInit = true;
		}
	}

	/* Where to buy */
	$('#exat_officelist div.line').click(function() {
		var parentLine = this;
		$(this).nextAll().each(function() {
			var lines = $(this).prevAll('div.line');
			if (lines.length && (lines[0] == parentLine) && !$(this).hasClass('line')) {
				$(this).toggle('normal');
			}
		})
	})


	/* Buble popup in form */
	if ($("#content .deposits .help_buble .icon")[0]) {
		$("#content .deposits .help_buble").hover(
		function() {
			$(this).children(".buble").css("display", "block");
			var r = getAbsolutePos(this);
			var newtop = 0;
			var newleft = 0;
			if (window.XMLHttpRequest) {
				newtop = r.y - $(this).children(".buble").attr("offsetHeight") + 16;
				newleft = r.x - 288;
			} else {
				newtop = 0 - $(this).children(".buble").attr("offsetHeight") + 45;
				newleft = 0 - 285;
			}
			$(this).children(".buble").css("top", newtop + "px");
			$(this).children(".buble").css("left", newleft + "px");
		},
		function() {
			$(this).children(".buble").css("display", "none");
		}
	);
	}


	/* Content rows hover */
	if ($("#content table tr")[0]) {
		$("#content table:not(.nohover) tr:not(.nohover)").hover(
		function() {
			$(this).addClass("hover");
		},
		function() {
			$(this).removeClass("hover");
		}
	);
	}

	/* Tabs on card pages */
	if ($("#content ul.card_tabs")[0]) {
		if (typeof (predef_cardTabsSel) != 'undefined') {
			$("#content ul.card_tabs").tabs({ fx: { opacity: 'toggle' }, selected: predef_cardTabsSel });
		} else {
			$("#content ul.card_tabs").tabs({ fx: { opacity: 'toggle' }, selected: 0 });
		}
	}
	/* Tabs on card pages */
	if ($("#content ul.inner_tabs")[0]) {
		$("#content ul.inner_tabs").tabs({ fx: { opacity: 'toggle' }, selected: 0 });
	}

	/* Filial list hover */
	if ($("#content .filial_map .filial_list p")[0]) {
		$("#content .filial_map .filial_list p").hover(
		function() {
			$("#content .filial_map .filial_list p").removeClass("hover");
			$(this).addClass("hover");
			$("#content .filial_map .map img").attr("src", $(this).attr("src"));
		},
		function() {
		}
	);
	}


	/* Main menu */
	if ($("#mainmenu")[0]) {
		$("#openmenu").mouseover(function() {
			$(this).siblings("ul").show();
		});
		$("#mainmenu").mouseleave(function() {
			$(this).children("ul").hide();
		});
		$("#mainmenu > ul > li").hover(
		function() {
			$(this).children("table").get(0).style.display = "block";
			$(this).addClass("hover");
		},
		function() {
			$(this).children("table").get(0).style.display = "none";
			$(this).removeClass("hover");
		}
	);
	}

	/* Wizard Info buble (help) */
	var ibTmOt = false;
	if ($(".help .info")[0] && $(".help a")[0]) {
		$(".help a.help").mouseover(
		function() {
			if (ibTmOt) clearTimeout(ibTmOt);
			$(".help .info:visible").hide("fast");
			if (this.bubleHeight == undefined) {
				$(this).siblings(".info").css("top", "-500px");
				$(this).siblings(".info").show();
				this.bubleHeight = $(this).siblings(".info").attr("offsetHeight");
				$(this).siblings(".info").hide();
			}
			pos = getAbsolutePos(this);
			$(this).siblings(".info").css("top", (pos.y - this.bubleHeight + 20) + "px");
			$(this).siblings(".info").css("left", (pos.x - 75) + "px");
			$(this).siblings(".info").show("fast");
		}
	);
		$(".help .info").mouseleave(
			function() {
				ibTmOt = setTimeout(function() { $(".help .info:visible").hide("fast"); }, 500);
			}
		);
		$(".help .info").mouseover(function() { clearTimeout(ibTmOt); });
	}


	/* Open/Close form in content or rightcolumn */
	if ($(".ocform")[0]) {
		var fClickOCForm = function(obj) {
			$(".ocform .cnt .price").css("visibility", "hidden");
			$(".ocform .cnt .g dt").css("visibility", "hidden");
			var cnt = obj.parent().siblings("div.cnt");
			if (cnt.is(":hidden")) {
				if (obj.parent().parent().hasClass("blue")) cnt.parent().css("background-color", "#cce9f2");
				else if (obj.parent().parent().hasClass("tourorder")) cnt.parent().css("background-color", "#efefef");
				else cnt.parent().css("background-color", "#ffe270");
				cnt.siblings("div.head").children("a.oc").css("background-image", "url('/files/images/travel_oc_up.png')");
				cnt.parent().siblings("fieldset").children(".cnt:visible").slideUp("slow", function() {
					$(this).parent().css("background-color", "#e0e0de");
					$(this).siblings("div.head").children("a.oc").css("background-image", "url('/files/images/travel_oc_down.png')");
				});
				cnt.slideDown("slow", function() {
					$(".ocform .cnt .price").css("visibility", "visible");
					if ($("#DoubleTrack-2").parents(".cnt").is(":visible") && !bDTrackInit) { dt_start(); bDTrackInit = true; }
					$(".ocform .cnt .g dt").css("visibility", "visible");
				});
			} else {
				cnt.slideUp("slow", function() {
					$(this).parent().css("background-color", "#e0e0de");
					$(this).siblings("div.head").children("a.oc").css("background-image", "url('/files/images/travel_oc_down.png')");
					$(".ocform .cnt .price").css("visibility", "visible");
					if ($("#DoubleTrack-2").parents(".cnt").is(":visible") && !bDTrackInit) { dt_start(); bDTrackInit = true; }
					$(".ocform .cnt .g dt").css("visibility", "visible");
				});
			}
			return false;
		}
		$(".ocform .head .ttl").click(function() { return fClickOCForm($(this)); });
		$(".ocform .head .oc").click(function() { return fClickOCForm($(this)); });

		var anc = location.hash || ocformAnc;
		if (anc.length && $(anc)[0] && $(anc).children(".cnt").is(":hidden")) {
			fClickOCForm($(anc).children(".head").children(".ttl"));
		}

	}

});

function init() {
	if ($("#container h2 a:not(.small)")[0]) {
		$("#container h2 a:not(.small)").append('<img src="/files/images/h2_arrow.png">');

		$("#container h2 a:not(.small)").hover(
				function() {
					$(this).queue("fx", []);
					$(this).children("img").animate({ marginLeft: "20px" }, 200);
				},
				function() {
					$(this).parent().queue("fx", []);
					$(this).children("img").animate({ marginLeft: "10px" }, 200);
				}
		);
	}
	if ($(".head2 a:not(.small)")[0]) {
		$("#.head2 a:not(.small)").append('<img src="/files/images/h2_arrow.png">');

		$(".head2 a:not(.small)").hover(
				function() {
					$(this).queue("fx", []);
					$(this).children("img").animate({ marginLeft: "20px" }, 200);
				},
				function() {
					$(this).parent().queue("fx", []);
					$(this).children("img").animate({ marginLeft: "10px" }, 200);
				}
		);
	}

	/* Red content banners on index page */
	if ($("#content .ban:not(.ins) input")[0]) {
		$("#content .ban input").hover(
		function() {
			this.className = "over";
			var pcn = this.parentNode.parentNode.className;
			$(this).parent().css("backgroundImage", "url('/files/images/cbb_" + pcn + "_over.jpg')");
		},
		function() {
			this.className = "";
			var pcn = this.parentNode.parentNode.className;
			$(this).parent().css("backgroundImage", "url('/files/images/cbb_" + pcn + ".jpg')");
		}
	);
	}


	/* Vote popup show */
	if ($("#content input.votebut")[0]) {
		$("#content input.votebut").click(
		function() {
			if ($("#votepopup").css("display") == "block" && $("#votepopup #curid").attr("value") == $(this).attr("id")) {
				$("#votepopup").css("display", "none");
			} else {
				$("#votepopup #curid").attr("value", $(this).attr("id"));
				var pos = getAbsolutePos(this);
				$("#votepopup").css("top", pos.y + "px");
				$("#votepopup").css("left", pos.x + "px");
				$("#votepopup").css("display", "block");
			}
			return false;
		}
	);
		if ($("#content .vote label input")[0]) {
			$("#content .vote input.but").attr("value", $("#content .vote label input:checked").attr("value"));
			$("#content .vote label input").change(
			function() {
				$("#content .vote input.but").attr("value", $(this).attr("value"));
			}
		);
			$("#content .vote label input").focus(
			function() {
				$("#content .vote input.but").attr("value", $(this).attr("value"));
			}
		);
		}
	}

}

function getAbsolutePos(el) {
	var r = { x: el.offsetLeft, y: el.offsetTop };
	if (el.offsetParent) {
		var tmp = getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}

/**********************************************************/
/******************* VALIDATION ***************************/
/**********************************************************/

var validFieds = new Array();
function addFieldToValidator(fieldID, types) {
	validFieds[fieldID] = types;
}
function validate() {
	var is_valid = true;
	for (var fid in validFieds) {
		for (var t in validFieds[fid]) {
			hideError(fid);
			if (validFieds[fid][t] == "require" && $("#" + fid).attr("value").length == 0) {
				is_valid = false;
				showError(fid, "Поле обязательно для заполнения.");
				break;
			}
			if (validFieds[fid][t] == "email") {
				var reg = /[0-9a-z_]+@[0-9a-z_^.]+\.[a-z]{2,3}/i;
				if (!reg.test($("#" + fid).attr("value"))) {
					is_valid = false;
					showError(fid, "Не верный формат email.");
					break;
				}
			}
			// Add checking here


		}
	}
	return is_valid;
}
function showError(fid, err) {
	var prnt = $("#" + fid).parent();

	prnt.addClass("err");
	if (prnt.children("dt")[0]) {
		prnt.children("dt:last").before("<p class='errmsg'>" + err + "</p>");
	} else prnt.append("<p class='errmsg'>" + err + "</p>");
}
function hideError(fid) {
	var prnt = $("#" + fid).parent();

	prnt.removeClass("err");
	prnt.children().remove("p.errmsg");
}
