var curBann = 0;
var newBann = 0;
$(document).ready(function() {
	/* ///////// MENU ///////////// */
	/* default position */
	if(!$('.h-menu a').hasClass('active'))  $('.h-menu a').eq(0).addClass('active');
	
	lft = $('.h-menu a.active').position().left;
	wid = $('.h-menu a.active').innerWidth();
	r=wid/15;
	$('.mnActive').stop(true,false).animate({width: wid + r,left: lft + r},500).animate({width: wid, left: lft});
	/* hover position */
	$('.h-menu a').hover(function(){
		$('.h-menu a.active').addClass('noact');
		lft = $(this).position().left;
		wid = $(this).innerWidth();
		lftA = $('.mnActive').position().left;
		lft < lftA ? r=-(wid/15) : r=wid/15;
		$('.mnActive').stop(true,false).animate({width: wid + r,left: lft + r},500).animate({width: wid, left: lft});
	});	
	/* back to defoult position */
	$('.h-menu').mouseleave(function(){
		lft = $('.h-menu a.active').position().left;
		wid = $('.h-menu a.active').innerWidth();
		$('.mnActive').stop(true,false).animate({width: wid,left: lft},500);
		clearTimeout(timeoutId);
		function act2(){$('.h-menu a').removeClass('noact');}
		timeoutId2 = setTimeout(act2, 400);		
	});
	/* ///////// top gallery ///////////// */
	var previewArray ='';
	$('.h-thumbs a').each(function(index){
		lastBann = index;
		prevSrc=$(this).attr('href');
		$(this).attr({
			href:'javascript:;',
			name: prevSrc,
			alt: index});
		curThumb = $(this).find('img');
		curThumb.css('backgroundImage','url('+curThumb.attr('src')+')');
		if($.browser.version=='6.0') {curThumb.attr('src','http://www.okna-modern.com.ua/images/spacer.gif');}
		else curThumb.attr('src','http://www.okna-modern.com.ua/images/header-im/header-ico-bg.png');
		previewArray = previewArray + '<li><img src="'+prevSrc+'" alt="" /></li>';
	});
	$('.h-prev-in').html('<ul>'+previewArray+'</ul>');
	$('.h-prev-in li').eq(curBann).fadeIn(100);
		
	ChangeInterval = setInterval(function autoChange(){
		curBann < lastBann ? newBann=(curBann+1) : newBann = 0;
		changeBg();
	}, 5000);

	$('.h-thumbs a').click(function(){
		newBann = Number($(this).attr('alt'));
		if(curBann != newBann){	changeBg();
			clearInterval(ChangeInterval);
			ChangeInterval = setInterval(function autoChange(){
				curBann < lastBann ? newBann=(curBann+1) : newBann = 0;
				changeBg();
			}, 10000);
		};

	});	

	/* bottom menu */
	var $menu = $('#ldd_menu');
	$menu.children('li').each(function(){
		var $this = $(this);
		var $span = $this.children('span');
		$span.data('width',$span.width());
		
		$this.bind('mouseenter',function(){
			$menu.find('.ldd_submenu').stop(true,true).hide();
			$span.addClass('activeSubs').stop().animate({'width':'419px'},300,function(){
			$this.find('.ldd_submenu').slideDown(300);
			});
		}).bind('mouseleave',function(){
			$this.find('.ldd_submenu').stop(true,true).hide();
			$span.removeClass('activeSubs').stop().animate({'width':$span.data('width')+'px'},300);
		});
	});
	
	/* recall popup */
	$('.lnk_recall').attr('href','javascript:;');
	$('.lnk_recall').click(function(){
		$('.recall-pop').fadeIn();
	});
	$('.pop_close').click(function(){
		$('.recall-pop').fadeOut();
	});
	
});
function changeBg(){
	$('.h-prev-in li').eq(curBann).css('zIndex',1).fadeOut(800);
	$('.h-prev-in li').eq(newBann).css('zIndex',10).fadeIn(800);	
	curBann = newBann;
}

