function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}

htmlEncode = escapeHTML = function(s) {
    var div = document.createElement('div');
    var text = document.createTextNode(s);
    div.appendChild(text);
    return div.innerHTML;
};

function addErrorLogger(){
    window.onerror = window.error = function(msg, url, line){ 
		jQuery.post("/mvc/log/create", { Url: url, Line: line, Message: msg, Browser: navigator.userAgent}); 
	};
}

if(typeof(jQuery) == "undefined"){
/*	var ieVersion = getInternetExplorerVersion();
	if (ieVersion > -1 && ieVersion < 8.0)
		document.write('<script src="/WebCommon/IE8.js" type="text/javascript"></script>');
*/
	
	document.write('<script src="/WebCommon/jquery-1.4.2.min.js" type="text/javascript"></script>');
	//only includes dialog, buttons, and datepicker
	//theme url: http://jqueryui.com/download/?themeParams=%3FffDefault%3DArial%252CHelvetica+Neue%252CHelvetica%252Csans-serif%26fwDefault%3Dnormal%26fsDefault%3D1.1em%26cornerRadius%3D4px%26bgColorHeader%3D3c4b62%26bgTextureHeader%3D03_highlight_soft.png%26bgImgOpacityHeader%3D55%26borderColorHeader%3DE1E1E1%26fcHeader%3Dffffff%26iconColorHeader%3Dffffff%26bgColorContent%3Dffffff%26bgTextureContent%3D01_flat.png%26bgImgOpacityContent%3D95%26borderColorContent%3Daaaaaa%26fcContent%3D222222%26iconColorContent%3D222222%26bgColorDefault%3DD7dce3%26bgTextureDefault%3D02_glass.png%26bgImgOpacityDefault%3D75%26borderColorDefault%3Dd3d3d3%26fcDefault%3D555555%26iconColorDefault%3D888888%26bgColorHover%3Dfce587%26bgTextureHover%3D02_glass.png%26bgImgOpacityHover%3D75%26borderColorHover%3D999999%26fcHover%3D212121%26iconColorHover%3D454545%26bgColorActive%3Dffffff%26bgTextureActive%3D02_glass.png%26bgImgOpacityActive%3D65%26borderColorActive%3Daaaaaa%26fcActive%3D212121%26iconColorActive%3D454545%26bgColorHighlight%3Dfbf9ee%26bgTextureHighlight%3D02_glass.png%26bgImgOpacityHighlight%3D55%26borderColorHighlight%3Dfcefa1%26fcHighlight%3D363636%26iconColorHighlight%3D2e83ff%26bgColorError%3Dfef1ec%26bgTextureError%3D05_inset_soft.png%26bgImgOpacityError%3D95%26borderColorError%3Dcd0a0a%26fcError%3Dcd0a0a%26iconColorError%3Dcd0a0a%26bgColorOverlay%3Daaaaaa%26bgTextureOverlay%3D01_flat.png%26bgImgOpacityOverlay%3D0%26opacityOverlay%3D30%26bgColorShadow%3Daaaaaa%26bgTextureShadow%3D01_flat.png%26bgImgOpacityShadow%3D0%26opacityShadow%3D30%26thicknessShadow%3D8px%26offsetTopShadow%3D-8px%26offsetLeftShadow%3D-8px%26cornerRadiusShadow%3D8px
	document.write('<script src="/WebCommon/jquery-ui-1.8.custom.min.js" type="text/javascript"></script>');
	document.write('<script src="/WebCommon/common.js" type="text/javascript"></script>');

	document.write('<link href="/WebCommon/jquery-ui-1.8.custom.css" rel="stylesheet" type="text/css" ></link>');
	document.write('<link href="/WebCommon/base.css" rel="stylesheet" type="text/css" ></link>');
}

var templateDefaults = {
	userDisplayName: null,
	legacyUserId: null,
	genericUserId: null,
	userType: null //"agent", "preservation company", "client", "asset manager", "portfolio manager", "premarketer"
};

