$(document).ready(displaySubmenu);

function checkRegistration() {
  var path = document.getElementById('registration');
  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

  if($.formLoading != false){
    function loadData(loadedData){
      var allData = loadedData.toUpperCase();
      allData = allData.split("<><>");

      var nicks = allData[0];
      nicks = nicks.split("<>");
      var mails = allData[1];
      mails = mails.split("<>");

      var users = nicks.length;
      var nickInUse = false;
      var mailInUse = false;

      for (i = 0; i < users; i++) {
        if (nicks[i] == path.nick.value.toUpperCase()) {
          nickInUse = true;
        }
        if (mails[i] == path.mail.value.toUpperCase()) {
          mailInUse = true;
        }
      }

      if (path.nick.value != "" && path.mail.value != "" && path.passwd1.value != ""){
        if (path.passwd1.value == path.passwd2.value){
          if (nickInUse == false){
            if (mailInUse == false){
              if (filter.test(path.mail.value)) {
                $.formLoading = false;
                $("form#registration").submit();
              }else{
                alert("E-mail je spatneho formatu!");
              }
            }else{
              alert("Tento e-mail je pouzivan nekym jinym!\nProsim zvolte jiny.");
            }
          }else{
            alert("Tento nick uz nekdo pouziva.\nProsim zvolte jiny.");
          }
        }else{
          alert("Hesla se lisi.");
        }
      }else{
        alert("Je nutne vyplnit vsechny pozadovane polozky!");
      }
    }

    $.get('./scripts/load_users.php', loadData);

    return false;
  }else{
    return true;
  }
}

function checkPersData(){
  var path = document.getElementById('change_data');

  if($.formLoading != false){
    function loadData(loadedData){
      var allData = loadedData.toUpperCase();
      allData = allData.split("<><>");

      var nicks = allData[0];
      nicks = nicks.split("<>");

      var users = nicks.length;
      var nickInUse = false;

      for (i = 0; i < users; i++) {
        if (nicks[i] == path.nick.value.toUpperCase()) {
          nickInUse = true;
        }
      }

      if(path.nick.value != ""){
        if(nickInUse == false){
          $.formLoading = false;
          $("form#change_data").submit();
        }else{
          alert("Tento nick uz nekdo pouziva.\nProsim zvolte jiny.");
        }
      }else{
        alert("Musite vyplnit nick.");
      }
    }

    $.get('./scripts/load_users.php', loadData);

    return false;
  }else{
    return true;
  }
}

