function initPage()
{
	var navRoot = document.getElementById("log-block");
	if (navRoot) 
	{
		navRoot.onmouseover = function()
		{
			this.className = "hover";
                }
		navRoot.onmouseout = function()
		{
			this.className = "";
		}
	}
	var navRoot = document.getElementById("searchmenu");
	if (navRoot) 
	{
		navRoot.onmouseover = function()
		{
			this.className = "hover";
                }
		navRoot.onmouseout = function()
		{
			this.className = "";
		}
	}

}

function addHref(){
	var username =fetch_cookie('username');
	var encodedGroupId=	fetch_cookie('enc_usergroupid');
	
	if(username==null){
			document.getElementById('createProfile').href='http://www.paltalk.com/people/webapp/my/info.wmt';	
	}
	
	else{
		document.getElementById('logdCreateProfile').href='http://www.paltalk.com/people/users/'+username+'/toolbox/my_info.wmt';		
		document.getElementById('logdViewProfile').href='http://people.paltalk.com//people/users/'+username+'/index.wmt';
		document.getElementById('logdEditProfile').href='http://www.paltalk.com/people/users/'+username+'/toolbox/my_info.wmt';
		document.getElementById('changePicture').href='http://www.paltalk.com/people/users/'+username+'/toolbox/photo.wmt';
		document.getElementById('logdCreateGrp').href='http://www.paltalk.com/g2/webapp/groups/ChooseGroupType.wmt';
		document.getElementById('logdViewGrp').href='http://www.paltalk.com/g2/paltalk/'+encodedGroupId;
		document.getElementById('profile-notification').href='http://www.paltalk.com/people/users/'+username+'/toolbox/';
		document.getElementById('createProfile').href='http://www.paltalk.com/people/users/'+username+'/toolbox/my_info.wmt';
		document.getElementById('viewProfile').href='http://www.paltalk.com/people/users/'+username+'/index.wmt';
		document.getElementById('editProfile').href='http://www.paltalk.com/people/users/'+username+'/toolbox/my_info.wmt';
		document.getElementById('profSettings').href='http://www.paltalk.com/people/users/'+username+'/toolbox/';
		document.getElementById('createGroup').href='http://www.paltalk.com/g2/webapp/groups/ChooseGroupType.wmt';
		document.getElementById('viewGroup').href='http://www.paltalk.com/g2/paltalk/'+encodedGroupId;
	}

}


function selectedState()
{
	var mainNavLists = document.getElementById("nav-states").getElementsByTagName("ul");
	var userNavLists = document.getElementById("userinfo-menu").getElementsByTagName("ul");
	
	for (var i=0; i<mainNavLists.length; i++) {
		
		if (mainNavLists[i].className == 'nav'){
			
			var dropDownLists = mainNavLists[i].getElementsByTagName("ul");
			for (var j=0; j<dropDownLists.length; j++) {
				
				dropDownLists[j].onmouseover=function() {
					var selectedTab = this.parentNode.firstChild;
					selectedTab.className += " tab-hover";
				}
				
				dropDownLists[j].onmouseout=function() {
					var selectedTab = this.parentNode.firstChild;
					selectedTab.className = selectedTab.className.replace(new RegExp("tab-hover\\b"), "");
				}

			}
			
		}
		
	}
	
	for (var i=0; i<userNavLists.length; i++) {

		userNavLists[i].onmouseover=function() {
			var selectedTab = this.parentNode.firstChild;
			selectedTab.className += " usermenu-selected";
		}

		userNavLists[i].onmouseout=function() {
			var selectedTab = this.parentNode.firstChild;
			selectedTab.className = selectedTab.className.replace(new RegExp("usermenu-selected\\b"), "");
		}
		
	}
}

function toggleVisibility(elem) {
	if (elem.className == "show") {
		elem.className = elem.className.replace ("show","hide");
	} else if (elem.className == "hide") {
		elem.className = elem.className.replace ("hide","show");
	}
}

function hideClass(elem) {
	if (elem.className == "show")
	elem.className = elem.className.replace ("show","hide");
} 
	
function showClass(elem) {
	if (elem.className == "hide")

	elem.className = elem.className.replace ("hide","show");
}

function setLoggedOutFlag(){
	var myDate = new Date();
	myDate.setDate(myDate.getDate()+5);
	set_cookie('userLoggedOut', 'User logged out.',myDate,'paltalk.com');
}

