var GB_DONE = false, GB_HEIGHT = 707, GB_WIDTH = 400, GB_TOP = 132;
var maActiveTab, maDefaultStore, oc = (typeof oc != 'undefined') ? encodeURIComponent(oc) : '';
var rpType = (typeof srt != 'undefined') ? srt : '';
var sur = "";
var redirectFn = "";

function GB_show(url, height, width) {   
  GB_HEIGHT = height || 400;
  GB_WIDTH = width || 400;
  if(!GB_DONE) {
    jQuery(document.body).append("<div id='GB_overlay'></div><div id='ma_wrapper'></div>");
    GB_DONE = true;
  }
  hideSelectBoxes();
  maOverlay();
  jQuery("#GB_overlay").show();
  jQuery("#GB_frame").remove();
  jQuery("#ma_wrapper").append("<iframe id='GB_frame' name='GB_frame' frameborder='0' scrolling='no' allowtransparency='true' width='943' height='709' src='"+url+"'></iframe>");  
  GB_position();   
  jQuery("#ma_wrapper").show();
}
function getSuccessRegistration() {
	return sur;
}
function resetSuccessRegistration() {
	sur = "";

}
function setSuccessRegistration() {
	sur = "sur";
}
function MA_show() {
	if (document.domain.indexOf("scholastic") != -1) {
   		document.domain = "scholastic." + document.domain.split(".").pop();
    }
	var url = MYACCOUNT_HOST + '/com.scholastic.myaccount.MyAccount/MyAccount.html';
	if (typeof maDefaultStore != "undefined") {
		url += maDefaultStore;
	}	
	if (typeof rpType != "") {
		if (url.indexOf('?') != -1) { url += '&amp;t=' + rpType; }
		else { url += '?t=' + rpType; }		
	}
	if (typeof oc != "") {
		if (url.indexOf('?') != -1) { url += '&amp;oc=' + oc; }
		else { url += '?oc=' + oc; }		
	}
	if (typeof maActiveTab != "undefined") {
		url +=  maActiveTab;
	}
	if (typeof sur != "") {
		if (url.indexOf('?') != -1) { url += '&amp;sur=' + sur; }
		else { url += '?sur=' + sur; }		
	}
	GB_show(url,614,943);		

	return false;	
}

function MA_showLoginForSSO() {
	if (document.domain.indexOf("scholastic") != -1) {
   		document.domain = "scholastic." + document.domain.split(".").pop();
	}
	var url = MYACCOUNT_HOST + '/com.scholastic.myaccount.MyAccount/MyAccount.html?sul=losso';
	
	if (typeof rpType != "") {
		if (url.indexOf('?') != -1) { url += '&amp;t=' + rpType; }
		else { url += '?t=' + rpType; }		
	}
	if (typeof oc != "") {
		if (url.indexOf('?') != -1) { url += '&amp;oc=' + oc; }
		else { url += '?oc=' + oc; }		
	}
	if (typeof sur != "") {
		if (url.indexOf('?') != -1) { url += '&amp;sur=' + sur; }
		else { url += '?sur=' + sur; }		
	}

	GB_show(url,614,943);	
	return false;	
}

function MA_showLoginForSSON() {
	if (document.domain.indexOf("scholastic") != -1) {
   		document.domain = "scholastic." + document.domain.split(".").pop();
	}
	var url = MYACCOUNT_HOST + '/com.scholastic.myaccount.MyAccount/MyAccount.html';
	
	if (typeof rpType != "") {
		if (url.indexOf('?') != -1) { url += '&amp;t=' + rpType; }
		else { url += '?t=' + rpType; }		
	}
	if (typeof oc != "") {
		if (url.indexOf('?') != -1) { url += '&amp;oc=' + oc; }
		else { url += '?oc=' + oc; }		
	}
	if (typeof sur != "") {
		if (url.indexOf('?') != -1) { url += '&amp;sur=' + sur; }
		else { url += '?sur=' + sur; }		
	}

	GB_show(url,614,943);	
	return false;	
}
function MA_showLogin() {
	if (document.domain.indexOf("scholastic") != -1) {
   		document.domain = "scholastic." + document.domain.split(".").pop();
	}
	var url = MYACCOUNT_HOST + '/com.scholastic.myaccount.MyAccount/MyAccount.html?t=lo&amp;oc=' + oc;
	GB_show(url,614,943);	
	return false;	
}

function RP_show() {
	if (document.domain.indexOf("scholastic") != -1) {
   		document.domain = "scholastic." + document.domain.split(".").pop();
	}
	var url = (typeof rpType != 'undefined' && rpType != '') ? '/com.scholastic.registration.Registration/Registration.html?t=' + rpType + '&amp;oc=' + oc : '/../registration/com.scholastic.registration.Registration/Registration.html?oc=' + oc;		
	
	url = (typeof sur != 'undefined' && sur != '') ? url+ '&amp;sur=' + sur:url;
		
	var uri = MYACCOUNT_HOST + url;
	GB_show(uri,614,943);	
	return false;	
}


