$(document).ready(function(){
	$images = ["images-01.png", "images-02.png", "images-03.png", "images-04.png"];
	//$images = ["images-01.png"];
	gotime = $images.length;	

	$('#top-menu li.active .sub-menu').show();
	$('#imagesetter').load("new_home/images/"+$images[0], function() {
		$('#header-slides').css("background-image","url(new_home/images/"+$images[0]+")"); 
	});

	$textcenter();
	$.each($images,function(e) {
		$(new Image()).load(function() {
			if (--gotime < 1) $begin($images);;
		}).attr('src',"new_home/images/"+this);
	});
});

$begin = function ($images) { $slideshow($images); };

$textcenter = function() {
	$val  = $('.top').width();
	var arr = [ "#top-1", "#top-2", "#top-3", "#top-4" ];
	jQuery.each(arr, function() {
      $(this+' .text').css("padding-right",($val-$(this+' .text').width())/2);
	  $(this+' .text').css("padding-left",($val-$(this+' .text').width())/2-5);
   	});
 };
 
$slideshow = function ($images) {
	var arr = [ "top-1", "top-2", "top-3", "top-4" ];
	$('#top-menu li.top').hover(function() {	
		$clicked = $(this).attr("id"); $count =0;
		//alert(.toString());
		if(!($('#'+$clicked).hasClass('active')))
		{
			
			jQuery.each(arr, function() {
				if($clicked==this) { 
					$file = "new_home/images/"+$images[$count];
					$('#imagesetter').load($file , function() {
						$('#top-menu li.top .sub-menu').hide();
						$('#top-menu li.top').removeClass('active');
						$('#header-slides').css("background-image","url("+$file+")"); 
						$('#'+$clicked+' .sub-menu').fadeIn(300);
						$('#'+$clicked).addClass('active');
					});
				}
				$count++;
			});
		}
	});
};
