
$(document).ready(function() {

  // -----------------------------------------------------
  //             Administrative functions
  // -----------------------------------------------------
  // PREPARE QUERY STRING // Takes in the query string and returns it as an object
  // -----------------------------------------------------
     function prepareQueryString(QS) {if(QS=='') return false;QS=QS.replace(/^\?/,'');var QSA=QS.split('&');var _final={};$.each(QSA,function(x,y){var temp=y.split('=');_final[temp[0]]=temp[1];});return _final;}
  // -----------------------------------------------------
  // CLEANSE // Takes in a string, strips special characters and spaces, returns the result
  // -----------------------------------------------------
     function cleanse(x){x=x.replace(/[^a-zA-Z 0-9]+/g,'');x=x.replace(/\s/g,'');return x;}
  // -----------------------------------------------------
  
  if($('#hero').length!=0) {
    if ($('body').width() > 648) {
      $('#hero').slidy({
        animation:  'fade',
        children:   'a',
        menu:       true,
        pause:      true,
        speed:      400,
        time:       6000,
        width:      1068,
        height:     267
      });
    } else {
      $('#hero').slidy({
        animation:  'fade',
        children:   'a',
        menu:       true,
        pause:      true,
        speed:      400,
        time:       6000,
        width:      648,
        height:     162
      });
    }
    $('.slidy-menu').after('<div style="clear:both"></div>');
  }
  
  if($('#tab-panel').length!=0) $('#tab-panel').tabs();

  if($('#contactForm').length!=0) {
    $('#contactForm').validate({
      messages: {
        firstname:  'Please provide your first name.',
        lastname:   'Please provide your last name.',
        title:      'Please provide your title.',
        company:    'Please provide the name of your company.',
        phone:      'Please provide a phone number to reach you at.',
        email:  {
          required: 'Please provide your email address.',
          email:    'The address you provided is invalid.'
        },
        comments:   'Please tell us how we can help you.'
      },
      errorContainer:      '#error-messages',
      errorLabelContainer: '#error-messages',
      wrapper: 'span', 
      debug: true
    });
  }
/*
  $('#people-listing .work-item').click(function() {
  
    var person = {
      'name'  : $('.user-name',this).text(),
      'title' : $('.user-title',this).text(),
      'bio'   : $('.user-bio',this).html(),
      'pic'   : $(this).attr('rel')
    };
    
    $('article h2').text(person.name).addClass('no-border');
    $('article h3').show().text(person.title);
    $('article .bio-container').addClass('border-top');
    $('article .bio-story').html(person.bio);
    $('article .bio-picture').show().attr('src',person.pic);

    $('.work-item').removeClass('active');
    $(this).addClass('active');
	
	window.scrollTo(0,0);
  
  });
*/
  $('#portfolio-listing .work-item').click(function() {
    
    $('.ui-tabs').css('visibility','visible');
    $('.default-text').hide();



        
    var client = {
      'name'          : $('.client-name',this).text(),
      'tagline'       : $('.client-tagline',this).text(),
      'overviewInfo'  : $('.information .client-overview',this).html(),
      'overviewImage' : $('.information .client-overview',this).attr('rel'),
      'strategyInfo'  : $('.information .client-strategy',this).html(),
      'strategyImage' : $('.information .client-strategy',this).attr('rel'),
      'processInfo'   : $('.information .client-process',this).html(),
      'processImage'  : $('.information .client-process',this).attr('rel'),
      'resultsInfo'   : $('.information .client-results',this).html(),
      'resultsImage'  : $('.information .client-results',this).attr('rel')
    };

    $('article h2').text(client.name).addClass('no-border');
    $('article h3').show().text(client.tagline);
    $('article #Overview img').attr('src',client.overviewImage);
    $('article #Overview div').html(client.overviewInfo);
    $('article #Strategy img').attr('src',client.strategyImage);
    $('article #Strategy div').html(client.strategyInfo);
    $('article #Process img').attr('src',client.processImage);
    $('article #Process div').html(client.processInfo);
    $('article #Results img').attr('src',client.resultsImage);
    $('article #Results div').html(client.resultsInfo);


    $('article a[href=#Overview]').trigger('click');

    $('.work-item').removeClass('active');
    $(this).addClass('active');
    

    $('article img').each(function(){ if($(this).attr('src')=='')$(this).hide(); });
    $('article img').show();
	
	window.scrollTo(0,0);

  });
  
  $('#news-listing .work-item').click(function() {
  
    var news = {
      'title'   : $('.news-title',this).text(),
      'release' : $('.news-release',this).html()
    };
    
    $('article h2').text(news.title);
    $('article div').html(news.release);
    
    $('.work-item').removeClass('active');
    $(this).addClass('active');
	
	window.scrollTo(0,0);
  
  });
  
  var queryObject = prepareQueryString(location.search);
  if (queryObject) {
    queryObject.panel = queryObject.panel.replace(/-/g,' ');
    queryObject.panel = queryObject.panel.replace(/%26/g,'&');
    $('.work-item').removeClass('active');
    $('.work-item span').each(function() { 
      if ($(this).text() == queryObject.panel) $(this).parent().parent().trigger('click'); 
      if ($(this).attr('rel') == queryObject.panel) $(this).parent().trigger('click'); 
    });
    if(queryObject.tab) $('li a[href=#'+queryObject.tab+']').trigger('click');
  }

  $.trackPage('UA-16263643-1');
  
  $('h2, h3').addClass('section');

  $('aside a, aside .work-item').track({
    category : function(el) { // Name of the header
      if ( $(el).parents().filter('.section').length!=0 ) {
        return cleanse($(el).parents().filter('.section:eq(0)').text());
      } else  if ( $(el).siblings('.section').length!=0 ) {
        return cleanse($(el).siblings('.section:eq(0)').text());
      } else { return false; }
    },
    label : function(el) { // The clients / persons name
      if ( $(el).hasClass('work-item') && $(el).find('.client-name').length!=0 ) {
        return cleanse($(el).find('.client-name').text());
      } else if ( $(el).hasClass('work-item') && $(el).find('.news-date').length!=0 ) {
        return cleanse($(el).find('.news-date').text());
      } else if ( $(el).hasClass('work-item') && $(el).find('.user-name').length!=0 ) {
        return cleanse($(el).find('.user-name').text());
      } else { return false; }
    }
  });
  
  $('.ui-tabs-nav a').track({
    category : function(el) { return cleanse($('article h2').text()); },       // Name of the client
    label    : function(el) { return cleanse($(el).children('span').text()); } // Name of the tab
  });
  
  $('h2, h3').removeClass('section');


// image preloads
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)
  jQuery.preLoadImages(
    'images/gctOverview.jpg',
    'images/gctStrategy.jpg',
    'images/gctProcess.jpg',
    'images/gctResults.jpg',
    
    'images/tcsOverview.jpg',
    'images/tcsStrategy.jpg',
    'images/tcsProcess.jpg',
    'images/tcsResults.jpg',
    
    'images/slOverview.jpg',
    'images/slStrategy.jpg',
    'images/slProcess.jpg',
    'images/slResults.jpg',
    
    'images/aonOverview.jpg',
    'images/aonStrategy.jpg',
    'images/aonProcess.jpg',
    'images/aonResults.jpg',
    
    'images/motoOverview.jpg',
    'images/motoStrategy.jpg',
    'images/motoProcess.jpg',
    'images/motoResults.jpg',
    
    'images/hsOverview.jpg',
    'images/hsStrategy.jpg',
    'images/hsProcess.jpg',
    'images/hsResults.jpg',
    
    'images/azOverview.jpg',
    'images/azStrategy.jpg',
    'images/azProcess.jpg',
    'images/azResults.jpg'
  );
});
