(function(){
	// if firefox 3.5+, hide content till load (or 1 second) to prevent FOUT
	var d = document, e = d.documentElement, s = d.createElement('style');
	if (e.style.MozTransform === '') { // gecko 1.9.1 inference
		s.textContent = 'body{visibility:hidden}';
		var r = document.getElementsByTagName('script')[0];
		r.parentNode.insertBefore(s, r);
		function f(){ s.parentNode && s.parentNode.removeChild(s); }
		addEventListener('load',f,false);
		setTimeout(f,1000); 
	}
})();


$(document).ready(function() {
	
	// test for IE
	var ie = $.browser.msie;
	
	// blur clicked nav links
	$('nav a').click(function(){ $(this).blur() });
	
	var intime	= 250,
		outtime	= 100;
	
	// HEADER NAV ROLLOVERS
	$('header nav > ul > li').each(function(i, li) {
		
		var li = $(li),
			ul = li.find('ul:hidden');
		
		if (ul.length) {
			
			li.hover(
				function(){
					ul.stop().hide().fadeTo(intime, 1, function(){
						if (jQuery.browser.msie) this.style.removeAttribute('filter');
					});
				},
				function(){
					ul.stop().fadeOut(outtime, function(){
						if (jQuery.browser.msie) this.style.removeAttribute('filter');
					});
				}
			);
			
		}
	});
	
	
	// THUMBNAIL ROLLOVERS
	$('.thumb-grid a').each(function(i, a) {
		
		var a = $(a),
			span = a.find('span'),
			li = a.parent(),
			shadow = a.next(),
			opacity = li.css('opacity'),
			activeservice = a.parent('.services-branding li.branding, .services-marketing li.marketing, .services-interactive li.interactive'),
			leftright = (li.hasClass('end')) ? 'right' : 'left',
			active = $('.thumb-grid .active a span'),
			fadetonum = (ie && opacity < 1) ? opacity : 1;
		
		// dim the images in IE
		if (ie && opacity < 1) {
			a.find('img').css('opacity', opacity);
		}
		
		if (!li.hasClass('active')) {
			
			a.hover(
				function(){
					span.stop().hide();
					shadow.stop().fadeTo(intime, fadetonum);
					if (!activeservice.length) {
						active.stop().fadeTo(outtime, 0);
					}
					
					if (leftright == 'right') {
						
						span.css({
								'right': '8px',
								'opacity': 0,
								'display': 'block'
							})
							.animate({
								'right': 0,
								'opacity': fadetonum
							},
							intime,
							function(){
								if (ie && opacity >= 1) this.style.removeAttribute('filter');
							});
							
					} else {
						
						span.css({
								'left': '-8px',
								'opacity': 0,
								'display': 'block'
							})
							.animate({
								'left': 0,
								'opacity': fadetonum
							},
							intime,
							function(){
								if (ie && opacity >= 1) this.style.removeAttribute('filter');
							});
					}
				},
				function(){
					span.stop().fadeOut(outtime);
					if (!activeservice.length) {
						shadow.stop().fadeTo(outtime, 0);
					}
					active.stop().fadeTo(intime, 1);
				}
			);
		}
		
	});
	
	
	// PROJECT IMAGE BEHAVIORS
	
	var imgholder = $('#img-holder');
	
	if (imgholder.length) {
		
		// set height for first image and show text
		var img0 = $('#img-0');
		img0.one("load", function(){
			imgholder.css('height', img0.outerHeight(true));
			$('#project-text').show();
		})
		.each(function(){
			if(this.complete) {
				$(this).trigger("load");
			}
		});
		
		var imgs = $('#img-holder img');
				
		if (imgs.length > 1) {
			
			// set current image
			var curr = 0;
			
			// create image nav container
			var imgnav = $('<div id="imgnav"></div>').insertBefore('#img-holder');
			
			// add previous image arrow
			$('<a id="prev" class="ir" href="#Previous">Previous Image</a>')
				.appendTo(imgnav)
				.click(function(){
					var prev = (curr == 0) ? (imgs.length - 1) : (curr - 1);
					imgtrans(prev);
					$(this).blur();
					return false;
				});
			
			// add image nav squares
			imgs.each(function(i){
				var status = (i == 0) ? 'active' : '';
				$('<a id="imgnav-' + i + '" class="ir imgsquare '+ status + '" href="#Image' + (i + 1) + '">Image ' + (i + 1) + '</a>')
					.appendTo(imgnav)
					.click(function(){
						imgtrans(i);
						$(this).blur();
						return false;
					});
			});
			
			// add next image arrow
			$('<a id="next" class="ir" href="#Next">Next Image</a>')
				.appendTo(imgnav)
				.click(function(){
					var next = (curr == (imgs.length - 1)) ? 0 : (curr + 1);
					imgtrans(next);
					$(this).blur();
					return false;
				});
		}
	}
	
	function imgtrans(i) {
		
		// change active square
		$('#imgnav a').removeClass('active');
		$('#imgnav-' + i).addClass('active');
		curr = i;
		
		// fade out image(s)
		$('#img-holder img:visible').stop().fadeOut(750);
		
		// fade in image
		var img_height = $('#img-' + i)
							.stop()
							.css({
								'opacity': 0,
								'display': 'block'
							})
							.animate({
								'opacity': 1
							},
							{
								'duration': 750
							})
							.outerHeight(true);
		// move text
		$('#img-holder')
			.stop()
			.animate({
				'height': img_height
			}, 750)
		
	}
	
});

window.log = function(){
  log.history = log.history || [];  
  log.history.push(arguments);
  arguments.callee = arguments.callee.caller;  
  if(this.console) console.log( Array.prototype.slice.call(arguments) );
};
(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();)b[a]=b[a]||c})(window.console=window.console||{});