/* Checks if the user is logged in or not and displays logged in or not logged state in the header */
function checkSignIn() {
	
	var username =fetch_cookie('username');
	var thumbnail =fetch_cookie('thumbnail');
					
	if(thumbnail!=null){
	var thumbnail35x35 =thumbnail.replace("SQ3","SQ5");
	}
	
	var pplCookieNotification=	fetch_cookie('people_notes');
	if (pplCookieNotification == null) pplCookieNotification = 0;
	var grpCookieNotification =	fetch_cookie('grp_notes');
	if (grpCookieNotification == null) grpCookieNotification = 0;
	var hasProfile =fetch_cookie('profile');
	var hasGroup = fetch_cookie('group'); 
	var joinedGroups = fetch_cookie('groups_joined');
	
	var loggedInUser = document.getElementById("userInfoLoggedIn");
	var loggedOutUser = document.getElementById("userInfoLoggedOut");
	var loggedInNav = document.getElementById("navLoggedIn");
	var loggedOutNav = document.getElementById("navLoggedOut");
	var searchBox = document.getElementById("masthead-search");
	var formHide = document.getElementById("search-form");
	
	var usernick = document.getElementById("nickname");
	var thumb = document.getElementById("userpic");
	var logoutLink = document.getElementById("log-out");
	var profileNotification=document.getElementById("profile-notification");
	var groupNotification=document.getElementById("group-notification");


	if ( username == null || username==''){
		toggleVisibility(loggedOutNav);
		toggleVisibility(loggedOutUser);
		toggleVisibility(formHide);
		searchBox.className = searchBox.className.replace ("hide","show");
		return false;
	} else {
		toggleVisibility(loggedInNav);
		toggleVisibility(loggedInUser);
		toggleVisibility(formHide);
		searchBox.className = searchBox.className.replace ("hide","show");
		nick = document.createTextNode(username);
		if(thumbnail!=null){
		thumb.src=thumbnail35x35;	
		}else{
		thumb.src = thumbnail;
		}

		usernick.appendChild(nick);
		profileNotification.appendChild(document.createTextNode(pplCookieNotification));
		groupNotification.appendChild(document.createTextNode(grpCookieNotification));
		if(pplCookieNotification==0){
			document.getElementById("profile-notification").className="notification messages-off";	
			document.getElementById("profile-notification").href="#";
		}
		else{
			document.getElementById("profile-notification").className="notification messages-on";	
		}
		if(grpCookieNotification==0){
			document.getElementById("group-notification").className="notification messages-off";
			document.getElementById("group-notification").href="#";			
		}else{
			document.getElementById("group-notification").className="notification messages-on";	
		}
		
		
		/******code for  Menu Display*******/
		if(hasProfile=='Y'){
			
			document.getElementById('logdCreateProfile').style.display="none";
			document.getElementById('logdViewProfile').style.display="";
			document.getElementById('logdEditProfile').className="";
			
			document.getElementById('createProfile').style.display="none";
			document.getElementById('viewProfile').style.display="";
			document.getElementById('editProfile').className="";
			document.getElementById('profSettings').className="";
		}else{
			
			document.getElementById('logdCreateProfile').style.display="";
			document.getElementById('logdViewProfile').style.display="none";
			document.getElementById('logdEditProfile').className="not-available";
			document.getElementById('logdEditProfile').href="#";
			
			document.getElementById('createProfile').style.display="";
			document.getElementById('viewProfile').style.display="none";
			document.getElementById('editProfile').className="not-available";
			document.getElementById('profSettings').className="not-available";
			document.getElementById('editProfile').href="#";
			document.getElementById('profSettings').href="#";

		}
		
		if(hasGroup=='Y'){
			document.getElementById('logdCreateGrp').style.display="none";
			document.getElementById('logdViewGrp').style.display="";
			document.getElementById('logdEditGrp').className="";
			
			
			document.getElementById('createGroup').style.display="none";
			document.getElementById('viewGroup').style.display="";
			document.getElementById('editGroup').className="";
			
	
		}else if(hasGroup=='N'){
			document.getElementById('logdCreateGrp').style.display="";
			document.getElementById('logdViewGrp').style.display="none";
			document.getElementById('logdEditGrp').className="not-available";
			document.getElementById('logdEditGrp').href="#";
			

			document.getElementById('createGroup').style.display="";
			document.getElementById('viewGroup').style.display="none"
			document.getElementById('editGroup').className="not-available";
			document.getElementById('editGroup').href="#";
			
		}
		if(joinedGroups >0){
			document.getElementById('logdJoindGrp').className="";
			document.getElementById('joindGroup').className="";
		}else{
			document.getElementById('logdJoindGrp').className="not-available";
			document.getElementById('logdJoindGrp').href="#";
			document.getElementById('joindGroup').className="not-available";
			document.getElementById('joindGroup').href="#";
		}
		logoutLink.onclick = function() { setLoggedOutFlag(); document.logout.submit();}
		return true;
	}
}
/*********************************************************/
/* Changes the search domain in the header's search */
function changeMenu(){

	domainSelect = document.getElementById('drop-arrow');

	chatSearch = document.getElementById('searchChat');

	memberSearch = document.getElementById('searchMembers');

	newsSearch = document.getElementById('searchNews');

	
	chatSearch.onclick = function () {

		document.frmMenu.searchTarget.value = 0;
		for ( var i=0; i<domainSelect.childNodes.length; i++) {
			if (domainSelect.childNodes[i].nodeType == 1 && domainSelect.childNodes[i].firstChild.nodeValue == 'Chat Rooms')
				showClass(domainSelect.childNodes[i]);
			else 
				hideClass(domainSelect.childNodes[i]);
		}
	}

	memberSearch.onclick = function () {

		document.frmMenu.searchTarget.value = 1;
		for ( var i=0; i<domainSelect.childNodes.length; i++) {
			if (domainSelect.childNodes[i].nodeType == 1 && domainSelect.childNodes[i].firstChild.nodeValue == 'Members' )
				showClass(domainSelect.childNodes[i]);
			else 
				hideClass(domainSelect.childNodes[i]);
		}
	}

	newsSearch.onclick = function () {
		document.frmMenu.searchTarget.value = 2;
		for ( var i=0; i<domainSelect.childNodes.length; i++) {
			if (domainSelect.childNodes[i].nodeType == 1 && domainSelect.childNodes[i].firstChild.nodeValue == 'Support' )
				showClass(domainSelect.childNodes[i]);
			else 
				hideClass(domainSelect.childNodes[i]);
		}
	}
}

