var changeit = function(tab) {
    showSkillTab('skillset_' + tab);
};


if ( document.id('main_menu') ) {
    var currentMainPageTabIndex = 1;
    var tabInterval = null;
    var inTab = false;

    var showTab = function(elem) {
        var id = elem.get('id').substr(elem.get('id').length-1, 1);
        var morph = elem.get('morph');
        var rollOverId = currentMainPageTabIndex;
        morph.cancel().clearChain().chain(function() {
            document.id('main_info_cont').getChildren('.main_info').addClass('hidden');
            document.id('main_info_' + id).removeClass('hidden');
            document.id('main_info').removeClass('illustration_1');
            document.id('main_info').removeClass('illustration_2');
            document.id('main_info').removeClass('illustration_3');
            document.id('main_info').removeClass('illustration_4');
            document.id('main_info').addClass('illustration_' + id);
            document.id('main_info_' + id).morph({ opacity: 1 });
//            $('main_info_cont').morph({ opacity: 1 });
            if ( !tabInterval ) {
                tabInterval = window.setInterval("cycleTab()", 15000);
            }
            //currentMainPageTabIndex = parseInt(id);
          });
//          $('main_info_cont').morph({ opacity: 0 });
          document.id('main_info_' + id).morph({ opacity: 0 });
          currentMainPageTabIndex = parseInt(id);

          elem.addClass('active').morph({ height: 118, lineHeight: 118, marginTop: -24, duration: 1000 });
    };

    var hideTab = function(elem) {
        var id = elem.get('id').substr(elem.get('id').length-1, 1);
        var morph = elem.get('morph');
        morph.cancel().clearChain().chain(function() {
      			elem.removeClass('active');
      	});
        elem.morph({ height: 96, lineHeight: 96, marginTop: 0, duration: 1000  });
    };


    function cycleTab ()
    {
        if ( inTab ) {
            return;
        }
        var i = currentMainPageTabIndex + 1;
        if ( i > 4 ) {
            i = 1;
        }
        hideTab( document.id('main_menu_' + currentMainPageTabIndex) );
        showTab( document.id('main_menu_' + i) );
    };
}

if (document.id('banner_makespecial')) {
   var currentTextId = 1;
   var showBannerText = function(direction) {
       var maxElems = 7;
       if ( (currentTextId >= maxElems && direction == 'next') || (currentTextId <= 1 && direction == 'prev')) {
           return;
       }
       document.id('banner_text_' + currentTextId).addClass('hidden');

       if ( direction == 'next' ) {
           currentTextId++;
           if ( currentTextId > maxElems ) {
               currentTextId--;
           }
       } else {
           currentTextId--;
           if ( !currentTextId ) {
               currentTextId++;
           }
       }
       document.id('banner_text_' + currentTextId).removeClass('hidden');
       document.id('banner_arrow_right').src = '/themes/main/images/banner_right_arrow' + (currentTextId < 7 ? '_active' : '') + '.png';
       document.id('banner_arrow_left').src = '/themes/main/images/banner_left_arrow' + (currentTextId > 1 ? '_active' : '') + '.png';

   }

}

var showSkillTab = function(tab) {
    ['skillset_1', 'skillset_2', 'skillset_3'].each(function(item) { document.id(item).addClass('hidden'); });
    document.id(tab).removeClass('hidden');
}

