// PRESERVE SCROLL START //
var db = (document.body) ? 1 : 0;
var scroll = (window.scrollTo) ? 1 : 0;

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;
}

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));
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else {
    if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
      //DOM compliant
      scrOfY = document.body.scrollTop;
      scrOfX = document.body.scrollLeft;
    } else {
      if( document.documentElement &&
          ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
      	//IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
      }
    }
  }
  //return [ scrOfX, scrOfY ];
  return (scrOfX + "_" + scrOfY);
}

function getCookieExpireDate() {
   var now = new Date();
   //now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
   // cookie expires in 1 day //
   now.setTime(now.getTime() + 1 * 24 * 60 * 60 * 1000);
   return(now);
}

function saveScroll(pagename) {
  if (!scroll) return;
  setCookie(pagename + "xy", getScrollXY(), getCookieExpireDate());
}

function loadScroll(pagename) {
  if (!scroll) return;
  var xy = getCookie(pagename + "xy");
  if (!xy) return;
  var ar = xy.split("_");
  if (ar.length == 2) scrollTo(parseInt(ar[0]), parseInt(ar[1]));
}

function openBox(pagename, boxname) {
	var c = getCookie(pagename);
	if ((c == "null") || (c == null))
		c = "";
	if (c.indexOf(boxname) < 0)
		c += "^" + boxname;
	setCookie(pagename, c, getCookieExpireDate());
}

/*
function saveBox(pagename, boxname) {
	var c = getCookie(pagename);
	if ((c == "null") || (c == null))
		c = "";
	if (document.getElementById(boxname).style.display != "none") {
		if (c.indexOf(boxname) < 0)
			c += "^" + boxname;
	} else {
		c = c.replace("^" + boxname, "");
	}
	setCookie(pagename, c, getCookieExpireDate());
}
*/

function saveBox(pagename, boxname) {
	var c = getCookie(pagename);
	if ((c == "null") || (c == null))
		c = "";
	c += "^";
	if (document.getElementById(boxname).style.display != "none") {
		if (c.indexOf("^" + boxname + "^") < 0)
			c += boxname + "^";
	} else {
		c = c.replace("^" + boxname + "^", "^");
	}
	c = c.substr(0, (c.length-1));
	setCookie(pagename, c, getCookieExpireDate());
}

function setBoxes(pagename) {
	var c = getCookie(pagename);
	if ((c == "null") || (c == null))
		return;
	arr = c.split("^");
	// skip the first item since it is blank //
	for(i=1; i<arr.length; i++) {
 		if (typeof(document.getElementById(arr[i])) == "object") {
			try {
				document.getElementById(arr[i]).style.display = "";
				/*if ((arr[i].search("div_category") >= 0) || (arr[i].search("div_lnav_group") >= 0) || (arr[i].search("div_ln_category") >= 0) || (pagename == 'syllabus') || (pagename == 'section_content') || (pagename == 'home_content') || (pagename == 'resources') || pagename == 'help_topics')
					document.getElementById("img" + arr[i].replace("div", "").replace("Box", "")).src = "images/icons/minimize-hide_16.gif";
				else {*/
					img_path = "images/icons/minimize-hide_16.gif";
					switch(arr[i]) {
						case("divPageNavBox"): 				img_path = "images/bar_QuickNavigation_f2.jpg"; break;
						case("divQuickNavBox"): 			img_path = "images/quickNav_QN_f2.gif"; break;
						case("divAssignmentInfoBox"): 		img_path = "images/quickNav_AI_f2.gif"; break;
						case("divResourcesBox"): 		    img_path = "images/quickNav_CR_f2.gif"; break;
						case("divActivitiesChecklistBox"): 	img_path = "images/quickNav_MWIR_f2.gif"; break;
						case("divGroupsBox"):
						case("divManageGroupsBox"): 		img_path = "images/bar_Groups_f2.gif"; break;
						case("divCalendarBox"): 			img_path = "images/bar_AssignmentCalendar_f2.jpg"; break;
					}
					document.getElementById("img" + arr[i].replace("div", "").replace("Box", "")).src = img_path;
				//}
			} catch(e) {
			}
			//document.getElementById(arr[i].replace("div_", "img_")).src = "images/gray_max.gif";
		}
	}
}

// PRESERVE SCROLL END //

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return(myWidth + '_' + myHeight);
}
