//Создание меню
$(function()
{
	$('.etu_hardGallery').each(function() {var myDinamic = new EachHardGallery(this)});
	$('.etu_simpleGallery').each(function() {var myDinamic = new EachSimpleGallery(this)});
	var myBlock = new etu_shadow();
	var myBlock = new etu_get();
});

var etu_shadow = function()
{
	$('.etu_shadow')
		.css('opacity',0)
		.delay(500)
		.animate({"opacity":1},1000);
}

var etu_showMenu = function(myClass)
{
	if (! myClass) return;
	
	$('.'+myClass+' a').each(function()
	{
		var myBlock = this.parentNode.parentNode;
		if ($(this).attr("class") == "sel") $(this).attr("rel","sel");
		$(this)
			.mouseenter(function()
			{
				$("a",myBlock).each(function() 
					{
						$(this).attr("class","");
					});
				$(this).attr("class","sel");
			});
	});
	
	
	

	$('.'+myClass).each(function()
	{
		$(this).mouseleave(function()
		{
			$("a",this).each(function() 
					{
						$(this).attr("class","");
					});
			if ($('a[rel=sel]',this).size()) $($('a[rel=sel]',this)[0]).attr("class","sel");	
		});
		
	});
	
}

////////////


var EachSimpleGallery = function(myObj, myTimeNext)
{
	this.myObj = myObj;
	if (myTimeNext !== false) this.myTimeNext = 5000;
	if (! this.myObj) return;
	this.myBg = $('li>*',this.myObj);
	this.size = this.myBg.size() - 1;
	if (this.size < 0) return; 
	this.myCurrent = false;
	
	this.timeOut = 2500;
	this.work = false;
	
	this.currentTimeout = false;
	
	
	this.Install = function()
	{
		//Обновление бэкграундов
		this.myBg 
			.css({"position":"absolute","top":0,"left":0,"width":"100%","height":"100%","opacity":0})
			
		this.myShow(0);
		
	}
	
	this.myShow = function(myNum)
	{
		if (this.currentTimeout) clearTimeout(this.currentTimeout);
		this.work = true;
		
		if (this.myCurrent !== false)
		{
			if (this.myBg[this.myCurrent]) $(this.myBg[this.myCurrent]).animate({"opacity":0},this.timeOut)
		}
		var myObj = this;
		this.myCurrent = myNum;
		if (this.myBg[myNum]) 
		{
			$(this.myBg[myNum]).animate({"opacity":1},this.timeOut, function() {myObj.work = false; if(myObj.myTimeNext) myObj.currentTimeout = setTimeout(function(){myObj.Next()},myObj.myTimeNext)});
		}
			
	}
	
	
	
	this.Next = function()
	{
		if (this.size < 1) return; 
		var myNum = this.myCurrent + 1;
		if (myNum > this.size) myNum = 0;
		this.myShow(myNum);
	}
	
	this.Install();
}


var EachHardGallery = function(myObj, myTimeNext)
{
	this.myObj = myObj;
	if (myTimeNext !== false) this.myTimeNext = 10000;
	if (! this.myObj) return;
	this.myIco = $('.etu_navigation li a',this.myObj);
	this.myBg = $('.etu_preview li .item',this.myObj);
	this.size = this.myIco.size() - 1;
	if (this.size < 1) return; 
	this.myCurrent = false;
	this.timeOut = 1500;
	this.work = false;
	this.currentTimeout = false;
	
	this.Install = function()
	{
		//Обновление бэкграундов
		this.myIco.attr("class","");
		$('.etu_preview .item',this.myObj)
			.css({"position":"absolute","top":0,"left":0,"width":"100%","height":"100%","opacity":0})

		//Убираем выделния
		var myObj = this;
		var k = 0;
		for (var i =0; i < this.myIco.length; i++)
			$(this.myIco[i]).bind('click', function()
			{
				if (myObj.work) return (false);
				myObj.myShow(parseInt($(this.parentNode).attr("rel")));
				return(false);
			});
		
		this.myShow(0);
		
		var myView = new etu_slider($('.etu_navigation',this.myObj)[0]);
	}
	
	this.myShow = function(myNum)
	{
		if (this.currentTimeout) clearTimeout(this.currentTimeout);
		this.work = true;
		if (this.myCurrent !== false)
		{
			if (this.myBg[this.myCurrent]) $(this.myBg[this.myCurrent]).animate({"opacity":0},this.timeOut)
			for (var i = 0; i < this.myIco.length; i++) 
				this.createNoSel(this.myIco[i]);
		}
		var myObj = this;
		this.myCurrent = myNum;
		if (this.myBg[myNum]) 
		{
			$(this.myBg[myNum]).animate({"opacity":1},this.timeOut, function() {myObj.work = false; if(myObj.myTimeNext) myObj.currentTimeout = setTimeout(function(){myObj.Next()},myObj.myTimeNext)});
		}
		if (this.myIco[myNum]) this.createSel(this.myIco[myNum]);
			
	}
	
	this.createNoSel = function(myObj)
	{
		$(myObj).attr("class","");
		if (! $(myObj).attr("rel") )return;
		var mySrc = $(myObj).attr("rel").split("====");
		$('img',myObj).css("backgroundImage","url("+mySrc[0]+")");
	}
	
	this.createSel = function(myObj)
	{
		$(myObj).attr("class","sel");
		if (! $(myObj).attr("rel") )return;
		var mySrc = $(myObj).attr("rel").split("====");
		//alert(mySrc[1]);
		$('img',myObj).css("backgroundImage","url("+mySrc[1]+")");
	}
	
	this.Next = function()
	{
		var myNum = this.myCurrent + 1;
		if (myNum > this.size) myNum = 0;
		this.myShow(myNum);
	}
	
	this.Install();
}


