/*	-------------------------------------------------------------
	function getBrowserWidth()
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Description:	Returns browser width
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	function getBrowserWidth(){
		if (window.innerWidth){
			return window.innerWidth;}	
		else if (document.documentElement && document.documentElement.clientWidth != 0){
			return document.documentElement.clientWidth;	}
		else if (document.body){return document.body.clientWidth;}		
			return 0;
	}
/*	-------------------------------------------------------------
	function dynamicLayout()
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Description:	Checks screen width and applied css
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	function dynamicLayout() {
		var expiryDate = new Date();
		expiryDate.setTime(expiryDate.getTime()+(1*24*60*60*1000));
		var browserWidth = getBrowserWidth();
		if (browserWidth < 850 ) {
			changeLayout("lowres");
			document.cookie = 'BrowserRes=lowres; expires=' + expiryDate.toGMTString() + '; path=/';
		}
		else if (browserWidth < 1050 ) {
			changeLayout("disabled");
			document.cookie = 'BrowserRes=disabled; expires=' + expiryDate.toGMTString() + '; path=/';
		}
		else {
			changeLayout("hires");
			document.cookie = 'BrowserRes=hires; expires=' + expiryDate.toGMTString() + '; path=/';
		}
	
	}
/*	-------------------------------------------------------------
	function changeLayout(description)
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Description:	Finds css and changes rel
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	function changeLayout(description) {
	   var rows = document.getElementsByTagName('link');
	   for(var i=0, row; row = rows[i]; i++){
		   if(row.getAttribute("title") == description){row.disabled = false;}
		   else if(row.getAttribute("title") != "default"){row.disabled = true;}
	   }
	}
	
	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;
	}
	
	// add event listeners
	addEvent(window, 'resize', dynamicLayout);
	
	// See if we know the resolution and can layout accordingly
	// Otherwise test the resolution
	var resCookie = readCookie("BrowserRes");
	if (resCookie != null)
	{
		changeLayout(resCookie);
	}
	else
	{
		dynamicLayout();
	}