$(document).ready(function() {
	$(document).bind("click", function() {
		$("#description_text:visible").slideUp("middle", function() {
			$("#description_text div.padding div.description_text:visible").css({display:"none"});
		});
		$("#description_items a.hover").removeClass("hover");
		$("#enter_form").removeClass("visible");
		$("div.select").each(function() {
			$(this).children("div.scroll").css({display:"none"});
			$(this).children("div.list").children("div.bottom").css({display:"none"});
			$(this).children("div.list").children("div.body").slideUp("fast", function() {
				$(this).parent().siblings("div.value").removeClass("open");
			});
			$(this).children("div.list").children("div.background").slideUp("fast");
			$(this).children("div.pointer").children("div").removeClass("up");
		});
	});
	if(window.location.hash != "") {
		$("div.properties div.item").removeClass("act");
		$("div.prop_menu a").removeClass("active");
		$("" + window.location.hash).addClass("active");
		$("" + window.location.hash + "_contants").addClass("act");
	}
	$("#description_text").click(function(e) {
		stopEvent(this, e);
	});
	$("div.button").hover(function() {
		$(this).addClass("button_hover");
	}, function() {
		$(this).removeClass("button_hover");
	});
	$("div.hidden_info div.link a.no_link").click(function() {
		$(this).parent().parent().children("div.form_field").slideToggle("middle");
		return false;
	});
	$("a.send").click(function() {
		checkForm(this);
		return false;
	});
	$("input.text").keypress(function(e){
		if(e.which == 13){
			checkForm(this);
			return false;
		}
	});
	$("div.pager div.pointer").hover(function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});
	$("#comment_form form div.button").click(function() {
		checkForm(this);
	});
	//top
	$("#designs").find("img").unbind("click").bind("click", function() {
		showHideLayer(this.className);
		showHideLayer("top_layer");
	});
	$("#top_layer").click(function(e) {
		$("div.design_image:visible").each(function() {
			showHideLayer($(this).attr("id"));
		});
		showHideLayer("top_layer");
		stopEvent(this, e);
	});
	$("div.design_image").click(function(e) {
		$("div.design_image:visible").each(function() {
			showHideLayer($(this).attr("id"));
		});
		showHideLayer("top_layer");
		stopEvent(this, e);
	});
	
	$("#description_items a").click(function() {
		$(this).parent().children("a.hover").removeClass("hover");
		$(this).addClass("hover");
		var textID = $(this).attr("class").split(" ")[0];
		var descriptionVisible = $("#description_text div.padding div.description_text:visible");
		if ($(descriptionVisible).get() != "") {
			if ($(descriptionVisible).attr("id") == textID) {
				$("#description_text:visible").slideUp("middle", function() {
					$("#" + textID).css({display:"none"});
				});
				$("#description_items a.hover").removeClass("hover");
			}
			else {
				$(descriptionVisible).animate({opacity:"hide"}, "slow", function() {
					$("#" + textID).animate({opacity:"show"}, "slow");
				});
			}
		}
		else {
			$("#" + textID).css({display:"block"});
			$("#description_text:hidden").slideDown();
		}
		return false;
	});
	$("#id_order_set").attr({value:"4"});
	$("#sets_list").find("div.icon").hover(function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	}).click(function() {
		$("#sets_list").find("div.checked").removeClass("checked").next("input").attr({value:""});
		$(this).addClass("checked");
		$("#id_order_set").attr({value:$(this).attr("id")});
	});
	$("#enter_remember").click(function() {
		if($(this).siblings("input").attr("value") == "N") {
			$(this).siblings("input").attr({value:"Y"});
			$(this).siblings("div.checkbox").children("div").addClass("checked");
		}
		else {
			$(this).siblings("input").attr({value:"N"});
			$(this).siblings("div.checkbox").children("div").removeClass("checked");
		}
	});
	$("#enter_checkbox").click(function() {
		if($(this).parent().siblings("input").attr("value") == "N") {
			$(this).parent().siblings("input").attr({value:"Y"});
			$(this).addClass("checked");
		}
		else {
			$(this).parent().siblings("input").attr({value:"N"});
			$(this).removeClass("checked");
		}
	});
	$("#partner_enter_remember").click(function() {
		if($(this).siblings("input").attr("value") == "N") {
			$(this).siblings("input").attr({value:"Y"});
			$(this).siblings("div.checkbox").children("div").addClass("checked");
		}
		else {
			$(this).siblings("input").attr({value:"N"});
			$(this).siblings("div.checkbox").children("div").removeClass("checked");
		}
	});
	$("#partner_enter_checkbox").click(function() {
		if($(this).parent().siblings("input").attr("value") == "N") {
			$(this).parent().siblings("input").attr({value:"Y"});
			$(this).addClass("checked");
		}
		else {
			$(this).parent().siblings("input").attr({value:"N"});
			$(this).removeClass("checked");
		}
	});
	$("#enter_link").click(function(e) {
		stopEvent(this, e);
		$("#enter_form").toggleClass("visible");
		return true;
	});
	$("#enter_form").click(function(e) {
		stopEvent(this, e);
	});
	$("input[name='remember_me']").attr({value:"N"});
	$("input.partner_enter_submit").hover(function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});
	$("input.partner_enter_submit").mousedown(function() {
		$(this).addClass("press");
	});
	$("input.partner_enter_submit").mouseup(function() {
		$(this).removeClass("press");
	});
	
	$("input.registration_form_submit").hover(function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});
	$("input.registration_form_submit").mousedown(function() {
		$(this).addClass("press");
	});
	$("input.registration_form_submit").click(function() {
		checkForm(this);
		return false;
	});
	$("input.registration_form_submit").mouseup(function() {
		$(this).removeClass("press");
	});
	$("input.registration_form_submit").keypress(function(evt) {
		$(this).addClass("press");
		if(evt.keyCode == 13) {
			checkForm(this);
			return false;	
		}
	});
	$("input.registration_form_submit").keyup(function() {
		$(this).addClass("press");
	});
	$("div.prop_menu a").click(function() {
		if(this.className.search("active") == -1) {
			$(this).siblings("a").removeClass("active");
			$(this).addClass("active");
			$(this).parent().siblings("div.act").removeClass("act");
			$("#" + $(this).attr("id") + "_contants").addClass("act");
		}
		return false;
	});
	$("#partners_map div.cities div.item").hover(function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});
	
	//select
	$("div.select ul li").not("[class*='h5']").hover(function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	}).click(function() {
		$(this).parents("div.select").children("div.value").children("span").text($(this).children("span").text());
		$(this).parents("div.select").children("input").attr({value:$(this).children("span").attr("class")});
		$("div.city_partners").hide();
		$("#" + $(this).children("span").attr("class")).show();
		$(window).scrollTop($("#select_partner_city").offset().top - 50);
	});
	$("div.select").each(function() {
		if($(this).find("ul").children("li").size() > 10) {
			$(this).find("ul").disableTextSelect();
			$(this).find("div.slider").disableTextSelect();
			selectWheel(this);
			sliderMove(this);
		}
	});
	$("div.select").click(function(e) {
		var thisSelect = this;
		$("div.select").each(function() {
			if (this != thisSelect) {
				$(this).children("div.scroll").css({display:"none"});
				$(this).children("div.list").children("div.bottom").css({display:"none"});
				$(this).children("div.list").children("div.body").slideUp("fast");
				$(this).children("div.pointer").children("div").removeClass("up");
			}
		});
		var listBodyObject = $(this).children("div.list").children("div.body");
		var listBackgroundObject = $(this).children("div.list").children("div.background");
		var ulObject = $(listBodyObject).children("ul");
		if($(ulObject).children("li").size() > 10) {
			$(listBodyObject).css({height:"180px"});
			if($(this).children("div.scroll").css("display") == "none") {
				$(thisSelect).children("div.value").addClass("open");
				$(thisSelect).children("div.scroll").children("div.slider").css({top:"37px"});
				$(thisSelect).children("div.list").children("div.body").children("ul").css({marginTop:"0"});
				$(listBodyObject).slideToggle("fast", function() {
					$(thisSelect).children("div.scroll").css({display:"block"});
					$(thisSelect).children("div.list").children("div.bottom").css({display:"block"});
				});
				$(listBackgroundObject).slideToggle("fast");
			}
			else {
				$(this).children("div.list").children("div.bottom").css({display:"none"});
				$(this).children("div.scroll").css({display:"none"});
				$(listBodyObject).slideToggle("fast", function() {
					$(thisSelect).children("div.value").removeClass("open");
				});
				$(listBackgroundObject).slideToggle("fast");
			}
//			selectWheel(this);
		}
		else {$(listBodyObject).slideToggle("fast");
		}
		$(this).children("div.pointer").children("div").toggleClass("up");
		stopEvent(this, e);
	});
	$("div.select div.scroll").click(function(e) {
		stopEvent(this, e);
	});
});