function setActiveTab(tabName) { maActiveTab = "#" + tabName; }
function setDefaultStore(storeID) { maDefaultStore = "?" + storeID; }
function setRegistrationType(type) {
	rpType = type; /* possible types are: c - clubs, ts - teacher store, lo - login only */
}

var myAccoutCloseHook, onSignOutHook, onSuccessRegistration, shopForEbooksHook, shopForGradeEBooksHook, aboutUsHook, viewEBookDetailsHook;
function setMyAccountCloseHook(hook){ myAccoutCloseHook = hook; }
function setOnSignOutHook(hook){ onSignOutHook = hook; }
function setOnSuccessRegistration(hook){ onSuccessRegistration = hook; }
function setShopForEBooksHook(hook){ shopForEbooksHook = hook; }
function setShopForGradeEBooksHook(hook){ shopForGradeEBooksHook = hook; }
function setAboutUsHook(hook){ aboutUsHook = hook; }
function setViewEBookDetailsHook(hook){ viewEBookDetailsHook = hook; }

function GB_hide() {
  jQuery("#ma_wrapper,#GB_overlay").hide();
  showSelectBoxes();
  jQuery("#GB_frame").remove();
  if (typeof nodisplay == "undefined") { chkLS(); }
  if (myAccoutCloseHook != null) { myAccoutCloseHook(); }
}
function setSSORegistrationComplete() {
	if(sur != '') {
		setRedirectFunction();
	}
}

function getRedirectFunction() {
	return redirectFn;
}
function setRedirectFunction() {
	redirectFn = "sso";
}

function resetRedirectFunction() {
	redirectFn = "";
}
function PostRegRedirect() {
  jQuery("#ma_wrapper,#GB_overlay").hide();
  showSelectBoxes();
  jQuery("#GB_frame").remove();
  if (onSuccessRegistration != null) { 
  	onSuccessRegistration(); 
  }
}

function RP_hide() { GB_hide(); }

function maOverlay() {  
  var arrayPageSize = getPageSize();
  var arrayPageScroll = getPageScroll();
  var h = arrayPageSize[1] + arrayPageScroll[1];
  if (h < (GB_HEIGHT + GB_TOP)) { h = GB_HEIGHT + GB_TOP + 10; }
  jQuery("#GB_overlay").css({height: h + 'px'});
}

function GB_position() {
  var de = document.documentElement;
  var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  jQuery("#ma_wrapper").css({width:GB_WIDTH+"px",height:GB_HEIGHT+"px",
    left: "22px",top: GB_TOP + "px"}); 
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	var arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	var pageHeight, pageWidth, xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	if(yScroll < windowHeight){ pageHeight = windowHeight; }
	else { pageHeight = yScroll; }

	if(xScroll < windowWidth){ pageWidth = windowWidth; }
	else { pageWidth = xScroll; }
	var arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) { selects[i].style.visibility = "visible"; }
}

function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) { selects[i].style.visibility = "hidden"; }
}

function displayPersonal() {
	personalText = "<strong>";
	if ( personal ) {
		var temp = unescape(personal).split('|');
		//personal = (', ' + temp[2] + ' ' + temp[3]).toLowerCase();
		personalText += (temp[2].substring(0,15));
	}
	else personalText += "";
	personalText += "</strong>";
	document.getElementById("unPersonalized").innerHTML = personalText;
}
function updatePersonal(which) {
	personalText = "<strong>";
	if (which == 'lo')
	{
		personalText += "";
	}
	else {
		if ( readCookie('SPS_UD') != null && readCookie('SPS_UD') != "" ) {
			var temp = unescape(readCookie('SPS_UD')).split('|');
			var temp2 = temp[2];// + ' ' + temp[3];
			if (temp2.length <= 15) personalText += temp2;
			else personalText += temp[2].substring(0,15);
		}
		else personalText += "";
	}
	personalText += "</strong>";
	document.getElementById("unPersonalized").innerHTML = personalText;
}
function displaySignInOut() {
	if ( location.href.indexOf('/sps_my_account/accmgmt/GenericSignin.jsp') != -1 ) {
		return '<span id="sio"><a href="/sps_my_account/accmgmt/GenericSignin.jsp" onclick="return MA_showLogin();" id="uniNavSIO"><div>Sign in</div></a></span>';

	}
	else {
		if ( personal != null && personal != "" ) {
			return '<span id="sio"><a href="' + SPS_HOST + '/sps_my_account/accmgmt/GenericSignin.jsp?finalSuccessURL=http://www.scholastic.com" onclick="maLogOut();return false;" id="uniNavSIO"><div>Sign Out</div></a></span>';
		}
		else {
			if ( (typeof schlKids == "undefined") || !schlKids )
				signinURL = SPS_HOST + '/sps_my_account/accmgmt/GenericSignin.jsp?finalSuccessURL=' + location.href;
			else
				signinURL = DOTCOM_HOST + "/kidslogin";
			return '<span id="sio"><a href="' + signinURL + '" onclick="return MA_showLogin();" id="uniNavSIO"><div>Sign In</div></a></span>';
		}
	}
}
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "="+value+expires+"; domain=scholastic." + document.domain.split(".").pop() + "; path=/";
}
function saveCookie(name, value) {
	var date = new Date();
	date.setTime(date.getTime() + (365*100*24*60*60*1000)); //setting persistance cookie for 100 years
	var expires = "; expires="+date.toGMTString();
	document.cookie = name + "="+value+expires+"; domain=scholastic." + document.domain.split(".").pop() + "; path=/";
}
function readCookie(name)
{

	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) { createCookie(name,"",-1); }
