//alert('foo');  

var stylearray = new Array();

$_(document).ready(function(){
	$_.getJSON(domaindir + "/local/css/styleswitcher.json",
		function(data){
			makeCssSwitchers(data);
		}
	);
});


function makeCssSwitchers(data){

	$_("#styleswitcher").css("z-Index","300");
	$_("#styleswitcher").css("position","absolute");
	$_("#styleswitcher").css("background-color","black");
	$_("#styleswitcher").css("color","white");
	$_("#styleswitcher").css("padding","2px");

	$_("#styleswitcher").append('<a id="togglestyleswitch">Change the way this site looks</a>');
	$_("#togglestyleswitch").css("color","#FFF");
	$_("#togglestyleswitch").css("cursor","pointer");
	$_("#togglestyleswitch").click(function(){
		$_(".styleswitcherform").slideDown("slow");
		$_("#togglestyleswitch").hide();
		
	});

	for ( var a = 0; a < data['styles'].length; a++){
		$_("#styleswitcher").append('<form class="styleswitcherform" id="styleswitcherform_' + a + '"></form>');
		
		var myStyle = data['styles'][a];
		$_("#styleswitcherform_"+a).append('<h4>' + myStyle['name'] + '</h4><p>' + myStyle['description'] + '</p>');
		
		for ( var b = 0; b < myStyle['values'].length; b++ ){
			$_("#styleswitcherform_"+a).append('<input type="hidden" name="' + myStyle['values'][b][0] + '" value="' + myStyle['values'][b][1] + '">');
		}
		
		$_("#styleswitcherform_"+a).click(function(){
			applyCss($_(this).serialize());
			$_(".styleswitcherform").hide();
			$_("#togglestyleswitch").slideDown("slow");
		});
	}
	
	$_("#styleswitcher").append('<form class="styleswitcherform" id="styleswitcherform_' + (a+1) + '"></form>');
	$_("#styleswitcherform_"+(a+1)).append('<h4>Reset</h4><p>Return to the default graphic style</p>');
	$_("#styleswitcherform_"+(a+1)).click(function(){
		clearStyles();
	});
	
	$_("#styleswitcher").append('<form class="styleswitcherform"><button id="closestyle">Close</button></form>');
	
	$_("#closestyle").click(function(){
		$_(".styleswitcherform").hide();
		$_("#togglestyleswitch").show();
		return false;
	});
	
	$_("#styleswitcher h4").css("margin","0px");
	$_("#styleswitcher p").css("margin","0px");
	$_(".styleswitcherform").css("background-color","#FFF");
	$_(".styleswitcherform:odd").css("background-color","#DEDEDE");
	$_(".styleswitcherform, .styleswitcherform p").css("color","#000000");
	$_(".styleswitcherform").css("margin","0px");
	$_(".styleswitcherform").css("cursor","pointer");
	$_(".styleswitcherform").css("padding","0.5em");
	$_(".styleswitcherform").css("border","1px solid #CCC");
	$_(".styleswitcherform").css("width","400px");
	$_(".styleswitcherform").hide();
}

function clearStyles(){
	$_.cookie('cssValue', null);
	location.reload();
}

