/*
CSS Browser Selector v0.3.5 (Feb 05, 2010)
Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/
/*
function css_browser_selector(u){var ua = u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1;},g='gecko',w='webkit',s='safari',o='opera',h=document.documentElement,b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3.5')?g+' ff3 ff3_5':is('firefox/3')?g+' ff3':is('gecko/')?g:is('opera')?o+(/version\/(\d+)/.test(ua)?' '+o+RegExp.$1:(/opera(\s|\/)(\d+)/.test(ua)?' '+o+RegExp.$2:'')):is('konqueror')?'konqueror':is('chrome')?w+' chrome':is('iron')?w+' iron':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?'mobile':is('iphone')?'iphone':is('ipod')?'ipod':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win':is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;}; css_browser_selector(navigator.userAgent);
*/


// Test for text-shadow support
// Note: FF3.0 will false positive on this test :(
/*
Modernizr.addTest('textshadow',function(){ 
    return document.createElement('div').style.textShadow === '';
});
*/

Modernizr.addTest('useragentwin',function(){
	ua = navigator.userAgent;
    if (ua.match(/Win(dows )?NT 6\.0/)) {
		return true; // Windows Vista ???
	}
	else if (ua.match(/Win(dows )?NT 5\.2/)) {
		return true; // Windows Server 2003 ???
	}
	else if (ua.match(/Win(dows )?(NT 5\.1|XP)/)) {
		return true; // Windows XP ???
	}
	else if (ua.match(/Win(dows)? (9x 4\.90|ME)/)) {
		return true; // Windows ME ???
	}
	else if (ua.match(/Win(dows )?(NT 5\.0|2000)/)) {
		return true; // Windows 2000 ???
	}
	else if (ua.match(/Win(dows )?98/)) {
		return true; // Windows 98 ???
	}
	else if (ua.match(/Win(dows )?NT( 4\.0)?/)) {
		return true; // Windows NT ???
	}
	else if (ua.match(/Win(dows )?95/)) {
		return true; // Windows 95 ???
	}
	return false;
});

/*
 * Author: http://www.thinktmb.com
 */

