var timeout	= 200;
var closetimer = 0;
var menudiv	= 0;
var cursorX = 0;
var cursorY = 0;

function updateCursorPosition(e){
 cursorX = e.pageX;
 cursorY = e.pageY;
}

document.onmousemove = updateCursorPosition;

function showDiv(id){
  document.getElementById(id).style.visibility = 'visible';
}

function hideDiv(id){
  document.getElementById(id).style.visibility = 'hidden';
}

function showPopup(text)
{
  div = document.getElementById('help');
  div.style.left = (cursorX+10) + 'px';
  div.style.top = (cursorY+10) + 'px';
  div.innerHTML = text;
  div.style.display = 'block';
}

function hidePopup()
{
  document.getElementById('help').style.display = 'none';
}

// Ajax functions

function ajaxOutput(id){
  var item = document.getElementById(id);
  item.innerHTML = httpRequest.responseText;
}

function moreStatuses(num, wbid){
  document.getElementById('statuses').innerHTML += '<span class="hr"></span>';
  document.getElementById('stmore').innerHTML = '<img src="/grafika/loader-wbstatus.gif" alt="Čakajte" />';
  if(window.XMLHttpRequest){
    httpRequest = new XMLHttpRequest();
  }else if(window.ActiveXObject){
    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  }
  httpRequest.open('GET', '/system/ajax/stmore.php?num='+num+'&wbid='+wbid, true);
  num += 20;
  httpRequest.onreadystatechange= function () { showMoreStatuses(num, wbid); } ;
  httpRequest.send(null);
}

function showMoreStatuses(num, wbid){
 var htmlReturn;
 if(httpRequest.readyState == 4){
   htmlReturn = httpRequest.responseText;
   document.getElementById('stmore').innerHTML = '<a href="javascript:void(0)" title="Zobraziť ďalšie odkazy" class="stmore" onclick="moreStatuses('+num+', \''+wbid+'\')">Zobraziť ďalšie odkazy</a>';
   document.getElementById('statuses').innerHTML += htmlReturn;
 }
}

