$(document).ready(function() {
	//$("h1").wrap("<div style='padding: 60px 0 0 0;float:left;'></div>")
	//$('body').easyTabs({defaultContent:1});
	options = {
		"height" : 0.5,
		"opacity" : 0.31
	};
	$(".reflection,#imgfool").reflect(options);
	//var imgremove = $("#imgfool").html();
	$("#imgfool").reflect(options);
	$(".input select").each(function() {
		$(this).wrap("<div id='select" + $(this).attr("name") + "'></div>");
		$(this).css("display", "none");
		wrap = $("#select" + $(this).attr("name"));
		wrap.append("<div class='select'></div>")
		newselect = wrap.find(".select");
		oldselect = wrap.find("select");
		i = 0;
		oldselect.find("option").each(function() {
			if(!i)
				newselect.append("<div class='option selected'><a href='" + $(this).attr("value") + "'>" + $(this).text() + "</a></div>");
			if($(this).attr("selected")) {
				newselect.append("<div class='option sel'><a href='" + $(this).attr("value") + "'>" + $(this).text() + "</a></div>");
			} else {
				newselect.append("<div class='option'><a href='" + $(this).attr("value") + "'>" + $(this).text() + "</a></div>");
			}
			i = 1;
		});
		name = $(this).attr("name");
		val = (oldselect.find(".sel").text() ? oldselect.find(".sel").text() : "");
		$(this).remove();
		selected = newselect.find(".sel").text();
		wrap.append('<input type="hidden" value="' + (newselect.find(".sel a").attr("href") != "" ? selected : "") + '" name="' + name + '" />')
		newselect.find(".selected a").text(selected);
	});
	$(".select .option a").live("click", function() {
		$(this).parent().removeClass("last");
		$(this).parent().parent().find(".option.selected a").text($(this).text()).attr("href", $(this).attr("href"));
		$(this).parent().parent().removeClass("hover");
		$(this).parent().parent().parent().find("input").css("display", "block").val($(this).attr("href"));
		$(this).parent().parent().find("div:last").addClass("last");
		return false;
	});
	$(".select .option.selected a").live("click", function() {
		if($(this).parent().parent().hasClass("hover")) {
			$(this).parent().parent().removeClass("hover");
		} else {
			$(this).parent().parent().addClass("hover");
		}
	});
	$(".select").mouseleave(function() {
		$(this).removeClass("hover");
	});
	$("input[type='text']").each(function(){
	if(!$(this).attr("ik")){
	$(this).attr("ik", $(this).val());
	}
	}).click(function(){
	if($(this).attr("value") == $(this).attr('ik')){
	$(this).attr("value", "");
	}
	}).blur(function() {
		if($(this).attr("value") == "") {
			$(this).attr("value", $(this).attr('ik'));
		}
	});
	$("input[type='submit']").live("click", function() {
		$(this).parents("form").find("input[type='text']").each(function() {
			if($(this).attr("value") == $(this).attr('ik')) {
				$(this).attr("value", "");
			}
		});
	});
	$("#photos a").click(function() {
		$("#imgfool").unreflect(options);
		$("#imgfool").attr("src", $(this).attr("href"));
		setTimeout(function() {
			options = {
				"height" : 0.5,
				"opacity" : 0.31
			};
			$("#imgfool").reflect(options);
		}, 1)
		return false;
	});
	$("#photos a:first").click();
	var width;
	var maxsidth = 865;
	var itemwidth = 160 + 11;
	var maxleft = 0;
	$("#slider-left, #slider-right").addClass("unactive");
	if($(".slideritem").size() > 0) {
		count = $(".slideritem").size();
		width = count * itemwidth;
		if(width >= maxsidth) {
			$("#slider-right").removeClass("unactive");
		}
		maxleft = width - maxsidth;
	}
	$("#slider-left").live("click", function() {
		if(!$(this).hasClass('unactive')) {
			left += itemwidth;
			$("#slider").animate({
				left : left
			});
			//left = $("#slider").css("left");
			$("#slider-right").removeClass("unactive");
			if(parseInt(left) * (-1) <= 0) {//<!-- -->
				$(this).addClass("unactive");
			}
		}
		return false;
	});
	var left = 0;
	$("#slider-right").live("click", function() {
		if(!$(this).hasClass('unactive')) {
			left -= itemwidth;
			$("#slider").animate({
				left : left
			});
			$("#slider-left").removeClass("unactive");
			/*left = $("#slider").css("left");*/
			if(parseInt(left) * (-1) >= maxleft - (itemwidth * 0)) {
				$(this).addClass("unactive");
			}
		}
		return false;
	});
	if($("#collectiondiv").size() > 0) {

		onresizedo();
	}
	var iactive;
	var myleft;
	var itemwidth;
	function onresizedo() {
		itemwidth = ($("#collectiondiv").hasClass("furn") ? 500 : 330);
		var count = $("#collectiondiv .collection").size();
		width = count * itemwidth;
		if($("#collectiondiv .collection").size() % 2 == 0) {
			width += itemwidth;
		}
		$("#collectionwindow, #collectiondiv").css('width', width + "px");
		windowWidth = $("html").width();
		$("#leftright").css("width", $(window).width()+"px");
		$("#collectiona").css("margin-left", (($(window).width()-500)/2)+"px");
		if(width > windowWidth) {
			l = ( width - windowWidth) / 2;
			wid = windowWidth + l;
			$("#collectionwindow").css('width', (windowWidth+l)+"px").css("left", "-" + l + "px");
		}
		k = $("#collectiondiv .collection").size();
		iactive = Math.floor(k / 2) + 1;
		count = iactive + ( iactive - 1);
		//Показывается по центру целых изображений
		left2 = ( windowWidth - (count * itemwidth)) / 2;
		left2 = 0;
		myleft = left2;
		active(iactive);
		$("#collectiondiv .collection img").bind("load", function() {
			active(iactive);
		});
	}
	var mymyiactive = 0;
	function active(i) {
		var activeitem = $($("#collectiondiv .collection")[( i - 1)]);
		$("#collectiondiv .collection .reflected, #collectiondiv .collection canvas").addClass("opr");
		activeitem.find(".reflected").removeClass("opr").parent().find("canvas").removeClass("opr");
		$("h1.center").html(activeitem.find(".title").text());
		$("#content").html(activeitem.find(".collectioncontent").html());
		href = activeitem.find(".collectioncontent a").attr('href');
		$("#collectiona a").attr("href", href);
		k = $("#collectiondiv .collection").size();
		iactive = Math.floor(k / 2) + 1;
		/* i = iactive;
		activeitem.css('border', '1px solid black'); */
		h100 = 397;
		varH = 0.1;
		$("#collectiondiv .reflect").css("width", "auto").css('height', "auto");
		if(mymyiactive < 8){
			mymyiactive ++;
			$("#collectiondiv .collection").find("img").css("height", (h100 - h100*varH*4)+"px").css("top", (h100*varH*4/2)+"px");
			$($("#collectiondiv .collection")[( i - 1)]).find("img").css("height", (h100 - h100*varH*0)+"px").css("top", (h100*varH*0/2)+"px");
			$($("#collectiondiv .collection")[( i - 2)]).find("img").css("height", (h100 - h100*varH*1)+"px").css("top", (h100*varH*1/2)+"px");
			$($("#collectiondiv .collection")[( i - 3)]).find("img").css("height", (h100 - h100*varH*2)+"px").css("top", (h100*varH*2/2)+"px");
			$($("#collectiondiv .collection")[( i - 4)]).find("img").css("height", (h100 - h100*varH*3)+"px").css("top", (h100*varH*3/2)+"px");
			$($("#collectiondiv .collection")[( i)]).find("img").css("height", (h100 - h100*varH*1)+"px").css("top", (h100*varH*1/2)+"px");
			$($("#collectiondiv .collection")[( i + 1)]).find("img").css("height", (h100 - h100*varH*2)+"px").css("top", (h100*varH*2/2)+"px");
			$($("#collectiondiv .collection")[( i + 2)]).find("img").css("height", (h100 - h100*varH*3)+"px").css("top", (h100*varH*3/2)+"px");
		}else{
			$($("#collectiondiv .collection")[( i - 1)]).find("img").animate({
				height : (h100 - h100*varH*0),
				top : (h100*varH*0/2)
			});
			$($("#collectiondiv .collection")[( i - 2)]).find("img").animate({
				height : (h100 - h100*varH*1),
				top : (h100*varH*1/2)
			});
			$($("#collectiondiv .collection")[( i - 3)]).find("img").animate({
				height : (h100 - h100*varH*2),
				top : (h100*varH*2/2)
			});
			$($("#collectiondiv .collection")[( i - 4)]).find("img").animate({
				height : (h100 - h100*varH*3),
				top : (h100*varH*3/2)
			});
			$($("#collectiondiv .collection")[( i)]).find("img").animate({
				height : (h100 - h100*varH*1),
				top : (h100*varH*1/2)
			});
			$($("#collectiondiv .collection")[( i + 1)]).find("img").animate({
				height : (h100 - h100*varH*2),
				top : (h100*varH*2/2)
			});
			$($("#collectiondiv .collection")[( i + 2)]).find("img").animate({
				height : (h100 - h100*varH*3),
				top : (h100*varH*3/2)
			});
		}
	}
	$(window).resize(function() {
		onresizedo();
	});

	var correct = ($("#collectiondiv").hasClass("furn") ? 40 : 0);
	var toright = 0;
	var toleft = 0;
	$("#collectionright").click(function() {
		myleft = itemwidth;
		$("#collectiondiv").animate({
			left : myleft
		}, function() {
			$("#collectiondiv").prepend($("#collectiondiv .collection:last"));
			$("#collectiondiv").css("left", 0);
			toright--;
			if(toright <= 0) {/*>*/
				toright = 0;
				active(iactive);
			} else {
				$("#collectionright").click();
			}
		});
		return false;
	});
	$("#collectionleft").click(function() {
		//alert("left");
		myleft = itemwidth;
		$("#collectiondiv").animate({
			left : -myleft
		}, function() {
			$("#collectiondiv").append($("#collectiondiv .collection:first"));
			$("#collectiondiv").css("left", 0);
			toleft--;
			if(toleft <= 0) {/*>*/
				toleft = 0;
				//iactive++;
				active(iactive);
			} else {
				$("#collectionleft").click();
			}
		});
		return false;
	});
	$("#news table a").click(function() {
		countstep = 0;
		i = 0;
		id = $(this).parents(".collection").attr("id");
		select = $("#collectiondiv #" + id);
		i = $("#collectiondiv .collection").index(select);
		if(i == -1) {
			myi = 0;
			;
			$("#collectiondiv .collection").each(function() {
				i++;
				if($(this).attr("id") == id) {
					myi = i;
				}
			});
			i = myi;
		}
		countstep = iactive - (i + 1);
		if(countstep < 0) {
			toleft = countstep * (-1);
			$("#collectionleft").click();
		} else {
			if(countstep != 0) {
				toright = countstep;
				$("#collectionright").click();
			}
		}
		return false;
	});
	$("#checkmyform").bind("submit", function(){
		name = ($("#name").attr("value") != $("#name").attr("ik") ? $("#name").attr("value") : "");
		email = ($("#email").attr("value") != $("#email").attr("ik") ? $("#email").attr("value") : "");
		textarea = $("#checkmyform .input.one-one.textarea").text();
		replytext = $("#reply").text();
		if(!name){
			alert(replytext+" "+$("#name").attr("ik"));
		}else{
			if(!email){// || emailCheck(email) == false
				alert(replytext+" "+$("#email").attr("ik"));
			}else{
				if(!textarea){
					alert(replytext+" "+$("#textarea").attr("ik"));
				}else{
					return true;
				}
			}
		}
		return false;
	});
});

