var oldTab;
var advOne = 1;

function remTab(getTabName){
	oldTab = getTabName;
	return oldTab;
}

function goToURL(getPgNumb) {
	this.listSpFrame.location.href = "list.php?pagenum="+getPgNumb;
	
	resetListWindow();
};

function getBrowserWindowSize(){
	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 {width:myWidth, height:myHeight};
};

//alert(pageHeight());
var getWinHeight = getBrowserWindowSize().height;

var frameWindow, iFrameLayer;
window.onload = function() {
	frameWindow = document.getElementById("summerContactLyr");
	iFrameLayer = document.getElementById("contactIframe");
	
	frameWindow.style.height = (getWinHeight - 86) + "px";
	iFrameLayer.style.height = (getWinHeight - 86) + "px";
	
	//Read the cookie
	var ckCookie = readCookie('remTab');
	if(!ckCookie){
		var selFirstTab = document.getElementById('tabName1');
		selFirstTab.className="selTab";
		oldTab = "tabName1";
	}else{
		readCookie('remTab');
		var outPutCookie = readCookie('remTab');
		if(outPutCookie.length == 8){
			advOne = outPutCookie.substr(outPutCookie.length-1, 1);
		}else if(outPutCookie.length == 9){
			advOne = outPutCookie.substr(outPutCookie.length-2, 2);
		}
	}
	var advPrev = document.getElementById("prev");
	var advFwrd = document.getElementById("next");
	//var echoNumberVal = document.getElementById("echoNumber");
	var selName;
	var sfEls = document.getElementById("pgNumb").getElementsByTagName("a");
	
	/////////////////////////////////////////////////////
	////Mouse Events for page buttons
	/////////////////////////////////////////////////////
	
	for (var i=0; i<sfEls.length; i++) {
		//if(i == 0){
			//sfEls[i].className="selTab";
			//showHidePrevNext('tabName1', sfEls.length-1);
		//}		
		sfEls[i].onmouseover=function() {
			this.className=this.className.replace(new RegExp("pgTab"), "sfhover");
		}
		
		sfEls[i].onmousedown=function() {
			if(this.id.length == 8){
				advOne = this.id.substr(this.id.length-1, 1);
			}else if(this.id.length == 9){
				advOne = this.id.substr(this.id.length-2, 2);
			}
			//echoNumberVal.value = advOne;
			if(!oldTab){
				oldTab = "tabName1";
			}
			
			if(this.className != "selTab"){
				this.className = this.className.replace(new RegExp("sfhover"), "selTab");
				var resTab = document.getElementById(oldTab);
				resTab.className=resTab.className.replace(new RegExp("selTab"), "pgTab");
			}
			
			var reportId = this.id;
			//showHidePrevNext(reportId, sfEls.length-1);
			
			//alert (this.id);//Outputs tabName4, or tabName5, etc
			//////////Make a cookie
			createCookie("remTab",reportId,1);
			//////////
			
			//resetListWindow();
			
		}
	
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp("sfhover"), "pgTab");
		}
	}
	
	/////////////////////////////////////////////////////
	////Mouse Events for Previous and Next Buttons
	/////////////////////////////////////////////////////
	
	//Previous
	//var advOne = 1;
	advPrev.onmouseup=function() {
		advOne--;
		//alert(advOne);
		if(advOne < 1){
			advOne = sfEls.length;
			sfEls[0].className="pgTab";
			oldTab = "tabName"+(sfEls.length);	
		}else{
			oldTab = "tabName"+advOne;
		}
		sfEls[advOne-1].className="selTab";
		if(advOne < sfEls.length){
			sfEls[advOne].className="pgTab";
		}
		//echoNumberVal.value = advOne;
		goToURL(advOne);
		
		createCookie("remTab",oldTab,1);
		
		//showHidePrevNext("tabName"+advOne, sfEls.length);
	}
	//Next
	advFwrd.onmouseup=function() {
		advOne++;
		if(advOne > sfEls.length){
			advOne = 1;
			sfEls[sfEls.length-1].className="pgTab";
			oldTab = "tabName1";		
		}else{
			oldTab = "tabName"+advOne;
		}
		sfEls[advOne-1].className="selTab";
		if(advOne > 1){
			sfEls[advOne-2].className="pgTab";
		}
		//echoNumberVal.value = advOne;
		goToURL(advOne);
		
		createCookie("remTab",oldTab,1);
		
		//showHidePrevNext("tabName"+advOne, sfEls.length);
		
	}
};
/////////////////////////////////////////////////////
////Show or Hide Previous and Next Buttons Function
/////////////////////////////////////////////////////
function showHidePrevNext(getId, last){
	var prev, next;
	if(getId == "tabName1"){
		prev = document.getElementById('tabPrev');
		prev.style.display = "none";
	}else{
		prev = document.getElementById('tabPrev');
		prev.style.display = "inline-block";
	}
	var lastToString = "tabName"+last;
	if(getId == lastToString){
		next = document.getElementById('tabNext');
		next.style.display = "none";
	}else{
		next = document.getElementById('tabNext');
		next.style.display = "inline-block";
	}

}



function createCookie(name,value,days) {
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	
	document.cookie = name+"="+value+expires+"; path=/"+path;	
	
}

function readCookie(name) {
	var selTab;
	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){ 
			selTab = document.getElementById(c.substring(nameEQ.length,c.length));
			selTab.className="selTab";
			oldTab = selTab.id;
			var remURL = oldTab.substr(oldTab.length-1, 1);
			goToURL(remURL);
			return oldTab;
		
		}
		
	}
	
	return null;
}

window.onresize = function (){
	getWinHeight = getBrowserWindowSize().height;
	frameWindow.style.height = (getWinHeight - 86) + "px";
	iFrameLayer.style.height = (getWinHeight - 86) + "px";
}


function resetListWindow(){
	$('#similarPkgs', window.parent.document)[0].innerHTML = "";	
	//listSpFrame
	$('#listSpFrame', window.parent.document).css('height', 445);
			
			
}









