var urlLocale;
$(document).ready(function(){

	if ($.browser.msie && $.browser.version < 8) $('#chat').css('margin-left', '-922px');

	/* FOR SHOWING MORE RESULTS IN THE 'I WANT TO' SECTION */
	$('.toggle-more .less-text').hide();
	$('.toggle-more').click(function(){
		$('.hidden').stop(false, true).slideToggle();
		$('.more-text', this).toggle('fast');
		$('.less-text', this).toggle('fast');
	});
});

function fixThemes(){
	for (theme in themes){
		var attrs = ['h1', 'p', 'links'];
		for (attr in attrs){
			var str = themes[theme][attrs[attr]]+'';
			str = str.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/:apos;/g, "'");
			themes[theme][attrs[attr]] = str;
		}
	}
}

function startThemeRotation(){
	// parse our external style sheets and then pass their contents to addThemeRotation
	for (var theme in themes) $().parsecssExt('/bc/assets/styles/theme-'+theme+'.css', addThemeRotation);

	// make sure our transition divs are hidden to start with
	$('#transitionHead').fadeTo(0, 0);
	$('#transitionBody').fadeTo(0, 0);

	// after themes are loaded but before the first rotation, set the next theme transition background
	setTimeout("swapTransition()", 5000);

	// start theme rotation after our rotation time plus a few seconds to make sure all of the themes are done being added to the rotation
	setTimeout("rotateTheme()", themeRotationTime + 5000);
}

function addThemeRotation(css){
	var thisTheme = new Object();
	var theme = css['extStyleSource'].match('styles/theme-([^\.]+)\.css')[1];
	thisTheme['source'] = css['extStyleSource'];
	thisTheme['name'] = theme;

	// add the body background image or color
	if (typeof(css['body#home']) != 'undefined' && typeof(css['body#home'].background) != 'undefined'){
		if (css['body#home'].background.indexOf('url(') != -1){
			var bg_url = css_root + css['body#home'].background.match('url.([^\)]+)\\\)')[1];
			thisTheme['body_bg_image_css'] = 'url(' + bg_url + ')';
		} else if (bgstr.indexOf('#') != -1){
			thisTheme['body_bg_color'] = css['body#home'].background;
		}
	}

	// add the header background image or color
	if (typeof(css['#header-wrap']) != 'undefined' && typeof(css['#header-wrap'].background) != 'undefined'){
		if (css['#header-wrap'].background.indexOf('url(') != -1){
			var bg_url = css_root + css['#header-wrap'].background.match('url.([^\)]+)\\\)')[1];
			thisTheme['header_bg_image_css'] = 'url(' + bg_url + ')';
		} else if (bgstr.indexOf('#') != -1){
			thisTheme['header_bg_color'] = css['#header-wrap'].background;
		}
	}

	// add this theme to our rotation list
	themeRotation[themes[theme]['idx']] = thisTheme;
}

function getNewIdx(){
	return (typeof(themeRotation[themes[current_theme]['idx']+1]) != 'undefined') ? themes[current_theme]['idx']+1 : 0;
}

function rotateTheme(){
	var newIdx = getNewIdx();
	var newName = themeRotation[newIdx]['name'];

	// fade in our transition theme, then call the swap style sheet swap
	$('#transitionHead').stop().fadeTo(themeTransitionSpeed, 1);
	$('#transitionBody').stop().fadeTo(themeTransitionSpeed, 1);
	setTimeout("swapStylesheet(" + newIdx + ")", themeTransitionSpeed + 500);

	// switch selected theme nav
	if ($('#nav-'+current_theme).length) $('#nav-'+current_theme).removeClass('selected');
	if ($('#nav-'+newName).length) $('#nav-'+newName).addClass('selected');
	$('input.btn-default').css('background-color', themes[newName]['color']);

	// change hotspot box content
	$('#teaser>h1').text(themes[newName]['h1']);
	$('#teaser>p').text(themes[newName]['p']);
	$('#teaser>div').html(themes[newName]['links']);
	$('#click-area').click(function (){location.href=themes[newName]['hotspot']});

	// start the next rotation
	setTimeout("rotateTheme()", themeRotationTime);
}

function swapStylesheet(newIdx){
	// swap main theme style sheet
	$('#theme-style').attr('href', themeRotation[newIdx]['source']);

	// change our current theme to the one we are swapping to
	current_theme = themeRotation[newIdx]['name'];

	setTimeout("hideTransition()", themeTransitionSpeed);
}

function hideTransition(){
	// hide our transition divs again and call the swap of theme images on the transition divs
	$('#transitionHead').fadeTo(0, 0);
	$('#transitionBody').fadeTo(0, 0);
	swapTransition();
}

function swapTransition(){
	newIdx = getNewIdx();
	// swap the images on the transition divs
	$('#transitionHead').css('background-image', themeRotation[newIdx]['header_bg_image_css']);
	$('#transitionBody').css('background-image', themeRotation[newIdx]['body_bg_image_css']);
}