var etu_slider = function(myObj)
{
	this.myObj = myObj;
	if (! this.myObj) return;
	this.blocks = $('li>*',myObj);
	this.size = this.blocks.size();
	this.work = false;
	
	if (this.size < 1) return(false);
	
	this.item = parseInt($(this.blocks[0]).width()) + parseInt($(this.blocks[0]).css("paddingLeft")) +  parseInt($(this.blocks[0]).css("paddingRight"));
	this.arrowLeft = $('.etu_arrowLeft', this.myObj).size() ?  $('.etu_arrowLeft', this.myObj)[0] : false;
	this.arrowRight = $('.etu_arrowRight', this.myObj).size() ?  $('.etu_arrowRight', this.myObj)[0] : false;
	
	var myObj = this;
	if (this.arrowLeft)
		$(this.arrowLeft).bind('click', function()
		{
			myObj.BackStep();
			return(false);
		});
	
	if (this.arrowRight)
		$(this.arrowRight).bind('click', function()
		{			
			myObj.NextStep();
			return(false);
		});
	
	this.myDiv = $('*',this.myObj) ? $('*',this.myObj)[0] : false;
	if (! this.myDiv) return;
	this.myUl = $('ul',this.myDiv) ? $('ul',this.myDiv)[0] : false;
	if (! this.myUl) return;
	
	$(this.myUl).css("position","relative");
	
	$(this.myUl).width(this.size * this.item);
	this.current = 0;
	
	$(window).bind('resize', function()
	{
		myObj.Install();
	});
	
	this.Install = function()
	{
		this.myMax = Math.floor($(this.myDiv).width()/ this.item);
		
		$(this.myDiv)
			.css({'width':this.myMax*this.item,'position': 'relative', 'margin': 'auto'});
		
		this.myMax = this.size - 1 - this.myMax;
		this.createArrow();
	}	
	
	this.createArrow = function()
	{
		if (this.arrowLeft)
		{
			if (! this.current) $(this.arrowLeft).hide();
			else $(this.arrowLeft).show();
		}
		
		if (this.arrowRight)
		{
			if (this.current > this.myMax) $(this.arrowRight).hide();
			else $(this.arrowRight).show();
		}
	}
	
	this.moveToPosition = function(myNum)
	{
		if (this.work) return;
		this.work = true;
		this.current = myNum;
		this.createArrow();
		var myObj = this;
		var myLeft = parseInt(this.current * this.item);
		$(this.myUl).animate({"left":0-myLeft},"slow", function()
		{			
			myObj.work = false;
		});
	}
	
	this.NextStep = function()
	{
		this.moveToPosition(this.current + 1);
	}
	
	this.BackStep = function()
	{
		this.moveToPosition(this.current - 1);
	}
	
	this.Install();
	
	
	
}


var etu_radius = function(myClass,myTop,myTop2,myBottom,myBottom2)
{
	$(myClass).css(
	{
		"borderTopLeftRadius":parseInt(myTop),
		"borderTopRightRadius":parseInt(myTop2),
		"borderBottomLeftRadius":parseInt(myBottom),
		"borderBottomRightRadius":parseInt(myBottom2)
	});
}

var etu_opacity = function(myClass, myOpacity)
{
	if (! myOpacity) myOpacity = 0.8;
	$(myClass)
		.css("opacity",myOpacity)
		.mouseenter(function() {$(this).animate({"opacity":1},"slow")})
		.mouseleave(function() {$(this).animate({"opacity":myOpacity},"slow")});
}


var etu_opacityBlock = function(myClass, myOpacity)
{
	if (! myOpacity) myOpacity = 0.8;
	$(myClass).each(function()
	{
		$(this).css({
		"position":"absolute",
		"top" :0,
		"left":0,
		"width":"100%",
		"height":"100%",
		"background":"black",
		"opacity" : myOpacity
		});
	});
}


