//videos
jQuery(document).ready(function(){
    jQuery(document.body).append('<div style="width:750px;height:460px;margin:8px auto;overflow:hidden" id="videoContainer"></div>');
    jQuery("#videoContainer").dialog({
       bgiframe: true,
       resizable: false,
       autoOpen: false,
       width: 750,
       height: 460,
       modal: true,
       title: "Video",
       close: function() {unloadVideo()}
   });
   jQuery.history.init(loadHashContent,{ unescape: ",/" });
});

function loadHashContent(hash)
{
    if(hash.match(/video-/))
        loadVideo(hash.replace('video-', ''));
}

//npId = native player id, fId = flash id
function loadVideo(dataKey)
{
    var data = hashVideoData[dataKey];
    var container = jQuery("#videoContainer");
    //detecting native player
   if(Modernizr.video)
   {
        //build native player
        if(jQuery('#native_video_player').length == 0) container.append('<video width="720" preload="auto" height="400" poster="'+data.poster+'" controls id="native_video_player"></video>');
        var nativePlayer = jQuery('#native_video_player');
        if(Modernizr.video.h264 != "")  //loading mp4
            nativePlayer.append('<source src="'+ data.video +'.mp4" type="video/mp4" />');
        else if(Modernizr.video.ogg != "") //loading ogv
            nativePlayer.append('<source src="'+ data.video +'.ogv" type="video/ogg" />');
        else if(Modernizr.video.webm != "") //loading webm
            nativePlayer.append('<source src="'+ data.video +'.webm" type="video/webm" />');
        setTimeout((function(){ document.getElementById('native_video_player').play(); }), 1000);
   }
   else //flash fallback
   {
        container.append('<div id="flash_video_player" style="width:720px; height:400px"></div>');
        setTimeout((function(){ 
			flowplayer('flash_video_player', "http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf", {
	            playlist:[data.poster, { url: "http://" + location.host + data.video + ".mp4", autoPlay:false, autoBuffering:false }]
        	});
			setTimeout((function(){flowplayer('flash_video_player').play(); }), 1000);

		}), 500);
   }
   jQuery("#videoContainer").dialog('open');
   
}

function unloadVideo(cId)
{
    try{ document.getElementById('native_video_player').pause(); } catch(err){ flowplayer('flash_video_player').pause(); }
    jQuery('#videoContainer').empty();
    location.href = '#';
}
