var imCountCheckInterval=15000;
var imSessionCheckInterval=5000;
var hashPrefix="#_";
var checkInterval = 300;
var currentHash = location.hash;
var currentDepth = 0;
var frameStack=new Array();
var hashStack=new Array();
var checkTimer;
var currentFrame;
var currentListPageNumber=1;
function $(id){return document.getElementById(id);}
addEventListener("load", function(event){
	currentHash=location.hash;
	hashStack.push(currentHash);
	doneLoading();
	window.setTimeout(checkForNewMessages,1000);
	setInterval(checkForNewMessages,imCountCheckInterval);
}, true);
function checkForNewMessages(){
	loadUrl('/beta/chat/message_count.esp','',{onCallDone:messageCountDone});
}
function messageCountDone(xhr,args,params){
	var count = parseInt(xhr.responseText);
	if(count==0 || isNaN(count)){
		$('envelope').innerHTML='&nbsp;';
	}else{
		$('envelope').innerHTML=count;
	}
}
function doneLoading(){
	window.setTimeout(hideTopBar,400);
	// restart the checker
	checkTimer = setInterval(checkLocation, checkInterval);
	
}
function hideTopBar(){
	window.scrollTo(0,1);
}
function checkLocation(){
	if (location.hash != currentHash){
		backOne();
	}
}
function newFrame(){
	if( typeof currentFrame != 'undefined' ){
		currentFrame.style.display="none";
		frameStack.push(currentFrame);
	}
	// stop the location checker
	clearInterval(checkTimer);
	currentDepth++;
	location.href = currentHash = hashPrefix + currentDepth;
	mainMenuShow(false);
	backButtonShow(true);
	currentFrame = document.createElement("div");
	currentFrame.innerHTML="<center><br><br><img src='/images/progress_bar.gif'></center>";
	document.getElementById('mainframe').appendChild(currentFrame);
	return currentFrame;
}
function backOne(event){
	clearInterval(checkTimer);
	if( typeof currentFrame == 'undefined' ){
		history.clear();
		return;
	}
	currentHash = location.hash;
	currentDepth--;
	currentFrame.parentNode.removeChild(currentFrame);
	currentFrame = frameStack.pop();
	if( currentFrame ){
		currentFrame.style.display='block';
	}else{
		mainMenuShow(true);
		backButtonShow(false);
	}
	doneLoading();
}
function mainMenuShow(show){
	if(show){
		document.getElementById('menu').style.display = 'block';
	}else{
		document.getElementById('menu').style.display = 'none';
	}
}
function backButtonShow(show){
	if(show){
		document.getElementById('backButton').style.display = 'block';
	}else{
		document.getElementById('backButton').style.display = 'none';
	}
}
function goingon(){
	loadUrl('/beta/pages/going_on.espx','',{target:newFrame(),onCallDone:doneLoading});
}
function sponsors(){
	loadUrl('/beta/pages/logos.espx','',{target:newFrame(),onCallDone:doneLoading});
}
function chathistory(){
	loadUrl('/beta/chat/email_chat_history.espx','',{target:newFrame(),onCallDone:doneLoading});
}
function loading(){
	loadUrl('/beta/pages/loading.html','',{target:newFrame(),onCallDone:doneLoading});
}
function contactsmenushow(){
	document.getElementById('contactresults' ).style.display = 'none';
	document.getElementById('contactmenu' ).style.display = '';
	setTimeout ( "scroll()", 2000 );
}
function schedule(trackId){
	if(typeof trackId=='undefined'){
		loadUrl('/beta/pages/schedule_menu.espx','',{target:newFrame(),onCallDone:doneLoading});
		return;
	}
	loadUrl('/beta/pages/session_list.espx',
		{trackId:trackId},
		{target:newFrame(),onCallDone:doneLoading}
	);
}
function rateSession(){
	var session;
	var email;
	var rating;
	var note;
	try{session = $("msgSession").value;}catch(e){};
	try{email = $("msgEmail").value;}catch(e){};
	rating = $("ratingSelector").value;
	speaker = $("speakerRatingSelector").value;
	content = $("contentRatingSelector").value;
	note = $("ratingNote").value;
	loadUrl("/beta/pages/rate_session.espx",
		{session:session,rating:rating,note:note,speaker:speaker,content:content},
		{onCallDone:function(){
			alert("Thank you!");
			//$('sessionRating').style.display='none';
			history.back();
		}}
	);
}
function openSessionRating(sessionId){
	loadUrl('/beta/pages/session_rating.espx',{sessionId:sessionId},{target:newFrame(),onCallDone:doneLoading});
}
function profile(){
	loadUrl('/beta/pages/update_profile.espx','',{target:newFrame(),onCallDone:doneLoading});
}
function contactsmenu(){
	loadUrl('/beta/pages/contact_menu.espx','',{target:newFrame(),onCallDone:doneLoading});
}	
function contacts(){
	loadUrl('/beta/pages/contacts.espx','',{target:newFrame(),onCallDone:doneLoading});
}	
function photos(){
	loadUrl('/beta/pages/photo_tags.espx','',{target:newFrame(),onCallDone:doneLoading});
}
function messages(){
	loadUrl('/beta/chat/message_history.esp','',{target:newFrame(),onCallDone:function(){
			doneLoading();
			currentListPageNumber=1;
			window.setTimeout(displayListButtons,200);
		}
	});
}
function photolist( id ) {
	loadUrl('/beta/pages/photo_list.espx',{tag:id},{target:newFrame(),onCallDone:doneLoading});
}	
function contactgroups( id ) {
	loadUrl('/beta/pages/contacts.espx',{group:id},{target:newFrame(),onCallDone:function(){
			doneLoading();
			currentListPageNumber=1;
			window.setTimeout(displayListButtons,200);
		}
	});
}	
function session( id ) {
	loadUrl('/beta/pages/session.espx',
			{edit:id},
			{	target:newFrame(),
				onCallDone:function(xhr,callArgs,callParameters){
					doneLoading();
					loadMessages(xhr,callArgs,callParameters);
				},
				resultsDiv:'userCommentsDiv',
				checkUrl:'/beta/chat/session_chat_ajp.esp'
			}
	);
}	
function contactsearch(event) {
	if(event.keyCode==10||event.keyCode==13){
		var groupId= '';
		var search = '';
		try{
			var searchTB = event.target;
			search = searchTB.value;
		}catch(e){};
		try{
			groupId = $('listGroup').value;
			var searchHI = $('listSearch').value;
			if(search==''){
				search = searchHI.value;
			}
		}catch(e){};
		loadUrl('/beta/pages/contacts.espx',
			{search:search,group:groupId},
			{target:newFrame(),onCallDone:function(){
				doneLoading();
				currentListPageNumber=1;
				window.setTimeout(displayListButtons,200);
			}
		});
		searchTB.value='';
		event.stopPropagation();
		event.preventDefault();
	}
}
function locationopen( id ) {
	loadUrl('/beta/pages/location.espx',{edit:id},{target:newFrame(),onCallDone:doneLoading});
}	
function submit_chat( evt, tb, bn ){
	if( evt.keyCode == 13 ){
		document.getElementsByName(bn)[0].onclick()
	}
	evt.cancelBubble = true;
}
	