function displayClock(){
  var clocktime = 1000 * time;

  var gmt = new Date();
  var cet = new Date();
  var kst = new Date();
  var pst = new Date();
  var est = new Date();

  gmt.setTime(clocktime);
  cet.setTime(clocktime + 1000 * 3600 * (1 + dayLight));
  kst.setTime(clocktime + 1000 * 3600 * 9);
  pst.setTime(clocktime + 1000 * 3600 * (-8 + dayLight));
  est.setTime(clocktime + 1000 * 3600 * (-5 + dayLight));

  hours = gmt.getHours();
  minutes = gmt.getMinutes();
  gmtTime = (hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes;

  hours = cet.getHours();
  minutes = cet.getMinutes();
  seconds = cet.getSeconds();
  cetTime = (hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes + ":" + (seconds < 10 ? "0" : "") + seconds;

  hours = kst.getHours();
  minutes = kst.getMinutes();
  kstTime = (hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes;

  hours = pst.getHours();
  minutes = pst.getMinutes();
  pstTime = (hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes;

  hours = est.getHours();
  minutes = est.getMinutes();
  estTime = (hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes;

  var clocks = "";
  
  clocks += '<div title="Pacific Standard Time">PST ' + pstTime + '</div>';
  clocks += '<div class="cut">|</div>';
  clocks += '<div title="Eastern Standard Time">EST ' + estTime + '</div>';
  clocks += '<div class="cut">|</div>';
  clocks += '<div title="Greenwich Mean Time">GMT ' + gmtTime + '</div>';
  clocks += '<div class="cut">|</div>';
  clocks += '<div title="Central Europe Time">CET ' + cetTime + '</div>';
  clocks += '<div class="cut">|</div>';
  clocks += '<div title="Korean Standard Time">KST ' + kstTime + '</div>';

  document.getElementById("clocks").innerHTML = clocks;

  time += 0.5;

  setTimeout(displayClock, 500);
}

function displaySubmenu(){
  var urlParts = window.location.href.split("/");
  var mainNav = urlParts[4].replace("?", "");

  if(mainNav == 'user'){
    var submenuId = 'submenu_' + mainNav + '_' + urlParts[5];
  }else{
    var submenuId = 'submenu_' + mainNav;
  }

  if(document.getElementById(submenuId) != null){
    document.getElementById(submenuId).style.display = 'block';
  }
}

function checkDel(){
  return confirm("Chcete to opravdu smazat?");
}

function changeVisibility(id){
  var element = document.getElementById(id);

  if(element.style.display == 'block'){
    element.style.display = 'none';
  }else{
    element.style.display = 'block';
  }
}

function insertText(id, string){
  var element = document.getElementById(id).text;
  var scrollTop = element.scrollTop;    

  element.value += string;
  element.focus();
  element.scrollTop = scrollTop;
}

function checkForumImages(){
  var images = document.images;

  for(i = 0; i < images.length; i++){
    if(document.images[i].className == 'forum'){
      image = document.images[i];

      $(image).bind( 'load', {img:image}, function(event) {

        if(event.data.img.width > 520){
          event.data.img.width = 520;
        }

      } );
      
      if(image.width > 520){
        image.width = 520;
      }
    }
  }
}

function resizeImage(image){
  if(image.width > 520){
    image.width = 520;
  }
}

function showLastPosts(element, thread_id, section){
  var popupWindow = new PopupWindow(element, 280, -50);

  popupWindow.creatDiv();
  popupWindow.setWindow();
  popupWindow.insertText('');

  function loadData(loadedData){  
    popupWindow.insertText(loadedData);
    popupWindow.open();
  }

  $.get('./scripts/load_last_posts.php?thread_id=' + thread_id + '&section=' + section, loadData);
}

function quoteComment(comment_id, section){
  function loadData(loadedData){
    insertText('commentAdd', loadedData + '\r\n');
  }

  $.get('./scripts/load_comment.php?comment_id=' + comment_id + '&section=' + section, loadData);
}

function changeCSS(name){
  var links = document.getElementsByTagName("link");
  var new_css = "./css/" + name + "/style.css";
  var header = document.getElementById("header").getElementsByTagName("img")[0];

  switch(name){
    case "untitled": header_src = "header_1_all.png";
      break;
    case "red_fashion": header_src = "header_1_all.png";
      break;
    case "green_fashion": header_src = "header_1_all.png";
      break;
    case "blue_fashion": header_src = "header_1_all.png";
      break;
    case "golden_touch": header_src = "header_2_all.png";
      break;
    case "simply_gray": header_src = "header_2_all.png";
      break;
    case "sand": header_src = "header_2_all.png";
      break;
    case "april": header_src = "header_3.png";
      break;
  }

  header.src = "./design/" + header_src;

  for(i = 0; i < links.length; i ++){
    if(links[i].rel = "stylesheet"){
      links[i].href = new_css;
    }
  }
}

/* ====================== pop up class ======================= */

function getElementXY(element){
  var left = 0;
  var top = 0;

  while (element.offsetParent) { 
    left += element.offsetLeft;
    top += element.offsetTop; 
    element = element.offsetParent;
  }

  left += element.offsetLeft;
  top += element.offsetTop;

  return {x:left, y:top};
}

function PopupWindow(element, width, moveX){
  var position = getElementXY(element);
  var positionX = position['x'];
  var positionY = position['y'];
  var moveX = typeof(moveX) != 'undefined' ? moveX : 0;

  this.creatDiv = creatDiv;
  this.setWindow = setWindow;
  this.insertText = insertText;
  this.open = open;

  function creatDiv(){
    if(!document.getElementById('popupWindow')){
      var div = document.createElement('div');

      div.setAttribute('id', 'popupWindow');
      document.body.appendChild(div);
    }
  }

  function setWindow(){
    document.getElementById('popupWindow').style.left = positionX + moveX + 'px';
    document.getElementById('popupWindow').style.top = positionY + 15 + 'px';
    document.getElementById('popupWindow').style.width = width + 'px';
  }

  function insertText(text){
    if(document.getElementById('popupWindow')){
      document.getElementById('popupWindow').innerHTML = text;
    }
  }

  function open(){
    var div = document.getElementById('popupWindow');

    if(div){ div.style.display = 'block'; }
  }
}

function closePopupWindow(){
  var div = document.getElementById('popupWindow');

  if(div) {document.body.removeChild(div);}
}

