var foFront = {
	pauseOnClick : 3000,
	periods : { main: 0, right: 0, bottom: 0 },
	intervals : { main: null, right: null, bottom: null },
	timeouts : { main: null, right: null, bottom: null },
	autoclick : false,
	
	parseHref: function(href)
	{
		var h = href.split('#');
		if (h.length > 1) {
			return '#'+h[h.length-1];
		} 
		return href;
	},
	
	init: function()
	{
		$('#fpTopLeft .tabs a').live('click', function(e){
			e.stopPropagation();
			if ($(this).attr('href') != '#') {
				var h = $(this).attr('href');
				$('#fpTopLeft').css({cursor: 'pointer'}).live('click', function() { window.location.href = h; });
			} else {
			  	$('#fpTopLeft').css({cursor: 'auto'}).die('click');
			}
			if (!foFront.autoclick) {
				foFront.autoclick = false;
				foFront.stopInterval('main');
			}
			$('#fpTopLeft .tabs li, #fpTopLeft .tabs td').removeClass('act');
			$('#fpTopLeft').css({ backgroundImage: 'url('+$(this).find('img').attr('src')+')'});
			$(this).first().parents().first().addClass('act');
			$(this).first().blur();
			return false;
		});
		
		var h = $('#fpTopLeft .tabs a').first().attr('href');
		$('#fpTopLeft').css({cursor: 'pointer'}).live('click', function() { window.location.href = h; });
		
		$('#fpTopRight .tabs a').live('click', function(e){
			$('#fpTopRight .tabs li, #fpTopRight .tabs td, #fpTopRight .contents div').removeClass('act');
			$(foFront.parseHref($(this).attr('href'))).addClass('act');
			$(this).first().parents().first().addClass('act');
			$(this).first().blur();
			if (!foFront.autoclick) {
				foFront.stopInterval('right');      
			}
			return false;           	
		});
		
		$('#tabBox .tabs a').live('click', function(e){
			$('#tabBox .tabs li, #tabBox .tabs td, #tabBox .contents').removeClass('act');
			$(foFront.parseHref($(this).attr('href'))).addClass('act');  
			$(this).first().parents().first().addClass('act');
			$(this).first().blur();
			if (!foFront.autoclick) {
				foFront.stopInterval('bottom');      
			}
			return false;
		});
		
		$('.subtabs a').live('click', function(e){
			$(this).parents('.subtabs').find('li').removeClass('act');
			$(this).parents('.subtabs').find('td').removeClass('act');
			$(this).parents('.contents').find('.subcontents').removeClass('act');
			$(foFront.parseHref($(this).attr('href'))).addClass('act');  
			$(this).first().parents().first().addClass('act');
			$(this).first().blur();
			if (!foFront.autoclick) {
				foFront.stopInterval('bottom');      
			}
			return false;
		});
		
		foFront.startInterval('main');
		foFront.startInterval('right');
		foFront.startInterval('bottom');
	},
	startInterval : function(box) 
	{ 
		if (foFront.periods[box] > 0) {
			foFront.intervals[box] = setInterval(function(){
				foFront.timeoutClick(box);	
			}, foFront.periods[box]);										
		}	
	},
	stopInterval : function(box)
	{
		if (foFront.periods[box] > 0) {
			clearInterval(foFront.intervals[box]);
			clearTimeout(foFront.timeouts[box]);	
		}
	},
	restartInterval: function(box)
	{
		if (foFront.periods[box] > 0) {
			clearInterval(foFront.intervals[box]);
			clearTimeout(foFront.timeouts[box]);
			foFront.timeouts[box] = setTimeout(function(){
				foFront.intervals[box] = setInterval(function(){
					foFront.timeoutClick(box);	
				}, foFront.periods[box]);		
			},foFront.pauseOnClick)	
		} 		
	},
	timeoutClick: function(box)
	{
	   var container = '';	
	   switch (box) {
			case 'main':
				container = 'fpTopLeft';
				break;
			case 'right':
				container = 'fpTopRight';
				break;
			case 'bottom':
				container = 'tabBox';
				break;
	   }
	   
	   foFront.autoclick = true;
	   if ($('#'+container+' .tabs li.act, #'+container+' .tabs td.act').next().length > 0) {
		    $('#'+container+' .tabs li.act, #'+container+' .tabs td.act').next().find('a').click();
		} else {
		    $('#'+container+' .tabs li, #'+container+' .tabs td').first().find('a').click();
		}
		foFront.autoclick = false;
	}
	
};
		
$(document).ready(function(){
	foFront.init();	
});
