var _labelClearTOO;
var _randomImageTOO;
var maxLength = 500;
var imgContainer = new Object ("Images");
var maskOp = 0;
var maskOpTOO;
var imgMainTOO;

imgContainer.resourcePath = "/images/";
imgContainer.resources = Array ("contact-bg.gif","copy-bg.gif","db-cust-more-ov.gif","db-cust-more.gif","db-cust-signin-ov.gif","db-cust-signin.gif","34,848 db-img-01.gif","db-logo-text.gif","db-start-bid-ov.gif","db-start-bid.gif","feat-bg.gif","img-main-bg.gif","img-sub-bg.gif","logo-bg-gray.gif","logo-bg-red.gif","map-icon-ov.gif","map-icon-ov.png","map-icon.gif","map-icon.png","msg-send-ov.gif","msg-send.gif","nav-tile-about-ov.gif","nav-tile-about.gif","nav-tile-contact-ov.gif","nav-tile-contact.gif","nav-tile-projects-ov.gif","nav-tile-projects.gif","nav-tile-services-ov.gif","nav-tile-services.gif","ph-icons.gif","msg-final-bg.gif","50-percent-mask.png","70-percent-mask.png","50-percent-mask.gif","70-percent-mask.gif");

function ini()
{
   var idx = Math.round(Math.random());
   var images = ["url(/images/logo-bg-gray.gif) center center no-repeat","url(/images/logo-bg-red.gif) center center no-repeat"];
   preload();
   document.getElementById('nav-wrapper').style.background = images[idx];
   document.getElementById('msg-final').style.left = (Math.round(document.body.offsetWidth / 2) - 175) + "px";
   document.getElementById('mask').style.width = (document.body.offsetWidth < 1280 ? (1280 + "px") : "100%");

}

function preload()
{
   // images
   imgContainer.contents = Array (imgContainer.resources.length);
   for (idx = 0; idx < imgContainer.resources.length; idx++)
   {
      imgContainer.contents[idx] = new Image();
      imgContainer.contents[idx].src = imgContainer.resourcePath + imgContainer.resources[idx];
   }
   randomizeImages();
   _randomImageTOO = setInterval(randomizeImages, 10000);
}

function randomizeImages()
{
   var idx = 0;
   // number of avail images - 1
   var randPoolLength = 5;
   var assignments = Array(0,0,0,0);
   var rand;
   var found;
   var intStr = "";
   var zeros = "0000";

   while(typeof assignments[3] != "string")
   {
      rand = Math.round(randPoolLength * Math.random());
      found = false;
      for(idx = 0; idx < 4; idx++)
      {
         if(typeof assignments[idx] != "string")
         {
            break;
         }
         if(assignments[idx] == rand)
         {
            found = true;
            break;
         }
      }
      if(typeof assignments[idx] != "string" && !found)
      {
         intStr = new String(rand);
         assignments[idx] = zeros.substr(intStr.length)+rand;
      }
   }

   var sm0 = new Image();
   sm0.src = "/images/sm-"+assignments[0]+".jpg";
   sm0.onload = function()
   {
      setTimeout("document.getElementById('sm0').style.background = \"url(/images/sm-"+assignments[0]+".jpg) center center no-repeat\"", 20);
      sm0.onload = null;
   }
   var sm1 = new Image();
   sm1.src = "/images/sm-"+assignments[1]+".jpg";
   sm1.onload = function()
   {
      document.getElementById('sm1').style.background = "url(/images/sm-"+assignments[1]+".jpg) center center no-repeat";
      sm1.onload = null;
   }
   var sm2 = new Image();
   sm2.src = "/images/sm-"+assignments[2]+".jpg";
   sm2.onload = function()
   {
      document.getElementById('sm2').style.background = "url(/images/sm-"+assignments[2]+".jpg) center center no-repeat";
      sm2.onload = null;
   }
   var lg = new Image();
   lg.src = "/images/lg-"+assignments[3]+".jpg";
   lg.onload = function()
   {
      clearInterval(imgMainTOO);
      maskOp = 0;
      setOpacity(document.getElementById('img-main'), 0);
      document.getElementById('img-main').style.background = "url(/images/lg-"+assignments[3]+".jpg) center center no-repeat";
      lg.onload = null;
      imgMainTOO = setInterval("_setMaskOp(document.getElementById('img-main'),imgMainTOO,2);", 60);
   }
}

function chgImg(target)
{
   var temp = new Image ();
   var src = "/images/lg-"+target.style.background.match(/[0-9]{4}\.jpg/i);

   temp.src = src;
   temp.onload = function()
   {
      maskOp = 0;
      clearInterval(imgMainTOO);
      setOpacity(document.getElementById('img-main'), 0);
      document.getElementById('img-main').style.background = "url("+src+") center center no-repeat";
      imgMainTOO = setInterval("_setMaskOp(document.getElementById('img-main'),imgMainTOO,2);", 15);
   }
}

function closeMsg()
{
   setTimeout("document.getElementById('msg-final').style.display = \"none\";setTimeout(\"document.getElementById('mask').style.display = \\\"none\\\";\",40);",20);
   _randomImageTOO = setInterval(randomizeImages, 10000);
}

