// JavaScript Document
var csangolivewindow;

function OpenLiveRadio()
{
  livediv = document.getElementById('liveradioctrls');
  //livediv.style.display = 'block';
}

function StartLiveRadio(player) {
  if(player == "java") {
    try {
      csangolivewindow.close();
    }
    catch (err) {
    }
    csangolivewindow = window.open("livecsango/java/csangolive.html", "csangolive", 'location=no,scrollbars=yes,toolbar=no,menubar=no,width=350,height=370');
  }
  else if(player == "sonic"){
    try {
      csangolivewindow.close();
    }
    catch (err) {
    }
    csangolivewindow = window.open("livecsango/sonic/index.php", "csangolive", 'location=no,scrollbars=yes,toolbar=no,menubar=no,width=1030,height=700');
  }
  else if(player == "link"){
    window.location="http://92.114.63.27:7300/listen.pls";
   }
    
  livediv = document.getElementById('liveradioctrls');
  //livediv.style.display = 'none';

}

function ShowHideGbForm()
{
  formdiv = document.getElementById('gbform');
  if(formdiv.style.display == 'none')
    formdiv.style.display='block';
  else
    formdiv.style.display='none';
}

var fromBoxArray = new Array();
var toBoxArray = new Array();
var selectBoxIndex = 0;
var arrayOfItemsToSelect = new Array();

function moveSingleElement()
{
  var selectBoxIndex = this.parentNode.parentNode.id.replace(/[^\d]/g,'');
  var tmpFromBox;
  var tmpToBox;
  if(this.tagName.toLowerCase()=='select'){     
    tmpFromBox = this;
    if(tmpFromBox==fromBoxArray[selectBoxIndex])tmpToBox = toBoxArray[selectBoxIndex]; else tmpToBox = fromBoxArray[selectBoxIndex];
  }else{
  
    if(this.value.indexOf('>')>=0){
      tmpFromBox = fromBoxArray[selectBoxIndex];
      tmpToBox = toBoxArray[selectBoxIndex];      
    }else{
      tmpFromBox = toBoxArray[selectBoxIndex];
      tmpToBox = fromBoxArray[selectBoxIndex];  
    }
  }
  
  for(var no=0;no<tmpFromBox.options.length;no++){
    if(tmpFromBox.options[no].selected){
      tmpFromBox.options[no].selected = false;
      tmpToBox.options[tmpToBox.options.length] = new Option(tmpFromBox.options[no].text,tmpFromBox.options[no].value);
      
      for(var no2=no;no2<(tmpFromBox.options.length-1);no2++){
        tmpFromBox.options[no2].value = tmpFromBox.options[no2+1].value;
        tmpFromBox.options[no2].text = tmpFromBox.options[no2+1].text;
        tmpFromBox.options[no2].selected = tmpFromBox.options[no2+1].selected;
      }
      no = no -1;
      tmpFromBox.options.length = tmpFromBox.options.length-1;
                    
    }     
  }
  
  
  var tmpTextArray = new Array();
  for(var no=0;no<tmpFromBox.options.length;no++){
    tmpTextArray.push(tmpFromBox.options[no].text + '___' + tmpFromBox.options[no].value);      
  }
  tmpTextArray.sort();
  var tmpTextArray2 = new Array();
  for(var no=0;no<tmpToBox.options.length;no++){
    tmpTextArray2.push(tmpToBox.options[no].text + '___' + tmpToBox.options[no].value);     
  }   
  tmpTextArray2.sort();
  
  for(var no=0;no<tmpTextArray.length;no++){
    var items = tmpTextArray[no].split('___');
    tmpFromBox.options[no] = new Option(items[0],items[1]);
    
  }   
  
  for(var no=0;no<tmpTextArray2.length;no++){
    var items = tmpTextArray2[no].split('___');
    tmpToBox.options[no] = new Option(items[0],items[1]);     
  }
}

function sortAllElement(boxRef)
{
  var tmpTextArray2 = new Array();
  for(var no=0;no<boxRef.options.length;no++){
    tmpTextArray2.push(boxRef.options[no].text + '___' + boxRef.options[no].value);     
  }   
  tmpTextArray2.sort();   
  for(var no=0;no<tmpTextArray2.length;no++){
    var items = tmpTextArray2[no].split('___');
    boxRef.options[no] = new Option(items[0],items[1]);     
  }   
  
}
function moveAllElements()
{
  var selectBoxIndex = this.parentNode.parentNode.id.replace(/[^\d]/g,'');
  var tmpFromBox;
  var tmpToBox;   
  if(this.value.indexOf('>')>=0){
    tmpFromBox = fromBoxArray[selectBoxIndex];
    tmpToBox = toBoxArray[selectBoxIndex];      
  }else{
    tmpFromBox = toBoxArray[selectBoxIndex];
    tmpToBox = fromBoxArray[selectBoxIndex];  
  }
  
  for(var no=0;no<tmpFromBox.options.length;no++){
    tmpToBox.options[tmpToBox.options.length] = new Option(tmpFromBox.options[no].text,tmpFromBox.options[no].value);     
  } 
  
  tmpFromBox.options.length=0;
  sortAllElement(tmpToBox);
  
}


