jQuery.noConflict();
(function($) { 
$(function() {
var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));
//--GLOBAL--\\	
//simple reveal
    $('a.reveal').click(function (e) {
        e.preventDefault();
        $this = $(this).parent('p');
        $reveal = $this.prev('div.reveal');
        if ($this.hasClass('open')) {
            $this.removeClass('open');
			$(this).text('Read about our History');
            $reveal.slideUp(400);
        } else {
            $this.addClass('open');
            $reveal.slideDown(400);
			$(this).text('Close');
        }
    });

//---COMPANY---\\	
	//people page stat count
	$('#sort li a span').each(function(){
		$this = $(this);
		var thisClass = $this.parents('li').attr('class');
		var thisCount = $('#employees li.'+thisClass).size();
		$this.text(thisCount);
	});
	//sort by position	
	$('#view[class=people] li').click(function(e){
		e.preventDefault();
		var $this = $(this);
		$('#view li a').removeClass('on');
		$this.find('a').addClass('on');
		var showItem = $this.attr('class');
		$('#employees').find('li.'+showItem).addClass('filtered').stop(true,true).animate({'width':'177px','margin-right':'4px','margin-left':'8px'}, 400).children('a').animate({'height':'220px','width':'177px','margin-top':'-110px','margin-left':'-88px'}, 400);
		$('#employees').find('li').not('.'+showItem).removeClass('filtered').children('a').stop(true,true).animate({'height':'0','width':'0','margin-top':'-22px','margin-left':'0'}, 400, function() {
			$('#employees').find('li').not('.'+showItem).animate({'width':'0','margin-right':'0','margin-left':'0'}, 400)
		});
		$('#sort li a span').each(function(){
			$this = $(this);
			var thisClass = $this.parents('li').attr('class');
			var thisCount = $('#employees li.filtered.'+thisClass).size();
			$this.text(thisCount);
		});
	});
	//sort by stats
	$('#sort li').click(function(e){
		e.preventDefault();
		var $this = $(this);
		var showItem = $this.attr('class');
		$('#sort li a').removeClass('on');
		$this.find('a').addClass('on');
		$('#employees').find('li.filtered.'+showItem).stop(true,true).animate({'width':'177px','margin-right':'4px','margin-left':'8px'}, 400).children('a').animate({'height':'220px','width':'177px','margin-top':'-110px','margin-left':'-88px'}, 400);
		$('#employees').find('li.filtered').not('.'+showItem).children('a').stop(true,true).animate({'height':'0','width':'0','margin-top':'-22px','margin-left':'0'}, 400, function() {
			$('#employees').find('li.filtered').not('.'+showItem).animate({'width':'0','margin-right':'0','margin-left':'0'}, 400)
		});
	});
	//careers images
	$('#listings li').hover(function(){
		$(this).find('img').stop(true,true).fadeIn(600);
		}, function() {
		$(this).find('img').stop(true,true).fadeOut(400);
	});
	
//---EXPERTISE---\\
	$('#inner-nav a').click(function(e){
		e.preventDefault();
		var thisID = $(this).attr('href');
	$('html, body').animate({
		scrollTop: $(thisID).offset().top
	}, 2000);
		
	});
	
//---WORK---\\
	$('#work div.info').each(function(){
		var infoH = $(this).height() / 2.2;
		$(this).css({marginTop:-infoH,'top':'45%'});
	});
	
	if (mobile) { $("div.item").show(); } else {
	$("div.item").hide();
	//$('div.item:first').fadeIn(1000);
	var winH = $(window).height();
	var topH = $('#featured').height();
	var showItems = Math.round((winH - topH) / 530);
	$('div.show:hidden:lt('+showItems+')').fadeIn(1000);
	window.onscroll = function () {
		var height = $(window).height();
		var scrollTop = $(window).scrollTop();
		var obj = $('#footer');
		var pos = obj.position();
		if (height + scrollTop > pos.top) {
		   $('div.show:hidden:first').fadeIn(1000);
		}	
	}
	}
	
	$('#view[class=work] li').click(function(e){
		e.preventDefault();
		var $this = $(this);
		$('#view li a').removeClass('on');
		$this.find('a').addClass('on');
		var showItem = $this.attr('class');
		$('#work').find('div.item').not('.'+showItem).removeClass('show').hide();
		var showItems = Math.round((winH - topH) / 530);
		$('#work').find('div.item.'+showItem).addClass('show');
		$('div.show:lt('+showItems+')').fadeIn();
	});
	 
//---BLOG---\\
	//tags dropdown
	var tagHover = {    
		 over: showTags,    
		 timeout: 600,   
		 out: hideTags    
	}
	$('#topics').hoverIntent(tagHover);
	$('#topics span').click(function(e){
		e.preventDefault();
		$('#tags').slideUp(300);
	});
	//share
	var shareHover = {    
		 over: showShare,    
		 timeout: 600,   
		 out: hideShare    
	}
	$('div.share').hoverIntent(shareHover);
	
/*	$('div.share').hover(function(){
		$this = $(this);
		$this.addClass('active').find('ul').stop(true,true).slideDown(500); 
	}, function(){
		$this.find('ul').stop(true,true).slideUp(300, function() {
			$this.removeClass('active')	
		});
	});*/
	$('div.share span').click(function(){
		$this = $(this);
		$this.find('ul').stop(true,true).slideUp(300, function() {
			$this.removeClass('active')	
		});
	});
	
	$('#topics li a').hover(function(){
		var thisTag = $(this).text();
		$('#topics span').text(thisTag)
	});
	//comments hover
	$('div.post div.comments').hover(function(){
		$(this).toggleClass('on');
	});
	
	//archives dropdown
	var yearHover = {    
		 over: showYear,    
		 timeout: 600,   
		 out: hideYear    
	}
	$('#archives div.year').hoverIntent(yearHover);
	
	$('#archives div.year a').click(function(e){
		e.preventDefault();
		var thisTag = $(this).text();
		$('div.year span.title').text(thisTag)
		$('#archives div.year ul').slideUp(300);
		var thisYear = $(this).parent('li').attr('class');
		$('#archives div.month').show();
		$('#archives div.month li').hide();
		$('#archives div.month li.'+thisYear).show();
	});
	
	var monthHover = {    
		 over: showMonth,    
		 timeout: 600,   
		 out: hideMonth    
	}
	$('#archives div.month').hoverIntent(monthHover);
	
//--PAGE NAV--\\
	var prevURL = $('#page-nav div.prev a').attr('href');
	var nextURL = $('#page-nav div.next a').attr('href');
    $(document).keypress(function(event) {
        switch (event.keyCode) {
            case 37:
                if (prevURL != null) {
				window.location.href = prevURL;
                break;
				}
            case 39:
                if (nextURL != null) {
                window.location.href = nextURL;
                break;
				}
        }
    });
	
//--CONTACT VALIDATION--\\
	$("#contactLW").validate();
	
//--MODAL--\\
	$('a.modal').click(function (e) {
        e.preventDefault();
        var id = $(this).attr('href');
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
        $('#mask').css({ 'width': maskWidth, 'height': maskHeight });
        $('#mask').fadeTo(500, 0.8);
        var winH = $(window).height();
        var winW = $(window).width();
        $(id).css('top', winH / 2 - $(id).height() / 1.5);
        $(id).css('left', winW / 2 - $(id).width() / 2);
        $(id).fadeIn(600);
    });

    $('div.modal a.close').click(function () {
        $('div.modal, #mask').hide();
    });
	
});