var selectHeightValue = 180;
var sliderPathValue = 103;
var liHeightValue = 28;
var margintop = 0;
var scrollTop = 0;
var sliderObject;
var ulObject;
var difference;
var selectKoeff;
function selectWheel(selectDiv) {
	var listBodyObject = $(selectDiv).children("div.list").children("div.body");
	$(listBodyObject).mousewheel(function (event, delta) {
		sliderObject = $(this).parent().parent().children("div.scroll").children("div.slider");
		ulObject = $(this).children("ul");
		difference = $(ulObject).height() - selectHeightValue;
		selectKoeff = difference/sliderPathValue;
		margintop = new String($(ulObject).css("marginTop"));
		margintop = parseInt(margintop.slice(0, margintop.length-2));
		scrollTop = new String($(sliderObject).css("top"));
		scrollTop = parseInt(scrollTop.slice(0, scrollTop.length-2));
		if((margintop <= 0) && (margintop + difference >= 0)) {
			$(ulObject).stop();
			$(sliderObject).stop();
			if(delta < 0) {
				margintop -= liHeightValue*3;
				scrollTop = Math.abs(Math.ceil(margintop/selectKoeff)) + 37;
				if (difference + margintop < 0) {
					margintop = -difference;
					scrollTop = sliderPathValue+37;
				}
				$(ulObject).animate({'marginTop':margintop});
				$(sliderObject).animate({'top':scrollTop});
			}
			else if(delta > 0) {
				margintop+=liHeightValue*3;
				scrollTop = Math.abs(Math.ceil(margintop/selectKoeff)) + 37;
				if (margintop > 0) {
					margintop=0;
					scrollTop=37;
				}
				$(ulObject).animate({'marginTop':margintop});
				$(sliderObject).animate({'top':scrollTop});
			}
		}
		event.stopPropagation();
		event.preventDefault();
	});
	$(selectDiv).children("div.scroll").children("div.up").click(function() {
		sliderObject = $(this).parent().children("div.slider");
		ulObject = $(this).parent().parent().children("div.list").children("div.body").children("ul");
		difference = $(ulObject).height() - selectHeightValue;
		selectKoeff = difference/sliderPathValue;
		margintop = new String($(ulObject).css("marginTop"));
		margintop = parseInt(margintop.slice(0, margintop.length-2));
		scrollTop = new String($(sliderObject).css("top"));
		scrollTop = parseInt(scrollTop.slice(0, scrollTop.length-2));
		if(margintop < 0) {
			$(ulObject).stop();
			$(sliderObject).stop();
			margintop+=liHeightValue;
			scrollTop = Math.abs(Math.ceil(margintop/selectKoeff)) + 37;
			if (margintop >= 0) {
				margintop=0;
				scrollTop = 37;
			}
			$(ulObject).animate({'marginTop':margintop});
			$(sliderObject).animate({'top':scrollTop});
		}
	}).hover(function() {$(this).addClass("up_hover");
	}, function() {$(this).removeClass("up_hover");
	});
	$(selectDiv).children("div.scroll").children("div.down").click(function() {
		sliderObject = $(this).parent().children("div.slider");
		ulObject = $(this).parent().parent().children("div.list").children("div.body").children("ul");
		difference = $(ulObject).height() - selectHeightValue;
		selectKoeff = difference/sliderPathValue;
		margintop = new String($(ulObject).css("marginTop"));
		margintop = parseInt(margintop.slice(0, margintop.length-2));
		scrollTop = new String($(sliderObject).css("top"));
		scrollTop = parseInt(scrollTop.slice(0, scrollTop.length-2));
		if((margintop <= 0)) {
			$(ulObject).stop();
			$(sliderObject).stop();
			margintop -= liHeightValue;
			scrollTop = Math.abs(Math.ceil(margintop/selectKoeff)) + 37;
			if (difference + margintop < 0) {
				margintop = -difference;
				scrollTop = sliderPathValue + 37;
			}
			$(ulObject).animate({'marginTop':margintop});
			$(sliderObject).animate({'top':scrollTop});
		}
	}).hover(function() {$(this).addClass("down_hover");
	}, function() {$(this).removeClass("down_hover");
	});
}
var oldY = 0;
var newY = 0;
var sliderElementObj;
function sliderMove(selectDiv) {
	var sliderElObj = $(selectDiv).children("div.scroll").children("div.slider");
	$(sliderElObj).mousedown(function(e) {
		ulObject = $(this).parent().parent().children("div.list").children("div.body").children("ul");
		$(ulObject).stop();
		$(sliderElObj).stop();
		oldY = e.pageY;
		sliderElementObj = this;
	});
	$(window).mouseup(function() {
    	oldY = 0;
    });
	$(window).mousemove(function(e) {
		if(oldY!=0) {
			ulObject = $(sliderElementObj).parent().parent().children("div.list").children("div.body").children("ul");
			difference = $(ulObject).height() - selectHeightValue;
			selectKoeff = difference/sliderPathValue;
			margintop = new String($(ulObject).css("marginTop"));
			margintop = parseInt(margintop.slice(0, margintop.length-2));
			scrollTop = new String($(sliderElementObj).css("top"));
			scrollTop = parseInt(scrollTop.slice(0, scrollTop.length-2));
			newY = e.pageY;
			if((scrollTop + newY - oldY) > 37 && (scrollTop + newY - oldY) < (sliderPathValue+37)) {
				scrollTop = scrollTop + newY - oldY;
				margintop = -1*Math.ceil((scrollTop-37)*selectKoeff);
			}
			if((scrollTop + newY - oldY) <= 37) {
				scrollTop = 37;
				margintop = 0;
			}
			if((scrollTop + newY - oldY) >= (sliderPathValue+37)) {
				scrollTop = sliderPathValue+37;
				margintop = -difference;
			}
			$(ulObject).css({'marginTop':margintop});
			$(sliderElementObj).css({'top':scrollTop});
			oldY = newY;
		}
    }); 
}

