$(document).ready(function() {
	
	if ($('#content-container').length > 0) {
		Cufon.replace('h1',{ fontFamily: 'Interstate-ExtraLight', hover: true});
		Cufon.replace('h2',{ fontFamily: 'Interstate-Regular',  hover: true});
		Cufon.replace('h3',{ fontFamily: 'Interstate-Regular', hover: true});			
		Cufon.replace('h4',{fontFamily: 'Interstate-ExtraLight', hover: true});	
		
		resizenow();
		
		$(".icon1").tooltip({  effect: 'slide' , relative: true, offset: [16, 163] });
		$(".icon2").tooltip({  effect: 'slide' , relative: true, offset: [16, 163] });
		$(".icon3").tooltip({  effect: 'slide' , relative: true, offset: [16, 163] });
		$(".icon4").tooltip({  effect: 'slide' , relative: true, offset: [16, 163] });
		$(".number").tooltip({  effect: 'slide', relative: true, offset: [0, 50] });
		
		Cufon.replace('#floating-nav-links li a',{fontFamily: 'Interstate-Light', textShadow: '1px 1px #4a4a47', hover: true});
	}else{
		$(".icon1").tooltip({  effect: 'slide' , relative: true, offset: [16, 163] });
		$(".icon2").tooltip({  effect: 'slide' , relative: true, offset: [16, 163] });
		$(".icon3").tooltip({  effect: 'slide' , relative: true, offset: [16, 163] });
		$(".icon4").tooltip({  effect: 'slide' , relative: true, offset: [16, 163] });
		$(".team").tooltip({  effect: 'slide', relative: true, offset: [25, -180] });
	}
	
	Cufon.replace('.quick_links',{ fontFamily: 'Interstate-Regular', hover: true});	
			
	if ($('#floating-content').length > 0) {
		$('#floating-nav-links li a').hover(function() {
			$(this).parents().find('.current').removeClass('current');
			$(this).addClass('current');
			Cufon.refresh();
			$('#floating-content div.inner').hide();
			$('#floating-content').show();
			$('#floating-content').children($(this).attr('href')).show();
		});
		
		$('#content-container .top-content').mouseleave(function() {
			$('#floating-nav-links').find('.current').removeClass('current');
			$('#floating-content').hide();
			Cufon.refresh();
		});
		
		$('#floating-content').find('.inner-block').find('li').hover(function() {
			$(this).addClass('hover');
		},function() {
			$(this).parents('.inner-block').find('.hover').removeClass('hover');
		});
	}
	
	if ($('#feat-images').length > 0) {
		var bgload = $('#feat-images').find('a:eq(0)').attr('href');
		bgimage(bgload);
	
		$('#feat-images ul li').each(function() {
			$(this).hover(function() {
				$(this).addClass('hover');
				//$(this).css({marginLeft: '-792px', opacity: 1});
				//$(this).animate({ marginLeft: -792, opacity: 1 }, {duration: 500, easing: 'easeOutQuad' })
				$(this).animate({ marginLeft: -792, opacity: 1 }, {duration: 500})
			},function() {
				$(this).parent('ul').find('.hover').removeClass('hover');
				//$(this).css({marginLeft: '0px', opacity: 0.3});
				//$(this).animate({ marginLeft: 0, opacity: 0.3 }, {duration: 200, easing: 'easeInQuad' })
				$(this).animate({ marginLeft: 0, opacity: 0.3 }, {duration: 200 })
			});
		});
		
		$('#feat-images').find('a').click(function() {
			bgimage($(this).attr('href'));
			var log = $(this).parent().siblings('span.feat-images-logo').children('img').attr('src');
			$('.top-content').css('backgroundImage', 'url('+log+')')
			return false;
		});
	}else{
		var bgload = $('#bg-image img').attr('src');
		bgimage(bgload);
		$('#bg-image img').remove();
	}
	
	$(".quick_links").click(function(){
		$("#footer-slide-container").slideToggle("slow");
		$(this).toggleClass("active"); return false;
	});

	if ($('#left-nav').length > 0 ) {
		$('#left-nav').find('li:not(li li)').children('a').addClass('parent');
		$('#left-nav ul ul').find('li').children('a').addClass('child');
		Cufon.replace('#floating-nav-links a.parent',{fontFamily: 'Interstate-Light', textShadow: '1px 1px #525252', hover: true});
		Cufon.replace('#floating-nav-links a.child',{fontFamily: 'Interstate-Regular', textShadow: '1px 1px #525252', hover: true});
/*		$('#floating-nav-links a.parent:not(li.sub a.parent)').click(function() {
			if ($(this).siblings('ul').is(":hidden")) {
				$(this).parent('li').addClass('show');
				$(this).siblings('ul').slideDown("slow");
			}else{
				$(this).parent('li').removeClass('show');
				$(this).siblings('ul').slideUp("slow");
			}

		});*/
		
	}
	
	if ($('.case-study').length > 0) {
		var c = 1;
		$('.case-study').children('.block').each(function() {
			if (c==2) {
				$(this).addClass('right').after('<div class="clear"></div>');
				c = 1;
			}else{
				c++;
			}
		});
	}
	
	if ($('.clients-list').length > 0) {
		var c = 1;
		$('.clients-list').children('.block').each(function() {
			if (c==3) {
				$(this).addClass('right').after('<div class="clear"></div>');
				c = 1;
			}else{
				c++;
			}
		});
	}
	
	if ($('.services-list').length > 0) {
		var c = 1;
		$('.services-list').children('.block').each(function() {
			if (c==2) {
				$(this).addClass('right').after('<div class="clear"></div>');
				c = 1;
			}else{
				c++;
			}
		});
	}
	
	if ($('#team-thumb').length > 0) {
		$('#team-thumb').find('li').each(function() {
			var img = $(this).children('a').children('img:eq(1)').attr('src');
			$(this).children('a').css('backgroundImage', 'url('+img+')');
			$(this).children('a').children('img:eq(1)').remove();
		});
	}
	
	twitterBlinkEyesOff();
	$("div#left-nav").hide().delay(500).show();
});


