/**
 * (c) Copyright SECRA GmbH
 */

var poolbar_video  = new Array(); 
var poolbar_gesamt = 0; 
var poolbar_start  = 0; 
var poolbar_anzahl = 0; 
var weite          = 86;
      
function onLoadAction(){
  loadPoolbar(0,poolbar_startwert);
};

function installListener(){
  $('#nextvideobtn').bind('click', nextVideos);
  $('#prevvideobtn').bind('click', previousVideos);
};

function uninstallListener(){
  $('#nextvideobtn').unbind('click', nextVideos);
  $('#prevvideobtn').unbind('click', previousVideos);
};

function responsePoolbar(ajax){
  var out = JSON.parse(ajax.getText());
  poolbar_video  = out['video'];
  poolbar_gesamt = out['meta']['gesamt'];
  poolbar_anzahl += out['meta']['anzahl'];
  
  var a;
  var text;
  var table;  
  var tbody;
  var tr;
  var td;
  
  var ausgabebereich = document.getElementById("poolbar_content");

  for(var i in poolbar_video) {

    if(poolbar_video.hasOwnProperty(i)){
        
        a = document.createElement('a');
        a.className = 'poolbar_link';
        a.href = "index.php?vnr="+poolbar_video[i]['link'];
        
        span = document.createElement('span');
        span.className = 'poolbar_titel';

        text = secra.Secrautility.html_entity_decode(poolbar_video[i]['titel']);
        text = document.createTextNode(text);
        span.appendChild(text);
        
        table = document.createElement('table');
        table.setAttribute('cellpadding','0');
        table.setAttribute('cellspacing','0');
        table.className = 'poolbar_link';

        tbody = document.createElement('tbody');
        table.appendChild(tbody);

        tr    = document.createElement('tr');
        tbody.appendChild(tr);

        td    = document.createElement('td');
        td.setAttribute('valign','middle');
        td.setAttribute('align','center');
        td.className = 'poolbar_td';
        td.style.backgroundImage = "url("+poolbar_video[i]['img']+")";
        $(td).bind('click', {vnr: poolbar_video[i]['link']}, link);

        tr.appendChild(td);
        a.appendChild(span);
        td.appendChild(a);

        ausgabebereich.appendChild(table);
    }
  }
};


function link (event) {
  document.location.href = "index.php?vnr="+event.data.vnr;
};

function loadPoolbar(start,anzahl){
  var ajax = new secra.AjaxHandler('poolbarloader');
  ajax.method = 'POST';
  ajax.url    = 'ajax/poolbar.php?action=load&startvideo='+start+'&anzahlvideo='+anzahl;
  ajax.async  = true;
  ajax.callbackfunc = {};
  ajax.callbackfunc['state4'] = responsePoolbar;
  ajax.registerCallback(ajax.callbackfunc, 'html');
  ajax.submit();

};

function nextVideos(e){
  
  uninstallListener();
  
  var step = 3;
  if(poolbar_anzahl + step > poolbar_gesamt){
    step = poolbar_gesamt - poolbar_anzahl;
  }
  
	poolbar_start = poolbar_anzahl;
	
  var diff;
  if(step > 0){
    $("#poolbar_content").width($("#poolbar_content").width() + weite*step);
    diff = weite*step;
  } else {
    diff = weite*3;
    if(diff > $("#poolbar_content").position().left + $("#poolbar_content").width() - $("#poolbar_port").width()){
      diff = $("#poolbar_content").position().left + $("#poolbar_content").width() - $("#poolbar_port").width();
    }
  }
  
  if($("#poolbar_content").position().left + $("#poolbar_content").width() >= $("#poolbar_port").width()){
	  $("#poolbar_content").animate({'left': $("#poolbar_content").position().left - diff}, {complete: installListener});
  } else {
    installListener();
  }
  
  if(step>0){
	  loadPoolbar(poolbar_start,step);
  }
};

function previousVideos(e){
  
  uninstallListener();
  
  var stepw = weite*3;
  
  if($("#poolbar_content").position().left + stepw >= 0){
    newpos = 0;
  } else {
    newpos = $("#poolbar_content").position().left + stepw;
  }
  $("#poolbar_content").animate({'left': newpos}, {complete: installListener});  
  
};

Handler.add(window, 'load', onLoadAction);
Handler.add(window, 'load', installListener);
