// Ronald Huereca (www.ronalfy.com)
// Custom Reading Width 0.5b
// Last Updated: 11/05/2006
// Find updates to this script at: http://www.devlounge.net/articles/custom-reading-width-beta

function ReadingWidth(readingContainer, defaultWidth, IsFixed, minWidth, maxWidth, IsPercentage) {
	this.defaultWidth = 0;
	this.sliderPosition = 0;
	this.container = null;
	this.IsPercentage = true;
	this.IsFixed = IsFixed;
	this.minWidth = 0;
	this.maxWidth = 100;
	this.lastSavedWidth = null;
	
	//Functions
	this.GetBrowserWidth = GetBrowserWidth;
	this.SliderChanged = SliderChanged;
	this.RadioChanged = RadioChanged;
	this.SetDefault = SetDefault;
	this.CreateCookie = CreateCookie;
	this.EraseCookie = EraseCookie;
	this.SetReadingWidthCookie = SetReadingWidthCookie;
	this.RemoveReadingWidthCookie = RemoveReadingWidthCookie;
	this.ReadCookie = ReadCookie;
	
	//Set whether the passed minwidth and maxwidth is a percentage
	(typeof( IsPercentage ) == 'boolean') ? this.IsPercentage = IsPercentage : this.IsPercentage = false;
	
	//Set the min amd max widths
	if (this.IsPercentage == true) {
		if (typeof(minWidth) == 'number' && minWidth <= 100 && minWidth >= 0) { this.minWidth = minWidth; }
		(typeof(maxWidth) == 'number' && maxWidth <= 100 && maxWidth > this.minWidth) ? this.maxWidth = maxWidth : this.maxWidth = 100;
	} else {
		if (typeof(minWidth) == 'number' && minWidth >= 0) { this.minWidth = minWidth;}
		(typeof(maxWidth) == 'number' && maxWidth > this.minWidth) ? this.maxWidth = maxWidth : this.maxWidth = this.GetBrowserWidth();
		}
	
	//Find the readingContainer
	this.container = document.getElementById(readingContainer);
	this.defaultWidth = defaultWidth;
	this.lastSavedWidth = this.container.style.width;
	
	//Make the overlay (LightBox) static to fix a bug in IE when using the BODY tag as a container - Comment the next two lines if you don't use the BODY tag as a container
		var overlay = document.getElementById("overlay");
		if (overlay != null) { overlay.style.width = this.GetBrowserWidth() };															 
}
function SetReadingWidthCookie(sliderValue) {
		var myObj = this;
		var IsFixed = 0;
		(myObj.IsFixed) ? IsFixed = 1 : IsFixed = 0;
		var cookieString = myObj.container.style.width + "," + sliderValue + "," + IsFixed;
		myObj.CreateCookie("ReadingWidth", cookieString, 365);
	}
function RemoveReadingWidthCookie() {
	var myObj = this;
	myObj.EraseCookie("ReadingWidth");
	myObj.container.style.width = myObj.defaultWidth;
}
function SliderChanged(percentage) {
	var myObj = this;
	if (typeof(percentage) == 'number') {
		//Convert everything to pixels
		var newmaxWidth = 0, mewminWidth = 0;			
		(myObj.IsPercentage == true) ? newmaxWidth = (myObj.maxWidth/100) * myObj.GetBrowserWidth() : newmaxWidth =  myObj.maxWidth;
		(myObj.IsPercentage == true) ? newminWidth =  (myObj.minWidth/100) * myObj.GetBrowserWidth() : newminWidth =  myObj.minWidth;
		var finalWidth =  Math.round(percentage * myObj.GetBrowserWidth());
				
		//Compare to maximum values
		if (finalWidth > newmaxWidth) { finalWidth = newmaxWidth;}
		if (finalWidth < newminWidth) { finalWidth = newminWidth;}
		
		//Stay with pixels or convert to percentages
		if (myObj.IsFixed == false) {
				finalWidth = Math.round((finalWidth/myObj.GetBrowserWidth())*100);
				myObj.container.style.width = finalWidth + "%";
		} else {
			finalWidth = Math.round(finalWidth);
			myObj.container.style.width = finalWidth + "px";
		}
	}	
}
function SetDefault() {
	var myObj = this; 
	myObj.container.style.width = myObj.lastSavedWidth;
}
//Called when the radio button (fixed or fluid) has changed 
//Takes in a value whether the layout is fixed or fluid
function RadioChanged(IsFixed, value) {
	var myObj = this;
	if (typeof(IsFixed) == 'boolean' && typeof(value) == 'number') {
		myObj.IsFixed = IsFixed;
		myObj.SliderChanged(value);		
	}
}

//From http://www.quirksmode.org/js/cookies.html
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 var 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;
}
function EraseCookie(name) {
	CreateCookie(name,"",-1);
}
//Returns the Browser Width
//From: http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
function GetBrowserWidth() {
	var myWidth = 0;
	
	if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
		myWidth = window.innerWidth - 20;
	} else if( document.documentElement && ( document.documentElement.clientWidth) ) {
	//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth-4;
	} else if( document.body && ( document.body.clientWidth) ) {
	//IE 4 compatible
		myWidth = document.body.clientWidth;
	}
	return myWidth;
}
//This point marks the end of the code for the readingWidth object.
//Credit: Simon Willison 2004
//From: http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
//Adds a load event to make the link to resize the page visible
addLoadEvent(function() {
	if (document.getElementById) {
		var readingLink = document.getElementById("reading-width-resize");
		readingLink.style.display = "inline";
	}
	
});

