var growDiv;
var growDivHeight = 0;
var newsVersionType = 0;
var mailWindow;

function slidingDisplay(divName,amount,growby){
	var div = document.getElementById(divName);
	if(!growby)
		growby = 10;
		
	if(div.style.display == "none"){
		var p = div.parentNode;
		//var growDiv = document.createElement("div");
		growDiv = document.createElement("div");
		growDiv.style.clear = "both";
		growDiv.id = "growdiv";
		growDiv.innerHTML = "&nbsp;<br><br><br>";
		p.appendChild(growDiv);
		growDivHeight = growDiv.offsetHeight;
		setTimeout("slidingDisplayGrow("+amount+",'"+divName+"',"+growby+");",10);
	}else{
		div.style.display = "none";
	}
}

function slidingDisplayGrow(x, y, heightGrow){
	if(x == 0){
		document.getElementById(y).style.display = "block";
		var gd = document.getElementById('growdiv');
		gd.parentNode.removeChild(gd);
		return;
	}
	//document.getElementById('growdiv').style.height = (document.getElementById('growdiv').offsetHeight + 10)+"px";
	growDiv.style.height = (growDivHeight + heightGrow)+"px";
	growDivHeight+=heightGrow;
	//setTimeout("slidingDisplayGrow("+(x-1)+",'"+y+"');",10);
	setTimeout("slidingDisplayGrow("+(x-1)+",'"+y+"',"+heightGrow+");",10);
}

function linkDeleteShow(x){
	document.getElementById('customDelete_'+x).style.display = "";	
}

function linkDeleteHide(x){
	document.getElementById('customDelete_'+x).style.display = "none";	
}

function linkDelete(x){
	var lie = document.getElementById('customLink_'+x);
	if(lie)
		lie.parentNode.removeChild(lie);
	var xhr = setupXHR(); 
	xhr.open("POST", "/remlink.php",  true); 
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	xhr.send("link="+escape(x)); 
	
}
function addNewLink(x){
	var b = document.getElementById('addNewLinkDivDialog');
	b.style.left = (x.offsetLeft+x.offsetWidth-2)+"px";
	b.style.top = (x.offsetTop-10)+"px";
	b.style.display = "block";

}

function closeAddNewLink(){
	document.getElementById('addNewLinkDivDialog').style.display = "none";
	document.getElementById('addNewLinkURL').value = "";
	document.getElementById('addNewLinkName').value = "";
}
var clinkNum = 0;

function addActualLink(){
	var name =document.getElementById('addNewLinkName').value;
	var link = document.getElementById('addNewLinkURL').value;

	if(name == "" || link.length < 5)
		return;
	
	if(link.substr(0,4) != "http")
		link = "http://"+link;
	closeAddNewLink();		
	var xhr = setupXHR(); 
	
	xhr.onreadystatechange  = function(){ 
		if(xhr.readyState  == 4){
			if(xhr.status  == 200){
				try{
					var temp = xhr.responseText;
				
					if(temp == ""){
						temp = clinkNum;
						clinkNum++;
					}
						var button = document.getElementById('newLinkButton');
						var newButton = document.createElement("li");
						newButton.id="customLink_"+temp;						
						newButton.style.position="relative";
						newButton.innerHTML = "<a href='"+link+"' target='_blank'>"+name+"</a>"+"<span style='width:45px;position: absolute; right:0px; padding-right:2px;' onmouseover='linkDeleteShow("+temp+");' onmouseout='linkDeleteHide("+temp+");'>&nbsp;<span id='customDelete_"+temp+"' style='display:none;cursor: pointer; background:url(/images/left_siderbar_list_bg.jpg) repeat-x;' onclick='linkDelete("+temp+");'>[ - ]</span></span>";
						insertAfter(newButton,button);

					
				}catch(e4){}
			}
		}
	}; 

	xhr.open("POST", "/link.php",  true); 
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	xhr.send("link="+escape(link)+"&name="+escape(name)); 	
}

