 /* ---------------------------------------------------------------------------------------

DOC NAME_ Core_behaviour.js / Kate Maestri
DOC TYPE_ Production
DOC AUTH_ DANNY BROOKS_PHAGE / WWW.PHAGEDESIGN.CO.UK
DOC DATE_ 21.01.11

------------------------------------------------------------------------------------------ */
// Avoid that flash of unstyled content
//$('html').addClass('js');

// Abracadabra...
$(document).ready(function() {
	
	$("#aside-contact").validate();
	$("#main-contact").validate();

	//Place the loading overlay in he gallery
	$("#intro-gallery").append("<div id=\"loader\"><\/div>");
	$("#gallery-wrap").append("<div id=\"gallery-loader\"><\/div>");
	
	// Global animation times in milliseconds
	var aniShort  = 250;
	var aniMed    = 500;
	var aniLong   = 750;
	var aniXLong  = 1000;
	var aniCustom = 2000;
	var aniDelay  = 2000;
	
	var aniStyle01 = 'easeInOutQuart';
	var aniStyle02 = 'easeInOutBounce';
	var aniStyle03 = 'easeInOutExpo';
	
	// Call upon some functions
	tooltip();

/* ---------------------------------------------------------------------------------------
Image galleries */

$("#intro-images").cycle({
	fx: 'fade',
	timeout: 6000,
 speed: aniXLong,
 delay: 3000,
 sync: 1,
 pause: 1,
 pager: '#gallery-dotnav',
 pagerAnchorBuilder: function(idx, slide) {
 return '<li><a href="#">' + idx + '</a></li>'; 
 } 
});

/* main galleries */

$("#gallery").cycle({
	fx: 'fade',
	timeout: 0,
 speed: 'aniXLong',
 sync: 1,
 prev: '#prev',
 next: '#next',
 after: function(curr,next,opts) {
	var caption = (opts.currSlide +1) + '/' + opts.slideCount;
	$('#gallery-folio').html(caption);
	}
 
});




// INIT GALLERY NAV BTNS
var $galleryBtns = $("#gallery-nav");

$galleryBtns.animate( {opacity: '1'}, {duration: 3000});
$galleryBtns.animate( {opacity: '0'}, {duration: 500});
	
$("#gallery-wrap").hover(
	function() {
		$galleryBtns.stop().animate( {opacity: '1'}, {duration: 500});  
	}, 
	function() {  
		$galleryBtns.stop().animate( {opacity: '0'}, {duration: 500});  
	});


/* Set up a nice page scroll before loading new gallery */
var $galleryLink = $("#gallery-list").find('a');

$galleryLink.click(function(event) {
	
	event.preventDefault();
	
	linkLocation = this.href;
	
	$('html, body').animate({scrollTop:0}, 'slow', function () {
		
		window.location = linkLocation;
		
	});
});

/* ******************************
ACCORDION */

$('h3').click(function() {
	
	$('h3').removeClass('on');//REMOVE THE ON CLASS FROM ALL BUTTONS
	
	$('.accord-answer').slideUp(500, aniStyle01); //NO MATTER WHAT WE CLOSE ALL OPEN SLIDES

if($(this).next().is(':hidden') == true) { //IF THE NEXT SLIDE WASN'T OPEN THEN OPEN IT
	
	$(this).addClass('on');
	
	$(this).next().slideDown(500, aniStyle01);
	}
});

$('h3').mouseover(function() {//ADDS THE .OVER CLASS FROM THE STYLESHEET ON MOUSEOVER 

	$(this).addClass('over');

}).mouseout(function() {
	
	$(this).removeClass('over');

});

$('.accord-answer').hide();// Hide all dropdowns

$('.accord-answer:first').delay(500).slideDown(500, aniStyle01);// Activate first accordian dropdown

$('h3:first').addClass('on');

// Anti-spam email, replace on dom ready
$('#antiEmail')
	.html('<a href="mailto:'+$('#antiEmail').html().replace('/','').replace('|','@').replace(':','.')+'?subject=Website Enquiry" class="tooltip" title="Send email">'
	+$('#antiEmail').html().replace('/','').replace('|','@').replace(':','.')+ '</a>');

}); /*Jquery Dom*/

/* -------------------------------------------------- 
FadeOut Overlay on window load */

$(window).load( function() {
	$('#loader').delay(500).fadeOut(750);
	$(this).remove();
	$('#gallery-loader').delay(500).fadeOut(750);
	$(this).remove();
});


/* -------------------------------------------------- 
Functions */

this.tooltip = function(){
	
	xOffset = -10;	
	yOffset = 10;		
	
	$("a.tooltip").hover(function(e){ // Grab all links with a class
		this.t = this.title;
		this.title = "";									  
		
		$("body").append("<div id='tooltip'><p>"+ this.t +"</p></div>");
		$("#tooltip").css({"opacity": 0.9})
		$("#tooltip").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px").delay(1500).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");
	});
};//Tooltip

function onAfter(curr,next,opts) {
	var caption = (opts.currSlide +1) + '/' + opts.slideCount;
	$('#gallery-folio').html(caption);
	}

$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) {
	$(pager).find('li').removeClass('currentli').filter('li:eq('+currSlideIndex+')').addClass('currentli');
	};