var etu_amimateLine = function(myClass)
{
	this.myObj = ($(myClass).size()) ? $(myClass)[0] : false;
	if (! this.myObj) return(false);
	
	this.IdTimeOut = false;
	this.Foward = true;
	this.timeOut = 50;
	this.current = 0;
	this.Step = 1;
	
	var myObj = this;
	
	$(window).bind('resize', function()
	{
		myObj.Install();
	});
	
	this.Install = function()
	{
		this.Size = $('.item',this.myObj).size();
		if (! this.Size) return;
		this.Item = $($('.item',this.myObj)[0]).width();
		this.myUl = $('ul',this.myObj);
		this.myUl.width(this.Size * this.Item);
		this.myUl.css("position","relative");
		
		var myCount = parseInt($(this.myObj).width() / this.Item );
		$(this.myObj).css(
		{
			"overflow" : "hidden",
			"position" : "relative",
			"width" : myCount * this.Item 
		});
		
		this.maxLeft = this.myUl.width() - $(this.myObj).width();
		
		var myObj = this;
		
		
		
		
	}
	
	this.Move = function()
	{
		if (this.Foward) this.current+=this.Step;
		else this.current-= this.Step;
		if (this.current >= this.maxLeft)
		{
			this.current-= this.Step;
			this.Foward = false;
		}
		if (this.current < 0)
		{
			this.current = 0;
			this.Foward = true;
		}
		this.myUl.css("left",0 - this.current);
		var myObj = this;
		this.IdTimeOut = setTimeout(function() 
		{
			myObj.Move();
		}, this.timeOut);
		
		
	}
	
	
	this.Install();
	this.Move();
	
	
	
	$(this.myObj)
	.mouseenter(function()
	{
		if(myObj.IdTimeOut) clearTimeout(myObj.IdTimeOut);
	})
	.mouseleave(function()
	{
		myObj.Move();
	});
}







var etu_amimateLine2 = function(myClass)
{
	this.myObj = ($(myClass).size()) ? $(myClass)[0] : false;
	if (! this.myObj) return(false);
	
	this.IdTimeOut = false;
	this.Foward = true;
	this.timeOut = 50;
	this.current = 0;
	this.Step = 1;
	
	var myObj = this;
	
	$(window).bind('resize', function()
	{
		myObj.Install();
	});
	
	this.Install = function()
	{
		this.Size = $('.item',this.myObj).size();
		if (! this.Size) return;
		this.Item = $($('.item',this.myObj)[0]).width();
		this.myUl = $('ul',this.myObj);
		this.myUl.width(this.Size * this.Item);
		this.myUl.css("position","relative");
		
		var myCount = parseInt($(this.myObj).width() / this.Item );
		$(this.myObj).css(
		{
			"overflow" : "hidden",
			"position" : "relative",
		});
		
		this.maxLeft = this.myUl.width() - $(this.myObj).width();
		
	}
	
	this.Move = function()
	{
		if (this.Foward) this.current+=this.Step;
		else this.current-= this.Step;
		if (this.current >= this.maxLeft)
		{
			this.current-= this.Step;
			this.Foward = false;
		}
		if (this.current < 0)
		{
			this.current = 0;
			this.Foward = true;
		}
		this.myUl.css("left",0 - this.current);
		var myObj = this;
		this.IdTimeOut = setTimeout(function() 
		{
			myObj.Move();
		}, this.timeOut);
		
		
	}
	
	
	this.Install();
	this.Move();
	
	
	$(this.myObj)
	.mouseenter(function()
	{
		if(myObj.IdTimeOut) clearTimeout(myObj.IdTimeOut);
	})
	.mouseleave(function()
	{
		myObj.Move();
	});
}



var etu_zoom = function(myClass, myPercent, myDopClass)
{
	this.myObj = ($(myClass).size()) ? $(myClass) : false;
	if (! this.myObj) return;
	
	this.myClass = (myDopClass) ? " "+myDopClass : "";
	this.mainClass = "etu_zoom";
	this.myPercent = myPercent;
	
	var myObj = this;
	
	this.myObj.mouseenter(function()
	{
		myObj.createZoom(this);
	});
	
	this.createZoom = function(myObj)
	{
		this.ClearZoom();
		var myNewPic = myObj.cloneNode(false);
		if (!myNewPic) return;	
		
		var myObject = this;
		
		$(myNewPic)
			.css({	'margin' : 0,
					'position' : 'absolute'	,
					'top' : $(myObj).offset().top,
					'left' : $(myObj).offset().left,
					'cursor' : 'pointer',
					'zIndex': 1000
			})
			.attr('class','')
			.attr('class',this.mainClass+this.myClass)
			.bind('click',function() {
				if ( $(myObj.parentNode).attr('href')) location.href = $(myObj.parentNode).attr('href');	
			
			})
			.appendTo(".body")
			.mouseleave(function()
			{
				myObject.ClearZoom();
			});
		
		
		
		$(myNewPic)
		.animate({
			width: parseFloat($(myObj).width()*this.myPercent),
			height: parseFloat($(myObj).height()*this.myPercent),
			left :  parseFloat($(myNewPic).offset().left - (($(myObj).width()*this.myPercent - $(myObj).width()) /2)),
			top :  parseFloat($(myNewPic).offset().top - (($(myObj).height()*this.myPercent - $(myObj).height()) /2))
		},		
		500,
		function() {
		
		});	
	}
	
	this.ClearZoom = function()
	{
		$("."+this.mainClass).remove();
	}
	
}