function addActualLinkSocial(link,name){
	var xhr = setupXHR(); 
	
	xhr.onreadystatechange  = function(){ 
		if(xhr.readyState  == 4){
			if(xhr.status  == 200){
				try{
					var temp = xhr.responseText;
				
					if(temp == ""){
						temp = clinkNum;
						clinkNum++;
					}



					var fDiv = parent.document.getElementById('favorrites_menu_table');
					
					
					var newTr = fDiv.insertRow(0);
					var newTd1 = newTr.insertCell(0);
					var newTd2 = newTr.insertCell(1);
					
					newTr.style.height = "16px";
					newTr.style.cursor = "drag";
					newTr.id = temp;
					
					newTd1.className = "leftFavorite draggable-class";
					newTd1.innerHTML="&nbsp;";
					
					
					var newDiv = parent.document.createElement("div");
					newDiv.innerHTML = '<a id="customLinkA_'+temp+'"  href="'+link+'">'+name+'</a> \
						<div onmouseover="document.getElementById(\'favoritelink_c'+parent.clinkNum+'\').style.display=\'\';" onmouseout="document.getElementById(\'favoritelink_c'+parent.clinkNum+'\').style.display=\'none\';" style="position: absolute;right:0px;top: 0px; width: 40px; "> \
							<div style="display:none; position:relative; background-color:#FFF; height:20px" id="favoritelink_c'+parent.clinkNum+'"> \
								<img style="cursor:pointer" onclick="renameSocialLink('+temp+');" src="./images/icons/action_postcomment.gif" title="Rename" />&nbsp;&nbsp;<img style="cursor:pointer" onclick="deleteSocialLink('+temp+');" src="./images/icons/remove_friend16.gif" title="Delete" /> \
							</div> \
							&nbsp; \
						</div>';
					newDiv.style.width = "100%";
					newDiv.style.position = "relative";
					newDiv.style.whiteSpace = "nowrap";
					newDiv.style.overflow = "hidden";
					newDiv.style.cursor = "default";
					
					newDiv.id="customLink_"+temp;
					newTd2.appendChild(newDiv);
					
					//fDiv.insertBefore(newDiv,fDiv.firstChild);
					//fDiv.insertBefore(newDiv,newTr.firstChild);
					parent.clinkNum++;
					buildDragFavorites();
				}catch(e4){}
			}
		}
	}; 

	xhr.open("POST", "/flink.php",  true); 
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	xhr.send("link="+escape(link)+"&name="+escape(name)); 	
}

function deleteSocialLink(id){
	var favRow = document.getElementById(id);
	favRow.parentNode.removeChild(favRow);
	linkDelete(id);
}

function renameSocialLink(id){
	var lDiv = document.getElementById('customLinkA_'+id);
	TB_show('Edit Favorite Link','favorite_add.php?edit=1&id='+id+'&name='+escape(lDiv.innerHTML)+'&link='+escape(lDiv.href)+'&TB_iframe=true&height=135&width=270','','./images/trans.gif');
}

function insertAfter(newChild, refChild) { 
	refChild.parentNode.insertBefore(newChild,refChild.nextSibling); 
} 


function setHomePage(){
   document.body.style.behavior='url(#default#homepage)';
   document.body.setHomePage(window.location.href);
}


var newsTimerCount = 10;
var newsTimer;
var paused = true;

function nextNews(){
	currentNews++;
	if(currentNews > newsarray.length)
		currentNews = 1;
	setNews();
}

function newsTick(){
	nextNews();
	newsTimer = setTimeout("newsTick();",newsTimerCount*1000);
}

function prevNews(){
	currentNews--;
	if(currentNews == 0)
		currentNews = newsarray.length;
	setNews();	
}

function pauseNews(){
	var p = document.getElementById('buttonPaused');
	var up = document.getElementById('buttonUnPaused');
	
	if(paused == true){
		paused = false;
		newsTimer = setTimeout("newsTick();",newsTimerCount*1000);
		up.style.display = "";
		p.style.display = "none";
	}else{
		clearTimeout(newsTimer);
		paused = true;
		p.style.display = "";
		up.style.display = "none";
	}
	
}

