var ie6 = jQuery.browser.msie && jQuery.browser.version < 7;
var ie7 = jQuery.browser.msie && jQuery.browser.version >= 7 && jQuery.browser.version < 8;

$ = jQuery;

document.createElement('header');
document.createElement('article');
document.createElement('section');
document.createElement('nav');
document.createElement('aside');
document.createElement('hgroup');
document.createElement('footer');

jQuery(function() {
  $('input.default').actsAsDefault();

  $.homeSlideshow();
  $.workSlideshow();
  $.caseStudySlideshow();
  
  $('#sit-social li').click(function() {
    window.location.assign($(this).find('a').attr('href'));
    return false;
  });
  if (ie6) {
    $('#sit-social li').mouseenter(function() {
      $(this).addClass('hover');
    }).mouseleave(function() {
      $(this).removeClass('hover');
    });
  }
  
  if (ie6 || ie7) {
    $('#header-newsletter .wrapper, #header-twitter .wrapper, #header-facebook .wrapper').before('<span class="trigger"></span>');
    $('#header-newsletter, #header-twitter, #header-facebook').mouseover(function() {
      $(this).addClass('hover');
    }).mouseout(function() {
      $(this).removeClass('hover');
    });
  }
});

jQuery.homeSlideshow = function() {
  if (!$('#home-featured')[0]) {
    return false;
  }
  
  var hfW = $('#home-featured').width();
  var margins = (hfW - 1035) / 2;
  
  $('#home-featured-slides').width(hfW * $('#home-featured li').length);
  $('#home-featured li').css('padding', '0 ' + margins + 'px').eq(0).addClass('selected');
  $('#home-featured .inner').after('<span id="home-featured-left"></span> <span id="home-featured-right"></span>');
  $('#home-featured-left, #home-featured-right').hide();
  $('#home-featured').mouseenter(function() {
    $('#home-featured-left, #home-featured-right').fadeIn(250);
  }).mouseleave(function() {
    $('#home-featured-left, #home-featured-right').fadeOut('slow');
  });
  
  $('#home-featured .inner').after('<div id="home-featured-circles"></div>');
  for (var i = 0; i < $('#home-featured li').length; i++) {
    $('#home-featured-circles').append('<span class="home-featured-circle"></span>');
  }
  $('#home-featured-circles span').eq(0).addClass('selected').end().click(function() {
    if ($(this).hasClass('selected')) {
      return false;
    }
    var newIndex = $('#home-featured-circles span').removeClass('selected').index($(this).addClass('selected'));
    $('#home-featured .inner').scrollTo($('#home-featured li').eq(newIndex), {duration: 500});
    $('#home-featured li.selected').removeClass('selected');
    $('#home-featured li').eq(newIndex).addClass('selected');
    clearTimeout(t);
  });
  var cW = 19 * $('#home-featured-circles span').length;
  $('#home-featured-circles').width(cW).css('margin-left', '-' + cW/2 + 'px');
  
  $('#home-featured-left').click(function() {
    homeSlideshowLeft();
    clearTimeout(t);
  });
  
  $('#home-featured-right').click(function() {
    homeSlideshowRight();
    clearTimeout(t);
  });
    
  $(window).resize(function() {
    hfW = $('#home-featured').width();
    margins = (hfW - 1035) / 2;
    
    $('#home-featured-slides').width(hfW * $('#home-featured li').length);
    $('#home-featured li').css('padding', '0 ' + margins + 'px');
    
    $('#home-featured .inner').scrollTo($('#home-featured-slides .selected'), {duration: 0});
  });
  
  var t = setTimeout('homeSlideshowPlay()', 8000);
  
  homeSlideshowPlay = function() {
    homeSlideshowRight();
    t = setTimeout('homeSlideshowPlay()', 8000);
  };
  
  homeSlideshowLeft = function() {
    if ($('#home-featured-slides li').index($('#home-featured-slides .selected')) === 0) {
      var el = $('#home-featured-slides li:last');
    } else {
      var el = $('#home-featured-slides li.selected').prev();
    }
    
    var newIndex = $('#home-featured-slides li').index(el);
    $('#home-featured .inner').scrollTo(el, {duration: 500});
    $('#home-featured li.selected').removeClass('selected');
    el.addClass('selected');
    $('#home-featured-circles span').removeClass('selected').eq(newIndex).addClass('selected');
  };
  
  homeSlideshowRight = function() {
    if ($('#home-featured-slides li').index($('#home-featured-slides .selected')) === $('#home-featured-slides li').length - 1) {
      var el = $('#home-featured-slides li:first');
    } else {
      var el = $('#home-featured-slides li.selected').next();
    }
    
    var newIndex = $('#home-featured-slides li').index(el);
    $('#home-featured .inner').scrollTo(el, {duration: 500});
    $('#home-featured li.selected').removeClass('selected');
    el.addClass('selected');
    $('#home-featured-circles span').removeClass('selected').eq(newIndex).addClass('selected');
  };
  
};

