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

var theFile;
var resourceType;

var player;
var video;
var myVideo;
var firstTry = true;

var info_on = false;


var videoPlayer = null;
function playerReady (thePlayer)
{
	videoPlayer = window.document[thePlayer.id];
	//videoPlayer.addControllerListener("PLAYLIST", "playlistLoaded");
	videoPlayer.addModelListener('ERROR','info');
}

function addListeners ()
{
	if (videoPlayer) 
	{ 
		videoPlayer.addModelListener ("STATE", "stateListener");
	} 
	
	else 
	{
		setTimeout ("addListeners()",100);
	}
}

function info (obj)
{
	var m = obj.message;
	video = theFile + '.mp4';
	
	if (firstTry)
	{
		var tryAgain = setTimeout("videoPlayer.sendEvent('LOAD', video)", 500);
		firstTry = false;
	}
};

var acceptable_browser = true;



// calls for fancyBox overlay
$(document).ready (function () {
	
	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 )
	{
		$('.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('href')).split('.');
			//return '/aux/js/lib/overlay/media_player.html?theFile=' + myVideo[0];
			var myVideo = String($(this).attr('name'));
			return '/aux/js/lib/overlay/media_player.html?theFile=' + myVideo;
		});
		
		$('.fancy_video').append('<span class="play_icon"></span>');
		
		
		
		// video overlay link
		$(".fancy_video").fancybox ({
				'overlayShow': true,
				'overlayOpacity': .8,
				'hideOnContentClick': false,
				'frameWidth': 720,
				'frameHeight': 423,
				//'zoomSpeedIn': 500,
				//'zoomSpeedOut': 500,
				'centerOnScroll': false,
				'padding': 20
		  });
		
		$('.fancy_video').click (function (e) { 
		  resourceType = 'video';
		  theFile = $(this).attr('name');
		  firstTry = true;
		  $('.infoButton').fadeIn('slow');
		  
		  $('body').addClass('videoContent');
		});
	}
	else
	{
		//alert ('no flash');
		$('.videoInfoList:not(.video-view-all .videoInfoList)').click (function (e) { 
		  window.location = $(this).children('a.fancy_video').attr('href');
		});
	}
	
	/*$('.video_tag_link').click (function (e) {
		e.stopPropagation();
	}).bind('mouseenter', function() {
		$(this).parent().parent().removeClass('hover');
	}).bind('mouseleave', function() {
		$(this).parent().parent().addClass('hover');
	});*/
	
	
	
	
	
	
	
	/*
	$(".fancy_overlay_link").fancybox ({
			'overlayShow': true,
			'overlayOpacity': .8,
			'hideOnContentClick': false,
			'frameWidth': 760,
			'frameHeight': 620,
			'zoomSpeedIn': 0,
			'zoomSpeedOut': 0,
			'centerOnScroll': false,
			'padding': 15
	  });
	*/
	
	// image overlay link
	$(".fancy_image").fancybox ({
			'overlayShow': true,
			'overlayOpacity': .8,
			'hideOnContentClick': true,
			'zoomSpeedIn': 500,
			'zoomSpeedOut': 500,
			'centerOnScroll': false,
			'padding': 8
	  });
	
	// feature image overlay link
	$(".fancy_feature").fancybox ({
			'overlayShow': true,
			'overlayOpacity': .8,
			'hideOnContentClick': true,
			'zoomSpeedIn': 500,
			'zoomSpeedOut': 500,
			'centerOnScroll': false,
			'padding': 8
	  });
	
	
	
	
	$('#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);
	
	// extra functionality added to fancyBox js file for info text
	
	$('.fancy_image').click (function (e) {
	  resourceType = 'image';
	  $('.infoButton').fadeIn('slow');
	  
	  $('body').removeClass('videoContent');
    });
	
	
	$('.fancy_feature').click (function (e) {
	  resourceType = 'feature';
	  $('.infoButton').fadeIn('slow');
	  
	  $('body').removeClass('videoContent');
    });
	
	
	/*
	$('.infoButton').click (function (e) { 
	  e.stopPropagation();
	  $('.infoBox').fadeIn();
	  //$('.stbutton:eq(0)').click();
    });
	*/
	
	$('.infoButton').click (function (e) { 
	  e.stopPropagation();
	  if (info_on == false)
	  {
		  info_on = true;
		  $('.infoButton').addClass('info_on');
		  $('.infoBox').fadeIn();
	  }
	  else 
	  {
		  info_on = false;
		  $('.infoButton').removeClass('info_on');
		  $('.infoBox').fadeOut();
	  }
    });
	
	
	/*$('.infoBox').not('body.visit_test .infoBox').click (function (e) {
	  e.stopPropagation();
	  $(this).fadeOut();
    });*/
	
	
	$('.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');
	});
	
	
	
	// *******************************************************************  overlay embed testing  ******************************************************************* //
	
	
	/*$('.visit_test .embed_code').click (function (e) { 
	  e.stopPropagation();
	  //e.css('color', '#FF3300').focus().select();
	  //alert ('you clicked');
	  $('.visit_test .embed_code input').css('color', '#FF3300');
    });*/
	
	/*$('.test_button').click (function (e) {
		alert ('you clicked');
		e.stopPropagation();
	});*/
	
	/*$('body.visit_test .info .link_code').click (function (e) { 
	  e.stopPropagation();
	  e.css('color', '#FF3300').focus().select();
    });*/
	
	/*$('body.visit_test .infoBox').click (function (e) {
	  e.stopPropagation();
	  //$(this).fadeOut();
    });*/
	
	/*
	$("input").triggerHandler("focus");
	*/
	
	
	
	// *******************************************************************  overlay embed testing  ******************************************************************* //
	
	//$('#pots-aw').click();
	
	
	
	
	// ********************** 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('&');
	
		for(var i = 0; i < hashes.length; i++)
		{
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
	
		return vars;
	}
	var hash = getUrlVars();
	
	if (hash['autovideo'] != "" && hash['autovideo'] != undefined && hash['autovideo'] != false) 
	{
		//var auto_launch = 'a.fancy_video#' + hash['autovideo'];
		var auto_launch = 'a.fancy_video[name="' + hash['autovideo'] + '"]';
		$(auto_launch).click();
	}
	// ********************** auto launch video ********************** //
	

});
