

$(function(){
  $(window).resize(function(){
    var body = $("body");
    var width = getClosestWidth();
    if(!body.hasClass(WIDTH_CLASS+width)&&navigator.userAgent.indexOf('iPad')==-1) {
      var bodyclasses = body[0].className.split(" ");
      for(var a = bodyclasses.length-1; a>=0; a--) {
        if(bodyclasses[a].indexOf(WIDTH_CLASS)==0){bodyclasses.splice(a,1);}
      }
      body[0].className = bodyclasses.join(" ");
      body.addClass(WIDTH_CLASS+width);
    }
    $("#project-content img").each(function(){resizeImage(this)});
    $("#project-content video").each(function(){resizeImage(this)});
    $("#project-content embed").each(function(){resizeImage(this)});
    $("#project-content div").each(function(){resizeImage(this)});
    $("#project-content object").each(function(){resizeImage(this)});
  });
  $(window).resize();
  
  
  if($("body").hasClass("stuff")) {
    setInterval(function(){
      var r = $("dl.emails dd.received").text().split(",").join("");
      r= Number(r)+1;
      $("dl.emails dd.received").text(number_format(r));
    }, 5000);
    
    setInterval(function(){
      var r = $("dl.emails dd.sent").text().split(",").join("");
      r= Number(r)+1;
      $("dl.emails dd.sent").text(number_format(r));
    }, 9000);
  }
  


  $("#project-content img.no-print").load(function(){
      var t = $(this);
      t.attr("data-width", t.width());
      t.attr("data-height", t.height());
      resizeImage(this);
   });
   
   $("#project-content img.print").load(function(){
      var p = $(this);
      p.attr("data-width", p.width());
      p.attr("data-height", p.height());
      resizePrintImage(this);
   });
   
  
});


Delegate={create:function(o,f){var aa=[];if(arguments.length>2)
for(var a=2;a<arguments.length;a++){aa.push(arguments[a]);}
var _f=function(){var aaa=[];for(var a=0;a<arguments.length;a++){aaa.push(arguments[a]);}
return f.apply(o,aaa.concat(aa));}
return _f;}}


getClosestWidth = function() {
var w=(document.documentElement.clientWidth/WIDTH_STEP >> 0) * WIDTH_STEP;
//alert(document.documentElement. clientWidth);
return (w>WIDTH_MIN) ? ((w<WIDTH_MAX) ? w : WIDTH_MAX ): WIDTH_MIN
}


function resizeImage(image) {
  
  var ratio = 1;
  if($("body").hasClass("s900")){
    ratio = 1.37853658536585;
  }
  if(navigator.userAgent.indexOf('iPad')!=-1) {
    ratio = 1.641426900584795;
  }
  
  i = $(image);
  printWidth = ((i.attr("data-width")/ratio)/1136)*100;
  if( i.attr("data-width")!=undefined ) {
    i.width( i.attr("data-width")/ratio )
    i.height( i.attr("data-height")/ratio )
    
    
  /*
  e.width( i.attr("data-width")/ratio )
    e.height( i.attr("data-height")/ratio )
*/
  }
  
}

function resizePrintImage(printImage) {
  
  var ratio = 1;
  if($("body").hasClass("s900")){
    ratio = 1.37853658536585;
  }
  if(navigator.userAgent.indexOf('iPad')!=-1) {
    ratio = 1.641426900584795;
  }
  
  iP = $(printImage);
  printWidth = ((iP.attr("data-width")/ratio)/1136)*100;
  if( iP.attr("data-width")!=undefined ) {
    iP.width( printWidth+'%' )
    iP.height('auto')
  }
  
}

$('h2.stage-carousel ul').cycle({ 
    fx:         'fade', 
    speed:		 5000,
    timeout:     2000, 
    pager:      'ul.project-thumbs',
    pagerEvent: 'mouseover', 
    pagerAnchorBuilder: function(idx, slide) { 
        // return selector string for existing anchor 
        return 'ul.project-thumbs li:eq(' + idx + ') a'; 
    },
    fastOnEvent: true,
    allowPagerClickBubble: true
    
});

// This is to ensure touch devices click through to the project page without simulating the above onMouseOver event
$('ul.project-thumbs li a').live('touchend', function(e) {
    var el = $(this);
    var link = el.attr('href');
    window.location = link;
});


$(document).ready(function(){

	$("#footer-company-info").hide(); 

	$("#footer-tab h5 a").click(function(){
		$('#footer-company-info').slideToggle(function(){
			$('html,body').animate({scrollTop: $("#footer-company-info").offset().top},'fast', 'swing');
			});
		return false;
	});
	
	
	
	$("#project-stage-social ul li").mouseenter(function(){
	 	$(this).children("div").hide();
	 	$(this).children("p").hide();
	 	$(this).children("div").fadeIn(300, function() {
        // Animation complete
      	});
	});

	$("#project-stage-social ul li.large").mouseenter(function(){
	 	$(this).stop().animate({width: "175px"}, 200, "swing");
	});
	
	$("#project-stage-social ul li.small").mouseenter(function(){
	 	$(this).stop().animate({width: "145px"}, 200, "swing");
	});
	
	$("#project-stage-social ul li.full").mouseenter(function(){
	 	$(this).stop().animate({width: "392px", height: "65px"}, 200, "swing");
	 	$(this).children("p").fadeIn(750, function() {
        // Animation complete
      	});
	});
	
	$("#project-stage-social ul li").mouseleave(function(){
		$(this).children("div").fadeOut(100, function() {
        // Animation complete
      	});
	 	$(this).stop().animate({width: "45px", height: "45px"}, 200, "swing");
	 	$(this).children("p").fadeOut(100, function() {
        // Animation complete
      	});
	 	
	});



});


function number_format(number, decimals, dec_point, thousands_sep) {
    number = (number+'').replace(',', '').replace(' ', '');
    var n = !isFinite(+number) ? 0 : +number, 
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}
