$(document).ready(function() {
	$(".jquery_enabled").css("display", "block");
	
	window.onresize = resizeScrollPane;
	window.onscroll = resizeScrollPane;

	resizeScrollPane();
	
/*	
	timer = setInterval(onTimer, 6000);
	timerActive = true;

	$('img#anim_state').attr("src", "img/ico_cycle.gif");
//	alert($('img#anim_state').attr("src"));
	
	$('div#right_column').hover(function(){
		scrollHover = true;
		if(timerActive){
			$('img#anim_state').attr("src", "img/ico_pause.gif");
		}
	}, function(){
		scrollHover = false;
		if(timerActive){
			$('img#anim_state').attr("src", "img/ico_cycle.gif");
		}
//		timer = setTimeout("onTimer()", 1000);
	});
	
	$('div.list_item').first().addClass("current_scroll_item");
// bugs with flash :(
//	$('div.scrollpane').css("overflow", "hidden");
	
	$("img#anim_state").click(function(){
		if(timerActive){
			clearInterval(timer);
			timerActive = false;

			$('img#anim_state').attr("src", "img/ico_play.gif");
		} else {
			timer = setInterval(onTimer, 6000);
			timerActive = true;

			$('img#anim_state').attr("src", "img/ico_pause.gif");
		}
	});
	
	$("div#scroll_down").click(function(){
		scrollToNextItem(false);
	});

	$("div#scroll_up").click(function(){
		scrollToPrevItem(false);
	});
*/
	
	$(".hover_controller").hover(function(){
		var master = $(this);
		if(master.attr("slave") in hoverTimer){
			clearTimeout(hoverTimer[master.attr("slave")]);
		}

		openController(master);
	}, function(){
		var master = $(this);
		hoverTimer[master.attr("slave")] = setTimeout(function(){
			closeController(master)
		}, 200);
	});

	$(".defaultController").click(function(){
		var $this = $(this);
		var slave = $("#" + $(this).attr("slave"));
		if(slave.css("display") == "none"){

		currentSlave = slave;		
		resizeScrollPane();
		
		if ($this.attr('callme')) {
			eval($this.attr('callme'));			
		}
		
		var bgFade = $('#bgFade');

		bgFade.css("display", "block");
		bgFade.css("opacity", "0");
		
		$("#popupClose").click(function(){
			removePopup(currentSlave);
		});
		
		bgFade.click(function(){
			removePopup(currentSlave);
		});

		bgFade.animate({opacity: 0.8}, 500, function(){});
		
			slave.slideDown("slow");
		} else {
			currentSlave = null;
			removePopup(slave);
		}
	});
	
	$(".schemeController").click(function(){
		var sl = $("#sch_salon");
		if(sl.css("display") == "block"){
			sl.slideUp("fast");
		}
		
		sl = $("#sch_tech");
		if(sl.css("display") == "block"){
			sl.slideUp("fast");
		}
	
		var slave = $("#" + $(this).attr("slave"));
		slave.slideDown("slow");
	});
	
	$(".submitCarController").click(function(){
		var $this = $(this);
		var slave = $("#submitCarPane");

		if(slave.css("display") == "none"){
			slave.slideDown("slow");
			
			var offset = slave.offset();
			
			$('html,body').animate({scrollTop: offset.top}, 1000);
			
			//alert(offset.top);
		} else {
			slave.slideUp("slow");
		}
		
	});
	
	
	function LoadPhotos(id, slave) {
		if (bigPhotos[id] != undefined) {
			slave.html(bigPhotos[id]);
		}		
	}
	
	$("#onSaleController").click(function(){
		if(onSaleVisible){
			closeOnSale();
		} else {
			openOnSale();
		}
	});
	
    $('#gallery a').lightBox();
	
	if(typeof onSaleTabOpen != 'undefined' && onSaleTabOpen){
		openOnSale();
	}
	
});

var hoverTimer = new Array();
var timer;

var scrollHover = false;
var timerActive = false;

var onSaleVisible = false;

var currentSlave = null;

