$(function() {
	var cars = $("#cars .cars");
	
	carsDistance(cars);
	$(window).resize(function() { carsDistance(cars); });
	
	cars.hover(function() {
		$(this).find(".size").css({visibility: "visible"});
		blinker($(this));
	}, function() {
		$(this).find(".size").css({visibility: "hidden"});
		clearInterval(timeBlink);
	}).click(function() {
		if ($(this).attr("id") != "car1") {
			var th = $(this);
			blinker(th);
			th.find(".size").css({visibility: "hidden"});
			var speed = ($("#cars").width() - parseInt(th.css("left")))*4;
			setTimeout(function() {
				th.css({zIndex:10}).animate({left:$("#cars").width()+150},speed,'',function() {
					location.href = "/price/#t"+th.attr("id").split("car")[1];
					urlHash($("#price, #price1"));
				});
			}, 1000);
		} else {
			location.href = "/price/#t1";
			urlHash($("#price, #price1"));
		}
	});
	
	$(".logo2").css({opacity:0.1}).hover(function() {
		$(this).animate({opacity: 1});
		blinker($(this));
	}, function() {
		$(this).animate({opacity: 0.1});
		clearInterval(timeBlink);
	});
	
	$("#price tr, #price1 tr").hover(function () { $(this).addClass("hover"); }, function () { $(this).removeClass("hover"); });
	
	textinInit($("form"));
	
	urlHash($("#price, #price1"));
	
	$(".gal li a").click(function() {
		var th = $(this);
		
		$(".bImg").remove();
		$(document.body).append($(document.createElement("div")).append('<div class="indicator"></div>').addClass("bImg").css({top:$("img", th).offset().top-4, left:$("img", th).offset().left-4 }).append($("img", th).clone()));
		
		var bImg = new Image();
		$(bImg).load(function () {
			var th1 = $(this);
			$(this).hide().appendTo($(document.body));
			$(".indicator").remove();
			$(".bImg").click(function() {
				$(this).remove();
			}).animate({top:Math.abs($(window).height()/2+$(document).scrollTop()-$(this).height()/2), left:Math.abs($(document.body).width()/2-$(this).width()/2)}, {complete:function() {
				$("img", $(this)).animate({width:th1.width(), height:th1.height()}, {complete:function() {
					$(this).attr("src", th1.attr("src"));
				}});
			}});
		}).attr("src", th.attr("href"));
		
		return false;
	});
});

function carsDistance(o) {
	var carsWidth = 879;
	var carsDist = parseInt(($("#cars").width() - carsWidth)/7);//alert(carsDist);
	carsWidth = 0;
	o.each(function(i) {
		var w = (i+1)*carsDist + carsWidth;
		$(this).animate({left:w}, 1000);
		carsWidth += $(this).width();
	});
}

var timeBlink = 0;
var blinkFreq = 300;
function blinker(o) {
	var bl = o.find(".blinker_front, .blinker_back")
	timeBlink = setInterval(function() {
		bl.show();
		setTimeout(function() {
			bl.hide();
		}, blinkFreq);
	}, blinkFreq*2);
}

function urlHash(o) {
	var url = location.hash;
	if (url != '') {
		if (url.split("#")[1] != '') {
			url = url.split("#")[1];
			$("td", o).removeClass("active");
			$("."+url+" td", o).addClass("active");
		}
	}
}

function textinInit(o) {
	$("input, textarea", o).each(function() {
		if ($(this).val() == '') {
			if ($(this).attr("type") == "password") {
				var th = $(this);
				th.after('<input type="text" value="'+$(this).attr("title")+'" />').hide();
				$("input:text", th.parent()).focus(function() {
					$(this).hide();
					th.show().focus();
				});
			}
			$(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() == '') {
				if ($(this).attr("type") == "password") {
					$(this).hide();
					$("input:text", $(this).parent()).show();
				}
				$(this).get(0).value = $(this).attr("title");
			}
		});
	});
}





