jQuery(function($) {

    var video = document.getElementsByTagName('video')[0], html5Video = null, flash,
		$trigger = $(".click-to-play"),
		$video_overlay = $("#demo .copy, .play-video");

    // Load VideoJS
    VideoJS.setup();

	// When the video player is ready, show the button
	playButtonReady = function() { $trigger.fadeIn(); }

    // Check if the browser supports video.
    if (video.canPlayType) { html5Video = true; playButtonReady(); }

    // Function to run after video plays
    videoFadeIn = function(obj) {
        // HTML5 video will call this function when the
        // video is completed. However, the flash player
        // will call it every time the status has changed.
        // So we must check if status == COMPLETED.
        if (html5Video || obj.newstate == "COMPLETED")
        	$video_overlay.fadeIn();
    }

	// Build flash player if no HTML5 video support
    if (!html5Video) {
        $.getScript("App_Atom/ux/video_player/jwplayer/swfobject.js", function() {

            // Runs when player is ready
            playerReady = function(obj) {
              flash = document.getElementById(obj.id);
              flash.addModelListener("state", "videoFadeIn");
			  playButtonReady();
            }
  			
            var s1 = new SWFObject('App_Atom/ux/video_player/jwplayer/player-licensed.swf','player', HFlashVid.width, HFlashVid.height,'9');
        	    s1.addParam('allowfullscreen','true');
        	    s1.addParam('allowscriptaccess','always');
        	    s1.addParam('wmode','transparent');
        	    s1.addParam('flashvars','file=' + HFlashVid.src + '&controlbar=over');
        	    s1.write(HFlashVid.id);
		});
		
    }

    // add video event listeners for both HTML5 video and flash players.
    if (html5Video) { video.addEventListener('ended', videoFadeIn, false); }

    // Click to play
    $trigger.click( function() {
        //$(".video-js-box").css("visibility", "visible");
        $video_overlay.fadeOut();
        //if html5 video support, play video.
        if (html5Video) { video.play(); } 
        // else flash controls
        else { flash.sendEvent("PLAY"); }
        
        return false;
    });

});
