var currentMenu = '';
var preSet      = '';
var url="http://beta.corpfin.net";
var title="Corpfin.Net";

function newwindow(location) {
	window.open(location,'sample');
}

function addfav() {
	if (document.all) {
		document.write('<br /><br /><a id="adfav" href="javascript:window.external.AddFavorite(url,title);" ');
		document.write('onMouseOver=" window.status=');
		document.write("'Add Corpfin.Net to your favorites.'; return true ");
		document.write('"onMouseOut=" window.status=');
		document.write("' '; return true ");
		document.write('">Add Corpfin.Net to your favorites.</a>');
        }
}

function toggleTree(toggleNode, toggleTo) {
	for (i = 0; i < toggleNode.childNodes.length; i++) {
		if      (toggleTo == 'preset') { 
			if (toggleNode.childNodes[i].nodeName == 'A') { toggleNode.childNodes[i].style.backgroundPosition = '0 -19px'; }
		}
		else if ((toggleNode.childNodes[i].nodeName == 'UL') && (document.all)) { toggleNode.childNodes[i].style.display = toggleTo; }
		else if ((toggleNode.childNodes[i].nodeName == 'A') && (preSet != toggleNode.id)) {
			if (toggleTo == 'block') { toggleNode.childNodes[i].style.backgroundPosition = '0 -19px'; }
			else                     { toggleNode.childNodes[i].style.backgroundPosition = '0 8px'; }
		}
	}
	return true;
}

function menuOn() {
	var args = getArgs();
	if (args['reset']) {
		eraseCookie('show');
	}
	var show = readCookie('show');
	var launch = false;
	jQuery('<div id="boxes">').appendTo('body');
	jQuery('<div id="mask">').appendTo('#boxes');
	if (args['blog']) {
		launch = true;
		jQuery('<div id="dialog" class="window">').html('<h3>Free Public Company Analysis Membership</h3><p>Please <a href="https://www.corpfin.net/cgi/cfn2/register?type=free">click here</a> to sign-up for a free membership that entitles you to create and save public company analyses and valuations on our secure server.</p><p class="nogo">No, Thanks, I would like to try the model first.</p><div id="boxclose"><a href="#" class="close">Close this Window</a></div>').appendTo('#boxes');
	}
	else if ((!show) || (args['show'])) {
		launch = true;
		jQuery('<div id="dialog" class="window">').html('<h3>Free Public Company Analysis Membership</h3><p>Please <a href="https://www.corpfin.net/cgi/cfn2/register?type=free">click here</a> to sign-up for a free membership that entitles you to create and save public company analyses and valuations on our secure server.</p><p><strong>Using our public company analysis model is a great way to practice/learn financial forecasting and business valuation concepts.</strong></p><p class="nogo">No, Thanks, I would like to continue browsing.</p><div id="boxclose"><a href="#" class="close">Close this Window</a></div>').appendTo('#boxes');
		createCookie('show', '1', 365);
	}

	if (launch) {
		//Put in the DIV id you want to display
		launchWindow('#dialog');
	}
	
	//if close button is clicked
	$('.close').click(function () {
		$('#mask').hide();
		$('.window').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});			



	if (document.getElementById) {
		menuBase = document.getElementById('mainnav');
		for (i1 = 0; i1 < menuBase.childNodes.length; i1++) {
			if (menuBase.childNodes[i1].nodeName == 'LI') {
				menuBase.childNodes[i1].onmouseover = function() { toggleTree(this, 'block'); }
				menuBase.childNodes[i1].onmouseout  = function() { toggleTree(this, 'none'); }
				if (menuBase.childNodes[i1].id == preSet) {
					toggleTree(menuBase.childNodes[i1], 'preset');
				}
			}
		} 
	}
}

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

function readCookie(name) {
	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) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function getArgs() {
	var args = new Object();
	var query = location.search.substring(1); 
	var pairs = query.split("&"); 
	for(var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('='); 
		if (pos == -1) continue; 
		var argname = pairs[i].substring(0,pos); 
		var value = pairs[i].substring(pos+1);
		args[argname] = unescape(value); 
	}
	return args; 
}

function launchWindow(id) {
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight,'background-color':'#333'});
//		$('#mask').show();
		
		//transition effect		
//		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height());
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(2000); 
}


window.onload = menuOn;