/* This function highlights options in the "to-boxes". It is needed if the values should be remembered after submit. Call this function onsubmit for your form */
function multipleSelectOnSubmit()
{
  for(var no=0;no<arrayOfItemsToSelect.length;no++){
    var obj = arrayOfItemsToSelect[no];
    for(var no2=0;no2<obj.options.length;no2++){
      obj.options[no2].selected = true;
    }
  }
  
}

function createMovableOptions(fromBox,toBox,totalWidth,totalHeight,labelLeft,labelRight)
{   
  fromObj = document.getElementById(fromBox);
  toObj = document.getElementById(toBox);
  
  arrayOfItemsToSelect[arrayOfItemsToSelect.length] = toObj;

  
  fromObj.ondblclick = moveSingleElement;
  toObj.ondblclick = moveSingleElement;

  
  fromBoxArray.push(fromObj);
  toBoxArray.push(toObj);
  
  var parentEl = fromObj.parentNode;
  
  var parentDiv = document.createElement('DIV');
  parentDiv.className='multipleSelectBoxControl';
  parentDiv.id = 'selectBoxGroup' + selectBoxIndex;
  parentDiv.style.width = totalWidth * 2 + 35 + 'px';
  parentDiv.style.height = totalHeight + 'px';
  parentEl.insertBefore(parentDiv,fromObj);
  
  
  var subDiv = document.createElement('DIV');
  subDiv.style.width = totalWidth + 'px';
  fromObj.style.width = totalWidth - 2 + 'px';

  var label = document.createElement('SPAN');
  label.innerHTML = labelLeft;
  subDiv.appendChild(label);
  
  subDiv.appendChild(fromObj);
  subDiv.className = 'multipleSelectBoxDiv';
  parentDiv.appendChild(subDiv);
  
  var buttonDiv = document.createElement('DIV');
  buttonDiv.style.verticalAlign = 'middle';
  buttonDiv.style.paddingTop = (totalHeight/2) - 50 + 'px';
  buttonDiv.style.paddingLeft = '2px';
  buttonDiv.style.paddingRight = '2px';
  buttonDiv.style.width = '30px';
  buttonDiv.style.textAlign = 'center';
  parentDiv.appendChild(buttonDiv);
  
  var buttonRight = document.createElement('INPUT');
  buttonRight.type='button';
  buttonRight.value = '>';
  buttonRight.tabindex='2';
  buttonDiv.appendChild(buttonRight); 
  buttonRight.onclick = moveSingleElement;  
  
  var buttonAllRight = document.createElement('INPUT');
  buttonAllRight.type='button';
  buttonAllRight.value = '>>';
  buttonRight.tabindex='3';
  buttonAllRight.onclick = moveAllElements;
  buttonDiv.appendChild(buttonAllRight);    
  
  var buttonLeft = document.createElement('INPUT');
  buttonLeft.style.marginTop='10px';
  buttonLeft.type='button';
  buttonLeft.value = '<';
  buttonRight.tabindex='4';
  buttonLeft.onclick = moveSingleElement;
  buttonDiv.appendChild(buttonLeft);    
  
  var buttonAllLeft = document.createElement('INPUT');
  buttonAllLeft.type='button';
  buttonAllLeft.value = '<<';
  buttonRight.tabindex='5';
  buttonAllLeft.onclick = moveAllElements;
  buttonDiv.appendChild(buttonAllLeft);
  
  var subDiv = document.createElement('DIV');
  subDiv.style.width = totalWidth + 'px';
  toObj.style.width = totalWidth - 2 + 'px';

  var label = document.createElement('SPAN');
  label.innerHTML = labelRight;
  subDiv.appendChild(label);
      
  subDiv.appendChild(toObj);
  parentDiv.appendChild(subDiv);    
  
  toObj.style.height = (totalHeight - label.offsetHeight) + 'px';
  fromObj.style.height = (totalHeight - label.offsetHeight) + 'px';

    
  selectBoxIndex++;
  
}