$(document).ready(function() {

	/*
	 * Script: Cufon replacement
	 */
	
	Cufon.replace('.navigation a', {
		hover:true, hoverables:{a:true},
		fontFamily: 'Helvetica Neue Light'
	});

	Cufon.replace('h1', {
		hover:true, hoverables:{a:true},
		fontFamily: 'Helvetica Neue Light'
	});
	
	Cufon.replace('h2',{fontFamily: 'Helvetica Neue thin'});
	
	/*
	Cufon.replace('.sub-navigation a', {
		hover:true, hoverables:{a:true},
		fontFamily: 'Helvetica Neue Light'
	});
	*/
	
	/*
	 * Script: Fixed leftColumn Animated
	 */
	
	/*
	var offset = $("#leftColumn").offset();
	var limit = ($(document).height()-$("#leftColumn").height())-20;

    $(window).scroll(function() {		
        if ($(window).scrollTop() > offset.top) {
			if(($(window).scrollTop() - offset.top) < limit){
	            $("#leftColumn").stop().animate({
	                marginTop: $(window).scrollTop() - offset.top
	            });
			}else{
				$("#leftColumn").stop().animate({
	                marginTop: limit
	            });
			}
        } else {
            $("#leftColumn").stop().animate({
                marginTop: 0
            });
        }
    });
	*/
	
	/*
	 * Generic: Fixing positions
	 * Only higher than IE 6 & 7 + Check height of browser window
	 */
	
	if(!$("body").hasClass('ie6')&&!$("body").hasClass('ie7')&&($("#leftColumn").height()<$(window).height())){
		$("body").css({
			"background-attachment": "fixed"
		});
	
		$("#leftColumn").css({
			"position": "fixed"
		});
	
		$("#services #rightColumn aside").css({
			"position": "fixed",
			"margin-top": "118px"
		})
	}
	
	/*
	 * Home: Rollover
	 */
	
	$("#home #our-work ul li").not(".intro").each(function()
	{

		var parent = $(this);
		
		$("img, h3", this).hover(
			function () {
				$("img", parent).stop().animate({"opacity": 0.7}, "fast");
				$("h3 a", parent).addClass("active");
			},
			function () {
				$("img", parent).stop().animate({"opacity": 1}, "fast");
				$("h3 a", parent).removeClass("active");
			}
		);
	});
	
	/*
	 * About: Scroll
	 */
	
	/*
	$.easing.elasout = function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	};
	
	if( $("a.scrollTo").length )
	{
		$.scrollTo(0);
	
		$('a.scrollTo').click(function(){
			$.scrollTo( this.hash, 2500, { easing:'elasout' });
			return false;
		});
	}
	*/

	/*
	 * Services & Clients: Fading
	 */
		
	if( $("#services #grid article").length ){
		var items = $("#services #grid > article").css({opacity:0});
		nextItemFade(items);
	}
	
	if( $("#clients section article").length ){
		var items = $("#clients section > article").css({opacity:0});
		nextItemFade(items);
	}
	
	function nextItemFade(items) {
		items.eq(0).animate({opacity:1}, "fast", function() {
			nextItemFade(items.slice(1));
		});
	}
	
	/*
	 * Services: Highlight & Rollover
	 */
		
	$("#services #grid article").each(function()
	{

		var services = [];
		var active = '';
		var parent = $(this);
		
		if($("aside a").hasClass('active')){
			active = $("aside a.active").attr("class").split(' ')[0];
		}
		
		$("ul li a", this).each(function() {
			if(!$(this).hasClass(active)) {
				services.push(
					$(this).attr("class")
				);
			}
		});
		
		$("img, h3", this).hover(
		  function () {
			$.each(services, function(index, value) {
				$("aside ." + value).addClass("active");
			});
			$("img", parent).stop().animate({"opacity": 0.7}, "fast");
			$("h3 a", parent).addClass("active");
			
			// Add service anchor
			var service = window.location.pathname.split('/')[2];
			if(service&&service!="client") {
				$("a", parent).attr("href", $("a", parent).attr("href")+"#"+service);
			}
			
		  },
		  function () {
			$.each(services, function(index, value) {
				$("aside ." + value).removeClass("active");
			});
			$("img", parent).stop().animate({"opacity": 1}, "fast");
			$("h3 a", parent).removeClass("active");
			
			// Remove service anchor
			var href = $("a", parent).attr("href");
			var anchor_index = href.indexOf('#');			
			if (anchor_index != -1) {
		    	$("a", parent).attr("href", href.substring(0, anchor_index));
		    }
			
		  }
		).click(function(){
			$("img", parent).stop().animate({"opacity": 1}, "fast");
		});
		
		//log(services.toSource());
		
	});
	
	/*
	 * Showcase: Highlight
	 */
	
	$("#services section article").each(function()
	{
		
		var services = [];
		
		$("nav span a", this).not(".buttons a").each(function() {
			services.push(
				$(this).attr("class")
			);
		});
		
		$.each(services, function(index, value) {
			$("aside ." + value).addClass("active");
		});
		
		//log(services.toSource());
		
	});
	
	/*
	 * Clients: Rollover
	 * Preload: $(window).bind('load', function() {});
	 */
	
	$("#clients section article").each(function()
	{
		
		/*
		$(window).bind('load', function() {

			log("Window Load");

			var sources = new Array("mazda","fiat","kia","vw","santander","systagenix","merck-serono","deloitte","medium","baa","hp","panasonic","samsung","sanyo","sol-melia","jumeirah","virgin-active","daiichi-sankyo","bard","chevron","hauser-bears","texaco","wips");
			var images = new Array();

			for (i = 0, length = sources.length; i < length; ++i) {
				images[i] = new Image();
				images[i].src = "/img/clients/"+sources[i]+"-off.gif";
				log("/img/clients/"+sources[i]+"-off.gif");
			}
		});
		*/
		
		var link = $("a", this).attr("href");
		var name = $("img", this).attr("src").match(/(.*)[\/\\]([^\/\\]+)\.\w+$/);
		var alt = $("img", this).attr("alt");
				
		if($(this).hasClass('active')){
		
			$(this).prepend('<img class="over" src="/img/clients/'+name[2]+'-off.gif" alt="'+alt+'" />');
			$(".over", this).hover(
				function () {
					$(this).stop().animate({"opacity": 0}, "fast");
				},
				function () {
					$(this).stop().animate({"opacity": 1}, "fast");
				}
			).click(function(){
				window.location = link;
				$(this).stop().animate({"opacity": 1}, "fast");
			});
		
		}else{
			
			$(this).prepend('<img class="over" src="/img/clients/'+name[2]+'-off.gif" alt="'+alt+'" />');
						
		}
	});
	
	/*
	 * Script: Tooltip
	 */
	
	if( $("a.screenshot, area.screenshot").length )
	{
		xOffset = 210;
		yOffset = -132;

		$("a.screenshot, area.screenshot").hover(function(e){
			var content = $(this).attr("id");
			
			if("AREA"==this.tagName){
				$("body").append("<div id='screenshot'><div id='screenshotBox'><div class='filler'>"+ content +"</div></div><div class='triangle'></div>"+"</div>");
			}else{
				$("body").append("<div id='screenshot'><div id='screenshotBox'><img src='"+ content +"'  /></div><div class='triangle'></div>"+"</div>");								 
			}
			$("#screenshot").css("top",(e.pageY - xOffset) + "px")
							.css("left",(e.pageX + yOffset) + "px")
							.fadeIn("fast");						
			},function(){	
			$("#screenshot").remove();
		});
		
		$("a.screenshot, area.screenshot").mousemove(function(e){
			$("#screenshot").css("top",(e.pageY - xOffset) + "px")
							.css("left",(e.pageX + yOffset) + "px");
		});	
	}

});
