/*
This code used to handle a slideshow, where each slide actually slid into and out of view.

On 7/1/11 it was modified so that the slides faded in and out of view. This has two advantages:
 * It is less dependent on the width of the slides of the containing element.
 * It looks classier
 
On 7/3/11, a short script was added to add an image replacement for all <h1> elements, so that <h1> titles appear to use Helvetica Thin.
This would be better off in a separate file, but there is no easy way to get this included on pageload

On 7/10/11 the Google Analytics tracking script was added. Account ID UA-24295947-2
*/

$(document).ready(function(){  /* This code is executed after the DOM has been completely loaded */
    
  
  /* Image replacement for <h1> - added 7/3/11 */
  h1 = $('h1');
  h1.each(function(){
    i=$(this).find('img');
    if(i.length==0){
      t=$(this).text();
      u = 'http://jetdesign.co.uk/emotionallogic/h1/';
      i=$('<img>');
      t_uri = encodeURIComponent(t);
      i.attr({
        'alt': t,
        'src': u+t_uri,
        'onError': '$(this).parent().text($(this).parent().text())' // Return <h1> to just text
      });
      $(this).wrapInner('<div>');
      $(this).append(i);
    }
  });
  
  
  
  /* SLIDESHOW */	
	var totWidth=0;
	var maxHeight=0;
	//var positions = new Array(); // no longer needed
	
	$('#slides .slide').each(function(i){
		/* Traverse through all the slides and store their accumulative widths in totWidth */
		//positions[i]= totWidth;
		totWidth += $(this).width();
		maxHeight = Math.max(maxHeight, parseInt($(this).outerHeight(true)));
		/* The positions array contains each slide's commulutative offset from the left part of the container */
		
		if(!$(this).width()){
			//alert("Please, fill in width & height for all your images!");
			return false;
		}
	});
	
	/* Change the cotnainer div's width to the exact width of all the slides combined */
	//$('#slides').width(totWidth); // not actually needed anymore 7/1/11

	$('#slides').css({height: maxHeight+20 });	/* ADDED 7/1/11 : fix the slide container height to the highest slide */
	
	/* ADDED 7/1/11 : Hide all but the first slide */
	$('#slides .slide').hide().first().show();
	$('#slides .slide').css({position:'absolute',top:0,left:0});

	$('#menu ul li a').click(function(e,keepScroll){
			/* On a thumbnail click */

			$('li.menuItem').removeClass('act').addClass('inact');
			$(this).parent().addClass('act');
			
			var pos = $(this).parent().index(); // neater and doesn't rely on class
			//var pos = $(this).parent().prevAll('.menuItem').length;
			
			$('#slides').stop();
			//$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
			
			$('#slides').css({marginLeft:0});			
			$('#slides .slide').fadeOut();
			$('#slides .slide').eq(pos).fadeIn();

			/* Start the sliding animation */
			
			e.preventDefault();	// Prevent the default action of the link
			
			if(!keepScroll) clearInterval(itvl);  // Stopping the auto-advance if an icon has been clicked:
	});
	
	$('#menu ul li.menuItem:first').addClass('act').siblings().addClass('inact');
	/* On page load, mark the first thumbnail as active */
	
	
	
	/*****
	 *	Enabling auto-advance.
	 ****/	 
	var current=1;
	function autoAdvance(){
		if(current==-1) return false;		
		$('#menu ul li a').eq(current%$('#menu ul li a').length).trigger('click',[true]);	// [true] will be passed as the keepScroll parameter of the click function on line 28
		current++;
	}

	// The number of seconds that the slider will auto-advance in:
	var changeEvery = 7;

	var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);

	/* End of customizations */
});






/* GOOGLE ANALYTICS */
var _gaq = _gaq || [];  
_gaq.push(['_setAccount', 'UA-24295947-2']);  
_gaq.push(['_trackPageview']);  
(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);  
})();
