var photos = [
			{
				"image":"fotos_header/header3.jpg"
			},{
				"image":"fotos_header/header4.jpg"
			},{
				"image":"fotos_header/header5.jpg"
			},{
				"image":"fotos_header/header6.jpg"
			},{
				"image":"fotos_header/header7.jpg"
			},{
				"image":"fotos_header/header1.jpg"
			},{
				"image":"fotos_header/header2.jpg"
			}];
	
	$(photos).each(function(i)
	{
		var photo = this;
		setTimeout(function()
		{
			var img = document.createElement('img');
			img.src = photo.image;
		}, i*2000);
	});
	
	var toggle = 0;
	var i = 0;
	var n = 1;
	var t = photos.length-2;
	var ssInterval;
	
	function ssPlay(){
		ssInterval = setInterval("nextImage()", 5000);
		
	}
	
	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:eq(0)').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>');
			}
			
			var strLink = '<div class="link-through"><strong><a href="'+photos[num].url+'">Discover more</a></strong></div>';
			$('div.link-through').replaceWith(strLink);
			
		}
		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();});
