/* THEME CHANGER */

$(document).ready(function() {
  
  // assign click events to all theme buttons
  
  $('a.themeSelect').bind('click', function() {
	  
	  switchStyle($(this).attr('href'), false);
	  
	  $.ajax(
	  {
		  type: 'POST', 
		  url: '/', 
		  data: 'action=setTheme&theme=' + $(this).attr('href')
	  });
	  
	  
	  return false;
  });
  
});

function switchStyle(name, disabled)
{
	// enable the correct stylesheet
	
	$('link[rel*=style][title]').each(function(i) {
	 
		this.disabled = true;
		if (this.getAttribute('title') == name) {
	        this.disabled = disabled;
	    }
	});
   
	// read the old cookie to get the current theme
	var oldTheme = getCookie('StopSiteStyle');
	
	if (oldTheme == null || oldTheme == '')
    	oldTheme = 'default';
	
	// set a new cookie
	setCookie('StopSiteStyle', name, 1000);
	
	// replace all images 
	$.each($('#content img[src*=/themes/'+oldTheme+'], #content input[src*=/themes/'+oldTheme+']'), function(index, value){
		value.src = value.src.replace('/themes/' + oldTheme, '/themes/' + name);
	});
	
	$.each($('#footerLinks img[src*=/themes/'+oldTheme+'], #content input[src*=/themes/'+oldTheme+']'), function(index, value){
		value.src = value.src.replace('/themes/' + oldTheme, '/themes/' + name);
	});
}