function initAlbumFiles(arrayindex, albumid, fileid){
  //alert(arrayindex);
  //alert(albumid);
  //alert(fileid);
  loadMediaFiles(arrayindex, albumid);
  //alert(mediaalbumarray[arrayindex][1]);
  $('.albumtitle a').html(mediaalbumarray[arrayindex][1] + " - " + mediafilearray[albumid][fileid][1]);
  addFilesItemUnbind();  
  startMediaFile(arrayindex, albumid, fileid);
}

function addAlbumItemUnbind() {
  $('.album-list li')
    .unbind('click')
    .bind('click', function() {
      //alert($(this).html());
      var listItem = document.getElementById($(this).html());
      //alert($('li').index(listItem));
      loadMediaFiles($('li').index(listItem), mediaalbumarray[$('li').index(listItem)][0]);
      $('.albumtitle a').html($(this).html());
      addFilesItemUnbind();
  });
}

function addFilesItemUnbind() {
  $('.file-list li')
    .unbind('click')
    .bind('click', function() {
      var embedcode = document.getElementById("embedcode"); 
      embedcode.style.display = 'none';

      var id = $(this).attr('id');
      //alert(id);
      var arrayindex = id.split("-")[0];
      var albumid = id.split("-")[1];
      var fileid = id.split("-")[2];
      $('.albumtitle a').html(mediaalbumarray[arrayindex][1] + " - " + mediafilearray[albumid][fileid][1]);
      startMediaFile(arrayindex, albumid, fileid);
    })
}

function addEmbedItemUnbind(arrayindex, albumid, fileid){
  var embeddiv = document.getElementById("embeddiv"); 
  embeddiv.style.display = 'block';
  
  $('.mediaembed img')
    .unbind('click')
    .bind('click', function() {
      var embedcode = document.getElementById("embedcode"); 
      embedcode.style.display = 'block';

      var serverroot = window.location.protocol + '//' + window.location.hostname + '/';
      var params = window.location.search.split("&");
      var link = serverroot + "media.php" + params[0] + "&" + params[1] + "&autoplay&id="+arrayindex+"-"+albumid+"-"+fileid;
      $('#txtlinktomedia').attr('value', link);
      
      //alert(window.location);
      //mediafilearray[albumid][fileid][4]
      //alert(serverroot);
      var embed = '<object type="application/x-shockwave-flash" id="player1" name="player1" data="'+serverroot+'player.swf" width="320" height="20"><param name="allowscriptaccess" value="always"><param name="flashvars" value="file='+serverroot+'media/'+mediafilearray[albumid][fileid][4]+'&amp;autostart=false"></object>'
      $('#txtembedcode').attr('value', embed);
    })
}

function deletePlayer(theWrapper, thePlaceholder, thePlayerId) { 
        swfobject.removeSWF(thePlayerId);
        var tmp=document.getElementById(theWrapper);
        if (tmp) { tmp.innerHTML = "<div id=" + thePlaceholder + "></div>"; }

  var embeddiv = document.getElementById("embeddiv"); 
  embeddiv.style.display = 'none';

}


function createPlayer(thePlaceholder, thePlayerId, theFile) {
        var flashvars = {
                file:theFile, 
                autostart:"true"
        }
        var params = {
                allowfullscreen:"true", 
                allowscriptaccess:"always"
        }
        var attributes = {
                id:thePlayerId,  
                name:thePlayerId
        }
        swfobject.embedSWF("player.swf", thePlaceholder, "320", "20", "9.0.115", false, flashvars, params, attributes);
}


function initPlayer(theFile) { 
        deletePlayer('playerdiv', 'flashContent', 'player1'); 
        createPlayer('flashContent', 'player1', theFile);
}

function loadMediaFiles(arrayindex, albumid) {
  var selectfile = document.getElementById("mediafiles");
  var x;
  chtml = "";
  for(x in mediafilearray[albumid]) {
    chtml = chtml + "<li id='" + arrayindex + "-" + albumid + "-" + x + "'><div class='mediacim'>" + mediafilearray[albumid][x][1] + "</div><div class='mediaszerzo'>" + mediafilearray[albumid][x][0] + "<span class='mediakiadas'>" + mediafilearray[albumid][x][2] + "</span></div><div class='medialeiras'>" + mediafilearray[albumid][x][3] + "</div></li>";
  }
  selectfile.innerHTML = chtml;
}

function startMediaFile(arrayindex, albumid, fileid) {
  initPlayer("media/" + mediafilearray[albumid][fileid][4]);
  addEmbedItemUnbind(arrayindex, albumid, fileid);
}

