/*
* WAAPA Website Preferences Library [text resize and preferences]
* Grant Heggie [Squiz.net.au]
* August 2009
* 
* Modification history:
* 
* 2009-11-26: Scott Hall
*             Replaced all uses of jquery alias $ with $j to avoid prototype alias conflicts
* 2009-11-19: Scott Hall
*             Removed header images when in theme none
* 2009-10-01: Andrew Dunbar
*             Updated library to new formating.
*
*
*
* 
*/


/* START globals and pre body load actions for text resize and preferences ************************/

var fontSizes = new Array();
fontSizes[0] = "0.85em";
fontSizes[1] = "0.95em";
fontSizes[2] = "1.0em";
fontSizes[3] = "1.1em";
fontSizes[4] = "1.2em";

var themeNormal='normal';
var themeContrast='contrast';
var themeNone='none';

var searchAskUs='askus';
var searchCourses='courses';
var searchStaff='staff';
var searchWebsite='website';

var savedFontSize = $j.cookie('curFontSize');
var savedTheme = $j.cookie('curTheme');
var savedSearch = $j.cookie('curSearch');
var savedFontIndex = null;

if(savedFontSize==null){
	setFontSize(fontSizes[2]);
	savedFontIndex = 2;
}else{
	setFontSize(savedFontSize);
	savedFontIndex = getIndex(fontSizes, savedFontSize);
}

if(savedTheme==null){
	savedTheme = themeNormal;
	setTheme(themeNormal);
}else{
	setTheme(savedTheme);
}

if(savedSearch==null){
	savedSearch = searchWebsite;
}


function setFontSize(selectedSize){
	$j('html').css('font-size', selectedSize);
	// Only save cookie with 30 day expiry when preferences are saved
	// See save click function later on
	$j.cookie('curFontSize', selectedSize, { path: '/', domain: 'ecu.edu.au' });
	savedFontSize = $j.cookie('curFontSize');
}

function setSearch(selectedSearch){
        $j("#cse-search-box input#tab").val(selectedSearch);
}

function setTheme(selectedTheme){
	switch(selectedTheme){
		case themeNormal:
			$j("link#h-contrast").attr('disabled', true); /*safari fix*/
			$j("link#h-contrast").remove();
			$j("link[rel*='stylesheet']").each(function(i){
				this.disabled = false;
			});
			break;
		case themeContrast:
			$j("link[rel*='stylesheet']").each(function(i){
				this.disabled = false;
			});
			var contStyleSource = "./?a=15788";
			$j("head").append('<link id="h-contrast" rel="stylesheet" type="text/css" href='+'"'+contStyleSource+'"'+' media="all" />');
			break;
		case themeNone:
			$j("link[rel*='stylesheet']").each(function(i){
				this.disabled = true;
			});
			$j("link#h-contrast").remove();
			break;
	}
}

function changeFontSize(direction){
	var fontIndex = getIndex(fontSizes, savedFontSize);
	var fontArrayLength = fontSizes.length;
	if((direction=="up") && (fontIndex < fontArrayLength)){
		fontIndex++;
	}
	if((direction=="down") && (fontIndex > 0)){
		fontIndex--;
	}
	setFontSize(fontSizes[fontIndex]);
	$j("#website-preferences input:radio[value='"+fontIndex+"']").attr('checked', true);
}

/* END globals and pre body load actions for text resize and preferences **************************/


/* START jquery doc ready function for text resize and setting preferences. ***********************/