function showHideLayer(layer_id) {
	var layer = document.getElementById(layer_id);
	if (layer.style.display == "none") {
		if (layer_id == "top_layer") {
			layer.style.height = $(document).height() + "px";
		}
		else {
			var layerHeight = layer.offsetHeight;
			if (document.documentElement.clientHeight > $(layer).height()) {
				layer.style.top = parseInt(document.documentElement.clientHeight)/2 + parseInt(document.documentElement.scrollTop) + "px";
			}
			else {
				layer.style.top = parseInt($(layer).height())/2 + parseInt(document.documentElement.scrollTop) + 20 + "px";
			}			
		}
		layer.style.display = "block";
		if (layer.className == "design_image") {
			$(layer).pngFix();
		}
	}
	else {layer.style.display = "none";
	}
}

function NavigateThrough(event) {
	if (!document.getElementById) return;
	if (window.event) event = window.event;
	if (event.ctrlKey) {
		var link = null;
		var href = null;
		switch (event.keyCode ? event.keyCode : event.which ? event.which : null) {
			case 0x25: link = document.getElementById ('PrevLink'); break;
			case 0x27: link = document.getElementById ('NextLink'); break;
			case 0x24: href = '/'; break;
		}
		if (link && link.href) document.location = link.href;
		if (href) document.location = href;
	}
}

