/* <![CDATA[ */
	
	var photos = [
			{
				"url":"",
				"image":"http://www.crouchendcars.com/photos/slider/photo1.jpg",
				"title":"",
				"line1":"",
				"line2":"",
				"line3":"",
				"line4":"",
				"linkThrough":"{link_through}"
			},			
			{
				"url":"",
				"image":"http://www.crouchendcars.com/photos/slider/photo2.jpg",
				"title":"",
				"line1":"",
				"line2":"",
				"line3":"",
				"line4":"",
				"linkThrough":"{link_through}"
			},
			{
				"url":"",
				"image":"http://www.crouchendcars.com/photos/slider/photo3.jpg",
				"title":"",
				"line1":"",
				"line2":"",
				"line3":"",
				"line4":"",
				"linkThrough":"{link_through}"
			},
			{
				"url":"",
				"image":"http://www.crouchendcars.com/photos/slider/photo4.jpg",
				"title":"",
				"line1":"",
				"line2":"",
				"line3":"",
				"line4":"",
				"linkThrough":"{link_through}"
			},
			{
				"url":"",
				"image":"http://www.crouchendcars.com/photos/slider/photo5.jpg",
				"title":"",
				"line1":"",
				"line2":"",
				"line3":"",
				"line4":"",
				"linkThrough":"{link_through}"
			},      			
		{}
	];
	
	$(photos).each(function(i)
	{
		var photo = this;
		setTimeout(function()
		{
			var img = document.createElement('img');
			img.src = photo.image;
		}, i*2000);
	});
	
	// toggle used to alternate between showing headerimages
	var toggle = 0;
	var i = 0;
	// n for next image. Starts at 1 since we load first 2 images to start
	var n = 1;
	// - 2 because we cant use backspace in this instance to fix last comma in array, so we add a blank to make all browsers the same length
	var t = photos.length-2;
	// for auto rotation
	var ssInterval;
	
	function ssPlay(){
		ssInterval = setInterval("nextImage()", 8000);
		
	}
	
	function ssStop(){clearInterval(ssInterval);}
	
	function nextImage(){
		if (i < t){i++;}
		else {
			i = 0;
		}
		
		if (n < t){n++;}
		else {
			n = 0;
		}
		
		changeImage(i,n);
	}
	
	function changeImage(i,n){
		if ($('div.header-caption').size()) {$('div.header-caption').fadeOut(350);}
		
		if(toggle == 0){
			$('div#header-image3').fadeIn(400);
			$('div#header-image2').fadeOut(400, function () {
				$('div#header-image2').css('background-image','url('+photos[n].image+')');
				forwardImg(i);
			});				
			
			toggle = 1;
		}
		else {
			$('div#header-image2').fadeIn(400);
			$('div#header-image3').fadeOut(400, function () {
				$('div#header-image3').css('background-image','url('+photos[n].image+')');
				forwardImg(i);
			});
			
			toggle = 0;
		}
	}
	
	function forwardImg(num) {
				
		if (photos[num].line1 || photos[num].line2 || photos[num].line3 || photos[num].line4){
		
			if (!$('div.header-caption').size()) {
				$('div#header-wrap').after('<div class="header-caption"></div>');
			}
			
			var str = '<div class="header-caption" style="display: none;">';
			
			if (photos[num].line1 || photos[num].line2){
				str+= '<p>';
				if (photos[num].line1){
					str+= '<span>'+photos[num].line1+'</span>';
				}
				if (photos[num].line2){
					str+= '<a href="'+photos[num].url+'" class="primary">'+photos[num].line2+'</a>';
				}
				str+= '</p>';
			}
				
			if (photos[num].line3){
				str+= '<p class="description line-3">'+photos[num].line3+'</p>';
			}
			
			if (photos[num].line4){
				str+= '<p class="description line-4">'+photos[num].line4+'</p>';
			}
			
			str+= '<p class="pictured"><strong>Pictured: <a href="'+photos[num].url+'">'+photos[num].title+'</a></strong></p>';
			
			str+= '</div>';
			
			$('div.header-caption').replaceWith(str);
			
			if ($.browser.mozilla) {
				setTimeout(function()
				{$('div.header-caption').show();}, 350);
			}
			else {$('div.header-caption').fadeIn(300);}
		}
		else {
			if ($('div.header-caption').size()) {$('div.header-caption').remove();}
		}
		if(photos[num].linkThrough == "y"){
			if (!$('div.link-through').size()) {
				$('div#header-image:eq(0)').append('<div class="link-through"></div>');
			}
		}
		else {
			if ($('div.link-through').size()) {$('div.link-through').remove();}
		}			
	}
	
	$.get('/enhanced-includes/widget-slideshow-controls-home', {}, function(data)
	{
	
		var $data = $(data);
		$('div#header-image').append($data);
		
		$('a.header-next').click(function(){
			nextImage();
			
			if($('a.header-play').hasClass('header-pause')){
				ssStop();
				ssPlay();
			}		
						
			return false;
		});
		
		$('a.header-previous').click(function(){		
		
			if (i > 0){i--;}
			else {
				i = t;
			}
			
			if (n > 0){n--;}
			else {
				n = t;
			}
			
			if(toggle == 0){$('div#header-image3').css('background-image','url('+photos[i].image+')');}
			else {$('div#header-image2').css('background-image','url('+photos[i].image+')');}
			
			//give previous time to render then change.
			setTimeout(function()
			{changeImage(i,n);}, 500);
						
			if($('a.header-play').hasClass('header-pause')){
				ssStop();
				ssPlay();
			}			
			
			return false;
		});
		
		$('a.header-play').click(function(){if($(this).hasClass('header-pause')){ssStop();}
			else{ssPlay();}
			
			$(this).toggleClass('header-pause');
										
			return false;
		});
		
	});
	
	$(document).ready(function()
	{ssPlay();});

/* ]]> */
  