$(window).load(function() {

	if ($('#content-container').length > 0) {
		$('.feat-images-thumb').each(function() {
			var gc = 0;
            var imgObj = $('.img-gray')[gc];
            
            if($.browser.msie){
                grayscaleImageIE(imgObj);
            } else {
                imgObj.src = grayscaleImage(imgObj);
            } 
			gc++;
		});
	}

});

$(window).resize(function() {
  resizenow();
});


function resizenow() {
	var nh = $('.wrapper').outerHeight() - 130;
	$('#content-container').css('height', nh+'px');
}

var eyesOnAfterTimeMin=100, eyesOnAfterTimeMax=400;
var eyesOffAfterTimeMin=100, eyesOffAfterTimeMax=2500;
var eyesOnAfterTime, eyesOffAfterTime, twitterBlinkEyesOnID, twitterBlinkEyesOffID;
var ANIMSPEED=1200;

function randomTime(Min, Max){return Math.floor(Math.random() * (Max - Min + 1)) + Min}

function twitterBlinkEyesOn(){
	$('a#btn-twitter').parent().removeClass('twitterBlinkEyesOff');
	clearInterval(twitterBlinkEyesOnID);
	eyesOffAfterTime= randomTime(eyesOffAfterTimeMin, eyesOffAfterTimeMax);
	twitterBlinkEyesOffID=setInterval('twitterBlinkEyesOff()', eyesOffAfterTime);
}

function twitterBlinkEyesOff(){
	$('a#btn-twitter').parent().addClass('twitterBlinkEyesOff');
	clearInterval(twitterBlinkEyesOffID);
	eyesOnAfterTime= randomTime(eyesOnAfterTimeMin, eyesOnAfterTimeMax);
	twitterBlinkEyesOnID=setInterval('twitterBlinkEyesOn()', eyesOnAfterTime);
}



function bgimage(src) {
	$('#backstretch').remove();
	$.backstretch(src);
}

function grayscaleImageIE(imgObj) {
	imgObj.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(grayScale=1)';
}

function grayscaleImage(imgObj) {
    var canvas = document.createElement('canvas');
    var canvasContext = canvas.getContext('2d');
        
    var imgW = imgObj.width;
    var imgH = imgObj.height;
    canvas.width = imgW;
    canvas.height = imgH;
        
    canvasContext.drawImage(imgObj, 0, 0);
    var imgPixels = canvasContext.getImageData(0, 0, imgW, imgH);
        
	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;
		}
	}
        
	canvasContext.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
	return canvas.toDataURL();
}