window.addEvent('domready', function() {

  swfobject.embedSWF("/themes/main/images/map.swf", "footer_map_flash", "353", "152", "10.0.0", "/themes/main/images/expressInstall.swf", null, {wmode: "transparent"});

  var mainMenu = document.id('main_menu');

  if ( document.id('flash_slider') ) {
      swfobject.embedSWF("/themes/main/images/slider.swf", "flash_slider", "627", "85", "10.0.0", "/themes/main/images/expressInstall.swf", null, {wmode: "transparent"});
  }

  if (mainMenu) {
    mainMenu.getElements('li').each(function(item) {
      item.set('morph', {duration: 500});
      item.addEvents({
        mouseenter: function() {
            var id = this.get('id').substr(this.get('id').length-1, 1);
            if ( id != currentMainPageTabIndex ) {
                window.clearInterval(tabInterval);
                tabInterval = null;
                hideTab( document.id('main_menu_' + currentMainPageTabIndex) );
                showTab(this);
                inTab = true;
            }
        },
        mouseleave: function() {
      	    inTab = false;
        }
      });
    });
    showTab( document.id('main_menu_' + currentMainPageTabIndex) );
  }




  var services = document.id('services');
  if (services) {
    var togglers = services.getElements('dt').addEvents({
      mouseenter: function() { this.addClass(this.hasClass('opened') ? 'opened_active' : 'active'); },
      mouseleave: function() { this.removeClass('opened_active').removeClass('active'); }
    });
    new Fx.Accordion(togglers, services.getElements('dd'), {
      display: 0, // The index of the element to show at start (with a transition). To force all elements to be closed by default, pass in -1.
      onActive: function(toggler, element) {
        toggler.addClass('opened');
      },
      onBackground: function(toggler, element) {
        toggler.removeClass('opened');
      }
    });
  }

  var tabs = document.id('skillset_tabs');
  if ( tabs ) {
      tabs.getElements('li a').addEvent('click', function(event) {
         event.stop();
         showSkillTab(this.get('rel'));
      });
  }

  var showSkillsetDetails = function(event) {
      event.stop();
      if (this.hasClass('skillset_popup_')) this.addClass('hidden');
      document.id('skillset').getElements('.skillset_details').addClass('hidden');
      var link = this.getElement('a') || this.retrieve('link');
      link && link.get('rel') && document.id('skillset_details_' + link.get('rel')).removeClass('hidden');//.tween('height', 0, 132);
//      .setStyle('height', 0)
      //var popup = document.id('skillset_popup_' + link.get('rel'));
      //popup.addClass('hidden');
    };

  ['skillset_map_1', 'skillset_map_2', 'skillset_map_3'].each(function(item) {
      var ssMap = document.id(item);
      var popupTimer;
      if (ssMap) {
        ssMap.getElements('li').addEvents({
           mouseenter: function() {
              ssMap.getElements('li').removeClass('active');
              this.addClass('active');
           },
           mouseleave: function(event) {
              this.removeClass('active');
           },
           click: showSkillsetDetails
        });
      }
  });

  var previewLinks = document.id('content') && document.id('content').getElements('a.viewable');
  var previewBalloon = document.id('preview_balloon');
  if (previewLinks && previewBalloon) {
    var timer;
    var whereToGo = null;
    previewBalloon.addEvents({
      click: function() { window.location = whereToGo; },
      mouseleave: function() { timer = (function() { previewBalloon.addClass('hidden'); }).delay(1000); }
    });
    previewLinks.addEvents({
      mouseover: function() {
        whereToGo = this.get('href');
        previewBalloon.getElement('img').set('src', this.get('preview'));
        var aligner = this.getElement('span');
        previewBalloon.removeClass('hidden').setStyles({
          top: aligner.getPosition().y - 164,
          left: aligner.getPosition().x - 23
        });
      },
      mouseleave: function(event) {
        console.log(1)
        if (event.relatedTarget && event.relatedTarget.id != 'preview_balloon') {
          timer = (function() { previewBalloon.addClass('hidden'); }).delay(1000);
        }
      }
    });
  }

  var illustrations = document.id('content') && document.id('content').getElements('.illustration');
  if (illustrations) {
    illustrations.each(function(ill) {
      var resize = ill.getElement('.tools .resize');
      if (resize) {
        resize.addEvent('click', function() {
          SqueezeBox.open(this.get('to'));
        });
      }
    });
  }



  if (document.id('content')) {
    var replacer = new Element('div', { 'class': 'select', html: '<span class="value"></span><span class="handle"></span>' });
    document.id('content').getElements('select').each(function(select) {
      var value = select.getElement('option[selected]').get('text');
      var hidden = new Element('input', { type: 'hidden', name: select.get('name'), value: value }).inject(select, 'before');
      var clone = replacer.clone().replaces(select);
      clone.getElement('.value').set('text', value);
      clone.store('select', select).store('hidden', hidden);
      clone.addEvent('click', function() {
        var coords = clone.getCoordinates();
        options.removeClass('hidden').setStyles({
          top: coords.bottom,
          left: coords.left + 1,
          width: coords.width - 2
        })
      });
      var options = new Element('ul', { 'class': 'select_values hidden' });
      select.getElements('option').each(function(option) {
        var opt = new Element('li', { text: option.get('text'), val: option.get('value') }).store('par', clone).inject(options);
      });
      options.inject(document.body).getElements('li').addEvents({
        mouseenter: function() { this.addClass('active'); },
        mouseleave: function() { this.removeClass('active'); },
        click: function() {
          var parent = this.retrieve('par');
          parent.getElement('.value').set('text', this.get('text'));
          parent.retrieve('hidden').set('value', this.get('val'));
          this.removeClass('active').getParent().addClass('hidden');
        }
      });
    });
  }
});


    if ( $("#MyForm_Form") ) {
        $("#MyForm_Form").submit(function(){
            if ( $("#MyForm_Form").validate().form() ) {

                jQuery.ajax({
                    type: "POST",
                    url: jQuery(this).attr("action"),
                    data: jQuery(this).serialize(),
                    success: function(data){
                        $('#MyForm_Form').find('input, textarea').val('');
                        SqueezeBox.open('/thank-you/', {size: {x: 435, y: 240}});
                    }
                });
            }
            return false;
        });
     }



