$(document).ready(function(){
	$("#peopleslider_butRight").mouseover(peopleslider.moveleft);
	$("#peopleslider_butRight").click(peopleslider.moveleftMuch);
	$("#peopleslider_butLeft").mouseover(peopleslider.moveright);
	$("#peopleslider_butLeft").click(peopleslider.moverightMuch);
	
	bannerChanger.init();
	$("#banner_news li.changeBanner").mouseover(bannerChanger.changeBanner);
	$("#banner_news li.changeBanner").mouseout(bannerChanger.mouseout);
	
	$(".kontakty .person").hover(function() { $(this).addClass("hover") }, function() { $(this).removeClass("hover"); })
	$(".album img").hover(function() { $(this).addClass("hover") }, function() { $(this).removeClass("hover"); })
	
	scroller.init(
		$("#box_Video"),
		$("#box_Video .holder")
	);
	
	form_error_init();
	$.ajaxSetup({
		complete:form_error_init
	});
	
	scroller.create($("#box_Video"));
});

var peopleslider = {
	moveleft : function(){
		var block = $("#peopleslider_content .inner");
		var parent = block.parent();
		var posLeft = block.position()["left"];
		var innerWidth = peopleslider.countWidthFromChildren(block);
		if(Math.abs(posLeft)>=(innerWidth-parent.width()))
		{
			block.animate({
				left: -1 * (innerWidth-parent.width())
			});
			return;
		}
		block.animate({
			left: "-=20px"
		});
		return false;
		
	},
	moveleftMuch : function(){
		var block = $("#peopleslider_content .inner");
		var parent = block.parent();
		var posLeft = block.position()["left"];
		var innerWidth = peopleslider.countWidthFromChildren(block);
		if(Math.abs(posLeft)>=(innerWidth-parent.width()))
		{
			block.animate({
				left: "-=20px"
			}, 100);
			block.animate({
				left: -1 * (innerWidth-parent.width())
			});
			return false;
		}
		block.animate({
			left: "-=100px"
		});
		return false;
	},
	moveright : function(){
		var block = $("#peopleslider_content .inner");
		var posLeft = block.position()["left"];
		if(posLeft>=0){
			block.animate({left:"0px"});
			return false;
		}
		block.animate({
			left: "+=20px"
		});
	},
	moverightMuch : function(){
		var block = $("#peopleslider_content .inner");
		var posLeft = block.position()["left"];
		if(posLeft>=0){
			block.animate({left:"20px"}, 100);
			block.animate({left:"0px"});
			return false;
		}
		block.animate({
			left: "+=100px"
		});
	},
	/// private
	countWidthFromChildren : function(el){
		var kids = el.children();
		var total = 0;
		kids.each(function(i, kid){
			total += $(kid).width();
		});
		return total;
	}
};

var bannerChanger = {
	init : function()
	{
		$("#banner").css({position: "relative", height: "832px"});
		//$("#banner").parent().css({height: "208px", overflow: "hidden"});
		this.autoChanger();
	},
	changeBanner : function()
	{
		var li = $(this);
		li.siblings("li").removeClass("hover");
		li.addClass("hover");
		
		clearTimeout(bannerChanger.timer);
		
		var banner = null;
		if(li.hasClass("_1"))
			banner = 1;
		else if(li.hasClass("_2"))
			banner = 2;
		else if(li.hasClass("_3"))
			banner = 3;
		else if(li.hasClass("_4"))
			banner = 4;
			
		if(!banner)
			return false;
			
		bannerPositionY = 208 * (banner-1) * -1;

		var bannerEl = $("#banner");
		bannerEl.stop();
		
		bannerEl.animate({
			top: bannerPositionY
		}, 200);

	},
	
	mouseout : function()
	{
		bannerChanger.autoChanger();
		$(this).removeClass("hover");
	},
	
	current : 1,
	timer : null,
	autoChanger : function()
	{
		this.autoChangerChange();
	},
	autoChangerChange : function()
	{
		var el = $("#banner_news li.changeBanner._"+this.current);
		this.current++;
		if(this.current == 5) this.current = 1;
		
		el.mouseover();
		
		this.timer = setTimeout('bannerChanger.autoChangerChange()', 5000);
	}
};

var scroller = {
	init : function(element, holder){
		this.element = element;
		this.holder = holder;
		this.hzWidth = 100;
		this.timeOut = null;
		
		element.css({position: "relative"});
		holder.css({position: "absolute", left: 0});
		
		this.countWidth();
		
		element.mousemove(scroller.mousemv);
		
	},
	mousemv : function(e){
		var ofs = $(this).offset();
		// mouse position relative to element
		var left = e.pageX - ofs.left;
		var top = e.pageY - ofs.top;
		// width
		var width = $(this).width();
		
		// count hotzones
		var hzw = scroller.hzWidth;
		var hz1 = {
			xMin: 0,
			xMax: hzw
		};
		var hz2 = {
			xMin: width - hzw,
			xMax: width
		};
		
		// scroll if we're in a hotzone
		if(left > hz1.xMin && left < hz1.xMax)
		{
			scroller.scrollLeft();
		}
		else if(left > hz2.xMin && left < hz2.xMax)
		{
			scroller.scrollRight();
		}
		else
		{
			clearTimeout(scroller.timeOut);
		}
	},
	scrollLeft : function(){
		scroller.move(scroller.holder, "left", -3);
	},
	scrollRight : function(){
		scroller.move(scroller.holder, "left", 3);
	},
	move : function(el, prop, val){
		
		// console.log(el, prop, val);
		var old = el.css(prop);
		old = old.replace("pt", "").replace("px", "");
		var n = parseInt(old) + val;
		
		if(old > 0)
		{
			el.css(prop, 0);
			clearTimeout(scroller.timeOut);
			return false;
		}
		
		var edge = scroller.element.width() - scroller.holder.width();
		if(old < edge)
		{
			el.css(prop, edge);
			clearTimeout(scroller.timeOut);
			return false;
		}
		
		// console.log(prop, n);
		el.css(prop, n);
		
		clearTimeout(scroller.timeOut);
		scroller.timeOut = setTimeout(function(){
			scroller.move(el, prop, val);
		}, 20);
	},
	
	countWidth : function(){
		var w = 0;
		this.holder.children().each(function(){
			w += $(this).width() + 23;
		});
		this.holder.width(w);
	}
};

function form_error_init () {
	$(".form_error").mouseover(form_error_over)
									.click(form_error_click);
									
	$(".input_holder input").mouseover(form_field_focus);
}

function form_field_focus () {
	$(this).siblings(".form_error").trigger("over");
}

function form_error_over () {
	$(this).animate({top: "-30px", opacity: 0}, 300);
}

function form_error_click () {
	$(this).fadeOut(300);
}