/* Controls the search functionality in the header */
function searchSubmit(){
	var checkInput = document.getElementById('mainsearch');
	var clickDetect = document.getElementById('go');
	var submitDetect = document.frmMenu;
	checkInput.onfocus = function() { if(this.value=='Enter search term(s)') this.value=''; }
	clickDetect.onclick = function() { setSearchTarget(); }
	submitDetect.onsubmit = function() { setSearchTarget(); return false; }
}

function setSearchTarget(){
	var search_type = document.frmMenu.searchTarget.value;
	if(search_type==null || search_type==""){
		search_type=0;
	}
	var search_val = document.frmMenu.mainsearch.value;
		if(isWhitespace(search_val) || search_val=="Enter search term(s)"){
			Highlight("mainsearch");
			return;
		}else if(search_val!=null && search_val<3){
			alert("please enter minimum of 3 characters");
			return;
		}

	if ( search_type==0){					            
		document.groups_search.searchkey.value=search_val;
		document.groups_search.submit();

	}else if ( search_type==1){
		document.people_search.tag.value=search_val;
		document.people_search.submit();	
		
	}else if ( search_type==2){
		document.support_search.q.value=search_val;
		document.support_search.submit();
	}
}

// This is the amount of time (in milliseconds) that will lapse between each step in the fade
var FadeInterval = 600;

// This is where the fade will start, if you want it to be faster and start with a lighter color, make this number smaller
// It corresponds directly to the FadeSteps below
var StartFadeAt = 7;

// This is list of steps that will be used for the color to fade out
var FadeSteps = new Array();
	FadeSteps[1] = "ff";
	FadeSteps[2] = "ee";
	FadeSteps[3] = "dd";
	FadeSteps[4] = "cc";
	FadeSteps[5] = "bb";
	FadeSteps[6] = "aa";
	FadeSteps[7] = "99";

// This function is just a small wrapper to use for the oncick events of the anchors
function Highlight(target) {
	// Get the target ID from the string that was passed to the function    
	DoFade(StartFadeAt, target);
}

