var items = {};
var proCurrent = 0;
var proPrevious = 5;
var proSpeed = 400;
var proTimeout = 6500;
var proTotal = 1;
var debugCode = 0;
var time = 0;
var proIds = {
	header			:		'#flashHolderMenu',
	headerTD		:		'#flashHolderMenu td',
	bits				:		'#flashHolderCont .flashHolderBit'
};

$(function(){
	// setting the items into the array
	var headers = $(proIds.headerTD);
	var bits = $(proIds.bits);
	items = {
		headers 	:	 headers,
		bits 			:	 bits
	}
	
	proTotal = items.headers.length-1;
	if(proTotal > 0) { 	time = setTimeout("goNext()",10000); }
	$(proIds.headerTD).click(function(){
		var clicked = $(this);
		$(proIds.header+' .onTd').removeClass('onTd');
		clicked.addClass('onTd');
		
		proPrevious = proCurrent;
		proCurrent = items.headers.index(clicked);
		
		debug(proCurrent+' : '+proTotal);
		
		if(proCurrent != proPrevious) fadeImage();
	});
});

function goNext() {
	proPrevious = proCurrent;
	proCurrent = proCurrent+1;
	if(proCurrent > proTotal) proCurrent = 0;
	fadeImage();
}

function fadeImage() {
	clearTimeout(time);
	time = setTimeout('goNext()',proTimeout);
	
	debug('-----------------------');
	debug('fade image');	
	
	debug('c: '+proCurrent+' : p: '+proPrevious);
	var prev = items.bits[proPrevious];
	var next = items.bits[proCurrent];

	if(proCurrent > proPrevious) {
		$(next).clearQueue().fadeIn(proSpeed,function(){
			$(next).removeClass('hide');
			$(prev).addClass('hide').hide();	
			$('.onTd').removeClass('onTd');
			$(items.headers[proCurrent]).addClass('onTd');
		});
	} else {
		$(next).removeClass('hide').show();
		$(prev).clearQueue().fadeOut(proSpeed,function(){
			$(prev).addClass('hide').hide();	
			$('.onTd').removeClass('onTd');
			$(items.headers[proCurrent]).addClass('onTd');
		});
	}
	
	debug('-----------------------');
}


function debug(data) {
	if(debugCode == 1) {
		if(window.console) {
			 console.log(data);
		} else {
			 alert(data);
	  }	
	}
}


if (!('indexOf' in Array.prototype)) {
	Array.prototype.indexOf= function(find, i /*opt*/) {
		if (i===undefined) i= 0;
		if (i<0) i+= this.length;
		if (i<0) i= 0;
		for (var n= this.length; i<n; i++)
			if (i in this && this[i]===find)
			return i;
		return -1;
	};
}
