$(function() {
	$("form").each(function() { textinInit($(this)); });
	
	$(".nav li a").hover(function() {
		$(this).parent().parent().find("li").addClass("hov");
		$(this).parent().removeClass("hov");
	},function() {
		if ($(".nav li.sel").length > 0) {
			$(this).parent().addClass("hov");
			$(".nav li.sel").removeClass("hov");
		} else {
			$(this).parent().parent().find("li").removeClass("hov");
		}
	});
	if ($(".nav li.sel").length > 0) {
		$(".nav li").addClass("hov");
		$(".nav li.sel").removeClass("hov");
	}
	
	if ($(".art").length > 0) {
		art(150);
		$(window).resize(function() { art(150); });
	}
	$(".gal ul li").click(function() {
		var th = $(this);
		var lnk  = $("a", $(this));
		$(".bigImg a").fadeOut("slow", function() {
			var th1 = $(this);
			var img = new Image();
			$(img).load(function() {
				$(".gal ul li:hidden").show();
				th.hide();
				th1.css({backgroundImage: "url(" + lnk.attr("href") + ")" });
				th1.attr("href", lnk.attr("rel"));
				th1.fadeIn("slow");
			}).attr("src", lnk.attr("href"));
		});
		return false;
	});
	if ($(".bigImg").length > 0) $(".bigImg a").lightBox();
	
	$(".orderBtn").click(function() {
		$(this).hide();
		$(".orderForm1").show();
	});
	
	$("#photoinp").change(function() { $("#photoinptext").get(0).value = $(this).val(); });
});

function art(speed) {
	var contw = $(".artcont").width();
	var actw = $("ul.art li").eq(0).width();
	var num = $("ul.art li").length;
	var w1 = contw / num;
	var w = (contw - actw) / (num - 1);
	
	$("ul.art li").each(function(i) {
		$(this).css({left:w1*i});
	});
	
	$("ul.art li").hover(function() {
		var indh = $("ul.art li").index(this);
	
		$(".img div", this).css({visibility:"visible"}).animate({backgroundPosition:0}, {duration:speed, queue:false});
		
		$("ul.art li").each(function() {
			var pa = $(this).prevAll().length;
			var ind = $("ul.art li").index(this);
			if (ind <= indh) { pa = w*pa; }
			else { pa = w*(pa-1) + actw; }
			$(this).animate({left:pa}, {duration:speed, queue:false});
		});
		
		$(".text", this).animate({marginTop:0}, {duration:speed, queue:false});
	}, function() {
		$("ul.art li").each(function(i) {
			$(this).animate({left:w1*i}, {duration:speed, queue:false});
		});
		
		$(".img div", this).animate({backgroundPosition:-130}, {duration:speed, queue:false, complete:function() { $(this).css({visibility:"hidden"}) }});
		
		$(".text", this).animate({marginTop:-190}, {duration:speed, queue:false});
	});
}

function textinInit(o) {
	$("input, textarea", o).each(function() {
		if ($(this).val() == '') {
			$(this).get(0).value = $(this).attr("title");
		};
		$(this).focus(function(){
			if ($(this).val() == $(this).attr("title")) {
				$(this).get(0).value = '';
			}
		}).blur(function(){
			if ($(this).val() == '') {
				$(this).get(0).value = $(this).attr("title");
			}
		});
	});
	
	if (o.hasClass("orderform") || o.hasClass("contactform") || o.hasClass("obmenform")) {
		o.submit(function() {
			if ($("input[type='text']:eq(1)", this).val() == '') {
				$("input[type='text']:eq(1)", this).parent().parent().prevAll("label").eq(0).css({color:"#bd1111"});
			} else {
				$("input[type='text']:eq(1)", this).parent().parent().prevAll("label").eq(0).css({color:""});
			}
			if ($("input[type='text']:eq(2)", this).val() == '') {
				$("input[type='text']:eq(2)", this).parent().parent().prevAll("label").eq(0).css({color:"#bd1111"});
			} else {
				$("input[type='text']:eq(2)", this).parent().parent().prevAll("label").eq(0).css({color:""});
			}
			if ($("input[type='text']:eq(1)", this).val() != '' && $("input[type='text']:eq(2)", this).val() != '') {
				if (o.hasClass("orderform")) {
					var params = { 
						isAjax: "1",
						template: "5"
					};
					var fields = $(this).serializeArray();
					jQuery.each(fields, function(i, field){
						params[field.name] = field.value;
					});
					$.post($(this).attr("action"), params, function(data) {
						$(".orderForm1").hide();
						var mes = $(document.createElement("div"));
						mes.html('<div class="avl"><span>'+$("message", data).text()+'</span></div>');
						
						if ($("message", data).attr("error") == "1") {
							setTimeout(function() {
								mes.remove();
								o.parent().show();
							}, 2000);
						}
						
						mes.insertAfter(o.parent());
					}, "xml");
				} else {
					return true;
				}
			}
			
			return false;
		});
	}
}