function applyCss(cssData){
	
	var myCSS=(unescape(cssData));
	var oldCSS=unescape($_.cookie('cssValue'));
	
	
	var oldCssArray=oldCSS.split("&");
	
	for(var i=0;i<oldCssArray.length;i++){
		
		var oldCssLine=oldCssArray[i].split("=");
		
		if(myCSS.indexOf(oldCssLine[0])==-1){
			myCSS+='&'+oldCssArray[i];
		} 
	
	}
	
	var cssArray=myCSS.split("&");
	
	for(var i=0;i<cssArray.length;i++){
	
		var cssLine=cssArray[i].split("=");
		
		switch(cssLine[0]){
			case 'background-images':
				if ( cssLine[1] == 'off' ){
					$_('body').css('background-image', 'none');
					$_('.tl, .t, .tr, .l, .r, .bl, .b, .br, a, #banner, li, .breadcrumbs, #navigation, #rightcolumn, #rightcolumn h3, .footer, #fascia, .rightthird, #rightthird, .count1, .count2, .count3, .count4, .count5,.count6, .count7, .count8,.count9,.count10,.count11, .keyword_feed_listing, .newblogfeed, .newnewsfeed, .newjobsfeed, .highlight, .dark, .light, .candy_dark, .candy_light, .blog, #sidebar, .pagemenu').css('background-image', 'none');
					$_('ul.keyword_index_list li').css('list-style-image','none');
					$_('ul.keyword_index_list li').css('list-style-type','disc');
				}
				break;
			case 'content_bg':
				$_("body").css("background-color",cssLine[1]);
				$_(".padding").css("background-color",cssLine[1]);
				$_("#border").css("background-color",cssLine[1]);
				$_("#banner").css("border-color",cssLine[1]);
				$_(".editable").css("border",'1px solid '+cssLine[1]);
				$_(".breadcrumbs").css("background-color",cssLine[1]);
				$_(".crumbtrail").css("background-color",cssLine[1]);
				$_("#content").css("background-color",cssLine[1]);
				$_("#container").css("background-color",cssLine[1]);
				$_("#sidebar").css("background-color",cssLine[1]);
				$_("fieldset").css("background-color",cssLine[1]);
				break;
			case 'content_col':
				$_("body").css("color",cssLine[1]);
				$_(".padding").css("color",cssLine[1]);
				$_("#border").css("color",cssLine[1]);
				$_(".breadcrumbs").css("color",cssLine[1]);
				$_(".crumbtrail").css("color",cssLine[1]);
				$_("#content").css("color",cssLine[1]);
				$_("#container").css("color",cssLine[1]);
				break;
			case 'banner_bg':
				$_("#banner").css("background-color",cssLine[1]);
				break;
			case 'banner_col':
				$_("#banner").css("color",cssLine[1]);
				$_("#banner").find("h1").css("color",cssLine[1]);
				break;
			case 'nav_bg':
				$_("#navigation").find("ul:first").css("background-color",cssLine[1]);
				$_("a.mailinglist").css("background-color",cssLine[1]);
				break;
			case 'nav_col':
				$_("#navigation").find("ul:first").find("li").css("color",cssLine[1]);
				$_("#navigation").find("ul:first").find("li").find("a").css("color",cssLine[1]);
				$_("a.mailinglist").css("color",cssLine[1]);
				break;
			case 'subnav_bg':
				$_("#navigation").find("ul:first").find("li").find("ul").find("li").find("a").css("background-color",cssLine[1]);
				break;
			case 'subnav_col':
				$_("#navigation").find("ul:first").find("li").find("ul").find("li").find("a").css("color",cssLine[1]);
				break;
			case 'navhighlight_bg':
				$_("#navigation").find("li.ancestor").find("a:first").css("background-color",cssLine[1]);
				$_("#navigation").find("li.expandhere").find("a:first").css("background-color",cssLine[1]);
				break;
			case 'navhighlight_col':
				$_("#navigation").find("li.ancestor").find("a:first").css("color",cssLine[1]);
				$_("#navigation").find("li.expandhere").find("a:first").css("color",cssLine[1]);
				break;
			case 'candydark_bg':
				$_(".dark").css("background-color",cssLine[1]);
				$_(".candy_dark").css("background-color",cssLine[1]);
				break;
			case 'candydark_col':
				$_(".dark").css("color",cssLine[1]);
				break;
			case 'candylight_bg':
				$_(".light").css("background-color",cssLine[1]);
				$_(".candy_light").css("background-color",cssLine[1]);
				break;
			case 'candylight_col':
				$_(".light").css("color",cssLine[1]);
				break;
			case 'containerwidth':
				$_("#container").css("width",cssLine[1]);
				break;
			case 'containerpadding':
				$_("#container").css("padding",cssLine[1]);
				break;	
			case 'font-size':
				$_("body").css("font-size",cssLine[1]);
				break;
			case 'link_col':
				$_("a").css("color",cssLine[1]);
				break;
			default:
				break;
		}
	}
	
	//	Set cookie:
	$_.cookie('cssValue', myCSS);
		
	return false;
}

function showCssCookie(){
	alert($_.cookie('cssValue'));
     return false;
}
function clearCssCookie(){
	$_.cookie('cssValue', null);
	return false;
}