function setNews(x){
	document.getElementById('currentArticleNumber').innerHTML = currentNews;		
	document.getElementById('currentArticleTitle').innerHTML = newsarray[currentNews-1].title;
	document.getElementById('currentArticleDescription').innerHTML = newsarray[currentNews-1].description;
	document.getElementById('currentArticleLink').href = newsarray[currentNews-1].link;
	if(newsVersionType == 1){
		if(newsarray[currentNews-1].image)
			document.getElementById('currentArticlePhoto').innerHTML = '<p> \
																			<a href="'+newsarray[currentNews-1].link+'" target="_blank"> \
																				<img align="left" border="0" src="'+newsarray[currentNews-1].image+'" title="'+newsarray[currentNews-1].title+'"> \
																			</a> \
																		</p> \
																		<br clear="all">';
		else
			document.getElementById('currentArticlePhoto').innerHTML = "";
	}else
		document.getElementById('currentArticlePhoto').innerHTML = newsarray[currentNews-1].image;
}



function changeTheme(x){
	document.styleSheets[x].disabled = false;
	for(var i = 0; i < 4; i++)
		if(i != x)
			document.styleSheets[i].disabled = true;
			
			

	var xhr = setupXHR(); 
	xhr.open("POST", "/theme.php",  true); 
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	xhr.send("theme="+escape(x)); 				
}


function bodyLoad(){
	//buildScrollableOptions('myNewsDiv',myNews,67,0);
	buildScrollableOptions('myMailDiv',myMail,67,0);
	buildScrollableOptions('mySNDiv',mySN,67,0);
	buildScrollableOptions('myIMDiv',myIM,67,0);
}

var myMail = new Array();
var myNews = new Array();
var myIM = new Array();
var mySN = new Array();
var myMC = new Array();

myMail[0] = new Object();
myMail[1] = new Object();
myMail[2] = new Object();
myMail[3] = new Object();
myMail[4] = new Object();
myMail[5] = new Object();
myMail[0].icon = "peoplestring";
myMail[1].icon = "bigstring";
myMail[2].icon = "yahoo";
myMail[3].icon = "gmail";
myMail[4].icon = "msn";
myMail[5].icon = "aim";
myMail[0].text = "PeopleString";
myMail[1].text = "BigString";
myMail[2].text = "Yahoo";
myMail[3].text = "Gmail";
myMail[4].text = "MSN";
myMail[5].text = "AOL";

myMail[0].link = "http://peoplestring.com/main/email/";
myMail[1].link = "http://bigstring.com";
myMail[2].link = "http://mail.yahoo.com";
myMail[3].link = "http://gmail.com";
myMail[4].link = "http://hotmail.com";
myMail[5].link = "http://webmail.aol.com";

myMail.extra = false;

myMail[0].onmouseover = function(){
	if(emailStatsLoaded == false){
		emailStatsLoaded = true;
		document.getElementById('mail_extra_iframe').contentWindow.loadEmailStats();
	}
	if(myMail.extra == false && document.getElementById('mail_extra').style.display == "none" && !document.getElementById('growdiv')) {
		myMailExtraDectiveTimeYahoo();
		myMailExtraDectiveTimeGmail();
		slidingDisplay('mail_extra',7,10);
	} else {
		myMail.extra = true;
	}
};

myMail[0].onmouseout = function(){
	myMailExtraDective();
};


myMail[2].onmouseover = function(){
	if(emailStatsLoaded == false){
		emailStatsLoaded = true;
		document.getElementById('mail_extra_iframe_yahoo').contentWindow.loadEmailStats();
	}
	if(myMail.extra == false && document.getElementById('mail_extra_yahoo').style.display == "none" && !document.getElementById('growdiv'))	{
		myMailExtraDectiveTime();
		myMailExtraDectiveTimeGmail();
		slidingDisplay('mail_extra_yahoo',7,10);
	} else {
		myMail.extra = true;
	}
};

myMail[2].onmouseout = function(){
	myMailExtraDectiveYahoo();
};


