// Define some errors and hold `em all in an array
var errors = new Array();
errors[0]  = "You either forgot to choose a LOL from our gallery or there was no LOL uploaded!";
errors[1]  = "Your uploaded/chosen LOL cannot be left uncaptioned!";
errors[2]  = "Please enter your name (so we can credit your LOL)!";
errors[3]  = "Please enter your e-mail address!";
errors[4]  = "Invalid e-mail address! Please re-enter.";
errors[5]  = "It seems that someone has already chosed this LOL before and it's locked by the system.<br /><br />You should wait a few hours while we review this LOL and unlock it, or choose another one from the gallery behind me :)";
errors[6]  = "Please enter the joke's title!";

var ajax; // the variable that makes ajax possible

function createHttpRequest()
{
      if (window.XMLHttpRequest)
      {
            return new XMLHttpRequest();
      } 
      else if (window.ActiveXObject)
      {
            try
            {
                  return new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
                  try
                  {
                        return new ActiveXObject("Microsoft.XMLHTTP");
                  }
                  catch (e)
                  {
                        return null;
                  }
            }
      }
}

function deleteLOL(url)
{
      ajax = createHttpRequest();
      ajax.onreadystatechange = function()
      {
            if (ajax.readyState == 4 && ajax.status == 200)
            {
                  deleted = ajax.responseText;
                  if(deleted == 1)
                  {
                        parent.window.scroll(0,0);
                        parent.document.getElementById('info').innerHTML='COOL!\nYour current uploaded LOL has been successfully deleted';
                        parent.document.getElementById('success_top').style.display='block';
                        msgDelay = setTimeout('hideMsg(1,"frame")',3000);
                  }
            }
      }
      ajax.open('GET', url, true);
      ajax.send(null);
}

function hideMsg(reloadGallery, comesFrom)
{
      var obj = (comesFrom == 'frame') ? parent.document : document;
      obj.getElementById('success_top').style.display='none';

      clearTimeout(msgDelay);

      if(reloadGallery == 1)
      {
            document.location.reload(true);
      }
}

function showPostedMsg()
{
      document.getElementById('success_top').style.display='block';
      document.getElementById('info').innerHTML='Your LOL will be reviewed by Pets Funky. If it will be considered funny, it will be posted on the first page :)';
      msgDelay = setTimeout('hideMsg(0,"dummy")',10000);
}

function sendToEditor(LOL, blogURL)
{
      var LOLstatus;

      ajax = createHttpRequest();
      ajax.onreadystatechange = function()
      {
            if (ajax.readyState == 4 && ajax.status == 200)
            {
                  LOLstatus = ajax.responseText;
                  if(LOLstatus == 1)
                  {
                        displayError(errors[5]);
                        return false;
                  }
                  else if(LOLstatus == -1)
                  {
                        alert("There was a problem while processing your request!");
                        return false;
                  }
                  else
                  {
                        document.getElementById('placeHolder').src = "getImage.php?path=" + LOL;
                        document.getElementById('uploadedLOL').value = LOL;
                        getPicName = LOL.split('/');
                        LOLname    = getPicName[1];
                        document.getElementById('LOLname').value = LOLname;
                  }
            }
      }
      ajax.open('GET', 'checkStatus.php?lol=' + LOL, true);
      ajax.send(null);
}

function SetAlignment(section, alignment)
{
      document.getElementById(section + 'alignment').value = alignment;
      document.getElementById(section + 'left').style.backgroundColor='white';
      document.getElementById(section + 'left').style.border = '1px solid #FFF0FF';
      document.getElementById(section + 'center').style.backgroundColor='white';
      document.getElementById(section + 'center').style.border = '1px solid #FFF0FF';
      document.getElementById(section + 'right').style.backgroundColor='white';
      document.getElementById(section + 'right').style.border = '1px solid #FFF0FF';
      document.getElementById(section + alignment).style.backgroundColor = '#FFF0FF';
      document.getElementById(section + alignment).style.border = '1px solid #F86BD4';
}

function toggleLayer( whichLayer )
{
      var elem, vis;

      if( document.getElementById )
      {
            elem = document.getElementById( whichLayer ); // standard way
      }
      else if( document.all ) // old versions
      {
            elem = document.all[whichLayer];
      }
      else if( document.layers ) // ns4
      {
            elem = document.layers[whichLayer];
      }
      
      vis = elem.style;

      if(vis.display == '' && elem.offsetWidth !=undefined && elem.offsetHeight != undefined)
      vis.display = (elem.offsetWidth !=0 && elem.offsetHeight !=0) ? 'block' : 'none';
      vis.display = (vis.display == '' || vis.display == 'block') ? 'none' : 'block';
}

