$(document).ready(function(){

	// If no size cookie is set, then determine the screen size and set it.
	setSizeCookie();

//	agent = navigator.userAgent;
//	result = agent.indexOf('Safari');

//	if (result > -1) {
//		setCookie('size', 'small', null, '/');
//	}

//	var size = getCookie('size');
//	toggleSS(size);	


//	$("#smallSize").removeClass("activeSize");
//	$("#mediumSize").removeClass("activeSize");
//	$("#largeSize").removeClass("activeSize");
	
//	$("#"+size+"Size").addClass("activeSize");
	
//	agent = navigator.userAgent;
//	result = agent.indexOf('Safari');

	$("#smallSize").click( function () {
		setCookie('size', 'small', null, '/');
	//	alert('tried to set cookie size to small');
		window.location.reload();
//		toggleSS("small");			
	});	
	
	$("#mediumSize").click( function () {
		//	alert("starting medium click");
//		if (result > -1) {
//			alert("Due to a bug in Safari web browser our page resize mechanism doesn't work here.  We are working to fix this problem.  In the mean time, please try another web browser to view larger page sizes.");
//		} else { 
			setCookie('size', 'medium', null, '/');
	//		alert('tried to set cookie size to medium');
			window.location.reload();
			//			toggleSS("medium");	
//		}
	});	
	
	$("#largeSize").click( function () {
		//	alert("starting large click");
		//	alert("starting small click");	
//		if (result > -1) {
//			alert("Due to a bug in Safari web browser our page resize mechanism doesn't work here.  We are working to fix this problem.  In the mean time, please try another web browser to view larger page sizes.");
//		} else { 
			setCookie('size', 'large', null, '/');
		//	alert('tried to set cookie size to large');
			window.location.reload();
//			toggleSS("large");	
//		}
	});	

});



function toggleSS(title)
{

	agent = navigator.userAgent;
	result = agent.indexOf('Safari');

	$("link[title=small]").each(function(i) { this.disabled = true; });
	$("link[title=medium]").each(function(i) { this.disabled = true; });
	$("link[title=large]").each(function(i) { this.disabled = true; });

	if (title == "small" || title == "medium" || title == "large") {
	if (title == "small") {	$("link[title=" + title + "]").each(function(i) { this.disabled = false; });	}
	
	//	alert('starting medium');
		if (title == "medium") {	
			$("link[title=" + title + "]").each(function(i) { 
			//	alert(this.disabled);
				this.disabled = false; 
			//	alert(this.disabled);
			});	
	
		}	
	//	alert('ending medium');
	
		if (title == "large") {	$("link[title=" + title + "]").each(function(i) { this.disabled = false; });	}
	}

	$("#smallSize").removeClass("activeSize");
	$("#mediumSize").removeClass("activeSize");
	$("#largeSize").removeClass("activeSize");

	$("#"+title+"Size").addClass("activeSize");

}


function setSizeCookie ()
{
	size = getCookie('size');
	
	if (size == null) {	
		
		var width = $(window).width();
		var height = $(window).height();
		
		if (width > 1600 && height > 1000)
		{
		//	alert("no size cookie set.  Setting size cookie to large");
			setCookie('size', 'large', null, '/');	
		}
		else if (width > 1280 && height > 800)
		{
		//	alert("no size cookie set.  Setting size cookie to medium");
			setCookie('size', 'medium', null, '/');
		}
		else
		{
		//	alert("no size cookie set.  Setting size cookie to small");
			setCookie('size', 'small', null, '/');
		}
		window.location.reload();
	}

}



// Utility function to mimic PHP's ucfirst function
function ucfirst (str) {
    // Makes a string's first character uppercase  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/ucfirst
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: ucfirst('kevin van zonneveld');
    // *     returns 1: 'Kevin van zonneveld'
    str += '';
    var f = str.charAt(0).toUpperCase();
    return f + str.substr(1);
}


/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}


