// Resources Overlay Settings //////////////////////////////////////

var theFile;
var resourceType;

var player;
var video;
var myVideo;
var firstTry = true;
var info_on = false;
var curr_video = null;
var videoPlayer = null;
var acceptable_browser = true;



// calls for fancyBox overlay
$(document).ready (function () {
	
	// make freeport links
	//$('a[href^='#launch-freeport-']').each(function(index){
	$("a[href*='#launch-freeport-']").each(function(index){
		var url = $(this).attr('href').split('#');
		var new_href = '/aux/js/lib/freeport/issues/' + url[1].replace('launch-freeport-', '');
		$(this).attr('href', new_href).addClass('freeport_launch');
	});
	
	// image overlay link
	$('.fancy_image').fancybox ({
			'overlayShow': true,
			'overlayOpacity': .8,
			'hideOnContentClick': true,
			'zoomSpeedIn': 500,
			'zoomSpeedOut': 500,
			'centerOnScroll': false,
			'padding': 8, 
			'callbackOnStart' : function (){
				$('body').removeClass('videoContent');	  
				resourceType = 'image';
				$('.infoButton').fadeIn('slow');
			}
	  });
	
	// feature image overlay link
	$('.fancy_feature').fancybox ({
			'overlayShow': true,
			'overlayOpacity': .8,
			'hideOnContentClick': true,
			'zoomSpeedIn': 500,
			'zoomSpeedOut': 500,
			'centerOnScroll': false,
			'padding': 8, 
			'callbackOnStart' : function (){
				$('body').removeClass('videoContent');
				resourceType = 'feature';
				$('.infoButton').fadeIn('slow');
			}
	  });
	
	// freeport overlay link	
	$('.freeport_launch').fancybox ({
		'overlayShow': true,
		'overlayOpacity': .8,
		'hideOnContentClick': false, 
		'frameWidth': 980,
		'frameHeight': 797, 
		'centerOnScroll': false,
		'padding': 0, 
		'callbackOnStart' : function (){
			resourceType = 'freeport';
			$('.infoButton').fadeIn('slow');
			$('#fancy_wrap').addClass ('fancy_bg_freeport');
			$('body').addClass('videoContent');
		}, 
		'callbackOnShow' : function (){			
			if (hash['freeportslide'] != '' && hash['freeportslide'] != undefined && hash['freeportslide'] != false)
			{
				var freeport_slide = '.slide_icons .icon' + hash['freeportslide'];
				$(freeport_slide).click();
			}
		}, 
		'callbackOnClose': function (){
			// remove class for Freeport
			$('#fancy_wrap').removeClass('fancy_bg_freeport');
			$('body').removeClass('videoContent');
		}
	});	
	
	jQuery.each(jQuery.browser, function(i, val) {
	  if( (i=='mozilla') && (Number(jQuery.browser.version.substr(0,3)) < 1.9) && (navigator.appVersion.indexOf('Mac')!=-1) )
	  {
		  acceptable_browser = false;
	  }
	});
	

	
	if ( swfobject.hasFlashPlayerVersion('9.0.28') && acceptable_browser )
	{
		// video overlay link
		$('.fancy_video').fancybox ({
				'overlayShow': true,
				'overlayOpacity': .8,
				'hideOnContentClick': false,
				'frameWidth': 720,
				'frameHeight': 429,
				'centerOnScroll': false,
				'padding': 20/*, 
				'callbackOnStart' : function (){
				}*/
		  });
		
		$('.videoInfoList .imageTitle h4 a').each(function (i) {
			$(this).replaceWith('<span>' + $(this).text() + '</span>');
		});
		
		$('.videoInfoList:not(.video-view-all .videoInfoList)').click (function (e) { 
		  $(this).children('a.fancy_video').click();
		});
		
		$('.video-view-all .videoInfoList .imageTitle').click (function (e) { 
		  $(this).parent().parent().children('a.fancy_video').click();
		});
		
		$('.fancy_video').attr('href', function () {			
			var myVideo = String($(this).attr('name'));
			return '/aux/js/lib/overlay/media_player_test.html?theFile=' + myVideo;
		});		
		
		
		$('.fancy_video')
		.click (function (e) { 
		  resourceType = 'video';
		  theFile = $(this).attr('name');
		  firstTry = true;
		  $('.infoButton').fadeIn('slow');
		  
		  $('body').addClass('videoContent');
		})
		.append('<span class="play_icon"></span>');
	}
	else
	{
		//alert ('no flash');
		$('.videoInfoList:not(.video-view-all .videoInfoList)').click (function (e) { 
		  window.location = $(this).children('a.fancy_video').attr('href');
		});
	}
	
	
	
	// extra functionality added to fancyBox js file for info text
	
	/*$('.fancy_image').click (function (e) {
	  $('body').removeClass('videoContent');	  
	  resourceType = 'image';
	  $('.infoButton').fadeIn('slow');
    });
	
	
	$('.fancy_feature').click (function (e) {
	  $('body').removeClass('videoContent');
	  resourceType = 'feature';
	  $('.infoButton').fadeIn('slow');
    });*/
	
	
	$('#fancy_inner').prepend('<div class="infoBox"><div class="infoBg"></div><div class="info"></div></div>');
	$('#fancy_inner').append('<div class="infoButton"></div>');
	$('.infoBg').fadeTo(0, 0.75);
	
	
	$('.infoButton').click (function (e) { 
	  //e.stopPropagation();
	  e.stopImmediatePropagation();
	  	  
	  //if (info_on == false)
	  if ($(this).parents('#fancy_inner').children('.infoBox:first').css('display') == 'none')
	  {
		  //console.log('info is on');
		  info_on = true;
		  $(this).parents('#fancy_inner').children('.infoBox:first').fadeIn('slow', function(){
		  	$(this).parents('#fancy_inner').children('.infoButton:first').addClass('info_on');
		  });
	  }
	  else 
	  {
		  //console.log('info is off');
		  info_on = false;
		  $(this).parents('#fancy_inner').children('.infoBox:first').fadeOut('slow', function(){
		  	$(this).parents('#fancy_inner').children('.infoButton:first').removeClass('info_on');
		  });
	  }
    });
	
	
	$('.videoInfoList:not(.video-view-all .videoInfoList)').bind('mouseenter', function() {
		$(this).addClass('hover');
	}).bind('mouseleave', function() {
	  	$(this).removeClass('hover');
	});
	
	$('.video-view-all .fancy_video').bind('mouseenter', function() {
		$(this).parent().addClass('hover');
	}).bind('mouseleave', function() {
	  	$(this).parent().removeClass('hover');
	});
	
	$('.video-view-all .videoInfoList .imageTitle').bind('mouseenter', function() {
		$(this).parent().parent().addClass('hover');
	}).bind('mouseleave', function() {
	  	$(this).parent().parent().removeClass('hover');
	});
	
	
	
	
	
	
	// ********************** freeport ********************** //
	
	
	
	
	/*$('.freeport_launch').click (function (e) { 
	  resourceType = 'freeport';
	  $('.infoButton').fadeIn('slow');
	  $('#fancy_wrap').addClass ('fancy_bg_freeport');
	  $('body').addClass('videoContent');
	});*/
	
	// ********************** freeport ********************** //
	
	
	
	// ********************** auto launch video ********************** //
	 
	// Read a page's GET URL variables and return them as an associative array.
	function getUrlVars()
	{
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		var clean = [];
		
		if (hashes != window.location.href && hashes != '')
		{
			for(var i = 0; i < hashes.length; i++)
			{
				hash = hashes[i].split('=');
				vars.push(hash[0]);
				
				clean = hash[1].split('#');
				
				vars[hash[0]] = clean[0];
			}
		}
		else vars = [];
	
		return vars;
	}
	var hash = getUrlVars();
	
	if (hash['autovideo'] != '' && hash['autovideo'] != undefined && hash['autovideo'] != false) 
	{
		var auto_launch = 'a.fancy_video[name="' + hash['autovideo'] + '"]';
		$(auto_launch).click();
	}
	else if (hash['autofreeport'] != '' && hash['autofreeport'] != undefined && hash['autofreeport'] != false)
	{
		var auto_freeport = 'a.freeport_launch[href="/aux/js/lib/freeport/issues/' + hash['autofreeport'] + '"]';
		
		$(auto_freeport).click();
	}
	
	// ********************** auto launch video ********************** //

});