var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

try {
    var pageTracker = _gat._getTracker("UA-2931722-6");
    pageTracker._trackPageview();
} catch(err) {}

if ( document.getElementById("under_footer") && screen.height > 1111) {
    document.getElementById("under_footer").style.height = (screen.height - 1111) + 'px';
}

function changeBtnBg()
{
    $("#request_button").attr('src','/themes/main/images/request_button.png');
}

$(document).ready( function()
{
    $("#request_button").mousedown(function(){
          $(this).attr('src','/themes/main/images/request_button_down.png');
          window.setTimeout("changeBtnBg()", 500);
        });
});

// TABS BOX
if($('.tbox_body').length !== 0){
    var currentSkillTab = 0;
	var w_tabs = $('.tbox_body').width();
	$('.tbox_scrollbody').width(((w_tabs+80)*6)+'px');
	$('#hww_arrow_right').click(function(){
	      if ( currentSkillTab < 5 ) {
		      currentSkillTab++;
	          $('.tbox_scrollbody').animate( { left:'-' + (currentSkillTab*(w_tabs + 80))+'px'}, 400);
	          $('#hww_arrow_left').removeClass('hww_arrow_non_left');
	      }
	      if ( currentSkillTab == 5 ) {
	          $(this).addClass('hww_arrow_non_right');
	      }
	});
	$('.tbox_scrollbody .back_button').click(function(){
	      currentSkillTab = 0;
          $('.tbox_scrollbody').animate( { left:'-' + (currentSkillTab*(w_tabs + 80))+'px'}, 400);
   	      $('#hww_arrow_left').addClass('hww_arrow_non_left');
   	      $('#hww_arrow_right').removeClass('hww_arrow_non_right');
	});
	$('#hww_arrow_left').click(function(){
	      if ( currentSkillTab  ) {
   		      currentSkillTab--;
	          $('.tbox_scrollbody').animate( { left:'-'+(currentSkillTab*(w_tabs + 80))+'px'}, 400);
	          $('#hww_arrow_right').removeClass('hww_arrow_non_right');
	      }
	      if ( !currentSkillTab ){
	          $(this).addClass('hww_arrow_non_left');
	      }
	});
}
// end tabs