 $(document).ready(function() {
 
		 
 //iPad specific CSS
 if(navigator.platform == "iPad")
 {
 	jQuery("#centerFooter").addClass("iPadSpecific");
 }
 
 
 
 
 //newsticker
if (jQuery('#newsCycler').length != 0) 
{
	jQuery('#newsCycler').cycle({
		fx: 'scrollUp',
		speed: 500,
		timeout: 5000,
		delay: 0
		//prev: '#news-ticker-nav-prev',
		//next: '#news-ticker-nav-next'
	    //pause:   -5
	});
}
 
	if($("a.video").length > 0) {
		$("a.video").fancybox({
			'width'				: 576,
			'height'			: 332,
			'margin'			: 0,
			'scrolling'         : 'no',
			'overlayOpacity'    : .5,
			'overlayColor'      : '#000000',
			'autoScale'			: true,
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'fade',
			'type'				: 'iframe',
			'centerOnScroll'    : true,
			'hideOnContentClick': true,
			'showCloseButton'   : true
		});
		
	}
	if($("a.home-video").length > 0) {
		$("a.home-video").fancybox({
			'width'				: 737,
			'height'			: 422,
			'margin'			: 0,
			'scrolling'         : 'no',
			'overlayOpacity'    : .5,
			'overlayColor'      : '#000000',
			'autoScale'			: true,
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'fade',
			'type'				: 'iframe',
			'centerOnScroll'    : true,
			'hideOnContentClick': true,
			'showCloseButton'   : true
		});
		
	}
	 

 
 /* Global Show/Hide Toggle
  ******************************************************************************************************************/
    
    $('.expand').click(function(){
    
   		var collapseDiv = $(this).next();

    	if($(this).attr("class")=="expand down") {
        	
    		collapseDiv.slideUp(
    			//Do something when done
    		);
    		$(this).removeClass('down');
        }
    	else
    	{
    		$(this).addClass("down");
    		
    		collapseDiv.slideDown(
    			
    			function()
    			{
    				//Do something when done
    			}
    		);
    	}
    	
    	
    });
 
 
 
 
/* HOMEPAGE ANIMATION START
 ******************************************************************************************************************/
  	
  		
	var $layer_1 = 	$("#layer1");
	var $layer_2 = 	$("#layer2");
	var $layer_3 = 	$("#layer3");
	
	var $layer_1_speed = 4; //The speed the first layer travels in corolation to the main one
	var $layer_2_speed = 2; //The speed the second layer travels in corolation to the main one
	var $mainAnimation_delay = 800; //Delays the scroll to 900px on the main nav
	var $bigScreenDragStartingPoint = 0;
	var $navDragStartingPoint = 0;
	var $swipeSensitivity = 200; //Amount of pixels to be dragged for the slide to go to the next/prev one
	var $dragDistance = 0; //The amount of pixel the slide was dragged for, used to figure out whether to go to another slide or not
	var $currentSlide = 1; //Starts with the first slide
	var $currentNavSlide = 1; //Starts with the first slide
	var $dragger = $('#dragger');
    var $navSlider = $('#homepage_nav_slider');
    var $easingSpeedForSwipe = 500; //The speed of easing when the user swipes
    var $easingSpeedForNavClick = 1000; //The speed of easing when the user clicks on the top nav 
    var $previouslyOpenedAnimationIcon = 0; // Holds the reference to the previously opened animation icon for quick reference
	
	var $slide_1 = [0,-900];
	var $slide_2 = [-900, -1800];
	var $slide_3 = [-1800, -2700];
	var $slide_4 = [-2700, -3600];
	var $slide_5 = [-3600, -4500];
	var $slide = [-900, -900, -1800, -2700, -3600];
	var $navSlide = [0, 0, 142, 283, 424];

 	 	
 	$(".animIcon").click(function(event){
 	
 		// Close previousely opened animation icons
 		if($previouslyOpenedAnimationIcon != 0)
 		{
 			$.fn.log("Close Previous "+$previouslyOpenedAnimationIcon);
 			
 	 		var closing_contentIDName = "#slide_"+$previouslyOpenedAnimationIcon.split("_")[1]+"_content_"+$previouslyOpenedAnimationIcon.split("_")[3];
	 		var $closing_contentHolder = $(closing_contentIDName);
	 		
	    	var $closing_animIcon_ContentHolder = $closing_contentHolder.find(".miniContentHolder");
	    	var $closing_animIcon_Content = $closing_contentHolder.find(".miniContentHolder div");
	    	var $closing_animIcon_ContentHeight = $closing_contentHolder.find(".miniContentHolder div").height();
	    	var $closing_animIcon_ContentCloseButton = $closing_contentHolder.find(".miniContentHolder .closeMiniContent");
	    	var $closing_animIcon_Title = $closing_contentHolder.find(".miniContentHolder h1").width();
	    	
	    	$.fn.hideIconContent($closing_animIcon_ContentHolder, $closing_animIcon_Content, $closing_animIcon_Title, $closing_animIcon_ContentCloseButton);
 		}
 	
 		var idName = this.id;
 		var contentIDName = "#slide_"+idName.split("_")[1]+"_content_"+idName.split("_")[3];
 		var $contentHolder = $(contentIDName);
 		
    	var $animIcon_ContentHolder = $contentHolder.find(".miniContentHolder");
    	var $animIcon_Content = $contentHolder.find(".miniContentHolder div");
    	var $animIcon_ContentHeight = $contentHolder.find(".miniContentHolder div").height();
    	var $animIcon_ContentCloseButton = $contentHolder.find(".miniContentHolder .closeMiniContent");
    	var $animIcon_Title = $contentHolder.find(".miniContentHolder h1").width();

 		if($animIcon_Content.css("display") == "none")
        {       
        	$.fn.log("Toggle Icon: ON");
           	
        	$.fn.showIconContent($animIcon_ContentHolder, $animIcon_Content, $animIcon_ContentHeight, $animIcon_ContentCloseButton);        	
        	$previouslyOpenedAnimationIcon = this.id;
        } 
        else
        {       
         	$.fn.log("Toggle Icon: OFF");       
                    	
        	$.fn.hideIconContent($animIcon_ContentHolder, $animIcon_Content, $animIcon_Title, $animIcon_ContentCloseButton);
 
        }

 	});
  	
  	
  	$('.closeMiniContent').click(function(event){
	  
	  $.fn.log("Close Icon Content");
	 
	  var idName = $(this).parents(".animIconContent").prev().attr('id');    
	  var contentIDName = "#slide_"+idName.split("_")[1]+"_content_"+idName.split("_")[3];
 	  var $contentHolder = $(contentIDName);
	  
      var $animIcon_ContentHolder = $contentHolder.find(".miniContentHolder");
	  var $animIcon_Content = $contentHolder.find(".miniContentHolder div");
	  var $animIcon_Title = $contentHolder.find(".miniContentHolder h1").width();
	  var $animIcon_ContentCloseButton = $contentHolder.find(".miniContentHolder .closeMiniContent");
	  var $animIcon_ContentHeight = $contentHolder.find(".miniContentHolder div").height();

	  
	  $.fn.hideIconContent($animIcon_ContentHolder, $animIcon_Content, $animIcon_Title, $animIcon_ContentCloseButton);
	  
	});
	
	// Opens info box when clicking on the title of the animation icon
	$('.miniContentHolder > h1').click(function(event){
	  
	  	// Close previousely opened animation icons
		if($previouslyOpenedAnimationIcon != 0)
		{
				
		 	var closing_contentIDName = "#slide_"+$previouslyOpenedAnimationIcon.split("_")[1]+"_content_"+$previouslyOpenedAnimationIcon.split("_")[3];
	 		var $closing_contentHolder = $(closing_contentIDName);
	 		
	    	var $closing_animIcon_ContentHolder = $closing_contentHolder.find(".miniContentHolder");
	    	var $closing_animIcon_Content = $closing_contentHolder.find(".miniContentHolder div");
	    	var $closing_animIcon_ContentHeight = $closing_contentHolder.find(".miniContentHolder div").height();
	    	var $closing_animIcon_ContentCloseButton = $closing_contentHolder.find(".miniContentHolder .closeMiniContent");
	    	var $closing_animIcon_Title = $closing_contentHolder.find(".miniContentHolder h1").width();
	    	
	    	$.fn.hideIconContent($closing_animIcon_ContentHolder, $closing_animIcon_Content, $closing_animIcon_Title, $closing_animIcon_ContentCloseButton);
		}
	
	 
	  var idName = $(this).parents(".animIconContent").prev().attr('id');  
	  var contentIDName = "#slide_"+idName.split("_")[1]+"_content_"+idName.split("_")[3];
 	  var $contentHolder = $(contentIDName);
	    
      var $animIcon_ContentHolder = $contentHolder.find(".miniContentHolder");
	  var $animIcon_Content = $contentHolder.find(".miniContentHolder div");
	  var $animIcon_ContentCloseButton = $contentHolder.find(".miniContentHolder .closeMiniContent");
	  var $animIcon_ContentHeight = $contentHolder.find(".miniContentHolder div").height();
	  
	  if($animIcon_Content.css("display") == "none")
	  {       
    	$.fn.showIconContent($animIcon_ContentHolder, $animIcon_Content, $animIcon_ContentHeight, $animIcon_ContentCloseButton);  
	  	$previouslyOpenedAnimationIcon = idName;
	  }   
	  
	});

	
 
 
 	$.fn.showIconContent = function(iconContentHolder, iconContent, iconContentHeight, iconCloseButton)
 	{
 		iconContentHolder.animate
    	(
    		{
    			height:iconContentHeight+15,
    			width:'180px'
    		}, 100, 'linear', function() 
    		{
				iconContent.fadeIn("fast");
				iconCloseButton.fadeIn("fast");
			}        	
    	).css({"padding-bottom":"22px"}).parents(".animIconContent").css({"z-index":"8000"});//so that the opening div to show over others 	
    	
   	}
 	
 	
 	$.fn.hideIconContent = function(iconContentHolder, iconContent, iconTitleWidth, animIcon_ContentCloseButton)
 	{
 		iconContent.fadeOut("fast", function()
 		{
 			animIcon_ContentCloseButton.fadeOut("fast");
 			iconContentHolder.animate
    		(
    			{
	    			height:"20px",
	    			width:iconTitleWidth+1 //the +1 is added for firefox, for some reason it would make some words collapse
	    		}, 100, 'linear'
	    	).css({"padding-bottom":"12px"}).parents(".animIconContent").css({"z-index":"7000"});//for the closing div to assume the old zindex value 
 		});
 		
  	}
 
 
 	/* START BIG SCREEN DRAG FUNCTIONALITY */
		
	$dragger.delay($mainAnimation_delay).animate
	(
		{
			left: -900,
			easing: 'easeOutQuad'
	  	}, 
		{
			duration: 2000,
			step: function( now, fx ) 
	  		{					
		    	$.fn.moveLayers(now);
				//$.fn.log("Dragger during animation: "+now);
		 	}
		}
	);
		
	//moves layers for the parallax effect	
	$.fn.moveLayers = function(xPosition)
	{

		$layer_1.css("left",xPosition/$layer_1_speed);
		$layer_2.css("left",xPosition/$layer_2_speed);
		$layer_3.css("left",xPosition);
		
		return;
	}

	$.fn.startedBigSreenDragging = function(event, ui) 
	{
		$bigScreenDragStartingPoint = ui.position.left;
		
	}

	$.fn.BigScreenDrag = function(event, ui) 
	{
		
		var rightBreakOffPoint = -3601;
		var leftBreakOffPoint = 0;
		
		$.fn.log("Big Screen | Position: "+ui.position.left);
		
		
		if(ui.position.left > rightBreakOffPoint && ui.position.left < leftBreakOffPoint)
		{
			$.fn.moveLayers(ui.position.left);
		}
		else if(ui.position.left < rightBreakOffPoint)
		{
			//$.fn.log("Too LEFT!!!");
			ui.position.left = rightBreakOffPoint;
		}
		else if(ui.position.left > leftBreakOffPoint)
		{
			//$.fn.log("Too RIGHT!!!");
			ui.position.left = leftBreakOffPoint;
		}
	}
	
	$.fn.whereToSnapBigScreen = function(xPosition)
	{
		$dragDistance = (xPosition - $bigScreenDragStartingPoint);
		
		$.fn.log("Big Screen | Started at:"+xPosition+" Finished at: "+$bigScreenDragStartingPoint+" Difference: "+$dragDistance);
		$.fn.log("Big Screen | Difference: "+Math.abs($dragDistance));
		$.fn.log("Big Screen | Current Slide: "+$currentSlide);
		
		if($currentSlide >= 1 && $currentSlide <= 4 && Math.abs($dragDistance) > $swipeSensitivity)
		{
			if($dragDistance > 0 )
			{
				//Dragged to the right
				
				if($currentSlide > 1)
					$currentSlide--;
					
				positionToSnapTo = $slide[$currentSlide];
			}
			else
			{
				//Dragged to the left
				
				if($currentSlide < 4)
					$currentSlide++;

				positionToSnapTo = $slide[$currentSlide];
			}
		}
		else 
		{
			positionToSnapTo = $slide[$currentSlide];
		}
				
		return positionToSnapTo;
	}
	
	
	$.fn.magic = function(event, ui)
	{
		$.fn.log("Big Screen | Amount Dragged"+ui.position.left);
		
		var positionToSnap = $.fn.whereToSnapBigScreen(ui.position.left);
		
		$dragger.animate(
			{
				left: positionToSnap,
				easing: 'easeOutQuad'
		  	}, 
			{
				duration: $easingSpeedForSwipe,
				step: function( now, fx ) 
		  		{					
					$.fn.moveLayers(now);
			 	},
			 	complete:function() 
		  		{					
					//Moves the top nav when the big screen animation is complete
					$('#homepage_nav_slider').animate(
						{
							left: $navSlide[$currentSlide],
							easing: 'easeOutQuad'
					  	}
					)
			 	}
			}
		);
		
	}

	$dragger.draggable({
			axis:'x',
			delay: 0,
			start: $.fn.startedBigSreenDragging,
			drag: $.fn.BigScreenDrag,
			stop: $.fn.magic
		}).addTouch();
        
        
		
/* END BIG SCREEN DRAG FUNCTIONALITY */
		
	
/* START NAV DRAG FUNCTIONALITY */
	
	//This function moves the main nav when a user clicks anywhere on the nav
	$("#homepage_nav").click(function(e){
 		
  		var offset = $(this).offset();
	    $.fn.log(e.clientX - offset.left);
	    $.fn.log(e.clientY - offset.top);
	    
	    //gets the clicked position on the nav
	    var clickedPosition = e.clientX - offset.left;
	    
 		
 		if(clickedPosition < ($navSlide[2]))
		{
			$.fn.log("Nav 1 Clicked");
			positionToSnap = $navSlide[1];
			$currentNavSlide = 1;
			
		}
		else if(clickedPosition > ($navSlide[2]) && clickedPosition < ($navSlide[3]))
		{
			$.fn.log("Nav 2 Clicked");
			positionToSnap = $navSlide[2];
			$currentNavSlide = 2;

		}
		else if(clickedPosition > ($navSlide[3]) && clickedPosition < ($navSlide[4]))
		{
			$.fn.log("Nav 3 Clicked");

			positionToSnap = $navSlide[3];
			$currentNavSlide = 3;
		}
		else if(clickedPosition > ($navSlide[4]))
		{
			$.fn.log("Nav 4 Clicked");
			
			positionToSnap = $navSlide[4];
			$currentNavSlide = 4;
		}

		//moves the homepage navigation slider to the right position where the user clicked
		$('#homepage_nav_slider').animate(
			{
				left: positionToSnap,
				easing: 'easeOutQuad'
		  	}, 
				function() 
		  		{					
		  			//once the navigation slider is moved to its position, the big screen layers move
					$.fn.moveBigScreenPerNav($slide[$currentNavSlide]);
			 	} 	
			
		);
 		
 		
 	});
	
	// Gets the starting x&y once the user starts dragging the nav slider 
	$.fn.startedNavDragging = function(event, ui) 
	{
		$navDragStartingPoint = ui.position.left;
	}

	// Makes sure the nav slider doesnt go out of the navigation borders
	$.fn.mainNavDrag = function(event, ui) 
	{
		
		var rightBreakOffPoint = 0;
		var leftBreakOffPoint = 424;
		
		$.fn.log("Main Nav | Position: "+ui.position.left);
		
		
		if(ui.position.left > rightBreakOffPoint && ui.position.left < leftBreakOffPoint)
		{
			//$.fn.moveLayers(ui.position.left);
		}
		else if(ui.position.left < rightBreakOffPoint)
		{
			//$.fn.log("Too LEFT!!!");
			ui.position.left = rightBreakOffPoint;
		}
		else if(ui.position.left > leftBreakOffPoint)
		{
			//$.fn.log("Too RIGHT!!!");
			ui.position.left = leftBreakOffPoint;
		}
	}
	
	// Figures out when to snap the nav slider once the user released it
	$.fn.whereToSnapNav = function(xPosition)
	{
		$navDragDistance = (xPosition - $navDragStartingPoint);
		
		$.fn.log("Main Nav | Started at:"+xPosition+" Finished at: "+$bigScreenDragStartingPoint+" Difference: "+$navDragDistance);
		$.fn.log("Main Nav | Difference: "+Math.abs($dragDistance));
		$.fn.log("Main Nav | Current Slide: "+$currentNavSlide);
		
		if(xPosition < ($navSlide[2]-70))
		{
	 		$.fn.log("Falls under slide 1");
	 		positionToSnapTo = $navSlide[1];
	 		$currentNavSlide = 1;
		}
		else if(xPosition > ($navSlide[2]-70) && xPosition < ($navSlide[3]-70))
		{
			$.fn.log("Falls under slide 2");
	 		positionToSnapTo = $navSlide[2];
	 		$currentNavSlide = 2;
		}
		else if(xPosition > ($navSlide[3]-70) && xPosition < ($navSlide[4]-70))
		{
			$.fn.log("Falls under slide 3");
	 		positionToSnapTo = $navSlide[3];
	 		$currentNavSlide = 3;
		}
		else if(xPosition > ($navSlide[4]-70))
		{
			$.fn.log("Falls under slide 4");
	 		positionToSnapTo = $navSlide[4];
	 		$currentNavSlide = 4;
		}
						
		return positionToSnapTo;
	}
	
	//Moves the big screen layers in accordance with that top nav
	$.fn.moveBigScreenPerNav = function(xPosition)
	{
		
		$dragger.animate
		(
			{
				left: xPosition,
				easing: 'easeOutQuad'
		  	}, 
			{
				duration: $easingSpeedForNavClick,
				step: function( now, fx ) 
		  		{					
			    	$.fn.moveLayers(now);
			    	$currentSlide = $currentNavSlide;//After the layers are moved set the current slide to the correct number
			 	}
			}
		);	
	}
	
	
	// Gets executed once the nav slider is released, slides the nav slider and moves the big screen layers
	$.fn.mainNavDragStop = function(event, ui)
	{
		$.fn.log("Main Nav | Amount Dragged"+ui.position.left);
		
		var positionToSnap = $.fn.whereToSnapNav(ui.position.left);
		
		$('#homepage_nav_slider').animate(
			{
				left: positionToSnap,
				easing: 'easeOutQuad'
		  	}, 
				function() 
		  		{					
					$.fn.moveBigScreenPerNav($slide[$currentNavSlide]);
			 	}
		);
		
	}


	//Dragging of the main nav slider
	$navSlider.draggable({
			axis:'x',
			delay: 0,
			start: $.fn.startedNavDragging,
			drag: $.fn.mainNavDrag,
			stop: $.fn.mainNavDragStop
		}).addTouch();		

	/* END NAV DRAG FUNCTIONALITY */

 /* HOMEPAGE ANIMATION END
  ******************************************************************************************************************/	
	
	
	$.fn.log = function(text) {
    if (window.console && console.log) {
        console.log(text);
    }
    return;
}

  });
	
