$(document).ready(function() {
	
	$('body').focus();

	var pathName = window.location.pathname;
	
	// image loader for case-study page
	$('#case-gallery-wrap a').click(function(e) {
		url = $(this).attr('href');
		$('#case-image img').attr('src',url);
		e.preventDefault();
	});


	// even out footer section heights
	/*Array.max = function(){
    	return Math.max.apply( Math, this );
	};*/
	var footerHeight = [];
	footerHeight.push($('#footer-contact').height());
	footerHeight.push($('#footer-twitter').height());
	footerHeight.push($('#footer-recent').height());
	footerHeight.push($('#footer-links').height());
	maxHeight = Math.max.apply( Math, footerHeight );
	//maxHeight = footerHeight.max();
	$('#footer-contact').height(maxHeight);
	$('#footer-twitter').height(maxHeight);
	$('#footer-recent').height(maxHeight);
	$('#footer-links').height(maxHeight);


	// ENABLE ACCORDION MENUS
	$(".accordion").accordion({ active: 0, autoHeight: false, navigation: true });


	// HIDE NEWSLETTER ERROR DIV
	$('#mce-responses').css('opacity', 0);


	// CLOSE NEWSLETTER RESPONSE (BUTTON)
	$('#response-close').click(function() {
		$('#mce-responses').fadeTo('slow',0);
	});
	

	// NEWSLETTER TEXT INPUT (FOCUS)
	defaultEmailText = $('#mce-EMAIL').val();
	$('#mce-EMAIL').focus(function(){
		curText = $('#mce-EMAIL').val();
		if (curText == defaultEmailText){
			$('#mce-EMAIL').val('');
			$('#mce-EMAIL').removeClass('text-input-nofocus');
		}
	})
	$('#mce-EMAIL').focusout(function(){
		curText = $('#mce-EMAIL').val();
		if (curText == ''){
			$('#mce-EMAIL').val(defaultEmailText);
			$('#mce-EMAIL').addClass('text-input-nofocus');
		}
	})
	

	// COMMENT-FORM TEXT INPUT (FOCUS)
	defaultCommentText = $('#comment-input-message').val();
	$('#comment-input-message').focus(function(){
		curText = $('#comment-input-message').val();
		if (curText == defaultCommentText){
			$('#comment-input-message').val('');
			$('#comment-input-message').removeClass('text-input-nofocus');
		}
	})
	/*$('#comment-input-message').focusout(function(){
		curText = $('#comment-input-message').val();
		if (curText == ''){
			$('#comment-input-message').val(defaultCommentText);
		}
	})*/


	// SHOW/HIDE COMMENT-FORM
	commentText = $('#comment-expand-button').text();
	commentAltText = 'Hide Comment Form';
	$('#comment-form-entry').slideUp(0);
	$('#comment-expand-button').click(function() {
		$('#comment-form-entry').slideToggle(500);
	});
	$('#comment-expand-button').toggle(function(){
		$('#comment-expand-button').text(commentAltText);
	},function(){
		$('#comment-expand-button').text(commentText);
	});


	// SHOW/HIDE FACEBOOK LIKE BUTTON
	$('#comment-expand-facebook').click(function() {
		$('#facebook-wrap').animate({
			opacity: 1
		},200);
	});
	$('#facebook').hover(
		function() {
			$('#facebook-wrap').animate({
				height: 'toggle',
				width: 'toggle',
				opacity: 1
			},200);
		},
		function() {
			$('#facebook-wrap').delay(600).animate({
				height: 'toggle',
				width: 'toggle',
				opacity: 0
			},200);
		}
	);


	// TWITTER
	//
	// SHOW/HIDE TWITTER TWEET BUTTON
	$('#comment-expand-tweet').click(function() {
		$('#tweet-wrap').animate({
			opacity: 1
		},200);
	});
	$('#tweet').hover(
		function() {
			$('#tweet-wrap').animate({
				height: 'toggle',
				width: 'toggle',
				opacity: 1
			},200);
		},
		function() {
			$('#tweet-wrap').delay(600).animate({
				height: 'toggle',
				width: 'toggle',
				opacity: 0
			},200);
		}
	);
	// OPEN TWEET SHARE BOX IN NEW WINDOW
	$('a#comment-expand-tweet').popupWindow({
		centerScreen:1,
		width: 550,
		height: 450
	});
	// HOVERCARDS
	twttr.anywhere(function (T) {
		T.hovercards({
			infer: true
		});
	});



	// SHOW/HIDE FACEBOOK LIKE BUTTON -footer
	$('#comment-expand-facebook-footer').click(function() {
		$('#facebook-wrap-footer').animate({
			opacity: 1
		},200);
	});
	$('#facebook-footer').hover(
		function() {
			$('#facebook-wrap-footer').animate({
				height: 'toggle',
				width: 'toggle',
				opacity: 1
			},200);
		},
		function() {
			$('#facebook-wrap-footer').delay(600).animate({
				height: 'toggle',
				width: 'toggle',
				opacity: 0
			},200);
		}
	);	


	// INITIALIZE PRETTYPHOTO
	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'dark_square'
	});
	
	
	// TOOLTIPS
	$.fn.tipsy.defaults = {
	    delayIn: 0,      // delay before showing tooltip (ms)
	    delayOut: 0,     // delay before hiding tooltip (ms)
	    fade: true,     // fade tooltips in/out?
	    fallback: '',    // fallback text to use when no tooltip text
	    gravity: 'n',    // gravity
	    html: true,     // is tooltip content HTML?
	    live: false,     // use live event support?
	    offset: 0,       // pixel offset of tooltip from element
	    opacity: 0.8,    // opacity of tooltip
	    title: 'title',  // attribute/callback containing tooltip text
	    trigger: 'hover' // how tooltip is triggered - hover | focus | manual
	};
	$('.img-item').tipsy({title: 'tipsy-title'});
	// FOOTER-LINKS
	$('#footer-link-list li a').tipsy({gravity: 'w'});
	// HOME-BUTTON TOOLTIP
	$('#sidebar-logo-link').tipsy({trigger: 'manual', title: 'tipsy-title'});
	if (pathName != '/'){
		$('#sidebar-logo-link').delay(2000).queue(function(){
			$(this).tipsy('show');
			$(this).clearQueue();
			$(this).delay(5000).queue(function(){
				$(this).tipsy('hide');
				$(this).tipsy({trigger: 'hover'});
			});
		});
	}else{
		$('#sidebar-logo-link').tipsy({trigger: 'hover'});
	}
	// BLOG-ENTRY LINKS
	$('.blog-content a').tipsy({gravity: 's'});
	// CASE-STUDY LINKS
	$('#case-description a').tipsy({gravity: 's'});
	// NEXT/PREV BLOG ENTRY
	$('.pagination-box-blog a').tipsy({gravity: 's'});
	// RECENT PROJECTS (FOOTER)
	$('.footer-recent-project a').tipsy();


	// FORM VALIDATIONs
	$('#comment-form').validate();
	$('#contact-form').validate();


	// ADD target="_blank" TO TWITTER-LINKS
	$('.twitter-status a').attr('target','_blank');


	// BARREL ROLL
	
	var keys = [];
	var barrelRollRight = '82,82'; // r
	var barrelRollLeft = '90,90';  // z
	
	$(this).keydown(function(e){
		
		keys.push( e.keyCode );
		if( keys.toString().indexOf( barrelRollLeft ) >= 0 ){
			$('#page_wrap').rotate({
				angle: 0,
				animateTo: -360,
				duration: 2000
			});
			keys = [];
		}
		
		if( keys.toString().indexOf( barrelRollRight ) >= 0 ){
			$('#page_wrap').rotate({
				angle: 0,
				animateTo: 360,
				duration: 2000
			});
			keys = [];
		}
	});



});
