// JavaScript Document

$.fn.hAlign = function() {
	return this.each(function(i){
	var ah = $(this).width();
	var ph = $(this).parent().width();
	var mh = (ph - ah) / 2;
	$(this).css('margin-left', mh);
	});
};


$(document).ready(function() {
	
		$('#navigation > li > ul > li > a').each(function(){
			$(this).html('<nobr>'+$(this).html()+'</nobr>');
		});
		$('#navigation > li > ul').each(function(){
			$(this).children().last().addClass('border-none');
			if($(this).width() > 213){
				$(this).css('width', $(this).width());
			}else{
			  $(this).css('width', '213px');
			}
			$(this).css("display", "none");
		});
	$('#navigation > li > ul > li').each(function(){
		if($(this).children().size() == 2){
			$(this).addClass('withThirdNav');
		}
	});
        
    $('#navigation > li').hover(function(){
		var firstDrop = $(this).children('ul');
		firstDrop.css('position', 'absolute');
		firstDrop.css('height', '0px');
		firstDrop.show();
		var firstDropHeight = findHeight(firstDrop);
		firstDrop.stop().animate({height: firstDropHeight}, 400, function(){
			 firstDrop.css('height', 'auto');
		});		
	},function(){
			var firstDrop = $(this).children('ul');
			firstDrop.stop().animate({height: 0}, 400, function(){ firstDrop.hide();});
		}
	);
	
	$('#navigation > li > ul > li').hover(function(){
	//	var firstDrop = $(this).parent();
		var secondDrop = $(this).children('ul');
		secondDrop.css('height', '0px');
		secondDrop.show();
		var secondHeight = findHeight(secondDrop);
		secondDrop.stop().animate({height: secondHeight}, 400);		
	},function(){
			var secondDrop = $(this).children('ul');
			secondDrop.stop().animate({height: 0}, 400, function(){secondDrop.hide();});
		}
	);
	
	  if($('#slider li').size() > 1){
			$("#slider").sudoSlider({
         auto:true,
				 speed: '800',
				 pause: '4000',
				 fade: true,
         prevNext:false
			});            
		}
                
                
    $('.welcomeBlock li:first').addClass('first');            
    $('.welcomeBlock li:last').addClass('last');
	
	$('.featAds li:first').addClass('first');          
    $('.featAds li:last').addClass('last');
        
          
	
	
	       
                
});


function findHeight(elem){
	var resHeight = 0;
	elem.children('li').stop().each(function(){
		/* console.log($(this)); */
		resHeight = resHeight + $(this).height()+parseInt($(this).css('paddingTop'))+parseInt($(this).css('paddingBottom'));
	});
	return resHeight;
}

$(window).load(function() {
   $('ul.welcomeBlock li a img').hAlign();	 
	$('ul.featAds li a img').hAlign();	 
});


