/*
onerror=handleErr;
var txt="";
function handleErr(msg,url,l) {
	txt="inside.\n\n";
	txt+="Error: " + msg + "\n";
	txt+="URL: " + url + "\n";
	txt+="Line: " + l + "\n\n";
	txt+="Click OK to continue.\n\n";
	alert(txt);
	return true;
}
*/
ignorePage = { 
				'/registration/':true
				,'/registration':true
				,'registration':true

				,'/testmymachine/':true
				,'/psagallery/':true
				,'/psagallery':true
				,'/gallery/':true
				,'/gallery':true
				,'/testmymachine':true
				,'testmymachine':true
			};
forcePage = {
				'/psagallery/':'/psagallery/'
				,'psagallery':'/psagallery/'
				,'gallery':'/psagallery/'
				// 'testmymachine.html':'/iframes/testmymachine.html'
				//,'/testmymachine.html':'/iframes/testmymachine.html'
				//,'/iframes/testmymachine.html':'/iframes/testmymachine.html'
			};
translatePage = {
					"behindp4b":Array("behindp4b_main_source.html",1)
					,"thesecretbox":Array("thesecretbox_main_source.html",2)
					,"weekbyweek":Array("weekbyweek_main_source.html",3)
					,"testimonials":Array("testimonials_main_source.html",4)
					,"registering":Array("registering_main_source2.html",5)
					,"educators":Array("p4b4educators_main3.html",6)
					,"parents":Array("parents_main_source.html",7)
					,"contact":Array("contact_form.html",10)
					//,"gallery":Array("/psagallery/",9)
				};

var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;	
var isMac = (navigator.appVersion.indexOf("Mac") != -1) ? true : false; 
var isIE4Mac = (isMac && isIE) ? true:false;

if (isIE) theVer  = parseInt(navigator.userAgent.substr(navigator.userAgent.indexOf('MSIE')+5,4).split(".")[0]);
else theVer = true;

if (window.location.pathname == window.top.location.pathname) {
	window.top.location.href = "/grownups/#" + ((typeof(errorPage) != 'undefined') ?  errorPage : document.location.pathname.split("/iframes/").join(""));
}

if (isIE && theVer < 6 || isIE4Mac) false;
else window.onload = onLoadInit;

lastPage = document.location.pathname;

function onLoadInit() {
	window.scroll(0,0);
	if (window.top.newVersion !== true) return;
	holdOrigStyles = document.getElementsByTagName("style");
	listOfCSS = document.getElementsByTagName("link");
	holdStyle = false;
	buildAnchorTags();
	buildListOfScripts();
	injectScripts();
	myBody = document.getElementsByTagName("body")[0];
	window.onload = function() {};
	if (!isIE) {
		navigateInterval = setInterval(goTo,250);
		window.onunload = function() {
			clearInterval(navigateInterval);
		}
	}
	window.top.document.title = html_entity_decode(document.title);
}
function html_entity_decode(str) {
	var temp = str.split("&amp;").join("&");
	return temp;
}
function buildListOfScripts() {
	var cells = document.getElementsByTagName('script');
	listOfScripts = Array();
	for (var i = 0; i < cells.length; i++) { 
		var temp = cells[i].getAttribute('src');
		listOfScripts[temp] = true;
	}
}
function injectScripts() {
	var injects = Array('/javascript/swfobject.js');
	for (var i=0;i<injects.length;i++) {
		var newTag = document.createElement('script');
		newTag.setAttribute('src',injects[i]);
		document.getElementsByTagName("head")[0].appendChild(newTag);
		listOfScripts[injects[i]] = true;
	}
}
function buildAnchorTags() {
	hashes = {};
	alterCells("A");
	alterCells("AREA");
	originalLocation = document.location.pathname;
	lastHash = window.top.location.hash;
}

