var timer;
var finalPos;
var onMove = false;
$(window).load(function(){

/////////////////////////////
// EVENT FOR THE MOUSE SCROLL  
/////////////////////////////
	finalPos = $(document).height() * -1;
	
	if(window.addEventListener) $(document).stop(true,true).bind('DOMMouseScroll',moveObject);
  	// for IE/OPERA etc
    document.onmousewheel = moveObject;
	timer = window.setInterval(animObject,10000);
	
	source_width = $('.background img').width();
	source_height = $('.background img').height();
	
	target_width = $(window).width();
	target_height = $(window).height();
	
	if(target_height < 700 && target_width < 1000 )
	{
		target_width = 1024;
		target_height = 768;
	}
	
	width_pos = 0;
	height_pos = 0;
	
	if (target_width / target_height > source_width / source_height) ratio = target_width / source_width;
	else ratio = target_height / source_height;
	
	height_pos = (Math.round(target_height)/2) - (Math.round(source_height*ratio)/2) ;
	width_pos = (target_width - source_width*ratio)/2;
	
	$('.background img').attr({'width': source_width*ratio,'height': source_height*ratio});
	$('.background img').css({'position':'absolute','left':width_pos+ 'px','top':height_pos +'px'});

///////////////////////		
// ARRANGE THE DISPLAY  
//////////////////////
	/* HEADER */
	var mainNav_width = $('.main-nav').width();
	$('.main-nav').css('margin-left',mainNav_width*-1);
	$('.logo').css('margin-right',mainNav_width);

	/* CONTENT */
	var screen_h = $(document).height();
	$('.content-wrapper').css('height',screen_h-200);
	
	$.each($('.slide-details'),function(){
		var detail_h = $(this).height()+70;
		$(this).css({'margin-top': (detail_h/2)*-1, 'height':detail_h});
	});

	/* PAGINATION */
	var pagination_h = $('.pagination').height();
	$('.pagination').css({'margin-top': (pagination_h/2)*-1, 'height':pagination_h});
	
///////////////////////		
// NAVIGATION  
//////////////////////
	$('.main-nav ul li.has_child').mouseenter(function()
	{
		$(this)
			.removeClass('bck')
			.find('ul').show();
			
		var  mxh = $(this).find('ul').css('max-height');

		$(this).find('ul')
					.stop(true,false)
					.css('opacity',1)
					.animate({'height':mxh},500,'easeInOutCubic', function(){ });
	});
	
	$('.main-nav ul li.has_child').mouseleave(function()
	{
		$(this).find('ul').stop(true,false).animate({'height':31},500,'easeOutCubic', function(){ $(this).fadeOut(200); $(this).parent().addClass('bck')});
	});
	
	$.each($('.sub_nav'),function()
	{
		var h = $(this).height();
		var w = $(this).width();
		$(this).css({'max-height': h,'height':36, 'width': w+10});	
	});
	$('.loading').hide();
	$('.maincontent').css({'visibility':'visible','display':'none'}).fadeIn(800);
});

$(window).resize(chgDisplay);

function chgDisplay()
{
	finalPos = $(document).height() * -1;
	
	source_width = $('.background img').width();
	source_height = $('.background img').height();
	
	target_width = $(window).width();
	target_height = $(window).height();

	width_pos = 0;
	height_pos = 0;
	
	if (target_width / target_height > source_width / source_height) ratio = target_width / source_width;
	else ratio = target_height / source_height;
	
	height_pos = (Math.round(target_height)/2) - (Math.round(source_height*ratio)/2) ;
	width_pos = (target_width - source_width*ratio)/2;
	
	$('.background img').attr({'width': source_width*ratio,'height': source_height*ratio});
	$('.background img').css({'position':'absolute','left': width_pos+ 'px','top': height_pos +'px'});
				
	$('.object').each(function(){
		if($(this).position().top < 0 )
		{
			$(this).css('top',(target_height*-1));
		}
	});
	
	/* CONTENT */
	var screen_h = $(document).height();
	$('.content-wrapper').css('height',screen_h-200);
}

function moveObject(event)
{          
	if(onMove == false)
	{     
		clearInterval(timer);
		onMove = true;
	    $(document).unbind('DOMMouseScroll');

		clearInterval(timer);
		if(!event) //For IE
		{
			event = window.event;
		}
		if (event.wheelDelta) 	//IE,Opera,Chrome,Safari
	    {
	        delta = event.wheelDelta/event.wheelDelta;
	        if(event.wheelDelta < 0)
	        {
	        	delta = delta * -1;
	        }
	    }
	    else if (event.detail) //Firefox
	    {

			delta = event.detail/event.detail;
			if(event.detail > 0)
	        {
	        	delta = delta * -1;
	        }
		}

		var currPos = $('.active').offset();
		var finalPos = $(document).height() * -1;

		if(delta == -1)
		{
			goToNext();
		}
		else
		{
			goToPrevious();
		}
	}
}