function sendStatus(){
  var status = document.getElementById('wbtextarea').value;
  var length = status.length;
  status = status.replace(/#/g, '%23');
  if(length <= 250 && length > 0){
    document.getElementById('wbsend').innerHTML = '<a href="javascript:void(0)" title="Odosielam..." style="background: url(\'/grafika/loader-wbstatus.gif\') center center no-repeat;"></a>';
    if(window.XMLHttpRequest){
      httpRequest = new XMLHttpRequest();
    }else if(window.ActiveXObject){
      httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    httpRequest.open('GET', '/system/ajax/sendstatus.php?status='+status, true);
    httpRequest.onreadystatechange= function () { showStatuses(); } ;
    httpRequest.send(null);
  }else{
    alert('Odkaz môže mať dĺžku v rozmedzí 1-250 znakov!');
  }
}

function showStatuses(){
 var htmlReturn;
 if(httpRequest.readyState == 4){
   htmlReturn = httpRequest.responseText;
   document.getElementById('wbsend').innerHTML = '<a href="javascript:void(0)" onclick="sendStatus()"></a>';
   document.getElementById('wbtextarea').value = '';
   charsleft(0, 250, 'chars');
   document.getElementById('statuses').innerHTML = htmlReturn;
 }
}

function reportItem(type, iid, sid){
 if(window.XMLHttpRequest){
  httpRequest = new XMLHttpRequest();
 }else if(window.ActiveXObject){
  httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
 }
 httpRequest.open('GET', '/system/ajax/report.php?type='+type+'&iid='+iid, true);
 httpRequest.onreadystatechange= function () { ajaxOutput(sid); } ;
 httpRequest.send(null);
}

function updateAnkety(volba){
 if(window.XMLHttpRequest){
  httpRequest = new XMLHttpRequest();
 }else if(window.ActiveXObject){
  httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
 }
 httpRequest.open('GET', '/system/ajax/poll.php?option='+volba, true);
 httpRequest.onreadystatechange= function () { ajaxOutput('anketa'); } ;
 httpRequest.send(null);
}

function dbGetImage(game, image){
  
 if(window.XMLHttpRequest){
  httpRequest = new XMLHttpRequest();
 }else if(window.ActiveXObject){
  httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
 }
 
 httpRequest.open('GET', '/system/ajax/dbgetimage.php?game='+game+'&image='+image, true);
 httpRequest.onreadystatechange = function () { dbShowGallery(); } ;
 httpRequest.send(null);
}

function dbShowGallery(){
 var htmlReturn;
 if(httpRequest.readyState == 4){
   htmlReturn = httpRequest.responseText;
   // JS evaluate
   var pattern = new RegExp("<script[^>]*>", "i");
   if(pattern.test(htmlReturn)){
     var start_pos = htmlReturn.match(pattern);
     var pattern2  = new RegExp("</script>", "i");
     var end_pos   = htmlReturn.search(pattern2);
     var st_rozsah = start_pos['index'] + start_pos[0].length;
     var ed_rozsah = end_pos - st_rozsah;
     var resjs     = htmlReturn.substr(st_rozsah, ed_rozsah);
     eval(resjs); 
   }
   // ---
   ImageObj = new Image();
   ImageObj.src = '/images/galleries/' + ImageName;
   if(ImageObj.complete){
     document.getElementById('gallery').innerHTML = htmlReturn;
   }else{
     ImageObj.onload = function(){ document.getElementById('gallery').innerHTML = htmlReturn; };
   }
 }else{
   htmlReturn = '<div id="thumbs"><span class="empty"></span></div><div id="full"><a href="javascript:void(0)" title="Predošlý obrázok" class="previous"></a><img src="/grafika/loader.gif" alt="Loading" /><a href="javascript:void(0)" title="Ďalší obrázok" class="next"></a></div>Načítavam...';
   document.getElementById('gallery').innerHTML = htmlReturn;
 }
}

// Other JS functions

function charsleft(written, total, id){
  var left = total - written;
  left = left + ' znakov ostáva';
  document.getElementById(id).innerHTML = left;
}

function galopen(get){
 window.open('/getimage.php?'+get, '', 'toolbar = 0, scrollbars = 1, resizable = 1');
}

function processUploadBar(){
 document.forms[1].style.display = 'none';
 document.getElementById('pridat_obrazok').innerHTML = 'Nahrávam';
 document.getElementById('upload-loading').style.display = 'block';
}

function mopen(id){	
 mcancelclosetime();
 if(menudiv) menudiv.style.visibility = 'hidden';
  menudiv = document.getElementById(id);
  menudiv.style.visibility = 'visible';
}

function mclose(){
 if(menudiv) menudiv.style.visibility = 'hidden';
}

function mclosetime(){
 closetimer = window.setTimeout(mclose, timeout);
}

function mcancelclosetime(){
 if(closetimer){
  window.clearTimeout(closetimer);
  closetimer = null;
 }
}

function popup(message){
 
 var width = document.body.offsetWidth;
 var height = screen.height;
 var posx = width/2 - 145;
 var posy = height/2 - 145;
 document.write('<div id="popup" style="top: '+posy+'px; left: '+posx+'px;" onclick="popupClose()">');
 document.write(message);
 document.write('</div>');
 document.write('<div id="overlay" style="width: '+width+'px; height: '+height+'px;"></div>');

}

function popupClose(){
 var popup = document.getElementById('popup');
 var overlay = document.getElementById('overlay');
 popup.style.visibility = 'hidden';
 overlay.style.visibility = 'hidden';
}

function passwordcheck(string){

 var points = 0;
 var content = '';
 var abc = '';
 var pass = document.getElementById(string).value;
 var sreturn = document.getElementById('passqual');

 if(pass.match(/[a-z]+/)){
  points++;
 }
 if(pass.match(/[A-Z]+/)){
  points++;
 }
 if(pass.match(/[0-9]+/)){
  points++;
 }
 if(pass.match(/[., _#@$&€\-\+\*\/\%]+/)){
  points++;
 }
 if(pass.length > 7){
  points++;
 }
 if(pass.length > 11){
  points++;
 }
 
 if(points > 5){
  content = '<span class="pass" style="background: #99FF00;"></span>&nbsp;Veľmi silné';
 }else if(points > 4){
  content = '<span class="pass" style="background: #CCFF00;"></span>&nbsp;Silné';
 }else if(points > 3){
  content = '<span class="pass" style="background: #FFFF00;"></span>&nbsp;Dobré';
 }else if(points > 2){
  content = '<span class="pass" style="background: #FF9900;"></span>&nbsp;Slabé';
 }else if(points < 3){
  content = '<span class="pass" style="background: #FF0000;"></span>&nbsp;Veľmi slabé';
 }
 
 sreturn.innerHTML = content;
 
}


function subGenre(index){
 var subgenre = document.dbform.subgenre; 
 subgenre.options.length = 0; 
 if(index == "1"){ 
  subgenre.options[subgenre.options.length] = new Option('nezaradené', 1)
  subgenre.options[subgenre.options.length] = new Option('1st person akcia', 2)
  subgenre.options[subgenre.options.length] = new Option('3rd person akcia', 3);
  subgenre.options[subgenre.options.length] = new Option('stealth', 4);
 }else if(index == "2"){
  subgenre.options[subgenre.options.length] = new Option('nezaradené', 1);
  subgenre.options[subgenre.options.length] = new Option('MMORPG', 1);
  subgenre.options[subgenre.options.length] = new Option('1st person RPG', 3);
  subgenre.options[subgenre.options.length] = new Option('3rd person RPG', 4);
 }else if(index == "3"){
  subgenre.options[subgenre.options.length] = new Option('nezaradené', 1);
  subgenre.options[subgenre.options.length] = new Option('realtime stratégia', 2);
  subgenre.options[subgenre.options.length] = new Option('ťahová stratégia', 3);
  subgenre.options[subgenre.options.length] = new Option('realtime-ťahová stratégia', 4);
  subgenre.options[subgenre.options.length] = new Option('budovateľská stratégia', 5);
 }else if(index == "4"){
  subgenre.options[subgenre.options.length] = new Option('nezaradené', 1);
  subgenre.options[subgenre.options.length] = new Option('2D adventúra', 1);
  subgenre.options[subgenre.options.length] = new Option('3D adventúra', 1);
 }else if(index == "5"){
  subgenre.options[subgenre.options.length] = new Option('nezaradené', 1);
  subgenre.options[subgenre.options.length] = new Option('futbal', 2);
  subgenre.options[subgenre.options.length] = new Option('hokej', 3);
  subgenre.options[subgenre.options.length] = new Option('tenis', 4);
  subgenre.options[subgenre.options.length] = new Option('basketbal', 5);
  subgenre.options[subgenre.options.length] = new Option('vodný šport', 6);
  subgenre.options[subgenre.options.length] = new Option('zimný šport', 7);
 }else if(index == "6"){
  subgenre.options[subgenre.options.length] = new Option('nezaradené', 1);
  subgenre.options[subgenre.options.length] = new Option('závody', 2);
  subgenre.options[subgenre.options.length] = new Option('letecký simulátor', 3);
  subgenre.options[subgenre.options.length] = new Option('železničný simulátor', 4);
  subgenre.options[subgenre.options.length] = new Option('ponorkový simulátor', 5);
 }
}
