function sliderTooltip(current){
	window.clearTimeout(tooltipTimer);
	var items 	= $('#flash ul');
	if(current == null){
		current = $('#flash ul li.current');
	}
	var tooltip = $('#flash span.tooltip');
	
	tooltip.html(current.html());
	
	tooltip.css({
		left: (items.position().left + current.position().left) - (tooltip.width()/2) + (current.width() - 3),
		top: (items.position().top - items.height() - 5)
	});
	tooltip.stop(1,1).fadeIn('slow');
	tooltipTimer = setTimeout(function(){
		tooltip.stop(1,1).fadeOut('fast');
	}, 3000);
}

function autoThemeChange(){
	nextTheme = $('#flash ul li.current').next();
	if(nextTheme.length == 0){ nextTheme = $('#flash ul li').first();}
	$('#flash ul li.current').removeClass('current');
	nextTheme.addClass('current');
	sliderTooltip();
	changeTheme(nextTheme.attr('theme').split('|'));
	autoThemeChangeTimer = setTimeout(function(){ autoThemeChange(); }, autoThemeChangeTime);
}

function changeTheme(themeInfo){
	
	theme 		 = themeInfo[0];
	background   = themeInfo[1];
	
	if(background == null){
		backgroundImage = 'assets/images/blank.gif';
	}else{
		backgroundImage = 'assets/images/themes/background/'+background+'.jpg';
	}
	
	$('#flash img.spinner').fadeIn();
	
	var backgroundloader = new ImageLoader(backgroundImage);
	
	backgroundloader.loadEvent = function(url, image){
		
		var themeLoader = new ImageLoader('assets/images/themes/objects/'+theme+'.png');
		
		themeLoader.loadEvent = function(url, image){
			$('#flash img.spinner').fadeOut();
			
			$('.themeBackground').each(function(){
				if ($(this).css('display') == 'block'){ $(this).fadeOut()};
			});
			
			$('#'+background).fadeIn();
			
			$('#flashContent').unbind('click').click(function(){
				window.location.href = $('#'+background).attr('href');
			});
			
			
			$('*').each(function(){
				var cssClass = '';
				if(cssClass = $(this).attr('class').match(/objects-(.+)-(.+)/i)){
					$(this).removeClass(cssClass[0]);
					$(this).addClass('objects-'+cssClass[1]+'-'+theme);
				}
			
				if($(this).css('backgroundImage').match(/themes\/objects/i)){
					$(this).css('backgroundImage', 'url(assets/images/themes/objects/'+theme+'.png)');
				}
			});
		}
		themeLoader.load();
	}
	backgroundloader.load();
	
}

function flashChangeTheme(code){
	if(code == '0x7c9a2d'){
		changeTheme(new Array('green'));
	}else if(code == '0x580f8b'){
		changeTheme(new Array('purple'));
	}else if(code == '0xcb2989'){
		changeTheme(new Array('purple2'));
	}else if(code == '0x333333'){
		changeTheme(new Array('gray'));
	}else{
		alert('Theme Not Found');
	}
}

var tooltipTimer 		 	= 0;
var autoThemeChangeTimer 	= 0;
var autoThemeChangeTime 	= 20000;
var themeList 			 	= new Array('gray','purple','purple2','green');
if(typeof(PAGE_EQUAL_HOME_PAGE) == 'undefined'){ PAGE_EQUAL_HOME_PAGE = false; }
$(document).ready(function(){ 

	if(PAGE_EQUAL_HOME_PAGE == true){

		if(swfobject.getFlashPlayerVersion().major >= 10)
		{
			var flashvars = {};
			var params = {};
			var attributes = {};
			params.wmode = "transparent";
			params.allowscriptaccess = "always";
			swfobject.embedSWF("slider.swf", "flashContent", "100%", "100%", "10.0.0", "assets/js/swfobject/expressInstall.swf", flashvars, params, attributes);
			//console.log($('#container').html().replace('<div id="flash"','<div style="height:415px;display:block;width:100%;"></div>'+"\r\n"+'<div id="flash"'));
			
			//findDiv = ($.browser.msie ? '<DIV id=flash' : '<div id="flash"');
			//console.log($('#container').html());
			//alert(findDiv);
		//	$('#container').html($('#container').html().replace(findDiv,'<div id="flash-space" style="height:440px;display:block;width:100%;"></div>'+"\r\n"+findDiv));
			$('.themeBackground, #flash').css({'position':'absolute','display':'none'});
			//$('.themeBackground, #flash').css({'position':'relative','visible':'hidden'});
			if(!$.browser.msie)
			{
				$('#container, #flash-space').css('pointer-events','none');
				$('#news-band, #boxes, #header').css('pointer-events','all');
			}
			cufonReplaceAll();
		}
		else
		{
			autoThemeChangeTimer = setTimeout(function(){ autoThemeChange(); }, autoThemeChangeTime);
			$('#container').html($('#container').html().replace('<div id="flash-space"></div>',''));
			$('#flash ul li').hover(function(){
				window.clearTimeout(tooltipTimer);
				sliderTooltip($(this));
			}, function(){
				$('#flash span.tooltip').hide();
			});
			
			$('#flash ul li').click(function(e){
				window.clearTimeout(autoThemeChangeTimer);
				
				autoThemeChangeTimer = setTimeout(function(){ autoThemeChange(); }, autoThemeChangeTime);
				newTheme 			 = $(this).attr('theme').split('|');
				if($.inArray(newTheme[0],themeList) == -1){ alert('Theme Not Found'); return false; }
				
				changeTheme(newTheme);
				
				$('#flash ul li.current').removeClass('current');
				$(this).addClass('current');
				sliderTooltip();
			});
		}
		//changeTheme(new Array('gray','twinbox'));
	}
});