function sendMsg()
{
   var data = document.getElementById('msg-input');
   if(data.value == "Click here to type a message." || data.value == "")
   {
      alert("You haven't typed a message.");
      return false;
   }
   if(data.value.length >= maxLength)
   {
      if(!confirm("Your message is too long and will be cut off. Would\nyou like to continue?"))
      {
         return false;
      }
   }
   setTimeout("document.getElementById('mask').style.display = \"inline\";setTimeout(\"document.getElementById('msg-final').style.display = \\\"inline\\\";\",40);",20);
   setTimeout("document.getElementById('msg-input-final').value = document.getElementById('msg-input').value;", 200);
   clearInterval(_randomImageTOO);
}

function _setMaskOp(target, TOO,interval)
{
   if(maskOp >= 100)
   {
      clearInterval(TOO);
      maskOp = 0;
   }
   else
   {
      setOpacity(target, maskOp);
      maskOp += interval;
   }
}

function completeSendMsg()
{
   var msgName = document.getElementById('msgName').value;
   var msgEmail = document.getElementById('msgEmail').value;
   var msgPhone = document.getElementById('msgPhone').value;
   var msgAddrPhys = document.getElementById('msgAddrPhys').value;
   var msgAddrCity = document.getElementById('msgAddrCity').value;
   var msgAddrState = document.getElementById('msgAddrState').value;
   var msgAddrZip = document.getElementById('msgAddrZip').value;
   var msg = document.getElementById('msg-input-final').value;

   url = "/contact.php?msgName="+escape(msgName)+"&msgEmail="+escape(msgEmail)+"&msgPhone="+escape(msgPhone)+"&msgAddrPhys="+escape(msgAddrPhys)+"&msgAddrCity="+escape(msgAddrCity)+"&msgAddrState="+escape(msgAddrState)+"&msgAddrZip="+escape(msgAddrZip)+"&msg="+escape(msg);
   if(xmlGET(url) == "0xff")
   {
      alert("Your message was sent.");
      document.getElementById('msg-input').value = "Click here to type a message.";
      document.getElementById('txt-label-info').innerHTML = '';
      closeMsg();
   }
   else
   {
      alert("There was a problem sending your message.")
   }
}

function _initStateChanges(target)
{
   var currentBG = target.style.backgroundImage;
   var newBG = target.style.backgroundImage.replace(/(\/images\/[-a-zA-Z0-9]*)(\.\w{1,5})/i,"$1-ov$2");
   target.onmouseout = function()
   {
      target.style.backgroundImage = currentBG;
   }
   target.onmouseover = function()
   {
      target.style.backgroundImage = newBG;
   }
   target.onmouseover.call();
}

function _initTextInput(target)
{
   target.onfocus = function()
   {
      clearTimeout(_labelClearTOO);
      target.style.color = "#000000";
      if(target.value == "Click here to type a message.")
      {
         target.value = "";
      }
      target.onkeypress = function(event)
      {
         var label = document.getElementById('txt-label-info');
         var code = false;
         var widthValue = Math.round(100 * (target.value.length / maxLength));

         widthValue = (widthValue > 100 ? 100 : widthValue);

         if(target.value.length >= maxLength)
         {
            try
            {
               code  = (window.event ? event.keyCode : event.which);
            }
            catch(error){ }
            label.innerHTML = "<div id=\"inputRemainBarDiv\" style=\"float:left;clear:none;width:102px;height:8px;border:1px solid #cccccc;margin-top:8px;\"><div style=\"float:left;clear:none;width:"+widthValue+"px;height:6px;background:#cccccc;margin:1px;font-size:4px;line-height:4px;\"></div></div>&nbsp;&nbsp;&nbsp;<span style=\"color:#885555;float:none;\" >Message is too long.</span> <a href=\"#\" onmousedown=\"javascript:alert('Your message can only be "+maxLength+" characters long.\\n\\nAfter you type your message and click on Send Message,\\nyou will have the chance to leave your name and some\\ncontact information.');\">[?]</a>";

            return (code && ((code < 48 || code > 126) && code == 8));
         }
         else
         {
            label.innerHTML = "<div id=\"inputRemainBarDiv\" style=\"float:left;clear:none;width:102px;height:8px;border:1px solid #cccccc;margin-top:8px;\"><div style=\"float:left;clear:none;width:"+widthValue+"px;height:6px;background:#cccccc;margin:1px;font-size:4px;line-height:4px;\"></div></div>&nbsp;&nbsp;&nbsp;Click <strong>Send Message</strong> to finish. <a href=\"#\" onmousedown=\"javascript:alert('After you type your message and click on Send Message,\\nyou will have the chance to leave your name and some\\ncontact information.');\">[?]</a>";
            return true;
         }

      }
      target.onkeypress.call();
   }

   target.onblur = function()
   {
      target.style.color = "#666666";
      if(target.value == "")
      {
         target.value = "Click here to type a message.";
         _labelClearTOO = setTimeout("document.getElementById('txt-label-info').innerHTML = '';", 200);
      }
      target.onkeypress.call();
   }
   target.onfocus.call();
}


function _initInput(target)
{
   target.onfocus = function()
   {
      target.style.color = "#000000";
      target.select();
   }
   target.onblur = function()
   {
      target.style.color = "#666666";
   }
   target.onfocus.call();
}


function setOpacity(target, opacity)
{
   opacity = (opacity == 100) ? 99.999 : opacity;

   // IE/Win
   target.style.filter = "alpha(opacity:"+opacity+")";

   // Safari<1.2, Konqueror
   target.style.KHTMLOpacity = opacity/100;

   // Older Mozilla and Firefox
   target.style.MozOpacity = opacity/100;

   // Safari 1.2, newer Firefox and Mozilla, CSS3
   target.style.opacity = opacity/100;
}
