// Javascript

// $('html').addClass('js'); // Flash of content fix (.js class hidden in CSS) - this now included inline header.php


(function (tos) {
	
	window.setInterval(function () {
		
		tos = (function (t) {
			
			return t[0] == 50 ? (parseInt(t[1]) + 1) + ':00' : (t[1] || '0') + ':' + (parseInt(t[0]) + 10);
		
		})(tos.split(':').reverse());
			
			window.pageTracker ? pageTracker._trackEvent('Time', 'Log', tos) : _gaq.push(['_trackEvent', 'Time', 'Log', tos]);
	
	}, 10000);

})('00');


$(document).ready(function() {
	
	
	/*
	==============================================================
		Track Outbound Links (GA)
	==============================================================
	*/
	
	(function($) {
		$(function() {
			$('a[href^="http"]:not([href*="' + document.domain + '"])').click(function(e) {
			
				// When a user clicks an external link
				if ($(this).attr('target') !== '_blank') {
				e.preventDefault();
				
				// If the link is not set to target='_blank'
				// set a 300ms delay before loading this page
				// which allows Google Analytics to register the event 
				(function(a) {
				setTimeout(function() {
					document.location = a.attr('href');
				},300)
					})($(this));
				}
				
				// Track URL and nearest element with an ID
				// and send to Google Analytics
				window.pageTracker ? pageTracker._trackEvent('Outbound', $(this).attr('href'), "#" + $(this).closest('[id!=""]').attr('id')) : _gaq.push(['_trackEvent', 'Outbound', $(this).attr('href'), "#" + $(this).closest('[id!=""]').attr('id')]);
				
			});
		})
	})(jQuery)
	
	
	/*
	==============================================================
		Remove No-Script Styles
	==============================================================
	*/
	
	$('.no-script').removeClass('no-script');
	$('html').removeClass('js'); // Flash of content fix (when doc is ready, pull the .js class)
	
	
	/*
	==============================================================
		Thumbnail Hover
	==============================================================
	*/
	
	$('.thumb h4').hide();
	
	$('.thumb').hover(function() {
		$(this).find('h4').fadeIn();
	}, function() {
		$(this).find('h4').fadeOut();
	});
	
	
	/*
	==============================================================
		Cycle Projects
	==============================================================
	*/
	
	
	// Adds hash value to URL for linking to specific slides
	//------------------------------------------------------------
	var index = 0, hash = window.location.hash;
	
	if (hash) {
		index = /\d+/.exec(hash)[0];
		index = (parseInt(index) || 1) - 1; // slides are zero-based
		initImg = (parseInt(index) || 0) + 1; // parse hash # for image init fadein
	} else {
		initImg = 1;
	}
	
	
	// Add Next/Prev links for First/Last slides
	//------------------------------------------------------------
	
	// If on last project, add link to "About" page
	if ( $('#next a').length == 0 ) {
		$('#next').append('<a href="../index.php?p=15">Next</a>');
	}
	
	// If on first project, add link to "Thumbs" page
	if ( $('#prev a').length == 0 ) {
		$('#prev').append('<a href="../index.php?p=20">Prev</a>');
	}
	
	
	// Setup content
	//------------------------------------------------------------
	
	// If no project copy exists hide project meta info
	if ( $('#content p').length == 0 ) {
		$('#project-info').hide();
	}
	
	// Wrap all images with a div id:slider
	$('#holder img').wrapAll('<div id="slider"></div><!-- slider -->');
	
	// Wrap each image with a div
	$('#holder img').wrap('<div />');
	
	// Add project info to second slide
	$('#project-info').appendTo('#slider div:nth-child(2)');
	
	// For images that break outside of the content frame
	$('.frame-breaker').appendTo('#slider div:nth-child(2)');
	
	// Add project copy to project info
	$('#holder blockquote, #holder p, #holder ul').appendTo('#project-info');
	
	// Add mini square pager
	$('#slider').before('<div id="pager"></div>');
	
	// Absolute position holder (stops load jump)
	$('#holder').css({position:'absolute'});
	
	// Position and hide all slides before load
	$('#slider > div').css({display:'none', position:'absolute'});
	
	// Work out which slide to fadeIn first based on hash tag initImg
	$('#slider > div:nth-child(' + (initImg) + ')').fadeIn(500, function() {
		$('#slider').cycle({ 
			fx:'fade', 
			speed:500, 
			timeout:0,
			startingSlide: index,
			pager:'#pager',
			next:'#next a',
			prev:'#prev a',
			before: onBefore,
			after: onAfter,
			nowrap: 1
		});
    });
	
	
	// Functions before/after cycle
	//------------------------------------------------------------
	
	// Set initCycle to true when page loads (fixes auto unbinding of slide opts on load)
	var initCycle = true;
	
	// Create handlers for click event to identify for unbind
	var nextHandler = function() {
		window.location = $('#next').find("a").attr("href");
	};
	var prevHandler = function() {
		window.location = $('#prev').find("a").attr("href");
	};
	
	function onBefore(curr, next, opts, fwd) {
		// If on second-last slide and direction is forward, or if on last slide on initiation (page load)
		if(opts.currSlide + 1 == opts.slideCount - 1 && fwd || opts.currSlide + 1 == opts.slideCount && initCycle){
			$('#next a').bind('click', nextHandler);
		}
		
		// If on second slide and direction is back, or if on first slide on initiation (page load)
		else if(opts.currSlide + 1 == 2 && !fwd || opts.currSlide == 0 && initCycle) {
			$('#prev a').bind('click', prevHandler);
		}
		
		// Unbind the URLs and rebind the Cycle event
		else {
			$('#next a').unbind('click', nextHandler);
			$('#prev a').unbind('click', prevHandler);
			$('#next a').bind('click.cycle', function(){return false;});
			$('#prev a').bind('click.cycle', function(){return false;});
		}
	}
	
	// Update hash URI
	function onAfter(curr, next, opts) {
		window.location.hash = opts.currSlide + 1;
		// After first slide anim, set initCycle to false
		initCycle = false;
	}
	
	
	/*
	==============================================================
		Logo Animation
	==============================================================
	*/
	
	$('#logo')
		.mouseover(function(){
			$(this).stop().animate(
				{top: '-60', height: '44'}, 
				{duration:50, easing: 'easeOutQuad'})
			})
		.mouseout(function(){
			$(this).stop().animate(
				{top: '-49', height: '37'}, 
				{duration:500, easing: 'easeOutBounce'})
			})
	
	
	/*
	==============================================================
		Block Element to Link
	==============================================================
	*/
	
	$('.link')
		.click(function(){
			window.location=$(this).find("a").attr("href");
			return false;
			})
	
	
	/*
	==============================================================
		Navigation Buttons Animation
	==============================================================
	*/
	
	$('#nav li a:not(.active)')
		.mouseover(function(){
			$(this).stop().animate(
				{backgroundColor:'#BFBFBF'}, 
				{duration:20, easing: 'easeOutQuad'})
			})
		.mouseout(function(){
			$(this).stop().animate(
				{backgroundColor:'#DEDEDE'}, 
				{duration:250, easing: 'easeInQuad'})
			})
		
		
	/*
	==============================================================
		Sub Navigation (About Page)
	==============================================================
	*/
	
	$('.sub-nav li:nth-child(1)').addClass('active');
	$('#experience, #technology').hide();
	$('.sub-nav li:nth-child(1)')
		.click(function(){
			$('#experience, #technology').hide();
			$('#about-me').fadeIn('slow');
			$('.sub-nav li').removeClass('active');
			$(this).addClass('active');
			/* Updates cufon to catch colour change */
			Cufon.replace('#project-info .sub-nav', { fontFamily: 'DIN', hover: true });
		})
	$('.sub-nav li:nth-child(2)')
		.click(function(){
			$('#about-me, #technology').hide();	
			$('#experience').fadeIn('slow');
			$('.sub-nav li').removeClass('active');
			$(this).addClass('active');
			/* Updates cufon to catch colour change */
			Cufon.replace('#project-info .sub-nav', { fontFamily: 'DIN', hover: true });
		})
	$('.sub-nav li:nth-child(3)')
		.click(function(){
			$('#about-me, #experience').hide();	
			$('#technology').fadeIn('slow');
			$('.sub-nav li').removeClass('active');
			$(this).addClass('active');
			/* Updates cufon to catch colour change */
			Cufon.replace('#project-info .sub-nav', { fontFamily: 'DIN', hover: true });
		})
		
});
