function writeHeader(options){
	addErrorLogger();
	$.datepicker.setDefaults({changeMonth: true, changeYear: true});
    
	$.widget('keystone.menu', {
		_init: function() {
			var me = this;
			var e = $(me.element);
			$(e).append('<div class="pageMenuLeft"></div>');
			var ul = this._listItems(me.options.items);
			ul.addClass('menuRoot');
			$(e).append(ul);
			$(e).append('<div class="pageMenuRight"></div>');
			
			
			$(ul).children('li').mouseover(function(){
				$(ul).find('li').removeClass('active');
				$(this).addClass('active');
			});
			
			$(ul).children('li').mouseout(function(){
				$(ul).find('li').removeClass('active');
			});
			
		},
		_listItems: function(items){
			var ul = $('<ul/>');
			for(var i = 0; i < items.length; ++i){
				var item = items[i];
				var link = $('<a/>').attr('href', item.url).text(item.label);
				var listItem = $('<li/>').append($('<div/>').append(link));
				if(item.subItems && item.subItems.length > 0){
					listItem.append(this._listItems(item.subItems));
				}
				ul.append(listItem);
			}
			return ul;
		}
	});

	$.extend($.keystone.menu, {
		defaults: {
			items: [{label: 'Test', url: '/'}]
		}
	});

	var o = jQuery.extend({}, templateDefaults, options);
//	document.write('<link REL="SHORTCUT ICON" HREF="/WebCommon/favicon.ico"></link>');
	document.write('<div class="page">');
	document.write('	<div class="pageInner">');
	//for whatever reason, IE6 ignorez the z-index when it's part of the stylesheet, so we inline it
	document.write('		<div class="pageHeader" style="z-index: 100">');
	document.write('			<div class="pageLogo"></div>');
	document.write('			<div class="pageAccountInfo">');
	var profileUrl = getProfileUrl(o.userType, o.legacyUserId, o.genericUserId);
	if(o.userDisplayName && profileUrl)
	    document.write('Logged in as <a href="' + profileUrl + '">' + htmlEncode(o.userDisplayName) + '</a> - <a href="/mvc/user/logout">Logout</a>');
	else if(o.userDisplayName)
	    document.write('Logged in as ' + htmlEncode(o.userDisplayName) + ' - <a href="/mvc/user/logout">Logout</a>');
	else
		document.write('<a href="/mvc/user/login">Login</a>');
	document.write(' | 215.855.3350');
	document.write('			</div> ');
	document.write(' ');
	document.write('			<div class="pageTagLine">');
//	document.write('				A Trusted Partner');
	document.write('				Connecting Lenders, Mortgage Servicers and Real Estate Professionals');
	document.write('			</div>');
	document.write('            <div id="pageMenu"></div>');
	document.write('		</div>');

	$.get(
        "/mvc/User/ExpirationDays",
        function (data) {
            if (data == "") { $('#ExpirationDays').hide(); }
            else { $('#ExpirationDays').html(data); }
        }
   );

	var mainMenuItems = [
		{label: 'Home', url: '/'},
		{label: 'About Us', url: '/about_us/about_us.asp', subItems: [
			{label: 'Corporate Leadership', url: '/about_us/corp_leadership.asp'},
			{label: 'Latest News', url: '/mvc/News'},
			{label: 'Calendar', url: '/about_us/calendar.asp'},
			{label: 'Careers', url: '/about_us/careers.asp'},
			{label: 'Contact Us', url: '/contact_us/contact_us.asp'}
		]},
		{label: 'Services', url: '/services/services.asp', subItems: [
/*			{label: 'REO Services', url: '/services/reo_services.asp'},
			{label: 'Default Management', url: '/services/default_manage.asp'},
			{label: 'Property Valuation', url: '/services/property_valuation.asp'}*/
		]},
		{label: 'Partnerships', url: '/partnership/partnership.asp', subItems: [
			{label: 'Real Estate Professionals', url: '/partnership/real_estate_professionals.asp'},
			{label: 'Attorneys', url: '/partnership/attorneys.asp'},
			{label: 'Property Preservation Services', url: '/partnership/prop_pres.asp'}
		]},
		{label: 'Industry Links', url: '/industry_links/industry_links.asp'},
		{label: 'Contact Us', url: '/contact_us/contact_us.asp', subItems: [
			{label: 'Agent Application', url: '/AgentApplication/AgentApplication.aspx'},
			{label: 'Appraiser Application', url: '/AppraiserApplication/'},
			{label: 'Attorney, Closing, and Title Application', url: '/mvc/VendorApplication/create'},
			{label: 'Property Preservation Services', url: '/partnership/prop_pres.asp'}
		]}
	];
	
	if(o.userType == "agent"){
		mainMenuItems[mainMenuItems.length] = {label: 'Agent Work', url: '/central_status/central_status_pv.asp'};
	}else if(o.userType == "vendor"){
		mainMenuItems[mainMenuItems.length] = {label: 'Vendor Navigation', url: '/vendor_login/vendor_nav.aspx'};
	}else if(o.userType == "client"){
		mainMenuItems[mainMenuItems.length] = {label: 'Client Navigation', url: '/client_login/client_nav.asp'};
	}else if(o.userType == "unified"){
        mainMenuItems[mainMenuItems.length] = { label: 'My Company', url: '/mvc/Company/MyCompany' };
    } else if (o.userType == "employee") {
        mainMenuItems[mainMenuItems.length] = { label: 'Employee Navigation', url: '/mvc/User' };
	}else if(o.userType == "asset manager"){
	}else if(o.userType == "portfolio manager"){
	}else if(o.userType == "premarketer"){
	}


	$('#pageMenu').menu({items: mainMenuItems});

	document.write('		<div class="pageContents" style="z-index:0">');	
	document.write('		<label id="ExpirationDays" class="fieldset-legend" style="width:600px;"></label>');
	
	// To prevent click-jacking. 
	if (parent.frames.length > 0) { 
		top.location.replace(document.location); 
		setTimeout(function() { if (parent.frames.length > 0) document.location = "http://www.google.com"; }, 10); 
	} 

}

function getProfileUrl(userType, legacyUserId, genericUserId){
	if(userType == "agent")
		return "/agent_login/agent_view_profile.asp";
	else if(userType == "client")
		return "/client_login/client_view_profile.asp";
	else if(userType == "unified")
		return "/mvc/User/Details/" + genericUserId;
	else
		return null;
}


function writeFooter() {
    String.prototype.repeat = function (num) {
        return new Array(num + 1).join(this);
    }

	document.write('</div>');
	document.write('<div class="pageFooter">');
	document.write('	<span class="pageFooterCopyright">');
	document.write('		Copyright &copy; ' + new Date().getFullYear() + ' Keystone Asset Management, Inc. All Rights Reserved.');
	document.write('	</span>');
	document.write('	<span class="pageFooterLinks">');
	document.write('		<a href="/PrivacyStatement.pdf">Privacy Statement</a>');
	
	document.write('	</span>');
	document.write('</div>');
	document.write('</div>');
	document.write('</div>');
}


