/* Wasabi Player functions */


function loadWasabiPlayer() {
  //init WasabiPlayer, loading thumbnails

  WasabiPlayer1 = flashembed("playercontainer", { src:"includes/wasabiplayer2.swf", width:400, height:336, id:'wasabiplayer', version:[7,0] },{ config: fpConf } );

  //preload tiny thumbnails
  if(document.getElementById('playerplaylist'))
    preloadTinyThumbs();
}


function onFlowPlayerReady() {
  //once the flowplayer has loaded, load the first ep in the list
  playEpisode(ep);
}


function preloadTinyThumbs() {
  //onload place thumbnails into the episode list and put selection variations into memory


  for(i=0;i<clipdata.length;i=i+2) {
    tinythumb[i] = new Image(55,45);
    tinythumb[i].src = clipdata[i]['tinythumb'];

    tinyselthumb[i] = new Image(55,45);
    tinyselthumb[i].src = clipdata[i]['tinyselthumb'];
    
    document.getElementById('tiny'+i).src = tinythumb[i].src;
  }
}



function clipDataDisplay(clipIndex) {

  //displays episode data in the HTML elements
  document.getElementById('episodename').innerHTML=clipdata[clipIndex]['name_longer']; //h3  
  document.getElementById('episodetitle').innerHTML=clipdata[clipIndex]['name']; //h3
  document.getElementById('episodeblurb').innerHTML=clipdata[clipIndex]['blurb']; //p
  document.getElementById('epblurbmorelink').href='?v=' + clipdata[clipIndex]['id'] + '&mode=index#' + clipdata[clipIndex]['id']; //a
  
  //update share buttons and data
  document.getElementById('sharebutton').href='javascript:displayClipBlurb(' + clipdata[clipIndex]['id'] + ')';
  document.getElementById('epsharelink').value=site_url + "?v=" + clipdata[clipIndex]['id'];
  document.getElementById('epsharefacebook').href=clipdata[clipIndex]['sharefacebook'];
  document.getElementById('epsharemyspace').href=clipdata[clipIndex]['sharemyspace'];
  
  //set rating class
  document.getElementById('episodename').setAttribute("class", clipdata[clipIndex]['ep_rating'] + "_rating");
  document.getElementById('episodename').setAttribute("className", clipdata[clipIndex]['ep_rating'] + "_rating");

  if(document.getElementById('epeditbutton'))
    document.getElementById('epeditbutton').href='?s=' + show + '&edit=' + clipdata[clipIndex]['id']; //a

  //prepare download or add to cart link
  document.getElementById('filesizemp4').innerHTML='(' + clipdata[clipIndex]['download_filesize'] + ')'; //em
  document.getElementById('filesize3gp').innerHTML='(' + clipdata[clipIndex]['mobile_filesize'] + ')'; //em

  document.getElementById('epaddtocartprice').innerHTML=clipdata[clipIndex]['ep_pricetag']; //span
  document.getElementById('epaddtocartbutton').href = clipdata[clipIndex]['download_url']; //a

  //prepare the 'add to cart' or 'view cart' button
  var in_cart = clipdata[clipIndex]['in_cart'];
  
  if(in_cart == 1) {
    document.getElementById('epaddtocartbutton').setAttribute("class", "epactionbutton view"); //a
    document.getElementById('epaddtocartbutton').href = '?mod=cart'; //a
  } else {
    document.getElementById('epaddtocartbutton').setAttribute("class", "epactionbutton"); //a
  }

  updateDownloadButton('mp4');
}


function updateDownloadButton(type) {

  if(type == "mp4") {
    document.getElementById('radiomp4').checked = 'checked';
    document.getElementById('epdownloadbutton').href = clipdata[clipIndex]['download_url'];
    document.getElementById('epdownloadbutton').setAttribute("class", "epactionbutton downloadmp4");
    document.getElementById('epdownloadbutton').setAttribute("className", "epactionbutton downloadmp4");

    document.getElementById('episodedownload').setAttribute("class", "highlight");
    document.getElementById('episodedownload').setAttribute("className", "highlight");
    document.getElementById('episodemobile').setAttribute("class", "");
    document.getElementById('episodemobile').setAttribute("className", "");

  } else if(type == "3gp") {
    document.getElementById('radio3gp').checked = 'checked';
    document.getElementById('epdownloadbutton').href = clipdata[clipIndex]['mobile_url'];
    document.getElementById('epdownloadbutton').setAttribute("class", "epactionbutton download3gp");
    document.getElementById('epdownloadbutton').setAttribute("className", "epactionbutton download3gp");
    
    document.getElementById('episodedownload').setAttribute("class", "");
    document.getElementById('episodedownload').setAttribute("className", "");
    document.getElementById('episodemobile').setAttribute("class", "highlight");
    document.getElementById('episodemobile').setAttribute("className", "highlight");
  }
}



