var DHTML = (document.getElementById || document.all || document.layers);

//pass min and max -measured against window width
function P7_MinMaxW(a,b){
	var nw="auto",w=document.documentElement.clientWidth;
	if(w>=b){nw=b+"px";}
	if(w<=a){nw=a+"px";}
	return nw;
}

function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}

function resizeScreen() {
	var scr_w = screen.availWidth;
	var scr_h = screen.availHeight;
	var browseWidth, browseHeight;
	
	if(document.layers||(document.getElementById&&!document.all)){ 
	   browseWidth=window.outerWidth;
	   browseHeight=window.innerHeight;
	}else if(document.all){
	   browseWidth=document.body.clientWidth;
	   browseHeight=document.body.clientHeight;
	}
	
	if (scr_w != browseWidth|| scr_h != browseHeight) {
	   //window.resizeTo(scr_w, scr_h);
	   //window.moveTo(0, 0);
	}
	if (!DHTML) return;
	var x = new getObj('content');
	alert("H:" + browseHeight);
	x.style.height = (browseHeight - 200) + "px";
}
function setHeight(){
	var availableHeight = 0;
	var availableWidth = 0;
	var contentDiv = document.getElementById('content').style;
	
	if(document.all) {
	    availableHeight = document.body.clientHeight;
	    availableWidth = document.body.clientWidth;
	} else {
	    availableHeight = innerHeight;
	    availableWidth = innerWidth;
	}
	
	contentDiv.height = availableHeight - 100;
	contentDiv.width = availableWidth;
}

function ajaxManager() {
	var args = ajaxManager.arguments;
	switch (args[0]) {
		case "load_page":
		if (document.getElementById) {
			var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
		}
		if (x) {
			x.onreadystatechange = function() {
				if (x.readyState == 4 && x.status == 200) {
					el = document.getElementById(args[2]);
					el.innerHTML = x.responseText;
				}
			}
			x.open("GET", args[1], true);
			x.send(null);
		}
		break;
		case "start_up":
			ajaxManager('load_page', 'basic.xml', 'contentLYR');
			break;
	}
}

function getStyle(el,styleProp) {
	var x = document.getElementById(el);
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	if (curleft > 700) {
		curleft = 700;
	}
	return [curleft,curtop];
}
function showKeywordBox(mySpan) {
	var pos=findPos(mySpan);
	var x = pos[0];
	var y = pos[1];
	var myDiv = document.getElementById("kwtag");
	myDiv.style.visibility = "visible";
	myDiv.style.left = x + 'px';
	myDiv.style.top = (y + 20) + 'px';
}
function hideKeywordBox() {
	var myDiv = document.getElementById("kwtag");
	myDiv.style.visibility="hidden";
	return false;
}

function displayKeywordPopup(curSpan, keyword) {
	var u = '/keywords/' + keyword + '.html';
	ajaxManager('load_page', u, 'kwtag');
	showKeywordBox(curSpan);
	return false;
}

function displayElink(s,c) {
	var u = '/gls/pages/check?s=' + s + '&c=' + c;
	ajaxManager('load_page', u, 'se');
}

/* get the full Cookie from the cookie jar*/
function getCookie(name) {
	var start;
	start=0;
	thisCookie = document.cookie.split("; ")
	for (i = 0; i < thisCookie.length; i++) {
		if (name == thisCookie[i].split("=")[0]) {
			chocchip=unescape(thisCookie[i].split("=")[1]);
			if (chocchip == "undefined" || chocchip == "") {
				return false;
			}
			return chocchip;
		}
	}
	return false;
}

function displaySignIn() {
	var myli = document.getElementById("signli");
	if (getCookie("a")) {
		myli.innerHTML = '<a href="/gls/pages/logout">Sign Out</a>';
	} else {
		myli.innerHTML = '<a href="/gls/pages/login">Sign In</a><ul><li><a href="/gls/pages/login">To Your Account</a></li><li><a href="/gls/pages/signup">Create New Account</a></li><li><a href="/contact.html">Contact GreatLifeStories</a></li></ul>';
	}
}

function displayMyStory() {
	var ms = document.getElementById("mystory");
	if (getCookie("a")) {
		var u = '/gls/pages/mystorylink';
		ajaxManager('load_page', u, 'mystory');
	}
}

function confirmCreateStory_v1(myLink) {
	if(getCookie("a")) {
		return true;
	} else {
		var x = window.confirm("Do you already have a story?");
		if(x) {
			document.location='/gls/pages/create';
		} else {
			document.location='/gls/pages/create';
		}
		return false;
	}
}
function confirmCreateStory(myLink) {
	if(! getCookie("a")) {
		var pos=findPos(myLink);
		var x = pos[0];
		var y = pos[1];
		var myDiv = document.getElementById("createconfirm");
		myDiv.style.visibility = "visible";
		myDiv.style.left = x + 'px';
		myDiv.style.top = (y + 20) + 'px';
		return false;
	}
}
function hideCreateConfirmBox() {
	var myDiv = document.getElementById("createconfirm");
	myDiv.style.visibility="hidden";
	return false;
}

function showChapterHint(myDiv) {
	var pos=findPos(myDiv);
	var x = pos[0];
	var y = pos[1];
	var myDiv = document.getElementById("storyhint");
	myDiv.style.visibility = "visible";
	myDiv.style.left = x + 'px';
	myDiv.style.top = (y + 20) + 'px';
}

function hideChapterHint() {
	var myDiv = document.getElementById("storyhint");
	myDiv.style.visibility="hidden";
}
