// Template Cookies and Overview animation functions
// needs collapsexpand.js to work

var cookie_set = false;

function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null){
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
	}
	document.cookie = window.location.pathname + ":" + name + "=" + escape(value) + expire;
}

function readCookie(name){
  var cookieValue = "";
  var search = window.location.pathname + ":" + name + "=";
  if(document.cookie.length > 0){ 
    offset = document.cookie.indexOf(search);
    if (offset != -1){ 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

function deleteCookie(name) {
	if ( readCookie(name) ) document.cookie = window.location.pathname + ":" + name + "=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

//save the current state of features divs and a record of the visit for an hour.
function saveState() {
	writeCookie("visited", "yes", 1);
	for (var i=1; i < 11; i++) {
		writeCookie("prod_feature" + eval(i), getState("prod_feature" + eval(i)), 1);
	}
}

var exptimer;
var ovHgt = 1;
var pauseTime = 500; // milliseconds to pause before expanding overview

// function for animated expansion of the overview
function expOverview() {
	clearTimeout(pausetimer);
	var ov = document.getElementById("overview");
	(ovHgt > 50)? ovHgt += 50 : ovHgt += ovHgt;
	if (ovHgt > 450) { //this value depends on the intial height of the section.
		clearTimeout(exptimer);
		ov.style.height = "auto";
		return false;
	} else if (ovHgt == 8) { //this shifts the padding during the animation and not at the end
		ov.style.padding = "10px";
		ov.style.height = ovHgt+"px";
		exptimer=setTimeout("expOverview()",50);
	} else {
		ov.style.height = ovHgt+"px";
		exptimer=setTimeout("expOverview()",50);
	}
}

function pauseExp() {	
	nullLinks(); // remove title hover text
	var wrn = document.getElementById("ckstate");
	writeCookie('test','none',1);
	if (readCookie('test')) {	// check to see if cookies are disabled
		wrn.style.display = "none";
		cookie_set = true;
		deleteCookie('test');
	} else {
		wrn.style.display = "block";
		wrn.innerHTML = 'For a better browser experience, please enable cookies. <a href="javascript:void(0);" onmouseover="doPopUp(event, popup000, 280)" onmouseout="hideTip()" class="rollover">Why is this important?</a>';
	}
	var ov = document.getElementById("overview");
	ov.style.display = "block";
	if (cookie_set && readCookie("visited") == "yes") { //restore previous state of collapsable sections & skip animated opening
		ov.style.padding = "10px";
		ov.style.height = "auto";
		for (var i=1; i < 11; i++) {
			forceState(readCookie("prod_feature" + eval(i)), "prod_feature" + eval(i), "ex_title" + eval(i));
		}
	} else { // use default state of sections & animate the expansion of the overview
		defaultState();
		pausetimer=setTimeout("expOverview()", pauseTime);
	}
}