var etu_layer = function(myContent, myFunc)
{
	this.myContent = myContent;
	this.myFunc = myFunc;
	
	this.Install = function()
	{
		this.CreateFon();
		this.CreateContent();
	}
	
	this.CreateFon = function()
	{
		if ($("#fon").size()) return;
		$('<div id="fon">&nbsp;</div>')
		.appendTo("body")
		.css({
			'position':'absolute',
			'top' : '0px',	
			'left' : '0px',
			'width' : '100%',
			'height' :  $(document).height(),
			'opacity' : 0.8,
			'zIndex' : 10000
		});
		
		$("body").css("position","relative");
		$("body").css("overflow","hidden");
	}
	
	this.CloseMe = function()
	{
		$("#bigWindow").remove();
		$("#fon").remove();
		$("html").css({	"overflow" : ""});
		$("body").css("position","");
		$("body").css("overflow","");
	}
	
	this.CreateContent = function()
	{
		
		var myObj = this;
		
		if (! $("#bigWindow").size())
		{
			
			var top = 0;
			var myTop = $(document).scrollTop() + top;
			var myHeight = $(window).height() - top;
			
			$('<div id="bigWindow"></div>')
			.appendTo("body")
			.css(
			{
				"height" : myHeight,
				"top" : myTop,
				"position": "absolute",
				"width" : "100%",
				"left" : 0,
				"overflow" : "auto",
				"zIndex" : 20000
			});
		
			$('body').bind('click',function(event) 
			{
				if ($(event.target).attr("id") == "bigWindow") myObj.CloseMe();
			});
		}
		
		
		
		$("#bigWindow").html(this.myContent);
		if (this.myFunc) this.myFunc();
		
		$("#bigWindow form").ajaxForm(function(myContent)
		{
			myObj.myContent = myContent;
			myObj.CreateContent();
			return(false);
		});
		
		$("#bigWindow .close").unbind('click');
		$("#bigWindow .close").bind('click', function()
		{
			myObj.CloseMe();
			return(false);
		});
	}
	
	this.Install();
	
	
		
}

var etu_post = function(myLink, myPost, myObj, myLayer, myFunc)
{
	this.myLink = myLink;
	this.myPost = myPost;
	this.myLayer = (myLayer) ? true : false;
	this.myObj = myObj;
	this.myFunc = myFunc;
	
		
	this.Install = function()
	{
		var myObj = this;
		$.post(this.myLink,this.myPost,function(myContent) {myObj.putIntoBody(myContent )});
	}
	
	this.putIntoBody = function(myContent)
	{
		if (this.myLayer) var myBlock = new etu_layer(myContent, (this.myFunc) ? this.myFunc:false);
		else 
		{
			$(this.myObj).html(myContent);
			if (this.myFunc) this.myFunc();
		}
		
		
		
		
	}
	
	this.Install();
}


var etu_myGet = function(myLink, myPost, myObj, myLayer, myFunc)
{
	this.myLink = myLink;
	this.myPost = myPost;
	this.myLayer = (myLayer) ? true : false;
	this.myObj = myObj;
	this.myFunc = myFunc;
	
		
	this.Install = function()
	{
		var myObj = this;
		$.get(this.myLink,this.myPost,function(myContent) {myObj.putIntoBody(myContent)});
	}
	
	this.putIntoBody = function(myContent)
	{
		if (this.myLayer) var myBlock = new etu_layer(myContent);
		else $(this.myObj).html(myContent);
		if (this.myFunc) this.myFunc();
		
		
		
	}
	
	this.Install();
}


var etu_get = function()
{
	this.Install = function()
	{
		var myObj = this;
		
		$('a.etu_get')
			.unbind('click')
			.bind('click', function()
			{
				var myHref = $(this).attr("href");
				
				$.get(myHref,{1:1}, function(myContent) 
				{
					var myBlock = new etu_layer(myContent);
					myObj.Install();
				});
				
				return(false);
			});
	}
	
	this.Install();
}