function deleteCookie(name) { saveCookie(name,"",-1); }
function kookie(name,value,days){
if (days)
	{
		var date = new Date();
		date.setTime(date.getTime() + (365*100*24*60*60*1000)); //setting persistance cookie for 100 years
		var expires = "; expires="+date.toGMTString();
	}
else var expires = "";
document.cookie = name + "="+value+expires+"; domain=scholastic." + document.domain.split(".").pop() + "; path=/";
}


function maLogOut(){
	kookies = ['SPS_TSP','SPS_SESSION','SPS_UD','lithiumSSO:scholastic','prefCTR:scholastic','PR_SUB','SPS_BUS','XUS_EI'];
	for (var i = 0; i < kookies.length; i++) { kookie(kookies[i],'',-1); }
	if ( document.getElementById('sio') ) {
		document.getElementById("sio").innerHTML = '<a href="' + SPS_HOST + '/sps_my_account/accmgmt/GenericSignin.jsp"  onclick="return MA_showLogin();" id="uniNavSIO"><div>Sign in</div></a>';
		updatePersonal('lo');
	}
	if (onSignOutHook != null) { onSignOutHook(); }
}
function chkLS() {
	if ( document.getElementById('sio') ) {
		if ( location.href.indexOf('/sps_my_account/accmgmt/GenericSignin.jsp') != -1 ) {
			document.getElementById("sio").innerHTML = '<a href="/sps_my_account/accmgmt/GenericSignin.jsp" onclick="return MA_showLogin();" id="uniNavSIO"><div>Sign In</div></a>';

		}
		else {
			if ( readCookie('SPS_UD') != null && readCookie('SPS_UD') != "" ) {
				document.getElementById("sio").innerHTML = '<a href="' + SPS_HOST + '/sps_my_account/accmgmt/GenericSignin.jsp?finalSuccessURL=http://www.scholastic.com" onclick="maLogOut();return false;" id="uniNavSIO"><div>Sign Out</div></a>';
			}
			else {
				if ( (typeof schlKids == "undefined") || !schlKids )
					signinURL = SPS_HOST + '/sps_my_account/accmgmt/GenericSignin.jsp?finalSuccessURL=' + location.href;
				else
					signinURL = DOTCOM_HOST + "/kidslogin";
				document.getElementById("sio").innerHTML = '<a href="' + signinURL + '" onclick="return MA_showLogin();" id="uniNavSIO"><div>Sign in</div></a>';
			}
			
		}
		updatePersonal();
	}
}

function shopForEBooks1(){
alert("there");
                if (shopForEbooksHook != null) { 
                                shopForEbooksHook(); 
                } else {
                                window.location = SCHWS_HOST + "/SchWS/link2COOL.jsp";
                }
}
function shopForEBooks(){
	if (shopForEbooksHook != null) { 
		shopForEbooksHook(); 
	} else {
	        window.location = SCHWS_HOST + "/SchWS/link2COOL.jsp";		
	}
}

function shopForGradeEBooks(grade){
	//alert("Grade - " + grade );
	if (shopForGradeEBooksHook != null) { 
		shopForGradeEBooksHook(grade); 
	} else {
	        window.location = SCHWS_HOST + "/SchWS/link2COOL.jsp";		
	}	
}

function AboutUs(){
	//alert("myaccount.js:AboutUs()" );
	if (aboutUsHook != null) { 
		aboutUsHook(); 
	} else {
	        window.location = SCHWS_HOST + "/SchWS/link2COOL.jsp";		
	}	
}
function viewEBookDetails(isbn){
	//alert("myaccount.js:viewEBookDetails :: Isbn - " + isbn );
	if (viewEBookDetailsHook != null) { 
		viewEBookDetailsHook(isbn); 
	} else {
	        window.location = SCHWS_HOST + "/SchWS/link2COOL.jsp";		
	}	
}

document.writeln('<style>#GB_overlay { background-image: url(' + imgRoot + 'overlay.png);position: absolute;margin: auto;top: 0;left: 0;z-index:  1000002;width:  100%;height: 100%;} * html #GB_overlay {background-color: #000;background-color: transparent;background-image: url(' + imgRoot + 'blank.gif);filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + imgRoot + 'overlay.png", sizingMethod="scale");} #ma_wrapper {top: 10px;left: 0px;position: absolute;overflow: hidden;z-index:  1000003;width: 943px;height: 614px;} #GB_frame {border: 0;margin: 0;overflow: hidden;width: 943px;height: 614px;}</style>');