function clipActionDisplay(clipIndex) {
  var allowed = clipdata[clipIndex]['download_allowed'];
  var disable_downloads = clipdata[clipIndex]['disable_downloads'];
  var price_raw = clipdata[clipIndex]['ep_price_raw'];

  if(disable_downloads == 1 && price_raw <= 0) {
    document.getElementById('epactionpanel').setAttribute("class", "epactionpanel hide");
    document.getElementById('epactionpanel').setAttribute("className", "epactionpanel hide");
    
    document.getElementById('epdownload').setAttribute("class", "epactionpanel hide");
    document.getElementById('epdownload').setAttribute("className", "epactionpanel hide");
    
    document.getElementById('epaddtocart').setAttribute("class", "epactionpanel hide");
    document.getElementById('epaddtocart').setAttribute("className", "epactionpanel hide");

    document.getElementById('epdownloaddisabled').setAttribute("class", "epactionpanel show");
    document.getElementById('epdownloaddisabled').setAttribute("className", "epactionpanel show");


  } else if(allowed == 1) {
    document.getElementById('epactionpanel').setAttribute("class", "epactionpanel hide");
    document.getElementById('epactionpanel').setAttribute("className", "epactionpanel hide");
    
    document.getElementById('epdownload').setAttribute("class", "epactionpanel show");
    document.getElementById('epdownload').setAttribute("className", "epactionpanel show");
    
    document.getElementById('epaddtocart').setAttribute("class", "epactionpanel hide");
    document.getElementById('epaddtocart').setAttribute("className", "epactionpanel hide");

    document.getElementById('epdownloaddisabled').setAttribute("class", "epactionpanel hide");
    document.getElementById('epdownloaddisabled').setAttribute("className", "epactionpanel hide");

  } else {
    document.getElementById('epactionpanel').setAttribute("class", "epactionpanel hide");
    document.getElementById('epactionpanel').setAttribute("className", "epactionpanel hide");

    document.getElementById('epdownload').setAttribute("class", "epactionpanel hide");
    document.getElementById('epdownload').setAttribute("className", "epactionpanel hide");
    
    document.getElementById('epaddtocart').setAttribute("class", "epactionpanel show");
    document.getElementById('epaddtocart').setAttribute("className", "epactionpanel show");

    document.getElementById('epdownloaddisabled').setAttribute("class", "epactionpanel hide");
    document.getElementById('epdownloaddisabled').setAttribute("className", "epactionpanel hide");
  }
}



function displayClipBlurb(ep_id) {
  //takes an ep_id and shows the blurb box. if box is already displayed, simply loads new data into elements
  clipIndex = getClipIndex(ep_id);

  //display episode blurb data
  document.getElementById('episodetitle').innerHTML=clipdata[clipIndex]['name']; //h3
  document.getElementById('episodeblurb').innerHTML=clipdata[clipIndex]['blurb']; //p
  document.getElementById('epblurbmorelink').href='?v=' + clipdata[clipIndex]['id'] + '&mode=index#' + clipdata[clipIndex]['id']; //a

  if(document.getElementById('epeditbutton'))
    document.getElementById('epeditbutton').href='?s=' + show + '&edit=' + clipdata[clipIndex]['id']; //a


  //display the panel
  thisdiv = document.getElementById('epblurbpanel');
  
  if(thisdiv.className != "show"){
    thisdiv.setAttribute("class", "show");
    thisdiv.setAttribute("className", "show");
  }
}


function getClipIndex(ep_id) {
  //function takes an ep_id and finds the associated clipIndex

  for(i=0;i<clipdata.length;i=i+2) {
    if(clipdata[i]['id'] == ep_id) {
      clipIndex = i;
      break;
    }
  }
  
  return clipIndex;
}


function getEpIdFromURL(ep_url) {
  //function takes a file path and finds the associated ep_id

  for(i=0;i<clips.length;i=i+2) {
    if(clips[i]['url'] == ep_url) {
      ep_id = clipdata[i]['id'];
      break;
    }
  }
  
  return ep_id;
}


function playEpisode(ep_id) {
  //loads all data and starts moves the playback to the selected clipIndex

  clipIndex = loadEpisodeData(ep_id); //prepare the player data of the episode
  clipSelected(clipIndex); //play the episode from a playlist
}


function clipSelected(clipIndex) {
  //loads a clip from within a playList, index (jpg) + 1 is the video file in the playlist
  WasabiPlayer1.ToClip(clipIndex + 1);
}


function onClipChanged(clip) {
  //when a user skips a clip, or a clip finishes and goes to the next, update the episode list selection
  var clipURL = clip.fileName;

  ep_id = getEpIdFromURL(clipURL);

  loadEpisodeData(ep_id); //changes selection in the episode list and updates all display data and action buttons
}


function loadEpisodeData(ep_id) {
  //pull episode data from array

  for(i=0;i<clipdata.length;i=i+2) {
    document.getElementById('clip'+i).setAttribute("class", "playlistitem");
    document.getElementById('clip'+i).setAttribute("className", "playlistitem");
    document.getElementById('info'+i).setAttribute("class", "playlistinfo");
    document.getElementById('info'+i).setAttribute("className", "playlistinfo");
    document.getElementById('tiny'+i).src = tinythumb[i].src;

    if(clipdata[i]['id'] == ep_id) {
      clipIndex = i;
      document.getElementById('clip'+i).setAttribute("class", "playlistitem selected");
      document.getElementById('clip'+i).setAttribute("className", "playlistitem selected");
      document.getElementById('info'+i).setAttribute("class", "playlistinfo selected");
      document.getElementById('info'+i).setAttribute("className", "playlistinfo selected");
      document.getElementById('tiny'+i).src = tinyselthumb[clipIndex].src;
    }
  }

  //scroll to the selected episode
  $("#playerplaylist ol").scrollTo("#clip"+clipIndex,300);

  //display episode data and update action buttons
  clipDataDisplay(clipIndex);
  clipActionDisplay(clipIndex);

  //record this view on google analytics
  pageTracker._trackPageview(clips[clipIndex]['url']);

  return clipIndex;
}