myMail[3].onmouseover = function(){
	if(emailStatsLoaded == false){
		emailStatsLoaded = true;
		document.getElementById('mail_extra_iframe_gmail').contentWindow.loadEmailStats();
	}
	if(myMail.extra == false && document.getElementById('mail_extra_gmail').style.display == "none" && !document.getElementById('growdiv'))	{
		myMailExtraDectiveTime();
		myMailExtraDectiveTimeYahoo();
		slidingDisplay('mail_extra_gmail',7,10);
	} else {
		myMail.extra = true;
	}
};

myMail[3].onmouseout = function(){
	myMailExtraDectiveGmail();
};



/*
myNews[0] = new Object();
myNews[1] = new Object();
myNews[2] = new Object();
myNews[3] = new Object();
myNews[0].icon = "peoplestring";
myNews[1].icon = "bigstring";
myNews[2].icon = "aim";
myNews[3].icon = "gmail";
myNews[0].text = "PeopleString";
myNews[1].text = "BigString";
myNews[2].text = "AOL";
myNews[3].text = "Gmail";
*/
mySN[0] = new Object();
mySN[1] = new Object();
mySN[2] = new Object();
mySN[3] = new Object();
mySN[0].icon = "peoplestring";
mySN[1].icon = "twitter";
mySN[2].icon = "facebook";
mySN[3].icon = "myspace";
mySN[0].text = "PeopleString";
mySN[1].text = "Twitter";
mySN[2].text = "Facebook";
mySN[3].text = "MySpace";
mySN[0].link = "http://social.peoplestring.com/user_home.php";
mySN[1].link = "http://twitter.com";
mySN[2].link = "http://facebook.com/";
mySN[3].link = "http://myspace.com";

myIM[0] = new Object();
myIM[1] = new Object();
myIM[2] = new Object();
myIM[3] = new Object();
myIM[0].icon = "msn";
myIM[1].icon = "yahoo-chat";
myIM[2].icon = "aim";
myIM[3].icon = "gchat";
myIM[0].text = "MSN";
myIM[1].text = "Yahoo";
myIM[2].text = "AIM";
myIM[3].text = "G-Talk";
myIM[0].link = "http://people.live.com/";
myIM[1].link = "http://messenger.yahoo.com/";
myIM[2].link = "http://aim.com/aimexpress.adp";
myIM[3].link = "http://google.com/talk";

function buildScrollableOptions(divName,arrayValues,width,startIndex,amount){
	if(!amount)
		amount = 4;
		
	var div = document.getElementById(divName);
	var arrayName = divName.substr(0,divName.length-3);
	if(!startIndex)
		startIndex = 0;
	arrayValues['showing'] = startIndex;
	arrayValues['sliding'] = false;
	for(var i = 0; i < arrayValues.length; i++){
		arrayValues[i].div = document.createElement("div");
		
		if(arrayValues[i].onmouseover)
			arrayValues[i].div.onmouseover = arrayValues[i].onmouseover;
		
		if(arrayValues[i].onmouseout)
			arrayValues[i].div.onmouseout = arrayValues[i].onmouseout;
		
		arrayValues[i].div.innerHTML = "<a href='"+arrayValues[i].link+"' target='"+divName+":"+i+"'><div class='"+arrayValues[i].icon+"'><span class='icon_text'>"+arrayValues[i].text+"</span></div></a>";
		arrayValues[i].div.className = "defaultMyMailClass";
		arrayValues[i].div.style.left = ((i-startIndex)*width)+"px";

		
		div.appendChild(arrayValues[i].div);
	}
	
	if(startIndex == 0)
		document.getElementById(divName+'Left').style.cursor = "not-allowed";	
	
	if(arrayValues.length-amount == startIndex)
		document.getElementById(divName+'Right').style.cursor = "not-allowed";	

}

function openNewWindow(url, windowtarget, arrayobject){
	if(!arrayobject.window || arrayobject.window.closed)
		arrayobject.window = window.open(url,windowtarget);
	else
		arrayobject.window.focus();
	return false;
}

