/* Copyright (c) 2008 Kean Loong Tan http://www.gimiti.com/kltan
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Copyright notice and license must remain intact for legal use
 * jFade
 * Version: 1.0 (Jun 30, 2008)
 * Requires: jQuery 1.2.6+
 * 
 *
 * Original Code Copyright (c) 2008 by Michael Leigeber
 * Website: http://www.leigeber.com
 * 
 *
 */ 
(function(A){A.fn.jFade=function(N){var B=A.extend({},A.fn.jFade.defaults,N);var F,C,K,E,H,J,I,M,D;var G=this;var L=function(){var Q=G;B.steps=B.steps||20;B.duration=B.duration||20;clearInterval(Q.timer);C=O(B.end);K=C[0];E=C[1];H=C[2];if(!Q.r){F=O(B.start);r=F[0];g=F[1];b=F[2];Q.r=r;Q.g=g;Q.b=b}J=Math.round(Math.abs(Q.r-K)/B.steps);I=Math.round(Math.abs(Q.g-E)/B.steps);M=Math.round(Math.abs(Q.b-H)/B.steps);if(J==0){J=1}if(I==0){I=1}if(M==0){M=1}Q.step=1;Q.timer=setInterval(function(){P(Q,B.property,B.steps,K,E,H,J,I,M)},B.duration);function P(V,d,a,e,U,X,c,Y,f){var T=V;var S;if(T.step<=a){var R=T.r;var W=T.g;var Z=T.b;if(R>=e){R=R-c}else{R=parseInt(R)+parseInt(c)}if(W>=U){W=W-Y}else{W=parseInt(W)+parseInt(Y)}if(Z>=X){Z=Z-f}else{Z=parseInt(Z)+parseInt(f)}S="rgb("+R+","+W+","+Z+")";A(V).css(d,S);T.r=R;T.g=W;T.b=Z;T.step=T.step+1}else{clearInterval(T.timer);S="rgb("+e+","+U+","+X+")";A(V).css(d,S)}}function O(R){var S=[parseInt(R.substring(0,2),16),parseInt(R.substring(2,4),16),parseInt(R.substring(4,6),16)];return S}};if(B.trigger=="load"){L()}else{A(this).bind(B.trigger,function(){G=this;L()})}return this};A.fn.jFade.defaults={trigger:"load",property:"background",start:"FFFFFF",end:"000000",steps:5,duration:30}})(jQuery);
function badBrowser(){
    if($.browser.msie && parseInt($.browser.version) <= 6){ return true;}
    
    return false;
}

function getBadBrowser(c_name)
{
    if (document.cookie.length>0)
    {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1)
        { 
        c_start=c_start + c_name.length+1; 
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
        } 
    }
    return "";
}   

function setBadBrowser(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

if(badBrowser() && getBadBrowser('browserWarning') != 'seen' ){
    $(function(){
        $("<div id='browserWarning'>You are using an unsupported browser. Please switch to <a href='http://getfirefox.com'>FireFox</a> or <a href='http://www.apple.com/safari/'>Safari</a>&nbsp;&nbsp;&nbsp;[<a href='#' id='warningClose'>close</a>] </div> ")
            .css({
                backgroundColor: '#fcfdde',
                'width': '100%',
                'border-top': 'solid 1px #000',
                'border-bottom': 'solid 1px #000',
                'text-align': 'center',
                padding:'5px 0px 5px 0px'
            })
            .prependTo("body");
        
        $('#warningClose').click(function(){
            setBadBrowser('browserWarning','seen');
            $('#browserWarning').slideUp('slow');
            return false;
        });
    }); 
}
/*******

    *** Anchor Slider by Cedric Dugas   ***
    *** Http://www.position-absolute.com ***
    
    Never have an anchor jumping your content, slide it.

    Don't forget to put an id to your anchor !
    You can use and modify this script for any project you want, but please leave this comment as credit.
    
*****/
        
jQuery.fn.anchorAnimate = function(settings) {

    settings = jQuery.extend({
        speed : 900
    }, settings);   
    
    return this.each(function(){
        var caller = this
        $(caller).click(function (event) {  
            event.preventDefault()
            var locationHref = window.location.href
            var elementClick = $(caller).attr("href")
            
            var destination = $(elementClick).offset().top;
            $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
                window.location.hash = elementClick
            });
            return false;
        })
    })
}
$(document).ready(function() {
	$("a.scroll").anchorAnimate()
});

/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);



/** CUSTOM SCRIPTS **/
$(document).ready(function(){

	var content = $('body');
    var contentOffset = content.offset();
    
    $('#back-to-top').hide();
    $(window).scroll(function() {
        var scrollDistance = $(this).scrollTop();
        if (scrollDistance > contentOffset.top) {
            $('#back-to-top').fadeIn('fast');
        }

        if (scrollDistance <= contentOffset.top) {
            $('#back-to-top').fadeOut('fast');
        }
    });
	
	$('article.thumb img').load(function(){
		$('article.thumb img').desaturate();
	});
	
	$('a.thumblink').hoverIntent( hideImage, showImage );
    
    $('a.thumblink').click(function(){
		caseStudy = $(this).attr('id');
    	$('article.full').slideUp();
    	$('a.thumblink').removeClass('active');
    	$('a.thumblink').not('#'+caseStudy).children('canvas').animate({opacity:1}, 300);
       		if ($('article#'+caseStudy).is(':visible')){
    			$('article.full').slideUp();
    			$('a.thumblink').removeClass('active');
    		} else {
    			$(this).addClass('active');
    			$('article#'+caseStudy).slideDown();
    		}
    	return false;
    });
    
    $("a").each(function(){
        if (this.href.indexOf(location.hostname) == -1) { $(this).attr('target', '_blank'); } 
    });
    
    $('nav#download').hide();
    setTimeout(function(){
    	$("nav#download").fadeIn('500')
    }, 1500);
        
});

function hideImage(){
	$(this).not('.active').children('canvas').animate({opacity:0.1}, 300);
}

function showImage(){
	$(this).not('.active').children('canvas').animate({opacity:1}, 300);
}


