var detailed = new Array();
var mailsgot = new Array();
var checked = new Array();
var action = 0;
var currentstyle = false;
var buttonstates = new Array();
var wait = false;
var ptimer = 0;
var dcTime = 250; // doubleclick time
var dcDelay = 100; // no clicks after doubleclick
var dcAt = 0; // time of doubleclick
var savEvent = null; // save Event for handling doClick().
var savEvtTime = 0; // save time of click event.
var savTO = null; // handle of click setTimeOut
var upl_frm_inited = false;
var states = new Cookie("states", 30 * 24 * 60, "/");

$(document).ready(function(){
  // fancybox
  $(".fancybox").fancybox({
    frameHeight: 1000
  });
  // warn
  if (loggedin) pagetimer('start');
  pulsatewarn();

  // infopanel hide/show
  $('#togglebutton').click(function(){
    if ($('#infopanel').css("display") == 'block'){
      $('#infopanel').fadeTo('slow', 0);
      $("#infopanel").slideUp('fast', function(){
        $("#infopanel").height('');
        states.SetItem('infopanel', 1);
      });
    }else{
      $("#infopanel").slideDown("fast");
      $("#infopanel").fadeTo("slow", 1);
      states.SetItem('infopanel', 0);
    }
    return false;
  });
  // profile showxxx
  $("#c30").change(function(){
    $("#pinc").val(prompt('A változás megerősítéséhez írd be a regisztrációkor e-malben kiküldött PIN kódodat.'));
	});
});

function clickevent(which, func){
  fteonce = func;
  switch (which){
  case "click":
    var d = new Date();
    var now = d.getTime();
    if ((now - dcAt) < dcDelay) return false;
    savEvent = which;
    d = new Date();
    savEvtTime = d.getTime();
    savTO = setTimeout("doClick()", dcTime);
    break;
  case "dblclick":
    doDoubleClick();
    break;
  default:
  }
}

function doClick(){
  if (savEvtTime - dcAt <= 0) return false;
  setTimeout(fteonce, 1);
}

function doDoubleClick(){
  var d = new Date();
  dcAt = d.getTime();
  if (savTO != null){
    clearTimeout(savTO);
    savTO = null;
  }
  setTimeout(fteonce, 1);
}

function pagetimer(action){
  if (action == 'start'){
    ptimer = setTimeout("timeout()", (25 * 60 * 1000));
  } else if (action == 'restart'){
    clearTimeout(ptimer);
    pagetimer('start');
  } else if (action == 'stop'){
    clearTimeout(ptimer);
  }
}

function timeout(){
  window.location.reload();
}

function pulsatewarn(){
  action = 1;
  if ($("#warn").css("display") != undefined && $('#infopanel').css("display") == 'block'){
    $('#warn').Pulsate(300, 4, function(){
      action = 0;
    });
  }else{
    action = 0;
  }
}

function cursors_wait(id){
  $('a').css('cursor', 'wait');
  document.body.style.cursor = 'wait';
}

function cursors_normal(id){
  $('a').css('cursor', 'pointer');
  document.body.style.cursor = 'default';
}

function collapse(id){
  $('#article' + id).toggle();
  if ($.id('article' + id).style.display == 'none') $('#button' + id).attr('src', $('#button' + id).attr('src').replace('minus', 'plus'));
  else $('#button' + id).attr('src', $('#button' + id).attr('src').replace('plus', 'minus'));
}

function updatelastbrowse(){
  cursors_wait();
  $.post('ajax/update.php', function(data, textStatus){
    pagetimer('restart');
    $('.flag_new').hide();
    cursors_normal();
  });
  return false;
}

function bookmarkit(id, page){
  cursors_wait();
  $.post('ajax/bmadd.php', {
    'id': id
  }, function(res, textStatus){
    pagetimer('restart');
    cursors_normal();
    if (res == 'full'){
      alert('A könyvjelzőid mappája megtelt!');
    }else{
      $('#iba' + id).hide();
      $('#ibd' + id).show();
      refresh_bookmarks(page);
    }
  });
  return false;
}

