
var persWin;
var returning = false;

var closedHed = '<div class="fake"><a href="javascript:showPane(\'o\');"><img src="http://images.forbes.com/media/fifth/a8abbcc.gif" width="30" height="240" border="0" id="closedPic"></a><\/div>';

var help = false;
var content;
var delArray = new Array();

var mapsHelp = 'Map an address, get directions, find locally.';
var watchListHelp = 'Track your favorite stocks. For more information, click "Edit."';
var listNewsHelp = 'Displays the most recent headlines based on your Stock Watch list. For more information, click "Edit"';
var weatherHelp = 'Display the forecasted weather in several cities. For more information, click "Edit"';
var feedsHelp = 'Displays the five most recent headlines of each RSS feed you enter. For more information, click "Edit"';
var storyTypeHelp = 'Track stories based on story type. For more information, click "Edit"';
var newslettersHelp = 'Displays the headlines of the five most recent newsletters you are subscribed to. For more information, click "Edit"';
var teamsHelp = 'Display results and upcoming game for selected teams or sports. For more information, click "Edit"';
var alertsHelp = 'Track stories based on keywords. For more information, click "Edit"';
var linksHelp = 'To be used to store favorite links, both Forbes.com and other Web sites. For more information, click "Edit"';
var channelHelp = 'Track stories based on channels and sections. For more information, click "Edit"';
var calendarHelp = 'Displays upcoming corporate events for companies on your Watch List. For more information, click "Edit"';
var authorHelp = 'Track stories by Forbes.com authors. For more information, click "Edit."';
var industryNewsHelp = 'Track stories based on industries. For more information, click "Edit"';
var bigEditHelp = '<b>Edit</b> lets you reorder your column, choose a color and add or remove other elements.';
var splitHelp = '<b>Detach</b> separates your personalized column from the main page, allowing you to view it regardless of what Web page you are on.';
var closeHelp = '<b>Close</b> closes your column entirely. Creates a button with which to reopen it.';
var bigHelpHelp = 'When <b>Help</b> is turned on, you get more information when you put the mouse over each module.<br>You can also get information on a particular module by clicking on the <b>Question Mark<\/b>.';
var thisTestStr = '';
var elArray = new Array();
var thisColor = '#a8abbc';
var lastId = 'none';
var hiddenArray = new Array();
var hidden = '';
hidden0 = document.cookie.indexOf('hiddenarray='); 
if (hidden0 != -1) {
	hidden = document.cookie.substr(hidden0+12);
	hiddenE = hidden.indexOf(';');
	if (hiddenE != -1) hidden = hidden.substr(0,hiddenE);
	hiddenArray = hidden.split(',');
}
var shownArray = new Array();
var shown = '';
shown0 = document.cookie.indexOf('shownarray='); 
if (shown0 != -1) {
	shown = document.cookie.substr(shown0+11);
	shownE = shown.indexOf(';');
	if (shownE != -1) shown = shown.substr(0,shownE);
	shownArray = shown.split(',');
}
function hidePane(x) {
	if (x=='c') {
		document.cookie="closed5=1;path=/";
	}
	if (document.getElementById("helpDiv")) document.getElementById("helpDiv").style.visibility = 'hidden';
	if (document.location.href.indexOf('split') != -1) {
		this.close();
	}
	else if ((document.location.href.indexOf('fifth/edit') == -1) && (document.location.href.indexOf('save') == -1)) {
		document.getElementById("colHed").innerHTML = closedHed;
		document.getElementById("persDiv").style.visibility = 'hidden';
	} 
	setBg(thisColor);
}

function showPane(x) {
	if (x=='o') {
		document.cookie="closed5=0;path=/";
		if (persWin && !persWin.closed) {
			persWin.close();
			document.cookie='split=0;path=/';
		}
	}
	document.getElementById("persDiv").style.visibility = 'visible';
	var openHed = '<div class="logo"><img src="http://images.forbes.com/media/fifth/a8abbc.gif" width="215" height="48" id="coLogo"></div><div class="controls"  id="openClose"><span id="help"><a href="javascript:helpBub(\'on\');" class="controllink">Help</a><\/span><a href="javascript:go(\'/fifth/edit.do?userId=' + userId + '\');" class="controllink" onMouseOver="dispHelp(\'bigEdit\');" onMouseOut="dispHelp(\'off\');">Edit<\/a><a href="javascript:doRetSplit(' + userId + ');" class="controllink" onMouseOver="dispHelp(\'split\');" onMouseOut="dispHelp(\'off\');">' + dispRetSplit() + '</a><a href="javascript:hidePane(\'c\');" class="controllink" onMouseOver="dispHelp(\'close\');" onMouseOut="dispHelp(\'off\');">Close</a></div>';
	
	if ((this.location.href.indexOf('fifth/edit') == -1) && (this.location.href.indexOf('save') == -1)) document.getElementById("colHed").innerHTML = openHed;
	else document.getElementById("colHed").innerHTML = '';
	document.getElementById("persDiv").style.visibility = 'visible';
	setBg(thisColor);

}

