	/*--------------------------------------------------------
			Inicio Slideshow fondo
	--------------------------------------------------------*/
	var photos
	
	$(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()", 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+= '<span class="primary" >'+photos[num].line2+'</span>';
				}
				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+= '</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();}
		}
		
	}
	
	$.get('./incluir/control.htm', {}, 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();});

