/**
 * 
 */

var quickbar_video  = new Array(); 
var quickbar_gesamt = 0; 
var quickbar_start  = 0; 
var quickbar_anzahl = 0; 
var hoehe = 80;
var quickbar_aufruf = false;
      
function onLoadActionQuickbar(){
  loadQuickbar(0,5);
  installListenerQuickbar();
};

function installListenerQuickbar(){
//	console.log('installListenerQuickbar');

  $('#nextvideosquickbar').bind('click', nextVideosQuickbar);
  $('#prevvideosquickbar').bind('click', previousVideosQuickbar);
  
};

function uninstallListenerQuickbar(){
  $('#nextvideosquickbar').unbind('click', nextVideosQuickbar);
  $('#prevvideosquickbar').unbind('click', previousVideosQuickbar);
};


function responseQuickbar(ajax){

  var out = JSON.parse(ajax.getText());

  quickbar_video  = out['video'];
  quickbar_gesamt = out['meta']['gesamt'];
  quickbar_anzahl += out['meta']['anzahl'];

  if (quickbar_gesamt == 0) {
    mediabox = 'quickbar';
    mediabox_filter = get_ort;
    onLoadActionQuickbar();
    return;
  }


  var ausgabebereich_quickbar = document.getElementById("quickbar_content");

  var div_main, a, img, titel, dauer, i;

  div_main  = document.createElement('div');
  a         = document.createElement('a');
  img       = document.createElement('img');
  titel     = document.createElement('span');
  dauer     = document.createElement('span');

  div_main.className = 'videobox videobox_quickbar';
  titel.className = 'titel';
  dauer.className = 'dauer';

  a.appendChild(img);
  div_main.appendChild(a);
  textnode_titel = document.createTextNode('');
  textnode_dauer = document.createTextNode('');
  titel.appendChild(textnode_titel);
  dauer.appendChild(textnode_dauer);

  div_main.appendChild(titel);
  div_main.appendChild(dauer);

  var text_titel, text_dauer, textnode_titel, textnode_dauer;

  var div_test = document.createDocumentFragment()

  for(i in quickbar_video) {
    if(quickbar_video.hasOwnProperty(i)){

        text_titel = secra.Secrautility.html_entity_decode(quickbar_video[i]['titel']);
        text_dauer = secra.Secrautility.html_entity_decode(quickbar_video[i]['dauer']);
        textnode_titel.data = text_titel;
        textnode_dauer.data = text_dauer;

        titel.replaceChild(textnode_titel, titel.firstChild);
        dauer.replaceChild(textnode_dauer, dauer.firstChild);

        img.setAttribute('src', quickbar_video[i]['img']);
        img.setAttribute('alt', quickbar_video[i]['titel']);
        a.href = "index.php?vnr="+quickbar_video[i]['link']+"&ort="+quickbar_video[i]['ort'];
        a.setAttribute('title', text_titel+' betrachten');

        div_test.appendChild(div_main.cloneNode(true));
    }
  }

  ausgabebereich_quickbar.appendChild(div_test);

};


function loadQuickbar(start_quickbar,anzahl_quickbar){

  var ort_exist = mediabox_filter != null && mediabox_filter != '';

  var ajax = new secra.AjaxHandler('quickbarloader');
  ajax.method = 'POST';
  var adresse;
  
  adresse = 'ajax/ajax_quickbar.php?action=load&startvideo='+start_quickbar+'&anzahlvideo='+anzahl_quickbar;

  if (ort_exist) {
    adresse += '&filter='+mediabox_filter;
  }

  adresse += '&type='+mediabox;

  ajax.url    = adresse;
  ajax.async  = true;
  ajax.callbackfunc = {};
  ajax.callbackfunc['state4'] = responseQuickbar;
  ajax.registerCallback(ajax.callbackfunc, 'html');
  ajax.submit();

};

function nextVideosQuickbar(e){
	
  uninstallListenerQuickbar();
  
  var step = 2;
  
  if(quickbar_anzahl + step > quickbar_gesamt){
    step = quickbar_gesamt - quickbar_anzahl;
  }
  quickbar_start = quickbar_anzahl;
	
  var diff;
  
  if(step > 0){
    $("#quickbar_content").height($("#quickbar_content").height() + hoehe*step);
    diff = hoehe*step;
  } else {
    diff = hoehe*3;
    if(diff > $("#quickbar_content").position().top + $("#quickbar_content").height() - $("#quickbar_port").height()){
      diff = $("#quickbar_content").position().top + $("#quickbar_content").height() - $("#quickbar_port").height();
    }
  }

  if($("#quickbar_content").position().top + $("#quickbar_content").height() >= $("#quickbar_port").height()){
	  $("#quickbar_content").animate({'top': $("#quickbar_content").position().top - diff}, {complete: installListenerQuickbar});
  } else {
    installListenerQuickbar();
  }

  if(step>0){
	  loadQuickbar(quickbar_start,step);
  }
};

function previousVideosQuickbar(e){
  
  uninstallListenerQuickbar();
  
  var stepw = hoehe*3;
  
  if($("#quickbar_content").position().top + stepw >= 0){
    newpos = 0;
  } else {
    newpos = $("#quickbar_content").position().top + stepw;
  }
  $("#quickbar_content").animate({'top': newpos}, {complete: installListenerQuickbar});  
  
};

//Handler.add(window, 'load', onLoadActionQuickbar);
//Handler.add(window, 'load', installListenerQuickbar);
