$(document).ready(function()
{
  hookButtons();
  resetMedia();
});

function reload(swf, width, height, moviename, clicked)
{
  var flashvars = {};
  flashvars.mn = moviename;
  var params = {};
  params.allowfullscreen = "true";
  params.menu = "false";
  params.quality = "high";
  params.play = "true";
  params.loop = "false";
  params.wmode = "window";
  params.scale = "showall";
  params.allowscriptAccess = "samedomain";
  var attributes = {};
  swfobject.embedSWF(swf, "player", width, height, "9.0.0", "expressInstall.swf", flashvars, params, attributes);  
  starterClicked(clicked.parentNode);    
}

function resetMedia()
{
  if($("div.divHolder").children('#media').length>1)
  {
    $("div.divHolder").children('#media').hide();
  }
  else
  {
    hldrclk($("div.divHolder"));
  }
}

function starterClicked(sender)
{
  $(sender.parentNode).children().removeClass('idf');
  $(sender).addClass('idf');
  $(sender.parentNode).children().removeClass('avhi');
  $(sender).addClass('avhi');
  moveIt(sender);
}

function hldrclk(sender) {
    $("div.divHolder").children('#spnlbl').show();
    $("div.divHolder").children('#media').hide();
    $("div.avdiv").removeClass('avdiv');
    //uncomment this after testing is done!!
    var obj = $(sender).children('#media');


    if (obj.is(':hidden')) {
        obj.show();
    }
    else {
        obj.hide();
    }
    $($(obj.children('.idf').children())).click();
    $(sender).addClass("avdiv");

    $(sender).children('#spnlbl').hide();
    
    if ($(sender).children('#media').children('span').length == 1) {
        $(sender).children('#media').children('span').hide();
    }

}


function moveIt(sender)
{
  $("#player").prependTo($(sender.parentNode));
  $("#player").show();
}

function hookButtons()
{
  $('.hfvd').each(
    function()
    {
      var temp = $(this).parent().children('span.vflid');
      $("#" + $(this).attr('id')).click(function(event) { 
       reload('flashers/svvid.swf', 500, 400, temp.text(), this);
       event.preventDefault();
      });
      //remove the filename span
      temp.remove();
    }
  )

  $('.hfad').each(
    function()
    {
      var temp = $(this).parent().children('span.vflid');
      $("#" + $(this).attr('id')).click(function(event) { 
       reload('flashers/svaud.swf', 330, 84, temp.text(), this);
       event.preventDefault();
      });
      //remove the filename span
      temp.remove();
    }
  )
}