function scrollableOptionsLeft(divName,arrayValues,width,moveAmount,amount){
	if(!amount)
		amount = 4;
		
	if(arrayValues['showing'] == 0)
		return false;
	if(arrayValues['sliding'] == true)
		return false;
	
	arrayValues['sliding'] = true;
	for(var i = 0; i < arrayValues.length; i++){
		if(i < (arrayValues['showing']-1) || i > (arrayValues['showing']+(amount - 1)) )
			arrayValues[i].div.style.left = (arrayValues[i].div.offsetLeft+width)+"px";
	}
	arrayValues['showing']--;
	
	document.getElementById(divName+'Right').style.cursor = "pointer";
	if(arrayValues['showing'] == 0){
		document.getElementById(divName+'Left').style.cursor = "not-allowed";
	}
	setTimeout(function(){
				scrollingDisplayLeft(arrayValues,width/10,10,width,amount);
				},10);
}

function scrollingDisplayLeft(arrayValues,width,time,initWidth,amount){
	if(time <= 0)
		return;
	if(time == 1){
		arrayValues[arrayValues['showing']+0].div.style.left = 				"0px";
		arrayValues[arrayValues['showing']+1].div.style.left = (initWidth)	+"px";
		arrayValues[arrayValues['showing']+2].div.style.left = (2*initWidth)+"px";
		arrayValues[arrayValues['showing']+3].div.style.left = (3*initWidth)+"px";
		if(amount == 4)arrayValues[arrayValues['showing']+4].div.style.left = (4*initWidth)+"px";
		arrayValues['sliding'] = false;
	}else{
		arrayValues[arrayValues['showing']+0].div.style.left = (arrayValues[arrayValues['showing']+0].div.offsetLeft+width)+"px";
		arrayValues[arrayValues['showing']+1].div.style.left = (arrayValues[arrayValues['showing']+1].div.offsetLeft+width)+"px";
		arrayValues[arrayValues['showing']+2].div.style.left = (arrayValues[arrayValues['showing']+2].div.offsetLeft+width)+"px";
		arrayValues[arrayValues['showing']+3].div.style.left = (arrayValues[arrayValues['showing']+3].div.offsetLeft+width)+"px";
		if(amount == 4)arrayValues[arrayValues['showing']+4].div.style.left = (arrayValues[arrayValues['showing']+4].div.offsetLeft+width)+"px";
	}
	setTimeout(function(){scrollingDisplayLeft(arrayValues,width,time-1,initWidth,amount);},10);
}

function scrollableOptionsRight(divName,arrayValues,width,moveAmount,amount){
	if(!amount)
		amount = 4;
		
	if(arrayValues['showing'] == arrayValues.length-amount)
		return false;
	if(arrayValues['sliding'] == true)
		return false;
	
	arrayValues['sliding'] = true;
	
		
	for(var i = 0; i < arrayValues.length; i++){
		if(i < arrayValues['showing'] || i > (arrayValues['showing']+amount) )
			arrayValues[i].div.style.left = (arrayValues[i].div.offsetLeft-width)+"px";
	}
	arrayValues['showing']++;		
	document.getElementById(divName+'Left').style.cursor = "pointer";
	if(arrayValues['showing'] == arrayValues.length-amount){
		document.getElementById(divName+'Right').style.cursor = "not-allowed";
	}
	
	setTimeout(function(){
				scrollingDisplayRight(arrayValues,width/10,10,width,amount);
				},10);			
}


function scrollingDisplayRight(arrayValues,width,time,initWidth,amount){
	if(time <= 0)
		return;
	if(time == 1){
		arrayValues[arrayValues['showing']-1].div.style.left = (-1*initWidth)+"px";
		arrayValues[arrayValues['showing']+0].div.style.left = "0px";
		arrayValues[arrayValues['showing']+1].div.style.left = (initWidth)+"px";
		arrayValues[arrayValues['showing']+2].div.style.left = (2*initWidth)+"px";
		if(amount == 4)arrayValues[arrayValues['showing']+3].div.style.left = (3*initWidth)+"px";
		arrayValues['sliding'] = false;
	}else{
		arrayValues[arrayValues['showing']-1].div.style.left = (arrayValues[arrayValues['showing']-1].div.offsetLeft-width)+"px";
		arrayValues[arrayValues['showing']+0].div.style.left = (arrayValues[arrayValues['showing']+0].div.offsetLeft-width)+"px";
		arrayValues[arrayValues['showing']+1].div.style.left = (arrayValues[arrayValues['showing']+1].div.offsetLeft-width)+"px";
		arrayValues[arrayValues['showing']+2].div.style.left = (arrayValues[arrayValues['showing']+2].div.offsetLeft-width)+"px";
		if(amount == 4)arrayValues[arrayValues['showing']+3].div.style.left = (arrayValues[arrayValues['showing']+3].div.offsetLeft-width)+"px";			
	}
	setTimeout(function(){scrollingDisplayRight(arrayValues,width,time-1,initWidth,amount);},10);
}

