/**
 * Rename this file and add extra JS added for the template.
 */
 
jQuery(document).ready(function(){
	
	// Hides the full screen detail link
	jQuery('#details').hide();
	jQuery('.galleria-thumbnails-container.thin').hide();
	
	// Test for window width on load and determine whether thubnails should display or not.
	var browserWidth = jQuery(window).width();
	
	if (browserWidth < 520) {
		jQuery('body').addClass('thin');
		jQuery('body').removeClass('wide');
	}
	else if (browserWidth > 520) {
		jQuery('body').addClass('wide');
		jQuery('body').removeClass('thin');
	}
	
	
	// Sets the default class on page load
	jQuery('body').addClass('nofull');

	// Fades or moves everything that gets changed when full screen initiated	
	jQuery('#overlayclose').click(function(){
		jQuery('#galleryoverlay,#mainBodyWrapper,#overlayclose,.navRow').fadeOut('fast');
		jQuery('body.wide .galleria-thumbnails-container').fadeIn();
		jQuery('body').addClass('fullscreen').removeClass('nofull');;
		jQuery('#details,#logo').fadeIn('fast');

		
		if ( jQuery('body').hasClass('thin') ) {
			jQuery('.galleria-thumbnails-container').fadeIn('fast');
		}
	});
	
	// Resets everything back to where it was when you show the content again
	jQuery('#details').click(function(){
		jQuery('#galleryoverlay,#mainBodyWrapper,#overlayclose,.navRow').fadeIn('fast');
		jQuery('#details').fadeOut('fast');
		
		if ( jQuery('body').hasClass('thin')) {
			jQuery('.galleria-thumbnails-container').fadeOut('fast');
		}
		
		if ( jQuery('#hiddenPanel').hasClass('open')) {
			jQuery('.galleria-thumbnails-container').hide();
		}
			
		jQuery('body').removeClass('fullscreen').addClass('nofull');;

	});
	
	
	// Code to create and control the play button
	jQuery('#galleryplay').click(function(){
		if ( jQuery('#galleryplay').hasClass('play') ) {
  			jQuery('#galleryplay').removeClass('play');
  			jQuery('#galleryplay').addClass('stop');
		} else {
	    	jQuery('#galleryplay').removeClass('stop');
  			jQuery('#galleryplay').addClass('play');
		}
	}); 
	
	// Hides the thumbnails when the menu is open
	jQuery('a#openPanel').click(function(){
		if ( jQuery('a#openPanel').hasClass('active') ) {
				jQuery('body.wide .galleria-thumbnails-container').fadeIn();
				jQuery('#hiddenPanel').addClass('closed').removeClass('open');
				
		} else {
				jQuery('body.wide .galleria-thumbnails-container').fadeOut();
				jQuery('#hiddenPanel').addClass('open').removeClass('closed');
		}
		});
	
	
	// This function toggles the left column to see if it should be fixed or absolutely positioned based on screen height
	function staticNav() {
	        var sidenavHeight = jQuery("#leftCol").height() + 350; //Get height of sidenav
	        var winHeight = jQuery(window).height(); //Get height of viewport
	        var browserIE6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false; //Check for IE6

	        if (browserIE6) { //if IE6...
	            jQuery("#leftCol,#topIcons,.galleria-thumbnails-container").css({'position' : 'absolute'});  //reset the sidenav to be absolute
	        } else { //if not IE6...
	            jQuery("#leftCol,#topIcons,.galleria-thumbnails-container").css({'position' : 'fixed'}); //reset the sidenav to be fixed
	        }

	        if (sidenavHeight > winHeight) { //If sidenav is taller than viewport...
	            jQuery("#leftCol,#topIcons,.galleria-thumbnails-container").addClass('absolute').css({'position' : 'absolute'}); //switch the fixed positioning to static. Say good bye to sticky nav!
	        }
	
			if (sidenavHeight < winHeight) { //If sidenav is taller than viewport...
	            jQuery("#leftCol,#topIcons,.galleria-thumbnails-container").addClass('fixed').css({'position' : 'fixed'}); //switch the fixed positioning to static. Say good bye to sticky nav!
	        }
			jQuery("#galleria").height(winHeight);
	 }

	    staticNav(); //Execute function on load

	    jQuery(window).resize(function () { //Each time the viewport is adjusted/resized, execute the function
	        staticNav();
			
		var browserWidth = jQuery(window).width();
			
			if (browserWidth < 520) {
					jQuery('body').addClass('thin').removeClass('wide');
					jQuery('body.thin.nofull .galleria-thumbnails-container').fadeOut();
			}
			else if (browserWidth > 520) {
					jQuery('body').addClass('wide').removeClass('thin');
					
					if (jQuery('#hiddenPanel').hasClass('closed')) {
						jQuery('body.wide.nofull .galleria-thumbnails-container').fadeIn();
					}
				}
	    });
		//Puts sticky footer at bottom of page on iphone
		if(navigator.platform == 'iPhone' || navigator.platform == 'iPod')
		{
		     jQuery(".footerRow").css("position", "static");
		};						
});
