﻿(function loadingImage() {
   var el = new Image();
   
   el.setAttribute('class', 'loading');
   el.setAttribute('src', '../assets/images/loading_dots.gif');
   el.setAttribute('style', 'margin:80px 0 0 300px;');

   $(document).ready(function() {
      $('div.banner .loading').remove();
      $('span.forward, span.backward').show();
      xcore.fader.init($('div.banner div.banner_item'), 6000, 2000);
   });

   return el;
}());

window.preloadimages = function() {
  var imgcount = document.images.length,
      imgs = document.images,
      loadedcount = 0;
      
  for(img in imgs) {
    imgs[img].onload = function() {
      loadedcount++;
      if(loadedcount == imgcount) $(document.body).fadeIn(800);
    };
  }
};


$(document).ready(function () {
   // dashboard
   var dashTimeout, $btndash = $('div.dash_toggle'), $dash = $('div.dash'), $feedbackPane = $('div.feedback-pane'),
       $btnFeedback = $('a.trigger'), hideDash = function () {
          $dash.animate({ 'top': '-24px' }, 500);
          $btndash.fadeIn(1000);
       },
       pagefader = new xcore.PageFader();

   $dash.css({ 'top': '-24px' });
   $(document).click(function (e) {
      var $target = $(e.target), els, i;

      if (!$target.hasClass('dash_toggle')) {
         els = $target.parentsUntil('div.dash, div.dash_toggle');

         for (i = 0; i < els.length; i++) {
            if (els[i].nodeName === 'BODY') {
               dashTimeout = setTimeout(hideDash, 500);
               break;
            }
         }
      }

      if (!$target.hasClass('trigger') && !$target.hasClass('feedback-pane')) {
         els = $target.parentsUntil('div.feedback-pane');

         for (i = 0; i < els.length; i++) {
            if (els[i].nodeName === 'BODY' && $btnFeedback.hasClass('active')) {
               dashTimeout = setTimeout(function () {
                  $feedbackPane.toggle("fast");
                  $btnFeedback.toggleClass("active");
                  pagefader.toggle();
               }, 200);
               break;
            }
         }
      }

      $('.status_message').fadeOut('slow');
      // $('[id*=ulStatus]').children().remove();
   });
   $btndash.fadeIn('slow');
   $btndash.click(function () {
      $dash.animate({ 'top': '0' }, 800);
      $btndash.fadeOut(400);
   });
   $dash.mouseenter(function () {
      clearTimeout(dashTimeout);
   });
   $btnFeedback.click(function () {
      pagefader.toggle();
      $feedbackPane.toggle("fast");
      $(this).toggleClass("active");
      return false;
   });

   // fade opacity of sidebar trigger if screen res becomes lower than the viewport
   $(window).resize(function () {
      if ($(window).width() <= 1050) {
         $btnFeedback.stop().fadeTo('fast', 0.3);
      }
      else {
         $btnFeedback.stop().fadeTo('slow', 1);
      }
   });
   // end dashboard and feedback

   $("div.wrapper_head input").click(function () {
      $("div.wrapper_head span.error").fadeOut("slow");
   });
   $("div.wrapper_head input").blur(function () {
      if ($(this).val() !== "") { $(this).css("background-image", "none"); }
   });

   // topmenu
   var currPage = $('input.contentPage').attr('value'),
   menuItems = $('ul.top_menu').children(),
   menuItem,
   menuTimeout;

   for (i = 0; i < menuItems.length; i++) {
      menuItem = $(menuItems[i]);

      if (menuItem.attr('id') === currPage) menuItem.addClass('active');
      else menuItem.removeClass('active');
   }

   menuItems.mouseenter(function () {
      clearTimeout(menuTimeout);
      $(this).children("ul.sub_menu").stop(true, true).slideDown("fast");
   }).mouseleave(function () {
      menuTimeout = setTimeout(function () {
         $(this).children("ul.sub_menu").stop(true, true).slideUp("slow");
      }, 300);
   });
   // end topmenu

   /* marquee */
   xcore.fader.init($('div.marquee p'), 6000, 2000);
   /* end marquee */


   /*   services menu functionality...   */
   var actionGroup = $('.service_list').children();
   actionGroup.each(function () {
      $($(this).children('ul')[0]).hide();
   });
   actionGroup.each(function () {
      $(this).mouseenter(function () {
         var el = $($(this).children('ul')[0]);
         el.stop(true, true).slideDown(700);
         $(this).addClass('active').mouseleave(function () { el.slideUp(300); });
      });
      $($(this).children('ul')[0]).children('li').each(function () {
         $(this).mouseenter(function () { $(this).stop(true, true).animate({ 'padding-left': '20px' }, 200, 'swing'); });
         $(this).mouseleave(function () { $(this).stop(true, true).animate({ 'padding-left': '5px' }, 200); });
      });
   });


   $('div.abilities p').mouseenter(function () {
      $(this).children('span').addClass('over');
   })
	.mouseleave(function () {
	   $(this).children('span').removeClass('over');
	});

});

// cufon


window.init_cufon = function (hoverColor) {
   var color = '#' + hoverColor;

   try {
      Cufon('.top_menu a', { hover: { color: color } });

      Cufon.replace('h1, h2, h3, h4, div.marquee p, div.marquee a, div.dash_toggle, ul.top_menu, .contact_info div, a.action_light, a.action_dark');
   }
   catch (e) { }
};

window.ProcessPage = function () {

   this.init = function () {
      var $els = $('div.ideate div, div.strategize div, div.develop div, div.deploy div'),
      i;

      $els.css({ 'height': '100%', 'width': '100%', 'opacity': '0', 'position': 'absolute', 'top': '0', 'left': '0' });
      $('div.process-description div[class*="process"]').hide();

      $els.parent('div').mouseenter(function () {
         $(this).children('div').stop(false, true).animate({
            opacity: 1
         }, "2500");
      }).mouseleave(function () {
         $(this).children('div').stop().animate({
            opacity: 0
         }, "2500");
      });
      $els.parent('div').click(function () {
         for (i = 0; i < $els.length; i++) {
            $($els[i]).parent().removeClass('on');
         }
         $(this).addClass('on');
         $('div.process-description div[class*="process"]');
      });
   };
};