jQuery.workSlideshow = function() {
  if (!$('#work-featured')[0]) {
    return false;
  }
  
  var wfW = $('#work-featured').width();
  var margins = (wfW - 975) / 2;
  
  $('#work-featured-slides').width(wfW * $('#work-featured li').length);
  $('#work-featured li').css('padding', '0 ' + margins + 'px').eq(0).addClass('selected');
  
  $('#work-featured').append('<div id="work-featured-nav"><span id="work-featured-left"></span> <span id="work-featured-right"></span></div>');
  
  $('#work-featured #work-featured-left').after('<div id="work-featured-circles"></div>');
  for (var i = 0; i < $('#work-featured li').length; i++) {
    $('#work-featured-circles').append('<span class="work-featured-circle"></span>');
  }
  $('#work-featured-circles span').eq(0).addClass('selected').end().click(function() {
    if ($(this).hasClass('selected')) {
      return false;
    }
    var newIndex = $('#work-featured-circles span').removeClass('selected').index($(this).addClass('selected'));
    if (!ie6) {
      $('#work-featured .inner').scrollTo($('#work-featured li').eq(newIndex), {duration: 500});
    } else {
      $('#work-featured .inner').scrollTo($('#work-featured li').eq(newIndex), {duration: 0});
    }
    $('#work-featured li.selected').removeClass('selected');
    $('#work-featured li').eq(newIndex).addClass('selected');
    clearTimeout(t);
  });
  var cW = 19 * $('#work-featured-circles span').length;
  $('#work-featured-circles').width(cW);
  
  $('#work-featured-left').click(function() {
    workSlideshowLeft();
    clearTimeout(t);
  });

  $('#work-featured-right').click(function() {
    workSlideshowRight();
    clearTimeout(t);
  });



  $(window).resize(function() {
    
    wfW = $('#work-featured').width();
    margins = (wfW - 975) / 2;

    $('#work-featured-slides').width(wfW * $('#work-featured li').length);
    $('#work-featured li').css('padding', '0 ' + margins + 'px');

    $('#work-featured .inner').scrollTo($('#work-featured-slides .selected'), {duration: 0});
  });

  var t = setTimeout('workSlideshowPlay()', 8000);

  workSlideshowPlay = function() {
    workSlideshowRight();
    t = setTimeout('workSlideshowPlay()', 8000);
  };

  workSlideshowLeft = function() {
    if ($('#work-featured-slides li').index($('#work-featured-slides .selected')) === 0) {
      var el = $('#work-featured-slides li:last');
    } else {
      var el = $('#work-featured-slides li.selected').prev();
    }

    var newIndex = $('#work-featured-slides li').index(el);
    if (!ie6) {
      $('#work-featured .inner').scrollTo(el, {duration: 500});
    } else {
      $('#work-featured .inner').scrollTo(el, {duration: 0});
    }
    $('#work-featured li.selected').removeClass('selected');
    el.addClass('selected');
    $('#work-featured-circles span').removeClass('selected').eq(newIndex).addClass('selected');
  };

  workSlideshowRight = function() {
    if ($('#work-featured-slides li').index($('#work-featured-slides .selected')) === $('#work-featured-slides li').length - 1) {
      var el = $('#work-featured-slides li:first');
    } else {
      var el = $('#work-featured-slides li.selected').next();
    }

    var newIndex = $('#work-featured-slides li').index(el);
    if (!ie6) {
      $('#work-featured .inner').scrollTo(el, {duration: 500});
    } else {
      $('#work-featured .inner').scrollTo(el, {duration: 0});
    }
    $('#work-featured li.selected').removeClass('selected');
    el.addClass('selected');
    $('#work-featured-circles span').removeClass('selected').eq(newIndex).addClass('selected');
  };
};