function submitCarPaneToggle(){
	$("#submitCarPane").toggle("slow");
}

function openController(master){
	var slave = $("#" + master.attr("slave"));
	slave.slideDown("fast");
		
	var imgslave = $("#" + master.attr("img_slave"));
	imgslave.attr("src", master.attr("img_over"));
}

function closeController(master){
	var slave = $("#" + master.attr("slave"));
	slave.slideUp("fast");

	var imgslave = $("#" + master.attr("img_slave"));
	imgslave.attr("src", master.attr("img_normal"));
}

function closeOnSale(){
	var onSale = $("#onSaleController");
	var slave = $("#" + onSale.attr("slave"));

	slave.slideUp("slow", function(){
		resizeScrollPane();
	});
	onSaleVisible = false;
}

function openOnSale(){
	var onSale = $("#onSaleController");
	var slave = $("#" + onSale.attr("slave"));

	slave.slideDown("slow", function(){
		resizeScrollPane();
	});
	onSaleVisible = true;
}

function removePopup(slave){
	$('#bgFade').animate({opacity:	0}, 500, function(){
		$('#bgFade').css("display", "none");
	});
	slave.slideUp("slow").css("display", "none");
}

function onTimer(){
	if(!scrollHover){
		scrollToNextItem(true);
	}
}

function onScrollEnd(){
//	timer = setTimeout("onTimer()", 1000);
}

function scrollToCurrent(){
	var offFirst = $('div#scrollcontent').offset();
	var offCurrent = $('div.current_scroll_item').offset();
	
//	$('div#scrollpane').scrollTop(offNextEl.top - off.top);

	$('div#scrollcontent').animate({top: offFirst.top - offCurrent.top + 15}, 1000, function() {
		onScrollEnd();
	});
}

function scrollToPrevItem(cycle){
	var el = $('div.current_scroll_item');
	var prevEl = el.prev();

	if(!prevEl.hasClass("list_item") && cycle){
		prevEl = $('div.list_item').last();
	}

	if(prevEl.hasClass("list_item")){
		el.removeClass("current_scroll_item");
		prevEl.addClass("current_scroll_item");

		scrollToCurrent();
	}
}

function scrollToNextItem(cycle){
	var el = $('div.current_scroll_item');
	var nextEl = el.next();

	if(!nextEl.hasClass("list_item") && cycle){
		nextEl = $('div.list_item').first();
	}
	
	if(nextEl.hasClass("list_item")){
		el.removeClass("current_scroll_item");
		nextEl.addClass("current_scroll_item");
	
		scrollToCurrent();
	}
}

function resizeScrollPane(){
/*
//высота всего, кроме основного блока - 475 px - 30 - паддинг right_column
//вообще конечно функция очень жесткая и кривая
	var dh = 0;
	var i;
	
	$(".calc").each(function () {
		dh += $(this).outerHeight();
	});

	var h = $(window).height() - dh - 475 - 30;
	var minH = $("#left_column").height() - 60;
	
	if(h < minH) h = minH;

	$("#scrollpane").css("height", h);
*/	
	var scrollTop = $(window).scrollTop();
	var wh = $(window).height();
	var ww = $(window).width();
	
	if(currentSlave != null){
		var orderForm = $("div#order_form");
		if(orderForm.length > 0){
			orderForm.css("top", (wh - orderForm.height()) / 2 + scrollTop);
			orderForm.css("left", (ww - orderForm.width()) / 2);
		}
		
		var photosForm = $("div#big_photos");
		if(photosForm.length > 0){
			photosForm.css("top", (wh - photosForm.height()) / 2 + scrollTop);
			photosForm.css("left", (ww - photosForm.width()) / 2);
		}
	
		var priceForm = $("div#same_price");
		if(priceForm.length > 0){
			priceForm.css("top", (wh - priceForm.height()) / 2 + scrollTop);
			priceForm.css("left", (ww - priceForm.width()) / 2);
		}
	
		var el = $("div#bgFade");
		if(el.length > 0){
			el.css("width", ww);
			el.css("height", $("body").height());
		}
	}

}