function delbookmark(id, page){
  cursors_wait();
  $.post('ajax/bmdel.php', {
    id: id
  }, function(data, textStatus){
    pagetimer('restart');
    if ($.id('ibd' + id)) $('#ibd' + id).hide();
    if ($.id('iba' + id)) $('#iba' + id).show();
    cursors_normal();
    refresh_bookmarks(page);
  });
  return false;
}

function refresh_bookmarks(page){
  cursors_wait();
  $('#wrapper').hide();
  $('#wrapper').load('ajax/bm.php', {
    page: page
  }, function(responseText, textStatus, XMLHttpRequest){
    pagetimer('restart');
    $('#wrapper').show();
    cursors_normal();
    if (page) set_cookie('bookmarkspage', page, 43200, '', '', '');
  });
  return false;
}

function tdetails(id, reload){
  var d = detailed[id];
  if (d < 0) return false;
  detailed[id] = -1;
  if (d > 0 && reload != 1){
    if (d == 1){
      $('#c' + id).slideUp(400, function(){
        $('#d' + id).hide();
        detailed[id] = 2;
      });
    }else{
      $('#d' + id).show();
      $('#c' + id).slideDown(400, function(){
        detailed[id] = 1;
      });
    }
  }else{
    cursors_wait();
    $('#c' + id).load('browse.php', {
      details: id
    }, function(responseText, textStatus, XMLHttpRequest){
      pagetimer('restart');
      if (reload != 1){
        $('#d' + id).show();
        $('#c' + id).slideDown(400, function(){
          detailed[id] = 1;
        });
      }
      cursors_normal();
    });
  }
  return false;
}

function rdetails(id, reload){
  var d = detailed[id];
  if (d < 0) return false;
  detailed[id] = -1;
  if (d > 0 && reload != 1){
    if (d == 1){
      $('#c' + id).slideUp(400, function(){
        $('#d' + id).hide();
        detailed[id] = 2;
      });
    }else{
      $('#d' + id).show();
      $('#c' + id).slideDown(400, function(){
        detailed[id] = 1;
      });
    }
  }else{
    cursors_wait();
    $('#c' + id).load('request.php', {
      details: id
    }, function(responseText, textStatus, XMLHttpRequest){
      //pagetimer('restart');
      if (reload != 1){
        $('#d' + id).show();
        $('#c' + id).slideDown(400, function(){
          detailed[id] = 1;
        });
      }
      cursors_normal();
    });
  }
  return false;
}

function hidetorrent(id, onbrowse){
  cursors_wait();
  if (confirm('Biztosan megváltoztatod a torrent állapotát?')){
	$('#ch' + id).load('ajax/hide.php', {id: id}, function(res, textStatus, XMLHttpRequest){
		pagetimer('restart');
		if (res != 'error' && onbrowse == 1) $('#ih' + id).toggle();
	});
  }
  cursors_normal();
  return false;
}

function lockcomments(id, onbrowse){
  cursors_wait();
  $('#cl' + id).load('ajax/lock.php', {id: id}, function(res, textStatus, XMLHttpRequest){
    pagetimer('restart');
    if (res != 'error' && onbrowse == 1) $('#il' + id).toggle();
  });
  cursors_normal();
  return false;
}

function checktorrent(id, onbrowse){
  if (checked[id]){
    gotouser(checked[id]);
  }else{
    cursors_wait();
    $.post('ajax/check.php', {id: id}, function(res){
      pagetimer('restart');
      if (res == 'error'){
        alert('Hiba!');
      }else{
        result = res.split('|');
        if (result[0] != 'checked') alert('A torrentet már ellenőrizték!');
        if (onbrowse == 1){
          if (result[0] == 'checked'){
            $('#ic0_' + id).hide();
            $('#ic1_' + id).show();
            $('#ic2_' + id).hide();
          }else{
            if (result[0] == '1'){
              $('#ic0_' + id).hide();
              $('#ic1_' + id).show();
              $('#ic2_' + id).hide();
            }else{
              $('#ic0_' + id).hide();
              $('#ic1_' + id).hide();
              $('#ic2_' + id).show();
            }
          }
          $('#cc' + id).html(result[2] + ' ellenőrizte');
          checked[id] = result[1];
          cursors_normal();
        }else{
          checked[id] = result[1];
          cursors_normal();
          alert('Sikeresen ellenőrizve!');
        }
      }

    });
  }
  return false;
}

