$(function(){
	try {slider($('#promotion'),$('#promotion > div.controls'),$('#extra-control > ul') );}
	catch (e) {}

if ( ($.browser.msie) && ($.browser.version < 9) ) 
{
	targetFix($('#content'),$('#content a'));
	
	if ($.browser.version < 7)
	{
		hoverOverFix($('#header-top ul.drop-list, #navigation li'));
	}
}
$("#header-top ul.drop-list").mouseleave(function() {
	$("li ul", this).hide();
});
$("#header-top ul.drop-list b").click(function() {
	$("ul", $(this).parent()).show();
});


/* Всплывающие окна */
	$("a.popup").fancybox({
		scrolling: "no",
		titleShow: false,
		padding: 0
	});
	
/* Классы для объектов в контенте */
	var arrPadder = ["table", "h3", "h4", "h2"];
	var arrBefore = ["p", "ul", "ol", "div", "br"];
	
	$.each(arrPadder, function() {
		var pThis = this;
		
		//$("#content " + pThis + ":first").addClass("first")
		
		$.each(arrBefore, function() {
			var bThis = this;
			
			$("#content " + bThis + " + " + pThis).addClass("padder-top");
		});
	});
});


function slider(target,controls,extraConrol) {
	if (target === undefined) return false
		else if (controls === undefined) return false

	var isAnimate = ($.browser.msie) ? (($.browser.version < 8) ? false : true) : true;
	var $ul = target.children('div.mask').children('ul');

	$ul.css('width',($ul.children('li').size() * 940) + 'px');

	var targetLeft = target.offset().left + parseFloat(target.css('padding-left'));
	var $liLast = $ul.children('li:last-child');
	var $liFirst = $ul.children('li:first-child');
	var stopPos = targetLeft - $liLast.offset().left;
//	var step = $liLast.outerWidth();

	//find li.active and move ul to active position
	var startPos = targetLeft - $ul.children('li.active').offset().left;
	$ul.css('left',startPos);

//	$('#navigation').append('<div id="test">');

	controls.find('a').click(function(){
		if ( (this.parentNode.className.length <= 0) ) return true; // bye-bye
		
		var $liActive = $ul.children('li.active');
		

		if (this.parentNode.className === 'previous')
		{
			var $active = $liActive.prev('li');
			if ($active.index() < 0)
				var $active = $liLast;

		} else if (this.parentNode.className === 'next') {
			var $active = $liActive.next('li');
			if ($active.index() < 0)
				var $active = $liFirst;
		} //else  var $active = $ul.children('li:nth-child(' + ($(this.parentNode).index()+ 1) + ')');

//		$('#test').text($active.index());

		runAction($ul,$active,isAnimate);
		indicate(extraConrol,$active.index());
		return false;
	});

	extraConrol.find('a').click(function(){
		var $active = $ul.children('li:nth-child(' + ($(this.parentNode).index()+ 1) + ')');
		runAction($ul,$active,isAnimate);
		indicate(extraConrol,$active.index());
		return false;
	});


	function runAction($ul, $active, isAnimate){

		var step = $active.outerWidth();
		var activeId = $active.index();
		var Xpos = - (activeId * step);
		
		if (isAnimate)   {	$ul.animate({'left':Xpos},300); }
					else {	$ul.css({'left':Xpos});         }

			$ul.children('li.active').removeClass('active');
			$active.addClass('active');

	} // runAction

	function indicate(EControl,ActIndx) {
		EControl.children('li.active').removeClass('active');
		EControl.children('li:nth-child(' + (ActIndx + 1) + ')').addClass('active');
	}
}

function targetFix(target,link) {
	link.click(function(){
		var achor = $(this).attr('href');
		var targetCont = target.find(achor);

		if (targetCont.length)
		{
			target.find('.target').removeClass('target');
			targetCont.addClass('target');
		}
	
	});
}

function hoverOverFix(box) {
	box.hover(
	  function () {
		$(this).addClass("hover");
	  },
	  function () {
		$(this).removeClass("hover");
	 });
}