function sendMessage(url) {
	var message = '';
	var email = '';
	var from = '';
	var cid = '';
	var toaddress = '';
	var ccaddress = '';
	var account = '';
	var session = '';
	var link = '';
	try{message = $("msgMessage").value;}catch(e){};
	try{email = $("msgEmail").value;}catch(e){};
	try{from = $("msgFrom").value;}catch(e){};
	try{cid = $("msgCid").value;}catch(e){};
	try{toaddress = $("msgToaddress").value;}catch(e){};
	try{ccaddress = $("msgCcaddress").value;}catch(e){};
	try{account = $("msgAccount").value;}catch(e){};
	try{session = $("msgSession").value;}catch(e){};
	//try{link = $("msgLink").value;}catch(e){};
	loadUrl(url,
	{cid:cid,email:email,from:from,message:message,toaddress:toaddress,
		ccaddress:ccaddress,session:session,account:account},{});
	try{$("msgMessage").value = '';}catch(e){};
	//try{$("msgLink").value = '';}catch(e){};
} 
function pagepopup(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=320,height=550');");
}
//---------------------------------
function loadMessages(xhr,sentArgs,callParameters){
	var session;
	var email;
	var cid;
	try{session = $("msgSession").value;}catch(e){};
	try{email = $("msgEmail").value;}catch(e){};
	try{cid = $("msgCid").value;}catch(e){};
	loadUrl(callParameters.checkUrl,
	{cid:cid,session:session,myemail:email,dude:cid,email:email,inline:'on'},
	{onCallDone:messagesLoaded,resultsDiv:callParameters.resultsDiv,checkUrl:callParameters.checkUrl});
}
function messagesLoaded(xhr,sentArgs,callParameters){
	var html = xhr.responseText;
	if(!html){
		html ='';
	}
	$(callParameters.resultsDiv).innerHTML=html;
	window.setTimeout(function(){
		loadMessages(null,null,callParameters);
	},imSessionCheckInterval);
}
//---------------------------------
function chatPopup(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "');");
}
function startChat( id ) {
	chatPopup('/beta/chat/email_chat_edit.espx?to=' + id);
}

