/*  styleswitcher.js - Copyright 2006 JoomlaShack
 *  Author: Dean Marshall - mainly tidying up and refactoring previous code.
 *
 *  http://www.joomlashack.com/ - Professional Joomla Templates
 *  For use with JoomlaShack templates only
 *
 *
 *	Usage:
 *	// This code should be placed immediately after the body tag.  
   	<script type="text/javascript">
 		js_init();
 	</script>

	List of functions in this package:
	==================================
	setWidth(width); // 'fixed' | 'fluid'
	toggleFluid()
	revertFont()
	changeFontSize(sizeDifference)// +1 | -1
	setFontSize(fontSize)// 10 | 12 | 14
 *
 *  
 */ 


/*
 * User Configurable Options 
 *
 */


	// Font sizes - maximum, minimum and default.
	var defaultFontSize =  76;
	var minimumFontSize =  60;
	var maximumFontSize = 100;
	var sizeIncrement = 5;

	// Debug option - for JoomlaShack use only.
	$debug_this_script = false; 	// set to true if events don't appear to be firing 




/*
 *	NO USER CONFIGURABLE OPTIONS BEYOND THIS POINT
 *	NO SUPPORT WILL BE OFFERED FOR UNAUTHORISED EDITS
 */

// Initialise script variables
var currentFontSize = defaultFontSize;


/*
 * This function runs as soon as body is created
 *
 */
	function js_init() {
		if($debug_this_script){  alert('window.onload firing');}

		// Font Related
			cookie = readCookie("fontSize");
			currentFontSize = (cookie) ? cookie : defaultFontSize;
			setFontSize(currentFontSize);

		if($debug_this_script){alert('window.onload is exiting');}
	}


/*
 * Width related functions
 *
 */
// Current templates - as of Aug 2006 - tend to use 'setWidth'.
function setWidth(width){
	if(width != "fluid"){
		// set fixed
		document.body.className = 'fixed';
		currentStyle = "fixed";
	}else{
		// otherwise set fluid
		document.body.className = 'fluid';
		currentStyle = "fluid";
	}
	// Save current fixed/fluid setting
	createCookie("pageWidth", currentStyle, 365);

}


// This is my next gen implementation of the style switcher
// Not iimplemented because defaultStyle / currentStyle is initially not set 
function toggleFluid(){
	var switchTo = (currentStyle == "fixed") ? "fluid" : "fixed";
	document.body.className = switchTo;
	currentStyle = switchTo;
}


/*
 * FONT RELATED FUNCTIONS
 *
 */
		// Set fontsize back to defaultFontSize
	function revertFont(){
		currentFontSize = defaultFontSize;
		changeFontSize(0);
	}

		// Checks that a requested change is within boundaries
	function changeFontSize(sizeDifference){

		// Calculate new font size
		currentFontSize = parseInt(currentFontSize) + parseInt(sizeDifference * sizeIncrement);

		// check new fontsize doesn't exceed minimum or maximum values	
		if(currentFontSize > maximumFontSize){
			currentFontSize = maximumFontSize;
		}else if(currentFontSize < minimumFontSize){
			currentFontSize = minimumFontSize;
		}

		setFontSize(currentFontSize);
	}

		// sets the fontsize
	function setFontSize(fontSize){
		if($debug_this_script){alert ('fontsize is being set: ' + fontSize);}
		document.body.style.fontSize = fontSize + '%';

		// Save font related settings
		createCookie("fontSize", fontSize, 365);
	}






/*
 * HELPER FUNCTIONS - 
 */

//cookie saving and reading

	function createCookie(name,value,days) {
	  if (days) {
	    var date = new Date();
	    date.setTime(date.getTime()+(days*24*60*60*1000));
	    var expires = "; expires="+date.toGMTString();
	  }
	  else expires = "";
	  document.cookie = name+"="+value+expires+"; path=/";
	}

	function readCookie(name) {
	  var nameEQ = name + "=";
	  var ca = document.cookie.split(';');
	  for(var i=0;i < ca.length;i++) {
	    var c = ca[i];
	    while (c.charAt(0)==' ') c = c.substring(1,c.length);
	    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	  }
	  return null;
	}




/*
 * ChangeLog
 *
	Cookies now written on every change - no more dancing around onUnload event
	
	Removed defaultStyle from .js - facilitates setting within template config.
	//	var defaultStyle = "fixed";



 */





