/* ************************************************************

NAME_ CORE BEHAVIOURS / SALLY DERNIE
AUTH_ DANNY BROOKS_PHAGE / PHAGEDESIGN.CO.UK
DATE_ 27.07.11

DEPENDANCIES_ GOOGLE APIS LATEST JQUERY + PLUGINS.JS

************************************************************ */

/* ************************************************************
Functions */

jQuery.fn.mailto = function() {
	return this.each(function(){
		var email = $(this).html().replace(/\s*\(.+\)\s*/, "@");
		$(this).before('<a href="mailto:' + email + '" rel="nofollow" title="Email ' + email + '">' + email + '</a>').remove();
	});
};

/* Animation times for global consistancy */
	var aniShort = 250,
			aniMed = 500,
			aniLong = 750,
			aniXLong = 1000,
			aniCustom = 1500;

/* Easing types for global consistancy */
	var aniStyle01 = 'easeInOutQuart',
			aniStyle02 = 'easeInOutBounce';
			aniStyle03 = 'easeInOutExpo';

/* ************************************************************
Abracadabra */

$(document).ready(function() {
	
	$("#pro-process").click(function(){
     window.location = $(this).find("a").attr("href");
     return false;
		});
		
	$("#pro-process").hover( function() {
     	$(this).css({ 'cursor' : 'pointer'});
     });

	// Lets hide the thumbnails ready for load
	/* $('.thumbnail img').css({ opacity : 0 }); */
	$('.thumbnail img').fadeTo(0, 0);
	$('.thumbnail img').css({ 'bottom' : '-999em'});
	

	$('.content').css({ opacity : 0 });
	$('.content').delay(300).animate({ opacity : 1 }, 250,function () {} );

	// Fade content out on click and direct to location
	$('a.fade').click( function( event ){

		if( this.href == "" || this.href == null ) { event.preventDefault(); return; }
		if( (this.href.indexOf("#") == -1) && (this.href.indexOf("mailto:") == -1) && (this.href.indexOf("javascript:") == -1) && (this.target != "_blank") ) {
			event.preventDefault();
			linkLocation = this.href;
			$('.content').animate({ opacity : 0 }, 250, redirectPage);
		}
	});

	// Set off some sexy tooltippin!
	tooltip();

	// call our safe mailto and attach to all
	$('a.mail').mailto();


/************************************************************
Slide-down Contact Panel */

$('#contact-details').hide();

var panel_visible = false;

var showText='Contact',
		hideText='Close contact';

var $slidePanel = $('#slidepanel_inner');

var startHeight = $('#slidepanel_inner').innerHeight();

var expandHeight = "224";

$('a#contact-slide').click(function (event) {

	$(this).html( (panel_visible) ? showText : hideText);

	if (panel_visible == false) {

		$slidePanel.animate({ height: + expandHeight + "px" }, { duration: aniLong, easing: aniStyle03 });
		panel_visible = true;
		$('#contact-details').show();

	} else {

		$slidePanel.animate({ height: + startHeight + "px" }, { duration: aniLong, easing: aniStyle03 });
		panel_visible = false;

	}
	return false;
});


/************************************************************
Intro (Homepage) Gallery Set-up */

var metaName = $('#intro-gallery a').children('img').attr('title');
var metaDesc = $('#intro-gallery a').children('img').attr('alt');
$("#details").html("<p class=\"meta-name\">" + metaName + "</p><p class=\"meta-desc\">" +metaDesc+ "</p>");

$('#intro-gallery').css({opacity:0});

$('#intro-gallery a img').preload({
	onFinish: function(){

		$('#intro-gallery').animate({opacity: 1.0});

		$('#intro-gallery').cycle({
			fx: 'fade',
/* 			delay: 1000, */
			speed: 750,
			timeout: 6000,
			sync: 1,
			pause: true,
			before: onBefore,
			after: onAfter
		});
	}
});

function onBefore() {
	//$("#details").animate({ "opacity" : 0 }, 500);
	//console.log('fade out');
	}

function onAfter() {
	var metaName = $(this).children('img').attr('title');
	var metaDesc = $(this).children('img').attr('alt');
	$("#details").html("<p class=\"meta-name\">" + metaName + "</p><p class=\"meta-desc\">" +metaDesc+ "</p>"); //.animate({ "opacity" : 1 }, 250)
	//console.log('fade in');
	}


/* ***********************************************************
Fancybox - used on the press page */


$('a.pressbox').fancybox( {
	'autoDimensions' : false,
	'height' : 600,
	/* 'width' : 668, */
	'width' : 'auto',
	'overlayColor' : '#4C2D37',
	'overlayOpacity' : '0.8',
	'padding' : 10,
	'titlePosition' : 'inside'
});


/*
Scroll to top of page function */

scrollPage = function() {
	var scrolling = false;
	if(scrolling === false){
		scrolling = true;
		$('html,body').animate({ scrollTop : 0 }, aniXLong, aniStyle01, function() {
			scrolling = false;
		});
	}
}; /* End Func */

/* Call Scroll function */
$('a#page-top').click(function () {
	scrollPage();
	return false;
});

/* FIXING THE GALLERY INFO PANEL WHEN SCROLLING */
var top = $('#gallery-info').offset().top - parseFloat($('#gallery-info').css('marginTop').replace(/auto/, 0));

/* $('a#page-top').css({ opacity : 0 }); */
$('a#page-top').hide();

$(window).scroll(function (event) {
	var y = $(this).scrollTop(); // what the y position of the scroll is
	if (y >= top) { // whether that's below the form
		$('#gallery-info').addClass('fixed'); // if so, ad the fixed className
		$('a#page-top').fadeIn(250);
} else {
		$('#gallery-info').removeClass('fixed'); // otherwise remove italic
		$('a#page-top').fadeOut(250);
	}
});/* End Scroll */

}); //End Dom Ready

/* FADE IN CASE STUDY THUMBNAILS ON LOAD */
$(window).load(function() {

	var $productWrap = $('.thumbnail');
	var $productImg = $productWrap.find('img');
	var delay = 250;

	$productImg.delay(delay).each( function() { //.hide()
		$('.thumbnail img').css({ 'bottom' : '0'});
		$(this).delay(delay).fadeTo(aniLong, 1);
		delay += aniMed;
	});

});/*END LOAD*/

function redirectPage() {
	window.location = linkLocation;
}

this.tooltip = function(){

	xOffset = -10;
	yOffset = 10;

	$("a.tooltip").hover(function(e){

		this.t = this.title;
		this.title = "";

		$("body").append("<div id='tooltip'><p>"+ this.t +"</p></div>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.delay(750)
			.fadeIn("fast");
    },
	function(){
		this.title = this.t;
		$("#tooltip").remove();
    });
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
};
