var oInterval 	= null;
var oStart = null;
var aHighlightCollection 	= new Array();
var nHighlightCounter = 0;
var nCurrentHighlight = 0;
var nTransition = 6000;
var dOpacity = 0.3;
var oTimeoutTitles = null;
var debug = false;


function showLog(message){
	if (debug==true) console.log(message);
}

function changeHighlight(){
	if($('#novo_titulo')){
		$('#novo_titulo').fadeOut('fast',
			function() {
				$('#mainSliderImg').fadeOut('fast',
					function() {
						showLog('Displaying '+(nCurrentHighlight+1)+' item from object list');
						$('#mySmallNavLinkz a').attr('class','');					
						$('#mySmallNavLinkz a:eq('+(nCurrentHighlight)+')').attr('class','current');
						$('#novo_titulo').html(aHighlightCollection[nCurrentHighlight].attr('title'))
							.attr('href',aHighlightCollection[nCurrentHighlight].attr('href'))
							.attr('title',aHighlightCollection[nCurrentHighlight].attr('title'))
						$('#mainSliderImg').html(aHighlightCollection[nCurrentHighlight].attr('image')).fadeTo('fast', 1, function() {
									if ($('#playControlz')) {
										$('#playControlz').attr('src',myURL+'pause.png'); 
									}
									$('#novo_titulo').fadeTo('fast', 1);
							});
						if(nCurrentHighlight == nHighlightCounter-1) {
							nCurrentHighlight = 0;
						 } else { 
							 nCurrentHighlight++;
						 }
						 
						 $('#mainSliderImg').fadeIn('fast');
						 $('#novo_titulo').fadeIn('fast');
					}
				);
			}
		);
	}
}


function startTransitionHighlight(){
	changeHighlight();
	oInterval = setInterval("changeHighlight()",nTransition); 
}


function displayTitles(){

	showLog('Starting Displaying Titles');
	if (oStart !== null) { clearTimeout(oStart); oStart = null; showLog('Cleaning oStart Timeout'); } 
//	if (oInterval !== null) { clearInterval(oInterval); oInterval = null; showLog('Cleaning oInterval Interval');  } 
	
	oStart = setTimeout("startTransitionHighlight()",200); 
	$('#nota').fadeOut('fast', function() {
		if($('#HighlightImageNote')) { $('#HighlightImageNote').html(null); }
		if($('#HighlightTitleNote')) { $('#HighlightTitleNote').html(null); }
		if ($('#mainSliderImg')) {
			$('#mainSliderImg').fadeTo('fast', 1, function() {
				$('#novo_titulo').fadeTo('fast', 1);
			});
		}
	});
}

$(document).ready(function(){ 
	if ($('#playControlz')) {
		$('#playControlz').click(function(){
			if (oInterval !== null) {
				clearInterval(oInterval);
				oInterval = null;
				$(this).attr('src',myURL+'play.png');
				showLog('Slideshow: STOP');
			}
			else
			{
				startTransitionHighlight();
				$(this).attr('src',myURL+'pause.png');
				showLog('Slideshow: PLAY');
			}
		});
	}
	$('#mySmallNavLinkz').find('a').each(function(){
		aHighlightCollection[nHighlightCounter++] =  $(this);
		
		$(this).attr('image',$(this).html())
			.hover(function(){
				var nNode = $(this);
				if (oTimeoutTitles !== null) {
					clearTimeout(oTimeoutTitles);
					oTimeoutTitles = null;
				}
				$('#nota').fadeIn('fast', function() {
					$('#HighlightImageNote').html(nNode.attr('image'));
					$('#HighlightTitleNote').html(nNode.attr('title'));
					$('#mainSliderImg').fadeTo('fast', dOpacity, function() {
						if (oInterval  !== null) {
								clearInterval(oInterval);
								oInterval = null;
								if ($('#playControlz')) {	$('#playControlz').attr('src',myURL+'play.png'); }
						}
					});
					
				});
				$('#novo_titulo').hide();
				showLog('Mouse HOVER');
				
		},function(){
			if (oStart !== null) { clearTimeout(oStart); oStart = null; } 
			if (oInterval !== null) { 
				clearInterval(oInterval); 
				oInterval = null; 
				if ($('#playControlz')) {
					$('#playControlz').attr('src',myURL+'play.png'); 
				}
			} 
			if (oTimeoutTitles !== null) { clearTimeout(oTimeoutTitles); oTimeoutTitles = null; } 
			oTimeoutTitles = setTimeout("displayTitles()",300); 
			showLog('Mouse OUT');
		})
		.html(nHighlightCounter);
	});
	startTransitionHighlight();
});
