$(document).ready(function() {
    $('a.modal').click(function(){
        modal($(this));
	});

	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();



		$(".icon").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{

		$(".icon").tooltip({  effect: 'slide' , relative: true, offset: [16, 163] });

		$(".team").tooltip({  effect: 'slide', relative: true, offset: [25, -130] }).dynamic({ bottom: { direction: 'down', bounce: true } });

	}



	if ($('.case-study').length > 0) {

		//alert('true');

		$(".icon").tooltip({  effect: 'slide' , relative: true, offset: [16, 163] });

	}



	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('rel')).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 projects = $('#feat-images ul').children();

		var pLength = projects.length;

		var i = Math.floor(Math.random()*pLength);;

		var sT;
		
		function setBg() {

			//alert(i);

			clearTimeout(sT);

			if(i != 0) {

				i--;

			} else {

				i = pLength-1;

			}

			var bgload = $('#feat-images').find('a.bg:eq('+i+')').attr('href');

			bgimage(bgload);

			$.cookies.set('backgroundImage', bgload);

			var logo = $('#feat-images').find('img.logo:eq('+i+')').attr('src');

			$('.top-content').css('backgroundImage', 'url('+logo+')');

			sT = setTimeout(setBg, 20000);

			var url_title = $('#feat-images').find('img.logo:eq('+i+')').attr('rel');

			$('.results a').attr('href','clients/project/'+url_title);



		}



		setBg();



		$('#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).stop(true, true).animate({ marginLeft: -550, 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).stop(true, true).delay(750).animate({ marginLeft: 0, opacity: 0.3 }, {duration: 200 })

			});

		});



		$('#feat-images').find('a').click(function() {

			var bgload = $(this).attr('href');

			$.cookies.set('backgroundImage', bgload);

			bgimage(bgload);

			//var log = $(this).parent().siblings('span.feat-images-logo').children('img').attr('src');

			var log = $(this).parent().siblings('span.feat-images-logo').children('img').attr('src');

			//alert(log);
			$('.top-content').css('backgroundImage', 'url('+log+')');

			//var url_title = $('#feat-images').find('img.logo:eq('+i+')').attr('rel');

			var url_title = $(this).parent().siblings('span.feat-images-logo').children('img.logo').attr('rel');

			$('.results a').attr('href','clients/project/'+url_title);

			return false;

		});

	}else{

		//var bgload = $('#bg-image img').attr('src');

		//var bgload = $.cookies.get('backgroundImage');

		//var bgload = '/images/templates/bg_default.jpg';

		//if(!bgload) {

			bgload = '/images/templates/bg_default.jpg';

		//}

		bgimage(bgload);

		//$('#bg-image img').remove();

	}

/*
	$('div#toggle').click(function(){
        $("#footer-slide-container").slideToggle("slow");

		$(this).toggleClass("active"); return false;
	    //alert( $(this).attr('id') )
	});
	$(".quick_links").click(function(){

		$("#footer-slide-container").slideToggle("slow");

		$(this).toggleClass("active"); return false;

	});


*/
	if ($('#left-nav').length > 0 ) {
									 //alert('test');
		$('#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 ($('.partners-list').length > 0) {

		var c = 1;

		$('.partners-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();

	$("#member_login_form").validate();

	$("#register_member_form").validate({

		rules: {

			email:{

				required: true,

				email: true

				}

			},

			messages: {

				email: {

				  required: "Please enter a email",

				  email: "Valid email needed"

				  }
		}
	});

	$("#member_login_form_small").validate();


	$("#newsletterForm").validate({

		rules: {

			cons_email:{

				required: true,

				email: true

				}

			},

			messages: {

				cons_email: {

				  required: "Please enter a email",

				  email: "Valid email needed"

				  }
		}
	});





	$("#contactForm1").validate({

		rules: {

			fname: "required",

			lname: "required",

			email: {

				required: true,

				email: true

				},



			fphone: "required",

			femployee: "required"



		},

		messages: {

			fname: "Please enter your firstname",

			lname: "Please enter your lastname",



			email: {

			  required: "Please enter a email.",

			  email: "Valid email needed."

			  }

		}

	})



	$("#contactForm2").validate({

		rules: {

			fname: "required",

			lname: "required",

			femail: {

				required: true,

				email: true

				},



			fphone: "required",

			femployee: "required"



		},

		messages: {

			fname: "Please enter your firstname",

			lname: "Please enter your lastname",



			femail: {

			  required: "Please enter a email.",

			  email: "Valid email needed."

			  }

		}

	})



});





$(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++;

		});

	}

     $('div.bottom-content').hide().delay(1000).show();
     $('div#left-nav').hide().delay(1000).show();


});