$(window).load(function(){
//---EMPLOYEE IMAGES (Company, Case Study, Careers)---\\	
	$("#employees img").animate({opacity:1},500);
	// clone image
	$('#employees img').each(function(){
		var el = $(this);
		el.css({"position":"absolute"}).wrap("<div class='img_wrapper' style='display: inline-block'>").clone().addClass('img_grayscale').css({"position":"absolute","z-index":"998","opacity":"0"}).insertBefore(el).queue(function(){
			var el = $(this);
			el.parent().css({"width":this.width,"height":this.height});
			el.dequeue();
		});
		if ($.browser.msie) {
			$(this).css({filter:'progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)'});
		} else {
			this.src = grayscale(this.src);
		}	
	});
	// Fade image 
	$('#employees img').mouseover(function(){
		$(this).parent().find('img:first').stop().animate({opacity:1}, 600);
	})
	$('.img_grayscale').mouseout(function(){
		$(this).stop().animate({opacity:0}, 400);
	});	

});

// Grayscale w canvas method
function grayscale(src){
	var canvas = document.createElement('canvas');
	var ctx = canvas.getContext('2d');
	var imgObj = new Image();
	imgObj.src = src;
	canvas.width = imgObj.width;
	canvas.height = imgObj.height; 
	ctx.drawImage(imgObj, 0, 0); 
	var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
	for(var y = 0; y < imgPixels.height; y++){
		for(var x = 0; x < imgPixels.width; x++){
			var i = (y * 4) * imgPixels.width + x * 4;
			var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
			imgPixels.data[i] = avg; 
			imgPixels.data[i + 1] = avg; 
			imgPixels.data[i + 2] = avg;
		}
	}
	ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
	return canvas.toDataURL();
}

//hoverIntent
(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 ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t)}if(e.type=="mouseenter"){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.bind('mouseenter',handleHover).bind('mouseleave',handleHover)}})(jQuery);
//hoverIntent options
function showTags(){$('#tags').slideDown(600);}
function hideTags(){$('#tags').slideUp(300);$('#topics span.tag').text('Choose a tag');$('#topics span.diary').text('Other Lab Diaries');}
function showShare(){$(this).addClass('active').find('ul').stop(true,true).slideDown(500);}
function hideShare(){$(this).find('ul').stop(true,true).slideUp(300); $('div.share').removeClass('active');}
function showYear(){$('#archives div.year ul').slideDown(600);}
function hideYear(){$('#archives div.year ul').slideUp(300);}
function showMonth(){$('#archives div.month ul').slideDown(600);}
function hideMonth(){$('#archives div.month ul').slideUp(300);}


})(jQuery);
