$(function() {
  
  var indexId = "";
  var index = "";
  var slug = "";
  var title = "";
  var desc = "";
  var flv = "";
  
	$("div#rubberband").codaSlider();
  $("div#rubberband").hide();
  $("div#rubberband").show();
  
  $("#search_media").click(function ()
  {
    $(this).css("color", "#ffffff");
  });
  $("#search_media").focus(function ()
  {
    $(this).attr("value", "");
    $(this).unbind("focus");
  });
  $("#search_media").blur(function ()
  {
    $(this).css("color", "#666");
  });
  $(".msg a").click(function ()
  {
    if($(this).hasClass("currently_playing"))
    {
      togglePause();
      $(this).toggleClass("video_paused");
      $(this).removeClass("currently_playing");
    }
    else
    {
      if($(this).hasClass("video_paused"))
      {
        $(".msg a").removeClass("currently_playing");
        $(this).removeClass("video_paused");
        togglePause();
        $(this).addClass("currently_playing");
      }
      else
      {
        $(".msg a").removeClass("currently_playing");
        $(".msg a").removeClass("video_paused");
        $(".msg a").removeClass("selected");
        indexId = $(this).attr("id");
        var index = indexId.split('_')[1];
        slug = $(this).attr("class");
        play(index);
        title = $("#" + slug + " .title").text();
        desc = $("#" + slug + " .desc").text();
        flv = $("#" + slug + " .flv").text();
        $("#video_info h4 span span").text(title);
        $("#video_meta span").text(desc);
        $(this).addClass("currently_playing");
      }
    }
  
  });
  $('.panel a[class!=more]').tooltip({ 
    track: true, 
    delay: 0, 
    showURL: false, 
    showBody: " - ", 
    fade: 250,
    extraClass: "videotip"
  });
  runHash();
  
});
function runHash()
{
  index = "0";
  var loc = document.location.toString();
  if (loc.match('#')) {
    var hash = loc.split('#')[1];
    var goto = loc.split('#/')[1];
    if(goto)
    {
      index = $("." + goto).attr("id");
      index = index.split('_')[1];
      if(goto)
      {
        title = $("#" + goto + " .title").text();
        desc = $("#" + goto + " .desc").text();
        flv = $("#" + goto + " .flv").text();
        $("#video_info h4 span span").text(title);
        $("#video_meta span").text(desc);
        i = "#video_" + index;
        resetPanel(i);
      }
    }
    else if(hash)
    {
      $("div#rubberband").find("div.panel").each(function (i,z)
      {
        if((i+1) == hash)
        {
          e = $(this).find(".msg:first a");
          
          index = e.attr("id");
          index = index.split('_')[1];
          
          return false;
        }
        
      });
    }
    else
    {
      index = "0";
    }
  }
  slug = $("#video_" + index).attr("class");
  $("#video_" + index).addClass("selected");
  title = $("#" + slug + " .title").text();
  desc = $("#" + slug + " .desc").text();
  $("#video_info h4 span span").text(title);
  $("#video_meta span").text(desc);
  
  flvars = "playList=/videos/playlist/&startIndex=" + index + "&continuous=1";

  flashvars = false;
  params = {
    menu: "false",
    flashvars: flvars,
    allowfullscreen: true,
    wmode: "opaque"
  };
  attributes = {
    id: "flashPlayerObj",
    name: "flashPlayerObj"
  };
  swfobject.embedSWF("/swf/fPlayerIn.swf", "flashplayer", "910", "511", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
  
}
function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

function togglePause()
{
  getFlashMovie("flashPlayerObj").togglePause(true);
}
function play(index)
{
  getFlashMovie("flashPlayerObj").play(index, true);
}
function load(index)
{
  getFlashMovie("flashPlayerObj").load(index, true);
}
function previous()
{
  getFlashMovie("flashPlayerObj").previous(true);
}
function next()
{
  getFlashMovie("flashPlayerObj").next(true);
}
function resetVideos()
{
  $(".msg a").removeClass("currently_playing");
  $(".msg a").removeClass("video_paused");
  $(".msg a").removeClass("selected");
}
function resetPanel(index)
{
  var panelWidth = $("div#rubberband").find("div.panel").width();
  link = $(index);
  p = link.parent().parent().parent().parent();
  var cnt = 0;
  $("div#rubberband").find("div.panel").each(function (i,z)
  {
    cnt = - (panelWidth*i);
    e = $(this).find(index);
    if(e.length)
    {
      return false;
    }
    
  });
	p.parent().animate({ left: cnt}, 750, "expoinout");
}
/* from flash interface */
function loadedPlaylist(count)
{
  
}
function playVideo(index)
{
  resetVideos();
  
  indexId = "#video_" + index;
  resetPanel(indexId);
  slug = $(indexId).attr("class");
  $(indexId).addClass("currently_playing");
  
  title = $("#" + slug + " .title").text();
  desc = $("#" + slug + " .desc").text();
  $("#video_info h4 span span").text(title);
  $("#video_meta span").text(desc);
}
function nextVideo(index)
{
  resetVideos();
  indexId = "#video_" + index;
}

function previousVideo(index)
{
  resetVideos();
  indexId = "#video_" + index;
}

function stopVideo(index)
{
  resetVideos();
  indexId = "#video_" + index;
}

function pauseVideo(index)
{
  resetVideos();
  indexId = "#video_" + index;
  $(indexId).addClass("video_paused");
}
function resumeVideo(index)
{
  resetVideos();
  indexId = "#video_" + index;
  $(indexId).addClass("currently_playing");
}
function loadVideo(index)
{
  resetVideos();
  indexId = "#video_" + index;
}