// This is the recursive function call that actually performs the fade
function DoFade(colorId, targetId) {
	 if (colorId >= 1) {
		document.getElementById(targetId).style.background = "#ffff" + FadeSteps[colorId];
		// If it's the last color, set it to transparent
        if (colorId==1) {
        	document.getElementById(targetId).style.background = "#fff";
		}
        colorId--;		
        // Wait a little bit and fade another shade
        setTimeout("DoFade("+colorId+",'"+targetId+"')", FadeInterval);
	}
}


/* checkOS() is a OS sniffer */
/* 
if a link's rel attribute is set to 'os-sniff' 
it sends windows users to the download page and 
mac users to the express page

if a link's rel attribute is set to 'os-sniff-auto' 
it sends windows users to the download page and initiates a download
and sends mac users to the express page 
*/
function download_sniff() {
	var jsurl="/start_chatting.shtml"
	if (navigator.appVersion.indexOf("Mac")!=-1) jsurl="/express/";
	window.location.href = jsurl;
}

function gotoChat(){
document.location="/download_auto.shtml";
}

function doChat(){
window.setTimeout('gotoChat()', 1000); 
ACTION='07';
frames['downloadframe'].location.href="http://www.paltalk.com/download/0.x/pal_install_r17711.exe";
}

function download_sniff_auto () {
	var jsurl="javascript:doChat();"
	if (navigator.appVersion.indexOf("Mac")!=-1) jsurl="/express/";
	window.location.href = jsurl;
}

function checkOS() {
	var sniffer = document.getElementsByTagName('a');
	for (var i=0; i<sniffer.length; i++ ) {
		if (sniffer[i].getAttribute("rel") == "os-sniff") {
			sniffer[i].onclick = function (){ download_sniff (); }
		} 
		
		else if (sniffer[i].getAttribute("rel") == "os-sniff-auto") {
			sniffer[i].onclick = function (){ download_sniff_auto (); }
		}
	}
}

/* 
checkref() checks for refc or advc params in the url string and appends them 
to the client link 
*/
function getUrlParam(name)
{
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;
  var results = regex.exec( tmpURL );
  if( results == null )
    return "";
  else
    return results[1];
}

function checkref() {
	var refc =getUrlParam('refc');
	var advc =getUrlParam('advc');
	var pmid =getUrlParam('pmid');
	if(refc!="" && isNaN(refc)) refc = "";
	if(advc!="" && isNaN(advc)) advc = "";
		
	if(refc == "" && advc == "" && pmid == "") return;
	// rename the download	
	var downurl = "http://www.paltalk.com/download/0.x/pal_install";
			
	if(pmid!=""){
		//alert(pmid);
		downurl = downurl + "_pmid" + pmid;	    
	} else {
		if(advc !="" ) downurl = downurl + "_a" + advc;
		if(refc !="" ) downurl = downurl + "_r" + refc;
	}
			
	downurl = downurl + ".exe";
			
	//alert(downurl);
	//update the href with correct link
	if (document.getElementById('dlu1')!= null)
	document.getElementById('dlu1').href=downurl;
	if (document.getElementById('dlu2')!= null)
	document.getElementById('dlu2').href=downurl;
	return;
}

/* checks rel link attribute, if it's set to external the link goes to a new window */
function newWin() {
   if (!document.getElementsByTagName) return false;
   var links = document.getElementsByTagName("a");
   for (var i=0; i<links.length; i++) {
      if (links[i].getAttribute("rel") == "external") {
         links[i].onclick = function() {
            return !window.open(this.href);
         }
      }
   }
}

var chk_loginStatus;

function isAjaxCallReq(){
	return false;
}


function addFunc(){
	document.onmousemove = closepopup;
	//document.onkeypress= reset_interval;
	//document.onclick= reset_interval;
	if(fetch_cookie('userLoggedOut') != null){
		$.jGrowl("You are logged out of the Paltalk site.",{theme:'growl-message',speed: 'slow'});
		delete_cookie('userLoggedOut','paltalk.com');
	}
	if(fetch_cookie('postLogoutMessage') != null){
		showLogin();
	}
}


function isUserLoggedIn() {
	
	var username = fetch_cookie('username');
	if (username != null && username != '') return true;
	return false;
}