function splitPanes(userId) {
	hidePane('c');
	document.cookie='split=1;path=/';
	persWin = window.open('/fifth/main.do?userId=' + userId + '&backto=' + backTo + '&split=true', 'personal', "width=232,height=760,toolbar=0,resizable=1,scrollbars=1");
	persWin.moveTo(780,0);
	persWin.focus();
}

function hideShow(id,x) {
	if (x==1) {
		if (hidden.indexOf(id) == -1) show(id);
	} else {
		if (shown.indexOf(id) == -1) hide(id);
	}
}

function hide(id) {
	if (hidden.indexOf(id) == -1) {
		hiddenArray[hiddenArray.length] = id;
		document.cookie='hiddenarray=' + hiddenArray;
		if (hidden.length != 0) hidden = hidden + ',';
		hidden = hidden + id;
		bound = shownArray.length;
		shown = '';
		for (i=0;i<bound;i++) {
			if (shownArray[i] != id) {
				if (shown.length > 0) shown = shown + ',';    
				shown = shown + shownArray[i];
			} else {
				shownArray = shownArray.slice(0,i).concat(shownArray.slice(i+1,shownArray.length));
			}
		}
		document.cookie='shownarray=' + shownArray;
	}
}

function show(id) {
	if (shown.indexOf(id) == -1) {
		shownArray[shownArray.length] = id;
		document.cookie='shownarray=' + shownArray;
		if (shown.length != 0) shown = shown + ',';
		shown = shown + id;
		bound = hiddenArray.length;
		hidden = '';
		for (i=0;i<bound;i++) {
			if (hiddenArray[i] != id) {
				if (hidden.length > 0) hidden = hidden + ',';    
				hidden = hidden + hiddenArray[i];
			} else {
				hiddenArray = hiddenArray.slice(0,i).concat(hiddenArray.slice(i+1,hiddenArray.length));
			}
		}
		document.cookie='hiddenarray=' + hiddenArray;
	}

}

function hideElement(id, userId) {
	if (document.getElementById(id + 'Help')) thisHelp = document.getElementById(id + 'Help').innerHTML
	else thisHelp = '<a href="javascript:helpElement (\'' + id + '\');" class="elementcontrollink">?<\/a>';
	hide(id);
	document.getElementById(id + 'Controls').innerHTML = '<a href="javascript:showElement(\'' + id + '\', \'' + userId + '\',\'new\');" class="elementcontrollink">Show<\/a><span id="' + id + 'Help">' + thisHelp + '<\/span>';
	document.getElementById(id + 'Title').style.fontWeight = 'normal';
	document.getElementById(id + 'Content').style.display="none" ;
}


function showElement(id, userId,type) {
	if (document.getElementById(id + 'Help')) thisHelp = document.getElementById(id + 'Help').innerHTML
	else thisHelp = '<a href="javascript:helpElement (\'' + id + '\');" class="elementcontrollink">?<\/a>';
	if (type == 'new') {
		show(id);
	}
	thisText = id + 'Label';
	if ((this.location.href.indexOf('fifth/edit') == -1) && (this.location.href.indexOf('save') == -1)) {
		document.getElementById(id + 'Controls').innerHTML = '<a href="javascript:editElement(\'' + id + '\', \'' + userId + '\');" class="elementcontrollink">Edit<\/a><a href="javascript:hideElement(\''+ id + '\', \'' + userId + '\');" class="elementcontrollink">Hide<\/a><span id="' + id + 'Help">' + thisHelp + '<\/span>';
	} else {
		document.getElementById(id + 'Controls').innerHTML = '<a href="javascript:editElement(\'' + id + '\', \'' + userId + '\');" class="elementcontrollink">Edit<\/a><a href="javascript:deleteElement (\'' + id + '\',\'' + userId + '\');" class="elementcontrollink">Delete<\/a>';
	}
	document.getElementById(id + 'Title').style.fontWeight = 'bold';
	document.getElementById(id + 'Content').style.display="block" ;
}

