/*
	Robox Modal Windows for jQuery
	r250
*/
(function(f){var i=null,u=1,g=false,e=null,l=null,q=null;function h(y,x,A,z){if(x instanceof jQuery){y="roboxevent_"+y;x.bind(y+".robox",z||{},A)}}function t(z,y,x){if(y instanceof jQuery){z="roboxevent_"+z;y.trigger(z,q)}}function k(){if(null==i){i=f('<div class="robox-modal-overlay"/>').hide().prependTo("body");f(".robox-modal-overlay").live("click",o.close);u=i.css("opacity")}return i}function n(x){if(g){return this}g=true;k().stop().fadeTo(x||300,u)}function r(x){if(!g){return this}g=false;k().stop().fadeTo(x||300,0,function(){k().remove()})}function b(y){if(!q){return this}q.before(i);var x=l;q.fadeTo(y||500,1,function(){t("loaded",x,q)})}function j(z){if(!q){return this}var y=l;var x=q;a();x.stop().fadeTo(z||500,0,function(){p(x);t("unloaded",y,x)})}function m(y){y.hide();y.addClass("robox-modal-window");var x=y.outerWidth();var z=y.outerHeight();y.css("margin",(z/2*-1)+"px 0 0 "+(x/2*-1)+"px");return y}function p(x){if(!x){return}x.removeClass("robox-modal-window").hide()}function w(y,x){if(x==q){return}t("load",y,x);m(x);l=y;q=x}function a(){t("unload",l,q);l=null;q=null}function d(y,x){if(q){return}n();w(y,x);b()}function c(){r();j()}function s(){if(null==e){e=f('<div class="robox-loading"/>').hide().prependTo("body")}return e}function v(){var x=s();if(q){x.width(q.width()).height(q.height())}m(x);q=x.show()}var o={init:function(y){var x={};if(y){f.extend(x,y)}this.unbind(".robox");this.bind("click.robox",x,o.open);for(eventName in x.event||{}){h(eventName,this,x.event[eventName],x)}},open:function(x){if(x instanceof jQuery.Event){x.preventDefault()}$link=(this instanceof jQuery)?this.eq(0):f(this).eq(0);var y=$link.attr("href");if("#"==y.charAt(0)){d($link,f(y))}},close:function(x){if(x instanceof jQuery.Event){x.preventDefault()}c();return this}};f.fn.robox=function(y,x){if(o[y]){return o[y].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof y==="object"||!y){o.init.apply(this,arguments);return this}else{f.error("Method "+y+" does not exist on jQuery.robox")}}}})(jQuery);


/*
	Tools
*/
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}



/*
	Test for HTML5 video element support
*/
// @var boolean, will hold the answer to the question
var _isSupported;

// lazy loads the answer to the question
function videoElementSupport(disableInIE9){
	if (typeof _isSupported == "undefined"){

		// returns true if browser supporst it
		_isSupported = (!!document.createElement('video').canPlayType);

		// i'm disabling support for HTML5 video on the home page in IE9 since it's not sizing correctly
		if (getInternetExplorerVersion() == 9 && disableInIE9 == true){
			_isSupported = false;
		}
	}
	return _isSupported;
}





/**
 * Modal Video Player
 */
$(document).ready(function() {
		
	/*
		Responisbile for starting a video when it is ready
		this function will be attached to the video's 'canplaythrough' event
	*/
	onCanPlay = function(){
		// prevent this from happening again, since this event may be fired again
		this.removeEventListener('canplaythrough', onCanPlay, false);
		this.removeEventListener('load', onCanPlay, false);
		
		// play it
		this.play();
	}

	/*
		Activate the modal window button
	*/
	$(".modal-launcher").robox({
		event: {
			// if there is a video in the window, this will load and start the video inside the modal window 
			// that has opened.  if the video element has preload="auto" then all you really need to do is start it
			// but the event listener stuff should help this work the same way on IOS devices
			// opera is not updating video.readyState properly, so the video will always open paused in opera
			loaded: function(event, modalContent){
								
				if (!videoElementSupport()) return false;
				
				var videoElement = $("video", modalContent).eq(0).get(0);

				if (!!videoElement){
				
					// starts video (starts it loading in the case of it not being ready yet, don't worry we'll pause it in a sec)
					videoElement.play();
				
					// if it's not actually ready to play yet
					if(videoElement.readyState !== 4){ //HAVE_ENOUGH_DATA
						videoElement.addEventListener('canplaythrough', onCanPlay, false);
						videoElement.addEventListener('load', onCanPlay, false); //add load event as well to avoid errors, sometimes 'canplaythrough' won't dispatch.
						setTimeout(function(){
							videoElement.pause(); //block play so it buffers before playing
						}, 1); //it needs to be after a delay otherwise it doesn't work properly.
					}
				}
				

			}
			,
			unload: function(event, modalContent){
				if (!videoElementSupport()) return false;

				var videoElement = $("video", modalContent).eq(0).get(0);
				if (!!videoElement){
					videoElement.pause()
				}
				
			}
			,
			// if there is a video in the window, this will rewind it.
			unloaded: function(event, modalContent){
				if (!videoElementSupport()) return false;
			
				var videoElement = $("video", modalContent).eq(0).get(0);

				if (!!videoElement){
					// opera doesn't take kindly to this, so wrap it in a try/catch
					try{
						videoElement.currentTime = 0;
					}
					catch (err){
						// sorry, no rewind for this browser.
					}
				}
				
			}
		}
	});
	
	
	
	/*
		Setup close button on inline modal content
	*/
	$('<a href="javascript:;" class="robox-close">Close</a>')
		.appendTo(".modal-content")
		.click(function(){
			$(this).robox("close");
		});
	
		

});




/**
 * Home Page Video Player
 */
$(document).ready(function() {

	// don't do this stuff if we're not on the home page
	if ($("#homepage-theater").length == 0) return;

	// if HTML5 video support (and the "true" excludes IE9)
	if (videoElementSupport(true)){
		var $video = $("#homepage-theater video");

		// setup play/pause click behavior
		$video
			.attr("title", "Click to pause video")
			.click(function(){
				if (this.paused){
					this.play();
					$(this).attr("title", "Click to pause video");
				}
				else{
					this.pause();
					$(this).attr("title", "Click to play video");
				}
			});

		// rewind to slogan frame and stop
		$video[0].addEventListener('ended', function(){
			this.currentTime = 0;
			 this.currentTime = 15.9;
			 this.pause();
		}, false);

	}

	// Flash fallback using Flowplayer
	$("#homepage-theater .flash-fallback a").flowplayer(
		{
			src:"js/flowplayer/flowplayer-3.2.7.swf",
			wmode:"opaque",
			bgcolor:"#ffffff",
			quality:"autohigh"
		}, 
		{
			clip: {
				scaling: "fit",
				 // loop
				  onBeforeFinish: function () {
					return true;
				}
			},
			plugins: {
		        // controls: {
		        //     url: 'flowplayer.controls-3.2.5.swf',
		        //     fullscreen: false,
		        //     backgroundColor: '#0093c0'
		        // }
		        controls: null
		    }

		}
	);

	
});