function animObject()
{

	if($('.pagination li.actif').hasClass('last'))
	{

		handlerChangeSlide($('.pagination ul li.actif').index(),0);
		clearInterval(timer);
		return;
	}
	else
	{
		goToNext();
		
		//$('.pagination li.actif').removeClass('actif').next().addClass('actif');
		/*
		var curPos = $('.active').offset();    
		var finalPos = screen.height * -1;
		$('.active').animate({'top':finalPos},500,function(){
			$(this).removeClass('active').next().addClass('active');
		});
		*/
	}
}

function goToNext()
{
	if($('.active').hasClass('last'))
	{
		$(document).delay(800).bind('DOMMouseScroll',moveObject);
		onMove = false;
	}
	else
	{
		$('.active').stop(false,false).animate(
			{
				'top': finalPos
			},
			{
				duration: 1000,
				easing:'easeInOutCubic',
				queue: true,
				step: function(now,fx)
				{
					var curr_pos = $(this).position();
					var curr_top = curr_pos.top + (target_height);

					var h = 100-(Math.round((now/finalPos)*100));

					$('.active .container').css('height',h +'%');
					
				},
				complete: function()
				{  
					$(this).removeClass('active').next().addClass('active');    
					$('.pagination li.actif').removeClass('actif').next().addClass('actif');
					$(document).delay(500).bind('DOMMouseScroll',moveObject);
					onMove = false;
				}
			}
		);		
	}
}

function goToPrevious()
{
	if($('.active').hasClass('first'))
	{       
		$(document).delay(800).bind('DOMMouseScroll',moveObject);
		onMove = false;
	}
	else
	{   
		$('.active').removeClass('active').prev().animate(
			{
				'top': 0
			},
			{
				duration : 1000,
				easing:'easeInOutCubic',
				queue: true,
				step : function(now,fx)
				{	
					var curr_pos = $(this).position();
					var curr_top = curr_pos.top;
									
					var h = 100 - Math.round((now/finalPos)*100);
					var next_pos = (target_height + curr_top)/5;
					
					//$(this).next().css('top',next_pos);	
							
					$(this).find('.container').css('height',h +'%');
				},
				complete :	function(){ 
					$(this).addClass('active'); 
					$('.pagination li.actif').removeClass('actif').prev().addClass('actif'); 
					$(document).delay(500).bind('DOMMouseScroll',moveObject); 
					/*timer = window.setInterval(animObject,5000);*/
					onMove = false;
				}			
			}
		);
	}
}


// Change slide with the navigation bar
function handlerChangeSlide(current,target)
{
	clearInterval(timer);
	if(current < target)
	{
		if($('.actif').hasClass('last'))
		{
			$(document).delay(800).bind('DOMMouseScroll',moveObject);
		}
		else
		{
			$('.active').stop(false,false).animate(
				{
					'top': finalPos
				},
				{
					duration: 800,
					easing:'easeInOutCubic',
					queue: true,
					step: function(now,fx)
					{
						var curr_pos = $(this).position();
						var curr_top = curr_pos.top + (target_height);
						var h = 100-(Math.round((now/finalPos)*100));
						$('.active .container').css('height',h +'%');
					},
					complete: function()
					{
						current++;  
						$(this).removeClass('active').next().addClass('active');
						$('.pagination li.actif').removeClass('actif').next().addClass('actif');
						$(document).delay(500).bind('DOMMouseScroll',moveObject); 
						if(current<target) handlerChangeSlide(current,target); 
					}
				}
			);		
		}
	}
	else
	{
		if($('.active').hasClass('first'))
		{
			$(document).delay(800).bind('DOMMouseScroll',moveObject);
		}
		else
		{
			$('.active').removeClass('active').prev().animate(
				{
					'top': 0
				},
				{
					duration : 800,
					easing:'easeInOutCubic',
					queue: true,
					step : function(now,fx)
					{	
						var curr_pos = $(this).position();
						var curr_top = curr_pos.top;
						var h = 100 - Math.round((now/finalPos)*100);
						var next_pos = (target_height + curr_top)/5;					
						$(this).find('.container').css('height',h +'%'); 
					},
					complete :	function(){
						current--; 
						$(this).addClass('active');
						$('.pagination li.actif').removeClass('actif').prev().addClass('actif');
						$(document).delay(500).bind('DOMMouseScroll',moveObject); 
						if(current>target) handlerChangeSlide(current,target); }			
				}
			);
		}
	}
}