function editElement(id, userId) {
	if (id == 'newsletters') {
		go('http://www.forbes.com/membership/editprofile.jhtml');
	} else if (id == 'watchList') {
		go('./ModuleDisplayAction.do?userId='+ userId +'&&moduleId=1');
	} else if (id == 'feeds') {
		go('./ModuleDisplayAction.do?userId='+ userId +'&&moduleId=2');
	} else if (id == 'links') {
		go('./ModuleDisplayAction.do?userId='+ userId +'&&moduleId=3');
	} else if (id == 'listNews') {
		go('./ModuleDisplayAction.do?userId='+ userId +'&&moduleId=4');
	} else if (id == 'author') {
		go('./ModuleDisplayAction.do?userId='+ userId +'&&moduleId=5');
	} else if (id == 'storyType') {
		go('./ModuleDisplayAction.do?userId='+ userId +'&&moduleId=6');
	} else if (id == 'alerts') {
		go('./ModuleDisplayAction.do?userId='+ userId +'&&moduleId=7');
	} else if (id == 'industryNews') {
		go('./ModuleDisplayAction.do?userId='+ userId +'&&moduleId=8');
	} else if (id == 'calendar') {
		go('./ModuleDisplayAction.do?userId='+ userId +'&&moduleId=9');
	} else if (id == 'weather') {
		go('./ModuleDisplayAction.do?userId='+ userId +'&&moduleId=10');
	} else if (id == 'teams') {
		go('./ModuleDisplayAction.do?userId='+ userId +'&&moduleId=11');
	} else if (id == 'channel') {
		go('./ModuleDisplayAction.do?userId='+ userId +'&&moduleId=12');
	} else if (id == 'newsLetter') {
		go('./ModuleDisplayAction.do?userId='+ userId +'&&moduleId=13');
	}
}

function goBack() {
	document.cookie = "closed5=0;path=/";
	document.cookie = "split=0;path=/";
	this.close();
	if (backTo == -1) backTo = 'http://www.forbes.com';
	returning = true;
	go(backTo);
}

function go(url) {
	if (url.indexOf('edit.do') > -1) {
		document.cookie = "closed5=0;path=/";
	        if (this.location.href.indexOf('split') > -1) this.close();
	}
        if (this.location.href.indexOf('split') > -1) {
        	if (opener && !opener.closed) {
			opener.parent.location.href=url
		} else {
            		newWin = window.open(url, 'main', 'toolbar=1,resizable=1,scrollbars=1,status=1,location=1,width=1030,height=760');
		}
        } else {
        	parent.location.href=url;
        }
}

function dispRetSplit() {
	if (document.location.href.indexOf('split') != -1) return 'Attach';
	else return 'Detach';
}


function doRetSplit(userId) {
	if (document.location.href.indexOf('split') != -1) {
		goBack();
	} else splitPanes(userId);
}

function makeCookies() {
	if (elArray.length>0) document.cookie = 'elarray=' + elArray.toString() + ';path=/';
	else  document.cookie = 'elarray=none;path=/';
	if (delArray.length>0) document.cookie = 'delarray=' + delArray.toString() + ';path=/';
	else  document.cookie = 'delarray=none;path=/';
}

