﻿var flashObjectId = 'videofeat';

$(function() {
    // Set inital video
    var objFirst = $('ul#nav-list > li:first');
    var clipid = objFirst.attr('data-clipid');
    
    CreateInitialVideo(clipid);
    objFirst.addClass('current');
    
    // Click handler
    $('ul#nav-list > li > a').click(function(event) {
        event.stopPropagation();
        
        var parentObj = $(this).parent();
        
        // Get Clip Id
        var clipid = parentObj.attr('data-clipid');
        
        // Remove selected class and assign it to the current item
        $('ul#nav-list > li.current').removeClass('current');
        parentObj.addClass('current');
        
        // Update Flash
        CreateInitialVideo(clipid);
    });
 });
 
 function CreateInitialVideo(clipId)
 {
    var expressinstall = false;
    var flashvars = {
        "server": "www.vimeo.com",
        "clip_id": clipId,
        "show_portrait": "0",
        "show_title": "0",
        "show_byline": "0",
        "fullscreen": "1",
        "autoplay": "1",
        "js_api": "1"
    };
    
    var params = {
        wmode: "transparent",
        allowfullscreen: "true",
        allowscriptaccess: "always"
    };

    var attributes = {id:flashObjectId, name:flashObjectId};

   //Check for existing SWF
    if (isObject(flashObjectId)) {
      //replace object/element with a new div
      replaceSwfWithEmptyDiv(flashObjectId);
    }
      
    swfobject.embedSWF('http://vimeo.com/moogaloop.swf', flashObjectId, '640', '360', '9.0.0', expressinstall, flashvars, params, attributes);
 }
 
 //Support function: creates an empty
//element to replace embedded SWF object

function replaceSwfWithEmptyDiv(targetID){

   var el = document.getElementById(targetID);
   if(el){
      var div = document.createElement("div");
      el.parentNode.insertBefore(div, el);
 
      //Remove the SWF
      swfobject.removeSWF(targetID);
   
      //Give the new DIV the old element's ID
      div.setAttribute("id", targetID);
   }
}

function isObject(targetID){

   var isFound = false;
   var el = document.getElementById(targetID);
   
   if(el && (el.nodeName === "OBJECT" || el.nodeName === "EMBED")){
      isFound = true;
   }
   
   return isFound;
}

function vimeo_player_loaded() {
    var obj = document.getElementById(flashObjectId);
    obj.play();
}