function gotouser(id){
  window.location = "userdetails.php?uid=" + id;
  return false;
}

function thankyou(id){
  cursors_wait();
  $.post('ajax/thankyou.php', {
    id: id
  }, function(res, textStatus){
    pagetimer('restart');
    if (res == 'error') alert('Már megköszönted! Várj 1-2 percet, amíg az oldalon is megjelenik.');
    else $('#thanks-'+id).html(res);
    cursors_normal();
  });
  return false;
}

function report(type,id){
  problem = prompt("Bejelenteni kívánt probléma:", "");
  if (problem == undefined) return false;
  if (problem.length > 5 & problem.length < 300){
    cursors_wait();
    $.post('ajax/report.php', {
      problem: problem,
      type: type,
      id: id
    }, function(res, textStatus){
      pagetimer('restart');
      if (res == 'error'){
        alert('Hiba!');
      }else{
        alert('Jelentésedet regisztráltuk!');
        $('#report').hide();
      }
      cursors_normal();
    });
  }else{
    alert('Minimum 5, maximum 300 karakter írható be!');
  }
  return false;
}

$.id = function(id){
  if (document.getElementById){
    var element = document.getElementById(id);
  } else if (document.all){
    var element = document.all[id];
  } else if (document.layers){
    var element = document.layers[id];
  }
  return (element);
}

function get_cookie(name){
  var start = document.cookie.indexOf(name + "=");
  var len = start + name.length + 1;
  if ((!start) && (name != document.cookie.substring(0, name.length))){
    return null;
  }
  if (start == -1) return null;
  var end = document.cookie.indexOf(";", len);
  if (end == -1) end = document.cookie.length;
  return unescape(document.cookie.substring(len, end));
}

function set_cookie(name, value, expires, path, domain, secure){
  // set time, it's in milliseconds
  var today = new Date();
  today.setTime(today.getTime());
  if (expires){
    expires = expires * 1000 * 60;
  }
  var expires_date = new Date(today.getTime() + (expires));
  document.cookie = name + "=" + escape(value) + ((expires) ? ";expires=" + expires_date.toGMTString() : "") + //expires.toGMTString()
  ((path) ? ";path=" + path: "") + ((domain) ? ";domain=" + domain: "") + ((secure) ? ";secure": "");
}

