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

NAME_ CORE BEHAVIOURS / LILLY HASTEDT
AUTH_ DANNY BROOKS_PHAGE / PHAGEDESIGN.CO.UK
DATE_ 27.07.11

DEPENDANCIES_ GOOGLE APIS LATEST JQUERY + PLUGINS.JS

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

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

$.fn.emailSpamProtection = function(className) {
	
	return $(this).find("." + className).each(function() {
	
	var $this = $(this);
	
	var s = $this.text().replace(" [at] ", "&#64;");
	
	$this.html("<a href=\"mailto:" + s + "\">" + s + "</a>");
	
	});

};// End emailSpam


//Development grid
/*
$.fn.toggleClassOnKeypress = function(options) {
	
	return this.each(function() {
	
	var $this = $(this);
	
	$(document).keypress(function(e) {
		
		switch(e.which) {
		
			case options.key:
		
			$this.toggleClass(options.class);
		
			break;
		};
	});
	});
};
*/


/* ------------------------------------------------------------------------------------------
Abracadabra */

$(document).ready(function() {

/* ------------------------------------------------------------------------------------------
Development Grid Toggle */
//$('#wrapper').toggleClassOnKeypress({ key: 98, class: 'dev'} );


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

var aniStyle01 = 'easeInOutQuart',
	aniStyle02 = 'easeInOutBounce',
	aniStyle03 = 'easeInOutExpo';

/* Spamo mail */
$('body').emailSpamProtection("email");


/* ------------------------------------------------------------------------------------------
Modal + map */

$('a[name=modal]').click(function(e) { //select all the a tag with name equal to modal
	
	e.preventDefault(); //Cancel the link behavior

	var id = $(this).attr('href'); //Get the modal id ref
	var mask = $('#page-mask');
	var maskH = $(document).height(); //Get the screen height and width
	var maskW = $(window).width();

	mask.css({'width': maskW,'height': maskH }); //Set heigth and width to mask to fill up the whole screen
	mask.fadeIn(aniMed).fadeTo(aniMed, 0.8); //transition effect

	var winH = $(window).height(); //Get the window height and width
	var winW = $(window).width();

	$(id).css('top',  winH/2-$(id).height()/2); //Set the popup window to center
	$(id).css('left', winW/2-$(id).width()/2);

	$(id).delay(aniMed).fadeIn(aniShort);
	initialize();//transition effect

});

$('.window .close-btn').click(function (e) {
	e.preventDefault();
	$('#page-mask').hide();
	$('.window').css('top','-1000em');
});


/* Sub-Navigation Navigation */

var $sub_nav = $('#sub-nav');

if($('body').hasClass('sub-show')) {
	
	$sub_nav.css({'display':'block'});

}else{
	
	$sub_nav.css({'display':'none'});

};

$('a#collections').click(function () {
	
	$sub_nav.fadeIn(aniShort);
	
	return false;
});


/* ------------------------------------------------------------------------------------------
Homepage Gallery */

var $gallery = $('#intro-gallery'),
		
		$slides = $gallery.find('.slide img'),
		
		$title = $('.slide h2');
		

$title.css({'marginTop': '-40px'});

/* onBefore Func */
var onBefore = function () {
	
	$title.animate({'marginTop': '-40px'}, aniMed);

};

var premierCount = $('#intro-gallery .slide').length,
		currentItem = 0;

var onAfter = function () {
	//if (currentItem == 0 || currentItem >= premierCount) { currentItem = 1; }
	//else if (currentItem < 0) { currentItem = premierCount; }
	//else { currentItem++; }
	//$('#page-count').html(currentItem + " of " + premierCount);
	$title.animate({'marginTop': '16px'}, aniMed);
};

$gallery.css({'opacity' : 0 });
$slides.css({'display' : 'block' });
$title.css({'display' : 'block' });


/* Start preload of cycle images */
$slides.preload({
	onFinish: function(){
	
	$gallery.cycle({
		fx: 'fade',
		//delay: 2000,
		speed: 1000,
		timeout: 4000,
		sync: 0,
		pause: true,
		before: onBefore,
		after: onAfter
	});
	
	$gallery.animate({'opacity': 1}, aniLong);
	//$('#gallery-wrap').spin(false);
	}
});//End preload


/* ------------------------------------------------------------------------------------------
Collection Slider */

$('#collection-pieces').css({'opacity': 0});

$('#collection-pieces li img').preload({
	
	onFinish: function(){
	
		$("#collection").easySlider({
		speed: aniLong,
		prevId: 'prevBtn',
		nextId: 'nextBtn',
		easeType: 'easeInOutExpo',
		keyboard: true
		});
		$('#collection-pieces').css({'display':'block'}).animate({'opacity': 1}, aniLong);
	}
});

}); // End jQuery