jQuery.caseStudySlideshow = function() {
  if (!$('article.node-case-study')[0]) {
    return false;
  }
  
  var maxH = 0;
  for (var i = 0; i < $('#slideshow .slide').length; i++) {
    var maxH = ($('#slideshow .slide').eq(i).height() > maxH) ? $('#slideshow .slide').eq(i).height() : maxH;
  }
  $('#slideshow').height(maxH);
  $('#slideshow-inner2').width($('#slideshow .slide').width() * $('#slideshow .slide').length);
  
  $('#slideshow .slide').eq(0).addClass('selected');
  
  $('#slideshow').append('<div id="slideshow-label">' + $('#slideshow .slide:first .caption').text() + '</div>');
  
  $('#slideshow').append('<div id="slideshow-nav"><span id="slideshow-left"></span> <span id="slideshow-right"></span></div>');
  
  $('#slideshow #slideshow-left').after('<div id="slideshow-circles"></div>');
  for (var i = 0; i < $('#slideshow .slide').length; i++) {
    $('#slideshow-circles').append('<span class="slideshow-circle"></span>');
  }
  $('#slideshow-circles span').eq(0).addClass('selected').end().click(function() {
    if ($(this).hasClass('selected')) {
      return false;
    }
    var newIndex = $('#slideshow-circles span').removeClass('selected').index($(this).addClass('selected'));
    $('#slideshow-inner').scrollTo($('#slideshow .slide').eq(newIndex), {duration: 500});
    $('#slideshow-label').animate({opacity: 0}, 200, function() {
      $('#slideshow-label').text($('#slideshow .slide .caption').eq(newIndex).text()).animate({opacity: 1}, 250);
    });
    $('#slideshow .slide.selected').removeClass('selected');
    $('#slideshow .slide').eq(newIndex).addClass('selected');
  });
  var cW = 18 * $('#slideshow-circles span').length;
  $('#slideshow-circles').width(cW);
  
  $('#slideshow-left').click(function() {
    caseStudySlideshowLeft();
  });

  $('#slideshow-right').click(function() {
    caseStudySlideshowRight();
  });
  
  caseStudySlideshowLeft = function() {
    if ($('#slideshow .slide').index($('#slideshow .selected')) === 0) {
      var el = $('#slideshow .slide:last');
    } else {
      var el = $('#slideshow .slide.selected').prev();
    }

    var newIndex = $('#slideshow .slide').index(el);
    $('#slideshow-inner').scrollTo(el, {duration: 500});
    $('#slideshow-label').animate({opacity: 0}, 200, function() {
      $('#slideshow-label').text($('#slideshow .slide .caption').eq(newIndex).text()).animate({opacity: 1}, 250);
    });
    $('#slideshow .slide.selected').removeClass('selected');
    el.addClass('selected');
    $('#slideshow-circles span').removeClass('selected').eq(newIndex).addClass('selected');
  };

  caseStudySlideshowRight = function() {
    if ($('#slideshow .slide').index($('#slideshow .selected')) === $('#slideshow .slide').length - 1) {
      var el = $('#slideshow .slide:first');
    } else {
      var el = $('#slideshow .slide.selected').next();
    }

    var newIndex = $('#slideshow .slide').index(el);
    $('#slideshow-inner').scrollTo(el, {duration: 500});
    $('#slideshow-label').animate({opacity: 0}, 200, function() {
      $('#slideshow-label').text($('#slideshow .slide .caption').eq(newIndex).text()).animate({opacity: 1}, 250);
    });
    $('#slideshow .slide.selected').removeClass('selected');
    el.addClass('selected');
    $('#slideshow-circles span').removeClass('selected').eq(newIndex).addClass('selected');
  };
};

jQuery.fn.actsAsDefault = function() {
  $(this).each(function() {
    var defaultValue = $(this).val();
    $(this).focus(function() {
      if ($(this).val() === defaultValue) {
        $(this).removeClass('default').val('');
      }
    }).blur(function() {
      if ($.trim($(this).val()) === '') {
        $(this).addClass('default').val(defaultValue);
      }
    });
  });
};;
/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);;