function checkCustomization()
{
      uploadedLOL = document.getElementById('placeHolder').src;
      LOL = uploadedLOL.split('/');
      if(LOL[LOL.length - 1] == 'bg_uploaded.gif')
      {
            displayError(errors[0]);
            return false;
      }

      theForm    = document.capit;
      jokeTitle  = theForm.joke_title.value;
      txtTop     = theForm.txtTop.value;
      txtBottom  = theForm.txtBottom.value;
      author     = theForm.author.value;
      email      = theForm.email.value;

      if(jokeTitle == '')
      {
            displayError(errors[6]);
            return false;
      }

      if(txtTop == '')
      {
            if(txtBottom == '')
            {
                  displayError(errors[1]);
                  return false;
            }
      }

      if(txtBottom == '')
      {
            if(txtTop == '')
            {
                  displayError(errors[1]);
                  return false;
            }
      }

      if(txtTop != '' || txtBottom != '' || txtTop != '' && txtBottom != '')
      {
            if(author == '')
            {
                  displayError(errors[2]);
                  return false;
            }

            if(email == '')
            {
                  displayError(errors[3]);
                  return false;
            }

            if(email != '' && !isEmail(email))
            {
                  displayError(errors[4]);
                  return false;
            }
      }

      return true;
}

function processUpload()
{
      if(document.upload.LOL.value != '')
      {
            document.getElementById('submit').style.display = 'none';
            document.getElementById('upload_message').style.display = 'block';
      }
      else
      {
            alert("Unknown request");
            return false;
      }
      return true;
}

function overlay(LOLsrc, LOLwidth, LOLheight)
{
      el = document.getElementById("overlay");
      el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";

      if(el.style.visibility == "visible")
      {
            el = document.getElementById('overlayedLoL');
            el.style.width  = LOLwidth + "px";
      }

      objImage        = new Image();
      objImage.src    = LOLsrc;
      objImage.onLoad = setTimeout("showLoadedLOL('"+LOLsrc+"')", 1000);

      el.innerHTML = '<span id="renderer_txt">Rendering image preview <img src="images/indicator.gif" alt="indicator" /></span>';
}

function showLoadedLOL(LOLsrc)
{
      el = document.getElementById('overlayedLoL');

      // Show lol
      LOLname = LOLsrc.split('/');
      el.innerHTML = '<img src="'+LOLsrc+'" alt="'+LOLname[1]+'" onclick="hideImg()" /><a href="#overlayClose" title="Close" onclick="hideImg()"><img class="icon_close" src="images/close_lol.gif" alt="Close" /> Close</a>';
}

function hideImg()
{
      el = document.getElementById("overlay");
      el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";

      if(el.style.visibility == "visible")
      {
            el.style.visibility == "hidden";
      }
      else
      {
            el.style.visibility == "visible";
      }
}

function checkSize()
{
      var myWidth = 800;
      if( typeof( window.innerWidth ) == 'number' )
      {
            // Non-IE
            myWidth = window.innerWidth;
      }
      else if( document.documentElement && ( document.documentElement.clientWidth ) )
      {
            // IE 6+ in 'standards compliant mode'
            myWidth = document.documentElement.clientWidth;
      }
      else if( document.body && ( document.body.clientWidth ) )
      {
            // IE 4 compatible
            myWidth = document.body.clientWidth;
      }
      return myWidth;
}

function displayError(error)
{
      // Content creation
      var content = new Array();
      var index = 0;

      content[index++] = '<h1><img src="images/fail.gif"/> Ooops!</h1>';
      content[index++] = error + "<br /><br />";
      content[index++] = '<div align="center"><a href="#step2" onclick="document.getElementById(\'errorBox\').style.visibility = \'hidden\'">Continue</a></div>';

      document.getElementById("errorBox").innerHTML = content.join("");
      document.getElementById("errorBox").style.visibility = "visible";
      parent.window.scrollTo(0,500);
}

function switchTab(objID)
{
      selected_tab = document.getElementById(objID);

      switch(objID)
      {
            case "newPics":
                 selected_tab.setAttribute("class","selected");
                 document.getElementById('oldPics').setAttribute("class","");
                 document.getElementById('oldLoLs').style.display='none';
                 document.getElementById('newLoLs').style.display='block';
                 break;
            case "oldPics":
                 selected_tab.setAttribute("class","selected");
                 document.getElementById('newPics').setAttribute("class","");
                 document.getElementById('oldLoLs').style.display='block';
                 document.getElementById('newLoLs').style.display='none';
                 break;
      }
}

function isEmail(email)
{
      if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))
      {
            return true;
      }
      return false;
}

function calcHeight()
{
      //find the height of the internal page
      var the_height = document.getElementById('gallery').contentWindow.document.body.scrollHeight;

      //change the height of the iframe
      document.getElementById('gallery').height = the_height;
}