function sendmyinfo( id ) {
	var cid = document.getElementById("cid").value;
	loadUrl('/beta/pages/send_my_info.espx',{to:id},{onCallDone:function(){
			alert("Contact Info Sent");
		}
	});
	setTimeout ( "scroll()", 2000 );
}
function sendmyinfoback( id ) {
	document.getElementById('menu' ).style.display = 'none';
	document.getElementById('mainframe' ).style.display = '';
	document.getElementById('backButton' ).style.display = '';
	var cid = document.getElementById("cid").value;
	loadUrl('/beta/pages/send_my_info.espx',{recip:off,to:id,cid:cid},'alert');
}
function bio( id ) {
    pagepopup('/beta/pages/profile.espx?lookup='+ id); 
}
function previousListPage(url){
	$('progressBar').style.visibility='visible';
	pageNumber = (currentListPageNumber-1);
	if(pageNumber<1){
		$('prevListPage').style.display='none';
		return;
	}
	var groupId = $('listGroup').value;
	var search = $('listSearch').value;
	var args = {newlist_page:pageNumber,group:groupId,search:search};
	loadUrl(url,args,{target:currentFrame,onCallDone:function(){
			currentListPageNumber=pageNumber;
			doneLoading();
			window.setTimeout(displayListButtons,200);
		}
	});
}
function nextListPage(url){
	$('progressBar').style.visibility='visible';
	pageNumber = (currentListPageNumber+1);
	var groupId = $('listGroup').value;
	var search = $('listSearch').value;
	var args = {newlist_page:pageNumber,group:groupId,search:search};
	loadUrl(url,args,{target:currentFrame,onCallDone:function(){
			currentListPageNumber=pageNumber;
			doneLoading();
			window.setTimeout(displayListButtons,200);
		}
	});	
}
function displayListButtons(){
	$('progressBar').style.visibility='hidden';
	if(!currentFrame){
		return;
	}
	var btn = findChildrenByAttribute(currentFrame,'id','nextListPageBtn','button');
	btn = btn[btn.length-1];
	btn.style.display='none';
	try{
		var nextBtn = findChildrenByAttribute(currentFrame,'class','newlistpager_next','td');
		if(nextBtn.length > 0){
			btn.style.display='block';
		}
	}catch(e){};
	var btn = findChildrenByAttribute(currentFrame,'id','prevListPageBtn','button');
	btn = btn[btn.length-1];
	btn.style.display='none';
	try{
		var prevBtn = findChildrenByAttribute(currentFrame,'class','newlistpager_pre','td');
		if(prevBtn.length > 0){
			btn.style.display='block';
		}
	}catch(e){};
	return;
	// //findChildrenByAttribute
	// var nr = $('noResults');
	// if(nr){
	// 	//alert("currentListPageNumber: "+currentListPageNumber);
	// 	if(currentListPageNumber >= 1){
	// 		$('nextListPageBtn').style.display='none';
	// 	}
	// 	if(currentListPageNumber <=1){
	// 		$('prevListPage').style.display='none';
	// 	}else{
	// 		$('prevListPage').style.display='block';
	// 	}
	// }else{
	// 	$('nextListPage').style.display='none';
	// 	if(currentListPageNumber<=1){
	// 		$('prevListPage').style.display='none';
	// 	}else{
	// 		$('prevListPage').style.display='block';
	// 	}
	// }
}