function displaySort(userId) {
	makeCookies();
	var newOrder= '';
	if (delArray.length > 0) {
		newOrder = '<div class="edithed" id="editHed2">Deleted Components</div>';
		for (i=0;i<delArray.length;i++) {
			newOrder = newOrder + document.getElementById(delArray[i]).outerHTML;
		}
		newOrder = newOrder + '<br>';
	} 

	if (elArray.length > 0) { 
		newOrder = newOrder + '<div class="edithed" id="editHed1">My Components<!--a href="javascript:sortAll(\'num\');"><img src="http://images.forbes.com/media/fifth/sortnum.gif" width="95" height="25" border="0" hspace="1"><\/a--><a href="javascript:sortAll(\'alpha\')"><img src="http://images.forbes.com/media/fifth/sortalph.gif" width="95" height="25" border="0" hspace="1"><\/a><\/div>';
		for (i=0;i<elArray.length;i++) {
			newOrder = newOrder + document.getElementById(elArray[i]).outerHTML;
		}
		newOrder = newOrder + '<div class="edithed" id="editHed1"><!--a href="javascript:sortAll(\'num\');"><img src="http://images.forbes.com/media/fifth/sortnum.gif" width="95" height="25" border="0" hspace="1"><\/a--><a href="javascript:sortAll(\'alpha\')"><img src="http://images.forbes.com/media/fifth/sortalph.gif" width="95" height="25" border="0" hspace="1"><\/a><\/div>';
	}
	document.getElementById("persDiv").innerHTML = "";
	document.getElementById("persDiv").innerHTML = newOrder;
	if (elArray.length > 0) {
		for (i=0;i<elArray.length;i++) {
			document.getElementById(elArray[i] + 'Controls').innerHTML = '<a href="javascript:editElement(\'' + elArray[i] + '\', \'' + userId + '\');" class="elementcontrollink">Edit<\/a><a href="javascript:deleteElement(\'' + elArray[i] + '\', \'' + userId + '\');" class="elementcontrollink">Delete <\/a>';
			//document.getElementById(elArray[i] + 'No').value = i+1;
			document.getElementById(elArray[i] + 'EditPic').innerHTML = '<img src="http://images.forbes.com/media/fifth/arrows.gif" width="14" height="23" border="0" usemap="#' + elArray[i] + 'Map" ismap>';
		}
		document.getElementById(elArray[0] + 'EditPic').innerHTML = '<a href="javascript:moveDown(\'' + elArray[0] + '\', \'' + userId + '\');"><img src="http://images.forbes.com/media/fifth/down.gif" width="14" height="23" border="0"></a>';
		document.getElementById(elArray[elArray.length-1] + 'EditPic').innerHTML = '<a href="javascript:moveUp(\'' + elArray[elArray.length-1] + '\', \'' + userId + '\');"><img src="http://images.forbes.com/media/fifth/up.gif" width="14" height="23" border="0"></a>';
	}
	if (delArray.length > 0) {
		for (i=0;i<delArray.length;i++) {
			document.getElementById(delArray[i] + 'Content').style.display="none" ;
			document.getElementById(delArray[i] + 'EditPic').innerHTML = '';
			document.getElementById(delArray[i] + 'Controls').style.backgroundColor = '#eeeeee';
			document.getElementById(delArray[i] + 'Title').style.color = '#666666';
			document.getElementById(delArray[i] + 'Title').style.fontWeight = 'normal';
			document.getElementById(delArray[i] + 'Controls').innerHTML = '<a href="javascript:addElement(\'' + delArray[i] + '\', \'' + userId + '\');" class="elementcontrollink">Add<\/a>';
			//document.getElementById(delArray[i] + 'No').value = '-';
		}
	}
}


function setBg(color) {
	if ((color == '') || (color.length<7)) color='#a8abbc';
	scolor = color.substr(1);
	if ((document.cookie.indexOf('closed5=1')!=-1) && (document.location.href.indexOf('split') == -1)) {
		document.getElementById("closedPic").src = 'http://images.forbes.com/media/fifth/' + scolor + 'c.gif';
	} else {
		document.getElementById("coLogo").src = 'http://images.forbes.com/media/fifth/' + scolor + '.gif';
		document.getElementById("persDiv").style.backgroundColor = color;
		if (color == "#ffffff") {
			document.getElementById("persDiv").style.borderWidth = 1;
			document.getElementById("persDiv").style.borderStyle = 'solid';
			document.getElementById("persDiv").style.borderColor = '#cccccc';
		} else {
			document.getElementById("persDiv").style.borderStyle = 'none';
		}
		document.body.style.scrollbarFaceColor = color;
		for (i=0;i<elArray.length;i++) {
			if(document.getElementById(elArray[i] + 'Space') != null) {	 
				document.getElementById(elArray[i] + 'Space').style.backgroundColor = color;
			}
		}
		for (i=0;i<delArray.length;i++) {
			if(document.getElementById(delArray[i] + 'Space') != null) {	 
				document.getElementById(delArray[i] + 'Space').style.backgroundColor = color;
			}
		}
	}
}