function checkForm(form_element) {
	var form = $(form_element).parents("form");
	var flag = 0;
	$(form).find("input[type!='hidden']").each(function() {
		if ($(this).attr("value") == "" && $(this).parent().children("h6").children("span").text() != "") {
			if (this.getAttribute("name") && this.getAttribute("name").search("PASSWORD_CONFIRM") != -1) {
				var passConfField = this;
				var passwordField;
				$(form).find("input[name*='PASSWORD']").each(function() {
					if (this != passConfField) {
						passwordField = this;
					}
				});
				if($(passwordField).attr("value") != ""){
					$(this).parent().addClass("attention");
					flag = 1;
				}
				else {$(this).parent().removeClass("attention");
				}
			}
			else {
				flag = 1;
				$(this).parent().addClass("attention");
			}
		}
		else {
			if ($(this).attr("name").toLowerCase().search("mail") != -1 && $(this).parent().children("h6").children("span").text() != "") {
				var mail = $(this).attr("value");
				var mailRegex = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i;
				if(!mail.match(mailRegex)){
					$(this).parent().addClass("attention");
					flag = 1;
				}
				else {$(this).parent().removeClass("attention");
				}
			}
			else {
				if (this.getAttribute("name") && this.getAttribute("name").search("PASSWORD_CONFIRM") != -1) {
					var passConfField = this;
					var passwordField;
					$(form).find("input[name*='PASSWORD']").each(function() {
						if (this != passConfField) {
							passwordField = this;
						}
					});
					if($(this).attr("value") != $(passwordField).attr("value")){
						$(this).parent().addClass("attention");
						flag = 1;
					}
					else {$(this).parent().removeClass("attention");
					}
				}
				else {
					$(this).parent().removeClass("attention");
				}
			}
		}
	});
	$(form).find("textarea").each(function() {
		if ($(this).text() == "" && $(this).attr("value") == "" && $(this).parent().children("h6").children("span").text() != "") {
			flag = 1;
			$(this).parent().addClass("attention");
		}
		else {
			$(this).parent().removeClass("attention");
		}
	});
	if ($("#conditions").find("input").attr("checked") == "") {
		flag = 1;
		$("#conditions").addClass("attention");
	}
	else {
		$("#conditions").removeClass("attention");
	}
	if (flag == 0) {
		$(form).submit();
	}
	else {
		return false;
	}
}

function stopEvent(object, e) {
if(!e) e = window.event;
if(e.stopPropagation) e.stopPropagation();
else e.cancelBubble = true;
}

//навигация с помощью стрелок
function NavigateThrough(event) {
	if (!document.getElementById) return;
	if (window.event) event = window.event;
	if (event.ctrlKey) {
		var link = null;
		var href = null;
		switch (event.keyCode ? event.keyCode : event.which ? event.which : null) {
			case 0x25: link = document.getElementById ('PrevLink'); break;
			case 0x27: link = document.getElementById ('NextLink'); break;
			case 0x24: href = '/'; break;
		}
		if (link && link.href) document.location = link.href;
		if (href) document.location = href;
	}			
}