$(window).resize(function() {

  resizenow();

});





function resizenow() {

	var nh = $('.wrapper').outerHeight() - 130;

	$('#content-container').css('height', nh-15+'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, {speed: 300});

}



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();

}



// display the modal
function modal(anchor){

	if($('#lightbox').size() == 0){
		var theLightbox = $('<div id="lightbox"></div>');
		var theShadow = $('<div id="lightbox-shadow"></div>');
 		$('body').append(theShadow);
		$('body').append(theLightbox);
	}

	// remove any previously added content
	$('#lightbox').empty();

	if(anchor.attr('id') == 'modal_facebook')
		iPhone_facebook(anchor);
	if(anchor.attr('id') == 'modal_iphone')
	    iPhone_apps(anchor);
	if(anchor.attr('id') == 'modal_ipad')
	    iPad(anchor);

}

function iPhone_facebook(anchor){
	// insert HTML content
	$('#lightbox').html($('div#modal_facebook').html());


	// move the lightbox to the current window top + 100px
	$('#lightbox').css('top', 100 + 'px');

	// display the lightbox
	$('#lightbox').show();
	$('#lightbox-shadow').show();

	//close modal event
	$('a.modal-close').click(function(){
		closeModal();
	});
/*    $('#lightbox-shadow').click(function(){
		closeModal();
	});*/
	//image thumb event
	$('div.thumb-row img').click(function(){
		thumbSrc = $(this).attr('src');

		imgSrc = thumbSrc.replace('thumbs/', '');
		imgSrc = imgSrc.replace('_thumb', '');


	    // hide lightbox and shadow <div/>'s
        $('div#phone-content a').css('background', 'url('+imgSrc+') no-repeat top left');
        modal(anchor);

	});
}

function iPhone_apps(anchor){
	// insert HTML content
	$('#lightbox').html($('div#modal_iphone').html());
	//alert()

	// move the lightbox to the current window top + 100px
	$('#lightbox').css('top',  100 + 'px');

	// display the lightbox
	$('#lightbox').show();
	$('#lightbox-shadow').show();

	//close modal event
	$('a.modal-close').click(function(){
		closeModal();
	});
/*    $('#lightbox-shadow').click(function(){
		closeModal();
	});*/
	//image thumb event
	$('div.thumb-row img').click(function(){
		thumbSrc = $(this).attr('src');

		imgSrc = thumbSrc.replace('png', 'gif');
	//	alert(imgSrc)

	    // hide lightbox and shadow <div/>'s
        $('div#phone-content2 a').css('background', 'url('+imgSrc+') no-repeat top left');
        modal(anchor);

	});
}

function iPad(anchor){
	// insert HTML content
	$('#lightbox').html($('div#modal_ipad').html());


	// move the lightbox to the current window top + 100px
	$('#lightbox').css('top', 75 + 'px');

	// display the lightbox
	$('#lightbox').show();
	$('#lightbox-shadow').show();

	//close modal event
	$('a.modal-close-ipad').click(function(){
		closeModal();
	});
/*    $('#lightbox-shadow').click(function(){
		closeModal();
	});*/
	//content
	$('div#ipad-content').load('index.html #content-container');

}

// close the lightbox
function closeModal(){

	// hide lightbox and shadow <div/>'s
	$('#lightbox').hide();
	$('#lightbox-shadow').hide();

	// remove contents of lightbox in case a video or other content is actively playing
	$('#lightbox').empty();

}