function makeControls(id) {
	if ((this.location.href.indexOf('fifth/edit')>-1) || (this.location.href.indexOf('save')>-1)) {
		if (thisTestStr.indexOf(id) == -1) {
			theseControls = '<span id="' + id + 'Edit"><span id="' + id + 'EditPic"><\/span> <!--input type="text" size="2" id="' + id + 'No" class="box" value="1"--><\/span>';
			thisTestStr = thisTestStr + id;
		} else theseControls = '';
	} else theseControls = '';
	document.write(theseControls);
}

function initCol(moduleArray,deleteArray,color, userId) {
	thisColor = color;
	elArray = moduleArray;
	delArray = deleteArray;
        if ((document.cookie.indexOf('closed5=1') != -1) && (this.location.href.indexOf('split') == -1)) {
		hidePane();
	} else showPane();
	if ((this.location.href.indexOf('fifth/edit') != -1) || (this.location.href.indexOf('save') != -1)) displaySort(userId);
	else {
		for(i=0;i<shownArray.length;i++) {
			showElement(shownArray[i],userId);
		}
		for(i=0;i<hiddenArray.length;i++) {
			hideElement(hiddenArray[i],userId);
		}
		for(i=0;i<delArray.length;i++) {
			document.getElementById(delArray[i]).style.display = 'none';
		}
		
	}
}

function helpElement(id,what) {
	if (what == 'off') {
		if (lastId != 'none') document.getElementById(lastId + "Help").innerHTML = '<a href="javascript:helpElement(\'' + lastId + '\',\'on\');" class="controllink">?<\/a>';
		document.getElementById("helpDiv").style.visibility = 'hidden';
		lastId = 'none';
	}
	else {
		document.getElementById(id + "Help").innerHTML = '<a href="javascript:helpElement(\'' + id + '\',\'off\');" class="controllink"><strike>?<\/strike><\/a>';
		if (lastId != 'none') document.getElementById(lastId + "Help").innerHTML = '<a href="javascript:helpElement(\'' + lastId + '\',\'on\');" class="controllink">?<\/a>';
		document.getElementById("helpDiv").style.visibility = 'visible';
		document.getElementById("helpDiv").innerHTML = eval(id + 'Help');
		document.getElementById("helpDiv").style.top = document.getElementById(id).offsetTop + 106;
		document.getElementById("helpDiv").style.left = document.getElementById(id).offsetLeft;
		lastId = id;
	}
}
function helpBub(x) {
	if (x == 'on') {
		help = true;
		dispHelp('bigHelp');
		document.getElementById("help").innerHTML = '<a href="javascript:helpBub(\'off\');" class="controllink" onMouseOut="dispHelp(\'off\');">Help Off<\/a>';
		for(i=0;i<elArray.length;i++) {
			document.getElementById(elArray[i] + "Help").innerHTML = '';
		}
	} else {
		document.getElementById("helpDiv").style.visibility = 'hidden';
		document.getElementById("help").innerHTML = '<a href="javascript:helpBub(\'on\');" class="controllink">Help<\/a>';
		for(i=0;i<elArray.length;i++) {
			document.getElementById(elArray[i] + "Help").innerHTML = '<a href="javascript:helpElement(\'' + elArray[i] + '\',\'on\');" class="controllink">?<\/a>';
		}
		help = false;
		lastId = 'none';
	}
}
function dispHelp(what) {
	if (help) {
		if (what == 'off') {
		document.getElementById("helpDiv").style.visibility = 'hidden';
		} else {
			document.getElementById("helpDiv").style.visibility = 'visible';
			document.getElementById("helpDiv").innerHTML = eval(what + 'Help');
			if (document.getElementById(what)) {
				document.getElementById("helpDiv").style.top = document.getElementById(what).offsetTop + 106;
				document.getElementById("helpDiv").style.left = document.getElementById(what).offsetLeft;
			} else {
				document.getElementById("helpDiv").style.top = 65;
				document.getElementById("helpDiv").style.left = 0;
			}
		}
	}
}
function startCol() {
	thisURL = this.location.href;
	document.cookie = 'fifthinit=1;path=/;'
	backtoUrl = thisURL.substr(thisURL.indexOf('backto=')+7);
	parent.location.href = '/membership/signup.jhtml?comingFrom=fifth&gotoURL=' + backtoUrl;
}