function delete_cookie(name, path, domain){
  if (get_cookie(name)) document.cookie = name + "=" + ((path) ? ";path=" + path: "") + ((domain) ? ";domain=" + domain: "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function Cookie(name, duration, path, domain, secure){
  this.affix = "";
  if (duration){
    var date = new Date();
    date.setTime(date.getTime() + (1000 * 60 * duration));
    this.affix = "; expires=" + date.toGMTString();
  }
  if (path) this.affix += "; path=" + path;
  if (domain) this.affix += "; domain=" + domain;
  if (secure) this.affix += "; secure=" + secure;
  function getValue(){
    var m = document.cookie.match(new RegExp("(" + name + "=[^;]*)(;|$)"));
    return m ? m[1] : null;
  }
  this.cookieExists = function(){
    return getValue() ? true: false;
  }
  this.Delete = function(){
    document.cookie = name + "=noop; expires=Thu, 01-Jan-1970 00:00:01 GMT";
  }
  this.SetItem = function(key, value){
    var ck = getValue();
    if (/[;, ]/.test(value)){
      //Mac IE doesn't support encodeURI
      value = window.encodeURI ? encodeURI(value) : escape(value);
    }
    if (value){
      var attrPair = "@" + key + value;
      if (ck){
        if (new RegExp("@" + key).test(ck)){
          document.cookie = ck.replace(new RegExp("@" + key + "[^@;]*"), attrPair) + this.affix;
        }else{
          document.cookie = ck.replace(new RegExp("(" + name + "=[^;]*)(;|$)"), "$1" + attrPair) + this.affix;
        }
      }else{
        document.cookie = name + "=" + attrPair + this.affix;
      }
    }else{
      if (new RegExp("@" + key).test(ck)){
        document.cookie = ck.replace(new RegExp("@" + key + "[^@;]*"), "") + this.affix;
      }
    }
  }
  this.GetItem = function(key){
    var ck = getValue();
    if (ck){
      var m = ck.match(new RegExp("@" + key + "([^@;]*)"));
      if (m){
        var value = m[1];
        if (value){
          //Mac IE doesn't support decodeURI
          return window.decodeURI ? decodeURI(value) : unescape(value);
        }
      }
    }
  }
}

function hide(obj, a, neg, noc){
  if (action != 0) return false;
  action = 1;
  obj_style = $.id(obj).style;
  if (obj_style.display != "none"){
    if (obj == 'infopanel'){
      $('#infopanel').hide();
      action = 0;
    }else{
      $('#' + obj).hide();
      action = 0;
    }
    if (obj != 'infopanel') a.parentNode.className = a.parentNode.className.replace(/expanded$/, 'collapsed');
    if (noc != 1) states.SetItem(obj, (neg == 1) ? null: 1);
  }else{
    if (obj == 'infopanel'){
      $('#infopanel').show();
      action = 0;
    }else{
      $('#' + obj).show();
      action = 0;
    }
    if (obj != 'infopanel') a.parentNode.className = a.parentNode.className.replace(/collapsed$/, 'expanded');
    if (noc != 1) states.SetItem(obj, (neg == 1) ? 1: null);
  }
  return false;
}

function set_avatar(pic){
  var baseurl = pic.split("?");
  var urlparts = baseurl[0].split(".");
  var extension = urlparts[urlparts.length - 1].toLowerCase();
  var allowed = new Array("jpg", "gif", "png", "bmp", "php");
  var imgok = false;
  for (ext in allowed) if (extension == allowed[ext]) imgok = true;
  if (imgok == true){
    var tester = new Image();
    tester.onload = function(){
      if (tester.width <= 170){
        if (tester.height <= 320){
          $('#av_pre').attr('src', pic);
        }else{
          alert('A kép túl magas, nem lehet 320 pixelnél magasabb!');
        }
      }else{
        alert('A kép túl széles, nem lehet 170 pixelnél szélesebb!');
      }
    }
    tester.onerror = function(){
      alert('A megadott kép nem létezik, vagy nem érvényes!')
    }
    tester.src = pic;
  }else{
    alert("Nem megengedett képformátum! Csak a következőket használhatod: JPG, GIF, PNG, BMP, PHP.");
  }
}

function refr_avatar(action, original, theme){
  if (action == 'reset') $('#av_url').val(original);
  av_image = $('#av_url').val();
  if (av_image == '') set_avatar('themes/' + theme + '/nophoto.png');
  else set_avatar(av_image);
  if (original == av_image) $('#pre_warn').hide();
  else $('#pre_warn').show();
  return false;
}

function changestyle(current, target){
  if (currentstyle != false) current = currentstyle;
  if ($.browser.msie == true && parseFloat($.browser.version) < 7 && target == "moobs-vista"){
    alert("Sajnos ez a stílus nem támogatja az IE6 böngészőt.\nKérjük használj inkább Firefoxot!");
  }else{
    currentstyle = target;
    $('#styleinput').val(target);
    alert("A választott stílus a mentést követően lesz aktív.");
  }
  return false;
}

function gettag(tag, which){
  switch (tag){
  case 'size':
    x = document.getElementById('sizes');
    o = "[size=" + x.options[x.selectedIndex].value + "]";
    c = "[/size]";
    break;
  case 'color':
    x = document.getElementById('colors');
    o = "[color=" + x.options[x.selectedIndex].value + "]";
    c = "[/color]";
    break;
  case 'font':
    x = document.getElementById('fonts');
    o = "[font=" + x.options[x.selectedIndex].value + "]";
    c = "[/font]";
    break;
  case 'bold':
    o = "[b]";
    c = "[/b]";
    break;
  case 'italic':
    o = "[i]";
    c = "[/i]";
    break;
  case 'underline':
    o = "[u]";
    c = "[/u]";
    break;
  case 'overline':
    o = "[o]";
    c = "[/o]";
    break;
  case 'linetrough':
    o = "[x]";
    c = "[/x]";
    break;
  case 'alignleft':
    o = "[left]";
    c = "[/left]";
    break;
  case 'aligncenter':
    o = "[center]";
    c = "[/center]";
    break;
  case 'alignright':
    o = "[right]";
    c = "[/right]";
    break;
  case 'alignjustify':
    o = "[justify]";
    c = "[/justify]";
    break;
  case 'quote':
    o = "[quote]";
    c = "[/quote]";
    break;
  case 'url':
    o = "[url]";
    c = "[/url]";
    break;
  case 'mail':
    o = "[email]";
    c = "[/email]";
    break;
  case 'google':
    o = "[google]";
    c = "[/google]";
    break;
  case 'picture':
    o = "[img]";
    c = "[/img]";
    break;
  case 'flash':
    o = "[swf=400x300]";
    c = "[/swf]";
    break;
  case 'media':
    o = "[wmp=400x300]";
    c = "[/wmp]";
    break;
  }
  if (which == 'o') return o;
  if (which == 'c') return c;
}

function addbbt(tag){
  if (buttonstates[tag] != 1){
    buttonstates[tag] = 1;
    o = gettag(tag, 'o');
    document.forms[form].elements[text].value = document.forms[form].elements[text].value + o;
    $("#"+tag).attr('src', $("#"+tag).attr('src').replace('.png', '_active.png'));
  }else{
    buttonstates[tag] = 0;
    c = gettag(tag, 'c');
    document.forms[form].elements[text].value = document.forms[form].elements[text].value + c;
    $("#"+tag).attr('src', $("#"+tag).attr('src').replace('_active.png', '.png'));
  }
}

function addbbw(prompttext, tag){
  o = gettag(tag, 'o');
  c = gettag(tag, 'c');
  input = prompt(prompttext, "");
  document.forms[form].elements[text].value = document.forms[form].elements[text].value + o + input + c;
}

function smileyselector(){
  link = 'smileyselector.php?form=' + form + '&text=' + text;
  newwin = window.open(link, 'moresmile', 'height=500,width=550,resizable=no,scrollbars=yes');
  if (window.focus) newwin.focus();
}

function addbbm(tag){
  textarea = document.forms[form].elements[text];
  o = gettag(tag, 'o');
  c = gettag(tag, 'c');
  if (!textarea.setSelectionRange){
    var selected = document.selection.createRange().text;
    if (selected.length <= 0) addbbt(tag);
    else document.selection.createRange().text = o + selected + c;
  }else{
    var pretext = textarea.value.substring(0, textarea.selectionStart);
    var codetext = o + textarea.value.substring(textarea.selectionStart, textarea.selectionEnd) + c;
    var posttext = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
    if (codetext == o + c) addbbt(tag);
    else textarea.value = pretext + codetext + posttext;
  }
  textarea.focus();
}

function deltorrent(){
  if (($.id('del_reason').options[$.id('del_reason').selectedIndex].value == 'other') && ($.id('other_del_reason').value == '')){
    alert('Nem írtad be a törlés indokát!');
    return false;
  }else if($.id('warn_reason') != null){
    if (($.id('warn_reason').options[$.id('warn_reason').selectedIndex].value == 'other') && ($.id('other_warn_reason').value == '') && ($.id('warn_uploader').checked)) alert('Nem írtad be a warnolás indokát!');
    else document.torrentdel.submit();
    return false;
  }else{
    document.torrentdel.submit();
  }
  return false;
}

function fpx(obj){
  var curleft = 0;
  if (obj.offsetParent){
    while (obj.offsetParent){
      curleft += obj.offsetLeft;
      obj = obj.offsetParent;
    }
  } else if (obj.x) curleft += obj.x;
  return curleft;
}

function fpy(obj){
  var curtop = 0;
  if (obj.offsetParent){
    while (obj.offsetParent){
      curtop += obj.offsetTop;
      obj = obj.offsetParent;
    }
  } else if (obj.y) curtop += obj.y;
  return curtop;
}

function mailstates(){
  wait = true;
  selectedmail = false;
  mailstates = new Array();
  mailheaders = $('#movemsgs').find('tr').not('.mail').not('.separator').not('.head').not('.').get();
  for (x in mailheaders) mailstates[mailheaders[x].id] = mailheaders[x].className;
  wait = false;
  return false;
}

function highlightmail(id){
  if (selectedmail == false){
    selectedmail = id;
    if ($('#head_' + id).attr('class').match('_u') == null){
      $('#head_' + id).attr('class', $('#head_' + id).attr('class') + "_u");
    }else{
      $('#head_' + id).attr('class', $('#head_' + id).attr('class').replace('_u', ''));
    }
  }else{
    if (selectedmail == id){
      selectedmail = false;
      if ($('#head_' + id).attr('class').match('_u') == null){
        $('#head_' + id).attr('class', $('#head_' + id).attr('class') + "_u");
      }else{
        $('#head_' + id).attr('class', $('#head_' + id).attr('class').replace('_u', ''));
      }
    }else{
      selectedmail = id;
      if ($('#head_' + id).attr('class').match('_u') == null){
        $('#head_' + id).attr('class', $('#head_' + id).attr('class') + "_u");
      }else{
        $('#head_' + id).attr('class', $('#head_' + id).attr('class').replace('_u', ''));
      }
    }
  }
  mailerbuttons();
  wait = false;
  return false;
}

function togglemail(id){
  var m = mailsgot[id];
  if (m < 0) return false;
  cursors_wait();
  mailsgot[id] = -1;
  if (m > 0){
    if (m == 1){
      $('#mail_' + id).slideUp(400, function(){
        mailsgot[id] = 2;
      });
    }else{
      $('#mail_' + id).slideDown(400, function(){
        mailsgot[id] = 1;
      });
    }
  }else{
    $('#content_' + id).load('ajax/getmail.php', {
      id: id
    }, function(responseText, textStatus, XMLHttpRequest){
      pagetimer('restart');
      $.id('head_' + id).className = $.id('head_' + id).className.replace(/_u$/, '');
      mailstates['head_' + id] = mailstates['head_' + id].replace(/_u$/, '');
      $('#mail_' + id).slideDown(400, function(){
        mailsgot[id] = 1;
      });
    });
  }
  cursors_normal();
  return false;
}

function mailactions(action){
  if (wait || !selectedmail) return false;
  wait = true;
  switch (action){
  case 1:
    post_data('mailer.php', 'module:reply;id:' + selectedmail);
    break;
  case 2:
    post_data('mailer.php', 'module:forward;id:' + selectedmail);
    break;
  case 3:
    cursors_wait();
    $.post('ajax/markmail.php', {
      id: selectedmail
    }, function(res, textStatus){
      pagetimer('restart');
      if (res == 'true'){
        $('#asterisk_' + selectedmail).show();
        $.id('markbtn').alt = 'Megjelölés törlése';
        $.id('markbtn').title = 'Megjelölés törlése';
        $.id('markbtn').src = $.id('markbtn').src.replace(/.png$/, '_active.png');
      }else{
        $('#asterisk_' + selectedmail).hide();
        $.id('markbtn').alt = 'Megjelölés';
        $.id('markbtn').title = 'Megjelölés';
        $.id('markbtn').src = $.id('markbtn').src.replace(/_active.png$/, '.png');
      }
    });
    cursors_normal();
    break;
  case 4:
    post_data('mailer.php', 'module:dl;id:' + selectedmail);
    break;
  case 5:
    post_data('mailer.php', 'module:movetotrash;id:' + selectedmail);
    break;
  default:
    return false;
  }
  wait = false;
  return false;
}

function composeform_submit(){
  if ($.id('to').value == ''){
    alert('Az üzenetnek nincs címzettje, így nem küldheted el!');
  } else if ($.id('subject').value == ''){
    alert('Az üzenetnek nincs tárgya, így nem küldheted el!');
  } else if ($.id('msgarea').value == ''){
    alert('Az üzenet nem tartalmaz szöveget, így nem küldheted el!');
  }else{
    document.compose.submit();
  }
}

function composeform(){
  if ($.id('msgtyp2').checked == true){
    nrpstat = $.id('noreply').checked;
    $.id('noreply').checked = true;
    $.id('noreply').disabled = true;
  }else{
    $.id('noreply').disabled = false;
    $.id('noreply').checked = nrpstat;
  }
}

function loadpmtemplate(){
  var tmplindex = $.id('templates').options[$.id('templates').selectedIndex].value;
  if (tmplindex == 'x') return false;
  $.id('msgarea').value == 'Várj...';
  $.post('ajax/loadpmtemplate.php', {
    id: tmplindex
  }, function(res, textStatus){
    pagetimer('restart');
    var tmpldetails = res.split('|#|');
    $.id('subject').value = tmpldetails[0];
    $.id('msgarea').value = tmpldetails[1];
  });
}

function selectmails(mode){
  if (wait) return false;
  wait = true;
  selectedmail = false;
  mailerbuttons();
  for (x in mailheaders) mailheaders[x].className = mailstates[mailheaders[x].id];
  var inputelements = $('#movemsgs').find("input").get();
  for (x in inputelements){
    if (mode == 3) inputelements[x].checked = inputelements[x].checked ? false: true;
    else if (mode == 2) inputelements[x].checked = false;
    else inputelements[x].checked = true;
  }
  wait = false;
  return false;
}

function mailerbuttons(){
  if (selectedmail == false){
    var mbuttons = $('#mbuttons').find('img').get();
    for (x in mbuttons){
      mbuttons[x].alt = '';
      mbuttons[x].title = '';
      mbuttons[x].style.cursor = 'default';
      if (mbuttons[x].src.match('_disabled') == null){
        if (mbuttons[x].src.match('_unable') != null) mbuttons[x].src = mbuttons[x].src.replace(/_unable.png$/, '_disabled.png');
        else if (mbuttons[x].src.match('_active') != null) mbuttons[x].src = mbuttons[x].src.replace(/_active.png$/, '_disabled.png');
        else mbuttons[x].src = mbuttons[x].src.replace(/.png$/, '_disabled.png');
      }
    }
  }else{
    $.post('ajax/getmail.php', {
      id: selectedmail,
      what: 'noreply'
    }, function(res, textStatus){
      pagetimer('restart');
      // replybutton
      if (res == 'no'){
        $.id('replybtn').alt = 'Válaszolok!';
        $.id('replybtn').title = 'Válaszolok!';
        $.id('replybtn').style.cursor = 'pointer';
        if ($.id('replybtn').src.match('_disabled') != null) $.id('replybtn').src = $.id('replybtn').src.replace(/_disabled.png$/, '.png');
        else if ($.id('replybtn').src.match('_unable') != null) $.id('replybtn').src = $.id('replybtn').src.replace(/_unable.png$/, '.png');
      }else{
        $.id('replybtn').alt = 'Erre a levélre nem válaszolhatsz.';
        $.id('replybtn').title = 'Erre a levélre nem válaszolhatsz.';
        $.id('replybtn').style.cursor = 'default';
        $('#replybtn').attr('onclick', '');
        if ($.id('replybtn').src.match('_disabled') != null) $.id('replybtn').src = $.id('replybtn').src.replace(/_disabled.png$/, '_unable.png');
        else if ($.id('replybtn').src.match('_unable') == null) $.id('replybtn').src = $.id('replybtn').src.replace(/.png$/, '_unable.png');
      }
      // forwardbutton
      $.id('forwardbtn').alt = 'Továbbítás';
      $.id('forwardbtn').title = 'Továbbítás';
      $.id('forwardbtn').style.cursor = 'pointer';
      $.id('forwardbtn').src = $.id('forwardbtn').src.replace(/_disabled.png$/, '.png');
      // markbutton
      $.id('markbtn').style.cursor = 'pointer';
      if ($.id('asterisk_' + selectedmail).style.display != 'none'){
        $.id('markbtn').alt = 'Megjelölés törlése';
        $.id('markbtn').title = 'Megjelölés törlése';
        if ($.id('markbtn').src.match('_disabled') != null) $.id('markbtn').src = $.id('markbtn').src.replace(/_disabled.png$/, '_active.png');
        else if ($.id('markbtn').src.match('_active') == null) $.id('markbtn').src = $.id('markbtn').src.replace(/.png$/, '_active.png');
      }else{
        $.id('markbtn').alt = 'Megjelölés';
        $.id('markbtn').title = 'Megjelölés';
        if ($.id('markbtn').src.match('_disabled') != null) $.id('markbtn').src = $.id('markbtn').src.replace(/_disabled.png$/, '.png');
        else if ($.id('markbtn').src.match('_active') != null) $.id('markbtn').src = $.id('markbtn').src.replace(/_active.png$/, '.png');
      }
      // savebutton
      $.id('savebtn').alt = 'Letöltés';
      $.id('savebtn').title = 'Letöltés';
      $.id('savebtn').style.cursor = 'pointer';
      $.id('savebtn').src = $.id('savebtn').src.replace(/_disabled.png$/, '.png');
      // movebutton
      $.id('movebtn').alt = 'Szemétbe vele!';
      $.id('movebtn').title = 'Szemétbe vele!';
      $.id('movebtn').style.cursor = 'pointer';
      $.id('movebtn').src = $.id('movebtn').src.replace(/_disabled.png$/, '.png');
    });
  }
}

function post_data(phpfile, arguments){
  if (phpfile == '') return false;
  if (arguments == '') return false;
  
  var argumentlist = arguments.split(';');
  do{
    var formid = String(Math.random()).replace('.', '');
  }while ($.id(formid));
  var appendstring = '<form method="post" action="' + phpfile + '" name="' + formid + '" id="' + formid + '">';
  
  for (x in argumentlist){
    var argdata = argumentlist[x].split(':');
    appendstring = appendstring + '<input type="hidden" name="' + argdata[0] + '" value="' + argdata[1] + '"/>';
  }
  
  $('body').append(appendstring + '</form>');
  $("#"+formid).submit();
  return false;
}

function rf_opt(action, id, type){
  switch (action){
  case 1:
    post_data(type + '.php', 'module:edit;id:' + id);
    break;
  case 2:
    if (confirm('Biztosan törlöd?')) post_data(type + '.php', 'module:delete;id:' + id);
    break;
  case 3:
    post_data(type + '.php', 'module:new');
    break;
  case 4:
    post_data(type + '.php', 'module:up;id:' + id);
    break;
  case 5:
    post_data(type + '.php', 'module:down;id:' + id);
    break;
  default:
    return false;
  }
  return false;
}

function rf_submit(){
  if ($.id('title').value == ''){
    alert('A cikknek nincs címe!');
  } else if ($.id('text').value == ''){
    alert('A cikknek nincs szövege!');
  }else{
    document.editor.submit();
  }
}

function checkfilebyext(field, ext){
  if ($('#' + field).val()){
    var tfileparts = $('#' + field).val().split(".");
    var tfileext = tfileparts[tfileparts.length - 1];
    if ((tfileext == ext) && (tfileparts.length >= 2) && (tfileparts[0] != '')){
      return true;
    }else{
      $('#' + field).val('');
      alert('A megadott fájl nem érvényes!');
      return false;
    }
  }
}