function getY(oElement) {
	var iReturnValue = 0;
	while(oElement != null) {
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}
function alterCells(kind) {
	var cells = document.getElementsByTagName(kind);
	for (var i = 0; i < cells.length; i++) { 
		var temp = cells[i].getAttribute('href');
		var tempName = cells[i].getAttribute('name');
		if (kind = "A" && tempName != null && !temp && !isIE) {
			hashes[tempName] = getY(cells[i]);
			//alert(tempName+":"+hashes[tempName]);
		}
		if (!temp) continue;
		var temp2 = document.location.href;
		if  (temp.substr(0,temp2.length) == temp2) {
			temp = temp.substr(temp2.length);
		}
		temp = temp.split("http://" + document.location.hostname + "/iframes/").join("");
		temp = temp.split("http://" + document.location.hostname).join("");
		var secondPart = true;
		if (temp.substr(0,4) == "http") {
			var secondPart = false;
			cells[i].target = "_blank";
		} else var secondPart = true;
		if (temp.substr(0,1) == "/" && false) {
			var tempname=  temp.split("/").join("")
			if (translatePage[tempname]) {
				cells[i].onclick = changeTheAnchor; 
				var secondPart = false;
			}
		}
		if (secondPart) {
			if (temp.indexOf(".pdf") != -1) {
				// do nothing
				cells[i].target = '_blank';
			} else if (ignorePage[temp] == true) {
				// do nothing
				cells[i].target = '_top';
			/*
			} else if (temp == "/gallery" || temp == "/gallery/") {
				cells[i].hold = temp;
				cells[i].onclick = function() {
					window.top.popwindow(this.hold,true); 
					return false;
				}
			*/
			} else if (temp.substr(0,1) == "#" || temp.substr(0,11) == "javascript:" || temp.substr(0,8) == "history.") {
				if (!isIE && temp.substr(0,1) == "#") {
					cells[i].onclick = function() {
						var temp1 = window.top.location.hash.split(",");
						temp2 = temp1[0];
						if (temp1[1]) temp2 += "," + temp1[1];
						temp2 += "," + this.href.split("#")[1];
						window.top.location.hash = temp2;
						return false;
					}
				}
			/*
			} else if (cells[i].innerHTML.indexOf('...back') > 0) {
				cells[i].onclick = function() {
					window.top.history.back();
					return false;
				}
			} else if (cells[i].innerHTML.indexOf('back to the top.') > 0 || cells[i].innerHTML.indexOf('="#top"')) {
				alert('back to the top');
				cells[i].onclick = function() {
					alert('back to the top2');
					window.scrollTo(0,0);
					return false;
				}
			*/
			} else {//if (temp.substr(0,7) != "http://" || (temp.substr(0,7) == "http://" && isP4B(temp))) {
				cells[i].onclick = changeTheAnchor;
			}
		}
	}
}

function isP4B(str) {
	str = str.split("http://").join("");
	var loc1 = str.indexOf("/");
	if (loc1 == -1) loc1 = str.length;
	str = str.substring(0,loc1);
	switch (str) {
		case "www.power4bones.com":
		case "power4bones.com":
		case "localhost":
			return true;
			break;
		default:
			return false;
			break;
	}
}
function changeTheAnchor() {
	var temp = this.getAttribute('href');
	temp = temp.split("http://localhost/").join("");
	temp = temp.split("http://www.power4bones.com/").join("");
	temp = temp.split("http://power4bones.com/").join("");
	temp = temp.split("http://test.power4bones.com/").join("");
	temp = temp.split("http://www.power4bones.com/").join("");
	temp = temp.split("iframes/").join("");
	var tempname = temp.split("/").join("");
	if (translatePage[tempname]) {
		var temptrans = translatePage[tempname];
		hold = window.top.dontSwap;
		window.top.dontSwap = false;
		window.top.navSwapImage('out',hold);
		if (temptrans[1] != 0) {
			window.top.navSwapImage('over',temptrans[1]);
			window.top.dontSwap = temptrans[1];
		}

		var temp = temptrans[0];
	}
	if (isIE) {
		window.top.frames['tracker1'].location.search = "hash=" + temp +"&href=" + temp + "&butt=" + window.top.dontSwap;
	} else {
		doAjax(temp,temp,false);
	}
	return false;
}
function doAjax(hash,href,forceButt) {
	//alert("doAjax("+hash+","+href+")");
	if (typeof(window.top.buttHilight) == 'undefined') {
		window.top.buttHilight = false;
	}
	var buttHilight = window.top.buttHilight;
	//alert(buttHilight);
	if (buttHilight && forceButt == -1) {
		forceButt = window.top.origButton;
	}
	var beforeComma = hash.split(",")[0];
	if (typeof(forcePage[beforeComma]) != 'undefined') {
		window.location.href = forcePage[beforeComma];
		return;
	}
	var temp = "#" + hash;
	if (buttHilight) {
		if (forceButt !== false) {
			temp += "," + forceButt;
		} else if (typeof(window.top.dontSwap) != 'undefined' && window.top.dontSwap !== false) {
			temp += "," + window.top.dontSwap;
		} else if (window.top.dontSwap === false) {
			temp += ",0";
		}
	}
	if (temp.substr(0,2) == "#," || temp == "#") lastHash = window.top.location.hash = "";
	else lastHash = window.top.location.hash = temp;
	if (buttHilight) window.top.swapIfDiff();
	ajaxGet(href.split(",")[0]);
}
function ajax(what,url) {
	//alert("ajax("+what+","+url+")");
    if (window.XMLHttpRequest) {
		this[what] = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
		this[what] = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		error('no ajax object',false);
		return false;
	}
	this[what].open('GET', url + "?qs=" + Math.random(), true);
	this[what].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	return true;
}

function ajaxGet(what) {
	//alert("ajaxGet("+what+")");
	var temp = what.split(",")[0];
	lastPage = what;
	//alert("lastpage = "+lastPage);
    if (ajax('ajaxGet1',what)) {
		ajaxGet1.onreadystatechange = function() {
			if (ajaxGet1.readyState == 4) {
				var text = ajaxGet1.responseText;
				var loc1 = text.indexOf("<body");
				if (loc1 == -1) var loc1 = text.indexOf("<BODY");
				if (loc1 == -1) var loc1 = false;
				else {
					var loc2 = loc1 + text.substr(loc1).indexOf(">") + 1;
					var loc3 = text.indexOf("</body");
					if (loc3 == -1) var loc3 = text.indexOf("</BODY");
				}
				if (loc1 == false || loc1 == -1 || loc2 == -1 || loc3 == -1) {
					//??
					return;
				}
				var topText = text.substring(0,loc1);

				myBody.innerHTML = "";

				var hh1 = document.getElementsByTagName('head')[0];
				
				//
				
				//
				for (var i=0;i<document.body.attributes.length;i++) {
					if (document.body.attributes[i].specified) {
						document.body.removeAttribute(document.body.attributes[i].name);
					}
				}
				var temp = text.substring(loc1,loc2-1).split("<body ").join("").split("<BODY ").join("") + " ";
				temp = temp.split('  ').join(' ')
				temp = temp.split('  ').join(' ')
				temp = temp.split("='").join('="')
				temp = temp.split('" ').join('"[~]')
				temp = temp.split("' ").join('"[~]');
				temp = temp.split("[~]");
				var bodyStyles = Array();
				for (var i=0;i<temp.length;i++) {
					var nvp = temp[i].split('="');
					if (nvp.length == 2 && nvp[1].substr(nvp[1].length - 1) == '"') {
						nvp[0] = nvp[0].split(" ").join("").toLowerCase();
						nvp[1] = nvp[1].split("#").join("");
						document.body.setAttribute(nvp[0],nvp[1].substr(0,nvp[1].length - 1));
						bodyStyles.push(Array(nvp[0],nvp[1].substr(0,nvp[1].length - 1)));
					}
				}
				//
				
				var tit = scrape(topText,'title');
				window.top.document.title = document.title = html_entity_decode(tit);

				if (holdStyle !== false) {
					//alert('remove style 1');
					hh1.removeChild(holdStyle);
					holdStyle = false;
				}
				if (holdOrigStyles.length > 0) {
					for (var i=0;i<holdOrigStyles.length;i++) {
						//alert('remove style 2');
						hh1.removeChild(holdOrigStyles[i]);
					}
					holdOrigStyles = Array();
				}
				var styles = scrape(topText,'style');
				if (styles || bodyStyles.length > 0) {
					bodyStylesText = "";
					if (bodyStyles.length > 0) {
						var translate = {
											 'link':'a:link { color: #[replace] } '
											,'vlink':'a:visited { color: #[replace] } '
											,'alink':'a:active { color: #[replace] } '
											,'text':'body { color: #[replace] } '
											,'bgcolor':'body { background-color: #[replace] } '
											,'background':'body { background-image: url([replace]) } '
										};
						for (var i=0;i<bodyStyles.length;i++) {
							var temp = translate[bodyStyles[i][0]];
							if (temp) bodyStylesText += temp.split("[replace]").join(bodyStyles[i][1]);
						}
					}
					//alert("bodyStylesText="+bodyStylesText);
					var ss1 = document.createElement('style');
					ss1.setAttribute("type", "text/css");
					if (ss1.styleSheet) {
						ss1.styleSheet.cssText = bodyStylesText + styles;
					} else {
						var tt1 = document.createTextNode(bodyStylesText + styles);
						ss1.appendChild(tt1);
					}
					holdStyle = hh1.appendChild(ss1);
				}

				if (listOfCSS.length > 0) {
					//alert('remove css');
					for (var i=0;i<listOfCSS.length;i++) {
						hh1.removeChild(listOfCSS[i]);
					}
				}
				listOfCSS = Array();
				var cssList = scrapeAttribute(topText,'link','href');
				for (var i=0;i<cssList.length;i++) {
					//alert('remove css:'+cssList[i]);
					var ss1 = document.createElement('link');
					ss1.setAttribute("type", "text/css");
					ss1.setAttribute("rel", "stylesheet");
					ss1.setAttribute("href", cssList[i]);
					hh1.appendChild(ss1);
					listOfCSS.push(ss1);
				}

				myBody.innerHTML = text.substring(loc2,loc3);

				var scriptList = scrapeAttribute(topText,'script','src');
				for (var i=0;i<scriptList.length;i++) {
					if (listOfScripts[scriptList[i]]) continue;
					listOfScripts[scriptList[i]] = true;
					var ss1 = document.createElement('script');
					ss1.setAttribute("language", "JavaScript");
					ss1.setAttribute("type", "text/javascript");
					ss1.setAttribute("src", scriptList[i]);
					hh1.appendChild(ss1);
				}

				var toExecute = scrape(text,'script');
				if (toExecute) {
					eval(toExecute);
				}

				scroll(0,0);

				buildAnchorTags();
			}
		}
		ajaxGet1.send("");
	}
}
function scrape(text,tag) {
	tag = tag.toLowerCase();
	text = text.split("<"+tag.toUpperCase()).join("<"+tag);
	text = text.split("</"+tag.toUpperCase()).join("</"+tag);
	var s1 = text.indexOf("<"+tag);
	var toReturn = "";
	cnt = 0;
	while (s1 != -1) {
		var s2 = text.substr(s1).indexOf(">");
		s2 += s1;
		var s3 = text.substr(s2).indexOf("</"+tag);
		s3 += s2;
		toReturn += text.substring(s2 + 1,s3);
		var s1 = text.substr(s3).indexOf("<"+tag);
		if (s1 != -1) s1 += s3;
		if (++cnt > 100) {
			alert("scrape: > 100");
			return false;
		}
	}
	return (toReturn.length > 0) ? toReturn.split("<!--").join(" ").split("-->").join(" ") + " " : false;
}
function scrapeAttribute(text,tag,attr) {
	tag = tag.toLowerCase();
	attr = attr.toLowerCase();
	text = text.split("<"+tag.toUpperCase()).join("<"+tag);
	text = text.split("</"+tag.toUpperCase()).join("</"+tag);
	text = text.split(attr.toUpperCase()).join(attr);
	var s1 = text.indexOf("<"+tag);
	var toReturn = Array();
	cnt = 0;
	while (s1 != -1) {
		var s2 = text.substr(s1).indexOf(">");
		s2 += s1;
		var s3 = text.substring(s1,s2).indexOf(attr+'=');
		if (s3 != -1) {
			s3 = s3 + s1 + ((attr + '=').length);
			var q = text.substr(s3,1);
			if (q == '"' || q == "'") {
				var s4 = text.substring(s3 + 1,s2).indexOf(q);
				if (s4 != -1) {
					s4 += s3 + 1;
					toReturn.push(text.substring(s3 + 1,s4));
				}
			}
		}
		s1 = text.substr(s2).indexOf("<"+tag);
		if (s1 != -1) s1 += s2;
		if (++cnt > 10) {
			alert("scrapeAttribute: > 100");
			return false;
		}
	}
	return (toReturn.length > 0) ? toReturn : false;
}
function goTo() {
	var temp = window.top.location.hash;
	if (lastHash != temp) {
		if (temp == "") document.location.href = originalLocation;
		else {
			var temp2 = temp.substr(1).split(",");
			if (temp2[2] && !isIE) {
				//alert("hash");
				window.scrollTo(0,hashes[temp2[2]]);
			} else {
				//alert("ajax");
				ajaxGet("/iframes/" + temp2[0]);
			}
		}
		window.top.swapIfDiff();
	}
	lastHash = temp;
	//var e = window.top.trc('lastHash='+lastHash+"<br>"+'hash inside='+document.location+"<br>");
}
