   function backgroundScale()
   {
      var imageRatio       = 1.64;
      var windowHeight     = document.body.clientHeight;
      var windowWidth      = document.body.clientWidth;
      var windowScale      = windowWidth / windowHeight;
      var targetWidth      = windowHeight * imageRatio;
      var targetWidthFull  = windowWidth;
      var leftPos          = - (targetWidth - windowWidth) / 2;
      var leftPosFull      = 0;

      if (windowScale <= imageRatio)
      {
         $('.background img').attr("width", targetWidth);
         $('.background').css("left", leftPos);
      } 
      else
      {
         $('.background img').attr("width", targetWidthFull);
         $('.background').css("left", leftPosFull);
      }
   }

   function checkPrevNext(navigatorDiv, targetDivs)
   {
      var evoWorkTotal = $(targetDivs).length;

      if (evoWorkTotal == 1)
         $(navigatorDiv + " img").addClass("displaynone");
      else if (evoWorkTotal == 2)
      {
         $(navigatorDiv + " img").eq(0).addClass("displaynone");

         $(navigatorDiv + " img").click(function(){
               var pos = 0;

               if ($(this).is(":first-child"))
                  var pos = 1;

               $(this).addClass("displaynone");
               $(navigatorDiv + " img").eq(pos).removeClass("displaynone");
         });
      }
   }

   function emailSubManagement(emailDiv, emailInput, emailMsg, method)
   {
      var emailVal = jQuery.trim($(emailInput).val());
      $(emailDiv).addClass("displaynone");

      if (emailVal == "" || emailVal == null || emailVal == "email@domain.com")
      {
         $(emailDiv).removeClass("displaynone");
         $(emailInput).css("border-color", "red");
      }
      else
      {
         var emailRegex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

         if (emailVal.match(emailRegex))
         {
            var controllerUrl = "sendEmail";
            var dataString    = 'email='+ emailVal + '&method=' + method;

            $(emailMsg).text("processing...");

            $.ajax({
               type: "POST",
               url: controllerUrl + ".php",
               data: dataString,
               success: function(msg) {
                  $(emailInput).css("border-color", "");
                  $(emailMsg).text(msg);
                  if (method == "unsubcribe")
                     $("#management").addClass("displaynone");
               }
            });
         }
         else
         {
            $(emailDiv).removeClass("displaynone");
            $(emailInput).css("border-color", "red");
         }
      }
   }

   $(document).ready(function(){
      backgroundScale();
      $.preloadCssImages();

      $(".swfReplace span").each(function(){
         var $target = $(this);
         var index   = $(".swfReplace span").index(this);
         var urlVal  = $(".swfReplace").eq(index).attr("href");

         $target.flash({  
            swf: "images/title_right_home.swf",
            flashvars: {texta: $target.text(),urllink: urlVal},
            wmode: 'transparent',
            height:40,
            width:470
         }); 
      }); 

      $("#tabsPanel li").click(function(){
         var index  = $("#tabsPanel li").index(this);
         var active = $("#tabsPanel").data("active");

         if (active == null)
            active = 0; // first element

         $("#tabsPanel").data("active", index);

         if (index != active)
         {
            $("#tabRssContainer .tabs:eq(" + active + ")").addClass("displaynone");
            $("#tabRssContainer .tabs:eq(" + index + ")").removeClass("displaynone");
            $("#tabsPanel li:eq(" + active + ")").removeClass("active");
            $("#tabsPanel li:eq(" + index + ")").addClass("active");
         }

         return false;
      });

      $('#testimonials').cycle({ 
          fx:     'fade', 
          speed:  'fast', 
          timeout: 0, 
          next:   '.prevNextTesti img:eq(1)', 
          prev:   '.prevNextTesti img:eq(0)' 
      });

      $(".prevNextNews img").click(function(){
         var pos = 0;

         if ($(this).is(":first-child"))
            var pos = 1;

         $(this).addClass("displaynone");
         $(".prevNextNews img").eq(pos).removeClass("displaynone");
      });

      $('#news').cycle({ 
         fx:     'fade', 
         speed:  'fast', 
         timeout: 0, 
         next:   '.prevNextNews img:eq(1)', 
         prev:   '.prevNextNews img:eq(0)' 
      });

      $(".prevNextPress img").click(function(){
         var pos = 0;

         if ($(this).is(":first-child"))
            var pos = 1;

         $(this).addClass("displaynone");
         $(".prevNextPress img").eq(pos).removeClass("displaynone");
      });

      $('#press').cycle({ 
         fx:     'fade', 
         speed:  'fast', 
         timeout: 0, 
         next:   '.prevNextPress img:eq(1)', 
         prev:   '.prevNextPress img:eq(0)' 
      });

      checkPrevNext("#prevNextEvoWork", ".glysection");
      $('#evoWork').cycle({ 
         fx:     'fade', 
         speed:  'fast', 
         timeout: 0, 
         next:   '#prevNextEvoWork img:eq(1)', 
         prev:   '#prevNextEvoWork img:eq(0)' 
      });

      checkPrevNext(".prevNextBlog", ".isectionBlog");
      $('#facts').cycle({ 
         fx:     'fade', 
         speed:  'fast', 
         timeout: 0, 
         next:   '.prevNextBlog img:eq(1)', 
         prev:   '.prevNextBlog img:eq(0)' 
      });

      $("#close").click(function(){
         if ($("#popup").is(":visible"))
         {
            $("#popup").css("z-index", "-1");
            $("#popup").hide("fast");
         }
      });

      $(".popup").click(function(){
         $("#popup").css("z-index", "500");
         $("#popup").toggle("fast");
         return false;
      });

      $("#management").click(function(){
         if($("#unSubscribe").hasClass("displaynone"))
            $("#unSubscribe").removeClass("displaynone");
         else
            $("#unSubscribe").addClass("displaynone");

         return false;
      });

      $("#submit").click(function(){
         emailSubManagement("#forms", "#email", "#message", "subcribe");
      });

      $("#submitUn").click(function(){
         emailSubManagement("#unSubscribe", "#emailUn", "#messageUn", "unsubcribe");
      });

      $(".contactUs").click(function(){
         window.open("contact.php", '_blank');
      });

      $(".contactUs").hover(
         function(){$(this).css("text-decoration", 'none');},
         function(){$(this).css("text-decoration", 'underline');}
      );

   });

   $(window).load(function(){
      $('#allListing').cycle({ 
          timeout:  5000, 
          pause:     1 
      });
   });
   $(window).resize(function()
   {
      var imageRatio       = 1.64;
      var windowHeight     = document.body.clientHeight;
      var windowWidth      = document.body.clientWidth;
      var windowScale      = windowWidth / windowHeight;
      var targetWidth      = windowHeight * imageRatio;
      var targetWidthFull  = windowWidth;
      var leftPos          = - (targetWidth - windowWidth) / 2;
      var leftPosFull      = 0;

      if (windowScale <= imageRatio)
      {
         $('.background img').attr("width", targetWidth);
         $('.background').css("left", leftPos);
      } 
      else 
      {
         $('.background img').attr("width", targetWidthFull);
         $('.background').css("left", leftPosFull);
      }
   });