function signInDialogShow(){
	var lik = document.getElementById('header_bottom_right');
	var lik2 = document.getElementById('signInLink');
	var u = document.getElementById('signInUser');
	var p = document.getElementById('signInPass');

	u.value = "";
	p.value = "";
	document.getElementById('signInDivDialog').style.left = (lik.offsetLeft+lik2.offsetLeft+lik2.offsetWidth)+"px";
	//document.getElementById('signInDivDialog').style.visibility = "visible";
	document.getElementById('signInDivDialog').style.display = "block";
	document.getElementById('signInError').innerHTML = "";
}

function signInDialogClose(){
	//document.getElementById('signInDivDialog').style.visibility = "hidden";
	document.getElementById('signInDivDialog').style.display = "none";
	var u = document.getElementById('signInUser');
	var p = document.getElementById('signInPass');
	u.value = "";
	p.value = "";
}

function signInAttempt(){
	var u = document.getElementById('signInUser');
	var p = document.getElementById('signInPass');
	
	if(u.value == "" || p.value == ""){
		document.getElementById('signInError').innerHTML = "Missing Information";	
		return;
	}
	
	
	var xhr = setupXHR(); 
	
	xhr.onreadystatechange  = function(){ 
		if(xhr.readyState  == 4){
			if(xhr.status  == 200){
				try{
					var temp = xhr.responseText;
				
					switch(temp){
						case "0":
							document.getElementById('signInError').innerHTML = "Invalid Username/Password";
							p.value = "";
							break;
						case "1":
							window.location.reload();
							break;
						case "2":
							document.getElementById('signInError').innerHTML = "Account is Disabled";
							break;
					}
				
					
				}catch(e4){}
			}
		}
	}; 

	xhr.open("POST", "/login.php",  true); 
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	xhr.send("user="+escape(u.value)+"&pass="+escape(p.value)); 
	
}

function keyEnter(e,func){
	var keyCode;
	if (e && e.which) keyCode = e.which
	else if(e && e.keyCode)	keyCode = e.keyCode;
	else return;
	
	if(keyCode == 13)
		func();
}

function searchSubmit2(){
	var xhr;
	try{
		xhr = setupXHR(); 
	}catch(e4){}
	
		xhr.onreadystatechange  = function(){ 
			if(xhr.readyState  == 4){
				if(xhr.status  == 200){
					try{
						var temp = xhr.responseText;
					}catch(e4){}
				}
			}
		}; 
			
	xhr.open("POST", "search.php",  true); 
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	xhr.send("");
}

function myMailExtraDective(){
	myMail.extra = false;
	setTimeout("myMailExtraDectiveTime();",1000);
}

function myMailExtraDectiveTime(){
	if(myMail.extra == false){
		document.getElementById('mail_extra').style.display = "none";
	}
}

function myMailExtraActive(){
	myMail.extra = true;
}

function myMailExtraDectiveYahoo(){
	myMail.extra = false;
	setTimeout("myMailExtraDectiveTimeYahoo();",1000);
}

function myMailExtraDectiveTimeYahoo(){
	if(myMail.extra == false){
		document.getElementById('mail_extra_yahoo').style.display = "none";
	}
}

function myMailExtraDectiveGmail(){
	myMail.extra = false;
	setTimeout("myMailExtraDectiveTimeGmail();",1000);
}

function myMailExtraDectiveTimeGmail(){
	if(myMail.extra == false){
		document.getElementById('mail_extra_gmail').style.display = "none";
	}
}


function loadMail(t,cmd){
	myMail[0].window.compose_func2();
}