$j(document).ready(function() {
	
	//setSearch(savedSearch);

	// remove tagged header images when in theme none
	if(savedTheme == "none"){
		$j("#media").css("display","none");
	}

	/* pre check the preference page form radio buttons using saved values */
	$j("#website-preferences input:radio[value='"+savedFontIndex+"']").attr('checked', true);
	$j("#website-preferences input:radio[value='"+savedTheme+"']").attr('checked', true);
	$j("#website-preferences input:radio[value='"+savedSearch+"']").attr('checked', true);

	$j(".decrease-font-size").click(function(event){
		changeFontSize("down");
		event.preventDefault();
	});
	
	$j(".increase-font-size").click(function(event){
		changeFontSize("up");
		event.preventDefault();
	});
				   
	$j("input[name='pref-text-size']").click(function(){
		var checkedValue = $j("input[name='pref-text-size']:checked").val() + "";
		if (checkedValue){
			setFontSize(fontSizes[checkedValue]);
		}
	});
	
	$j("input[name='pref-style']").click(function(){
		var checkedValue = $j("input[name='pref-style']:checked").val() + "";
		if (checkedValue){
			displayRadioCheckedTheme(checkedValue);
		}
	});
	
	$j("input[name='pref-search']").click(function(){
		var checkedValue = $j("input[name='pref-search']:checked").val() + "";
		if (checkedValue){
			setSearch(checkedValue);
		}
	});
	
	$j("#website-preferences input:radio").click(function(){
		$j("#saved-success").removeClass("show");
	});
	
	function displayRadioCheckedTheme(checkedValue){
		var docLocation = document.location.href;
		switch(checkedValue){
			case 'normal':
				$j("body").fadeOut(1000, function () {
					setTheme(themeNormal);
					docLocation = removeAnchors(docLocation);
					document.location.href = docLocation + '#top-page';
				});
				$j("body").fadeIn(1000);
				break;
			case 'contrast':
				$j("body").fadeOut(1000, function () {
					setTheme(themeContrast);
					docLocation = removeAnchors(docLocation);
					document.location.href = docLocation + '#top-page';
				});
				$j("body").fadeIn(1000);
				break;
			case 'none':
				$j("body").fadeOut(1000, function () {
					setTheme(themeNone);
					docLocation = removeAnchors(docLocation);			
					document.location.href = docLocation + '#change-website-preferences';
					$j("#save-preferences span").css('visibility','hidden');
				});
				$j("body").fadeIn(1000);
				break;
		}
	}
	
	// Clean up URL anchors
	function removeAnchors(varURL){
		varURL = removeTokenFromString(varURL,'#top-page');
		varURL = removeTokenFromString(varURL,'#change-website-preferences');
		return varURL;
	}
	
	
	// Save selected preferences
	$j("#website-preferences #save-button").click(function(event){
																			
		if($j("input[name='pref-style']:checked").val()==themeNone){
			$j("#save-preferences span").css('visibility','hidden');
		}else{
			$j("#save-preferences span").css('visibility','visible');
		}
		
		var curRadioTheme = $j("input[name='pref-style']:checked").val() + "";
		$j.cookie('curTheme', curRadioTheme, { expires: 30, path: '/', domain: 'ecu.edu.au' });
		savedTheme = $j.cookie('curTheme');
		
		var curRadioSearch = $j("input[name='pref-search']:checked").val() + "";
		$j.cookie('curSearch', curRadioSearch, { expires: 30, path: '/', domain: 'ecu.edu.au'});
		savedSearch = $j.cookie('curSearch');
		
		$j.cookie('curFontSize', savedFontSize, { expires: 30, path: '/', domain: 'ecu.edu.au' });
		
		$j("#website-preferences input:radio").attr('disabled', true);
		$j("#website-preferences #save-button").attr('disabled', true);	
		$j("#saved-success").removeClass("show");
		$j("#saved-feedback").addClass("show");
		
		$j("#saved-feedback img").fadeOut(0).fadeIn(4000, function () {																			  
			$j("#saved-feedback").removeClass("show");
			$j("#saved-success").addClass("show").fadeOut(0).fadeIn(1000);
			$j("#website-preferences input:radio").attr('disabled', false);
			$j("#website-preferences #save-button").attr('disabled', false);		
		});
		
	});
	
});

/* END jquery doc ready function for text resize and setting preferences. *************************/