/* Ajax objektum meghívása */
function GetXmlHttpObject(handler) {
  var objXMLHttp=null
  if (window.XMLHttpRequest) {
    objXMLHttp=new XMLHttpRequest()
  } else if (window.ActiveXObject) {
    objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
  return objXMLHttp
}

/* Komment doboz */
tinyMCE.init({
	theme : "advanced",
	mode : "none",
	plugins : "inlinepopups,inlinepopupstable,advimage,advlink,emotions,contextmenu,nonbreaking,xhtmlxtras",
	language : "hu",
	theme_advanced_buttons1 : "bold,italic,underline,separator,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,separator,undo,redo,separator,link,unlink,insPicture,charmap,separator,forecolor,fontsizeselect,styleselect,removeformat,cleanup",
	theme_advanced_buttons2 : "",
	theme_advanced_buttons3 : "",
	theme_advanced_toolbar_location : "bottom",
	theme_advanced_toolbar_align : "center",
	theme_advanced_styles : "Idézet=quoteStyle;Kód=codeStyle",
	content_css : "addons/commentbox/bbcode.css",
	entity_encoding : "raw",
	add_unload_trigger : true,
	remove_linebreaks : false,
	inline_styles : false,
	convert_fonts_to_spans : false,
	theme_advanced_resizing : true,
	extended_valid_elements : "a[href],img[src|width|height|align],hr[class|width|size|noshade],font[face|size|color],span[class|align]",
  editor_selector : "mceNormal",
  setup : function(ed) {
    ed.addButton('insPicture', {
      title : 'Kép beszúrása',
      image : 'addons/commentbox/themes/advanced/img/insPicture.gif',
      onclick : function() {
        var IMAGE;
        if(IMAGE = prompt("Írd be a kép elérését!","http://")){
          if (! IMAGE || IMAGE=="http://"){
            alert("Nem adtad meg a kép elérését!");
          }
          else
          {
            ed.selection.setContent('<a href="'+IMAGE+'"><img src="images/linked_picture.png"></a><br>');
          }
        }
      }
    });
  }
});


/* Ajax küldő */
function send(url,method,data,action){
  this.action = action;
  uniHttp=GetXmlHttpObject();
  if (uniHttp==null) {
    alert ("A böngészője nem támogatja az Ajax technológiát! Használjon újabb verziót!");
    return;
  }
  if(method=="GET"||method=="get"){
    uniHttp.onreadystatechange=universalState
    uniHttp.open("GET",url+'?'+data,true)
    uniHttp.send(null)
  }
  else
  {
    uniHttp.onreadystatechange=universalState
    uniHttp.open("POST",url,true)
    uniHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    uniHttp.setRequestHeader("Content-length", data.length);
    uniHttp.setRequestHeader("Connection", "close");
    uniHttp.send(data);
  }
}
function universalState(){
  if (uniHttp.readyState==4 || uniHttp.readyState=="complete"){
    var response = uniHttp.responseText;
    if(action!=undefined)
      eval(action+"('complete',response);");
  }
  else
  {
    eval(action+"('loading');");
  }
}
function encodeToSend(txt){
  txt=txt.replace(/[+]/g, "(plus)");
  txt=encodeURI(txt);
  return txt;
}
function decodeToSend(txt){
  txt=txt.replace(/[+]/g, "(plus)");
  txt=encodeURI(escape(txt));
  return txt;
}

/* Tálca műveletek */
function trayAdd() {
  send('ajax_tray.php','POST','function=addTray&hash='+decodeToSend($('hash').value)+'&command='+decodeToSend($('command').value)+'&directlink='+decodeToSend($('directlink').value),'trayAddState');
}
function trayAddState(state,response){
  if(state=='loading'){
    $('fixdiv').style.display='';
  }
  if(state=='complete'){
    if(response!='FULL'){
      $('popup').style.display='none';
      $('popup').innerHTML='';
      $('overlay').style.display='none';
      
      $('fixdiv').style.display='none';
      if($('tray').style.display=='none'){
        trayGet('true');
      }
      else
      {
        trayRefresh('true');
      }
    }
    else
    {
      $('infopopup').innerHTML='<img src="style/base/icon_delete.png" align="left"><font color="red"><b>&nbsp;A tálcádra már nem fér több elem!</b></font>';
      $('infopopup').style.display='';
      setTimeout('$("infopopup").style.display="none";',4000);
      $('fixdiv').style.display='none';
    }
  }
}

function trayGet(light,mode) {
  if(mode=='loadpage') {
    send('ajax_tray.php','GET','function=getTray&light='+light,'trayGetNoAniState');
  }
  else
  {
    send('ajax_tray.php','GET','function=getTray&light='+light,'trayGetState');
    trayGetMore();
  }
}
function trayGetState(state,response){
  if(state=='loading'){
    document.getElementById('fixdiv').style.display='';
  }
  if(state=='complete'){
    document.getElementById('fixdiv').style.display='none';
    document.getElementById('tray').innerHTML=response;
    new Effect.Opacity('tray', {duration:1, from:0, to:1.0});
    document.getElementById('tray').style.display='';
    Sortable.create('tray_items',{overlap:'horizontal',ghosting:false,constraint:'horizontal',
    onUpdate:function(sortable){traySerialize(Sortable.serialize(sortable))}});
    if($('lighting').value!='false'){
      new Effect.Highlight($('lighting').value, { startcolor: '#ae9457', endcolor: '#545557' });
    }
  }
}
function trayGetNoAniState(state,response){
  if(state=='loading'){
    document.getElementById('fixdiv').style.display='';
  }
  if(state=='complete'){
    document.getElementById('fixdiv').style.display='none';
    document.getElementById('tray').innerHTML=response;
    document.getElementById('tray').style.display='';
    Sortable.create('tray_items',{overlap:'horizontal',ghosting:false,constraint:'horizontal',
    onUpdate:function(sortable){traySerialize(Sortable.serialize(sortable))}});
  }
}

function trayRefresh(light) {
  send('ajax_tray.php','GET','function=getTray&light='+light,'trayRefreshState');
}
function trayRefreshState(state,response){
  if(state=='loading'){
    document.getElementById('fixdiv').style.display='';
  }
  if(state=='complete'){
    document.getElementById('fixdiv').style.display='none';
    document.getElementById('tray').innerHTML=response;
    if($('lighting').value!='false'){
      new Effect.Highlight($('lighting').value, { startcolor: '#ae9457', endcolor: '#545557' });
    }
    Sortable.create('tray_items',{overlap:'horizontal',ghosting:false,constraint:'horizontal',
    onUpdate:function(sortable){traySerialize(Sortable.serialize(sortable))}});
  }
}

function traySerialize(serial){
  send('ajax_tray.php','POST','function=serializeTray&serial='+decodeToSend(serial),'traySerializeState');
}
function traySerializeState(state,response){
  if(state=='complete'){
    trayRefresh();
  }
}

function trayClose() {
  Sortable.destroy('tray_items');
  new Effect.Opacity(document.getElementById('tray'),{from:1.0, to:0});
  new Effect.Opacity(document.getElementById('tray_torrents'),{from:1.0, to:0});
  setTimeout("document.getElementById('tray').style.display='none'; document.getElementById('tray_torrents').style.display='none';",1000);
  send('ajax_tray.php','GET','function=closeTray','trayCloseState');
}
function trayCloseState(state,response){
  //Nincs művelet
}


function trayDel(id) {
  send('ajax_tray.php','POST','function=delTray&id='+id,'trayDelState');
  new Effect.Highlight($('item_'+id), { startcolor: '#7e7f81', endcolor: '#c43f3f', restorecolor: '#c43f3f' });
}
function trayDelState(state,response){
  if(state=='complete'){
    setTimeout("trayRefresh();",1000);
  }
}

function trayFix() {
  send('ajax_tray.php','POST','function=fixTray','trayFixState');
}
function trayFixState(state,response){
  if(state=='complete'){
    trayRefresh();
  }
}

function trayTorrents(){
  if(document.getElementById('tray_torrents').style.display!=''){
    document.getElementById('tray_torrents').style.bottom=document.getElementById('tray').offsetHeight+'px';
    send('ajax_tray.php','GET','function=getTorrents','trayTorrentsState');
  }
  else
  {
    document.getElementById('tray_torrents').style.display='none';
    document.getElementById('tray_torrents').innerHTML='';
  }
}
function trayTorrentsState(state,response){
  if(state=='loading'){
    document.getElementById('fixdiv').style.display='';
  }
  if(state=='complete'){
    document.getElementById('tray_torrents').innerHTML=response;
    document.getElementById('tray_torrents').style.display='';
    document.getElementById('fixdiv').style.display='none';
  }
}