var currentPath = getPath();
var sitePath = getSitePath();
var retUrlPath = window.location.href.substring(0,window.location.href.lastIndexOf('/')+1);
var isIndexPage = currentPath==(sitePath+"index.html");
var isIE = /*@cc_on!@*/false;
var flagsDropdown = null;
var solutions = null;
var mapUrlPrefix = "http://maps.google.com/maps?f=q&source=embed&hl=en&geocode=&q=";
var mapUrlSuffix = "&z=14&iwloc=near";

// NEWS ITEM PARAMETERS: 
// "title" - Main heading
// "date" - OPTIONAL: If the event has a date, put it here (no date format, any text permitted). It will appear in a highlighted colour. 
// "image" - OPTIONAL: File name of the news image in IMAGES/NEWSIMAGES. Images must be 64x64 pixels (any format). A default image is displayed if no image specified
// "text" - Details of the story
// "href" - URL of the news story
// "direct" - When "direct":"true" is specified, clicking the news item on the front page will take you straight there (rather than going to the general news page first) 

var newsItems = [
	//{"title":"CALUMO 11.8", "date":"", "image":"11-8.png", "text":"Our latest release includes full cross-browser support, Report Vault, Private Reports, Smart Tabs and many more exciting new features.", "href":sitePath+"newfeatures/index.html","direct":"true"},
		{"title":"Breaking News", "date":"", "image":"", "text":"We've announced our platform of choice for the future of BI and Planning. It's unequivocally Microsoft.", "href":"http://www.calumo.com/comms/news/"},
		{"title":"Breaking News", "date":"", "image":"", "text":"The future of CALUMO software is Microsoft. What does this mean for our TM1 customers?", "href":"http://www.calumo.com/comms/news/tm1users.html"},
		{"title":"Breaking News", "date":"", "image":"", "text":"Why Microsoft? and Why now?", "href":"http://www.calumo.com/comms/news/why.html"},
		{"title":"CALUMO Education Whitepaper", "date":"", "image":"whitepaper.png", "text":"Higher Education Providers are facing commercial challenges that have a lot in common with business. This paper provides a framework, from a Business Intelligence perspective, for considering the effective investment of institutional focus and energy in responding to the intensified demands of commercial-like competition in the Higher Education market.", "href":"http://www.calumo.com/education/"},
	{"title":"CALUMO for Education", "date":"", "image":"msblog.png", "text":"Ray Fleming (Microsoft) talks about CALUMO education case studies in his Education Blog.", "href":"http://blogs.msdn.com/b/education/archive/2011/05/17/business-intelligence-for-universities.aspx"},
	{"title":"IBM Cognos 10 Intelligence Unleashed", "date":"", "image":"ibm_cognos10.png", "text":"IBM's Cognos 10 gives decision-makers broader access to data in a more interactive and intuitive analytical workspace. It also leverages better caching and query management to deliver results faster.", "href":"http://www-01.ibm.com/software/analytics/cognos/cognos10/"},
	{"title":"CALUMO Builds Customer Satisfaction",  "image":"graph_up.png", "text":"CALUMO is a Microsoft GOLD partner, a status that requires continued levels of excellence. The latest independent survey of CALUMO's customer satisfaction shows continued positive feedback.  Click ''Learn More'' to see the results.", "href":sitePath+"pdf/MS NSAT Results_CALUMO.pdf"},
	{"title":"CALUMO Training Academy", "date":"", "image":"light_bulb.png", "text":"The CALUMO Academy course offerings have been ramping up! We now offer our clients (and potential clients) the most professional and comprehensive courses for mastering CALUMO, TM1, Excel, MDX, Advanced Reporting and Analysis Services Cube Design.", "href":sitePath+"training.html","direct":"true"},
	{"title":"Microsoft SharePoint 2010 Out Now", "date":"", "image":"sharepoint.png", "text":"SharePoint 2010 is now out of BETA.  Check out what's new and download the free trial to experience the benefits.", "href":"http://sharepoint.microsoft.com"},
	{"title":"Introducing IBM Cognos Express", "date":"", "image":"ibm_logo.png", "text":"CALUMO is very excited to be partnering with IBM in support of the recently released IBM Cognos Express.", "href":"http://www-01.ibm.com/software/data/cognos/products/cognos-express/"}
	
];

// MENU ITEM PARAMETERS: 
// "name" - Text that will appear on your tab/menu item
// "level" - "1":Main tabs  "2":Left-panel options (not on main page)  "3":Indented left-panel option (excluded from site map in page footer)
// "href" - Relative page URL 
// "hidden":"true" - OPTIONAL: Allows a page to be hidden from the menus of all other pages. Used mainly with "thank you" pages which appear only after submitting a form.
// NOTE: If browsing to a CALUMO page that was not added to menuItems, it will appear under a "VIP Area" menu item in the COMPANY tab.

var menuItems = [
	{"name":"SOLUTIONS", "level":"1", "href":"benefits_managers.html"},
		{"name": "How We Can Help", "level":"2" , "href" : "solutions.html", "hidden":"true"},
		{"name": "Benefits", "level":"2" , "href" : "benefits_managers.html"},
		{"name": "For Managers", "level":"3" , "href" : "benefits_managers.html"},
		{"name": "For Sales/Marketing", "level":"3" , "href" : "benefits_sales.html"},
		{"name": "For Finance", "level":"3" , "href" : "benefits_finance.html"},
		{"name": "For IT", "level":"3" , "href" : "benefits_it.html"},
		{"name": "For HR", "level":"3" , "href" : "benefits_hr.html"},
		{"name": "White Paper Registration", "level":"3" , "href" : "education/education_paper.html", "hidden":"true"},
		{"name": "White Paper Registration", "level":"3" , "href" : "education/educationwp_thankyou.html", "hidden":"true"},
		{"name": "Solution Benefits", "level":"2" , "href" : "benefits_ra.html"},
		{"name": "Reporting & Analysis", "level":"3" , "href" : "benefits_ra.html"},
		{"name": "Planning & Forecasting", "level":"3" , "href" : "benefits_pf.html"},
		{"name": "Dashboards & KPIs", "level":"3" , "href" : "benefits_kpi.html"},
		{"name": "Testimonials", "level":"2" , "href" : "testimonials.html"},
		{"name": "Case Studies", "level":"2" , "href" : "case_studies.html"},
	{"name":"SERVICES", "level":"1", "href":"support.html"},
		{"name":"Overview", "level":"2", "href":"support.html"},
		{"name":"Consulting", "level":"2", "href":"consulting.html"},
		{"name":"Training Courses", "level":"2", "href":"training.html"},
		{"name":"Book Now", "level":"3", "href":"training_registration.html"},
		{"name":"Details Received", "level":"3", "href":"training_response.html", "hidden":"true"},
		{"name":"Venues", "level":"3", "href":"training_venues.html"},
		{"name":"Customer Support", "level":"2", "href":"support_online.html"},
	{"name":"TECHNOLOGY", "level":"1", "href":"tech_overview.html"},
		//{"name": "What's New", "level":"2" , "href" : "newfeatures/index.html"},
		{"name": "Product Overview", "level":"2" , "href" : "tech_overview.html"},
		{"name": "CALUMO for SSAS", "level":"2" , "href" : "tech_calumo_ssas.html"},
		{"name": "CALUMO Skylights", "level":"2" , "href" : "skylights/index.html"},
		{"name": "Overview", "level":"3" , "href" : "skylights/info.html"},
		{"name": "FAQ", "level":"3" , "href" : "skylights/faq.html"},
		{"name": "Brochure", "level":"3" , "href" : "skylights/Skylights brochure web.pdf"},
		{"name": "Demos and Trials", "level":"3" , "href" : "skylights/registration_form.html"},
		{"name": "Enquiry Received", "level":"3" , "href" : "skylights/response.html","hidden":"true"},
		{"name": "CALUMO Sparks", "level":"2" , "href" : "tech_calumo_sparks.html"},
		{"name": "BI Platform", "level":"2" , "href" : "partners_microsoft.html"},
	{"name":"COMPANY", "level":"1", "href":"company.html"},	
		{"name": "News", "level":"2" , "href" : "news.html"},	
		{"name": "About Us", "level":"2" , "href" : "company.html"},
		{"name": "Our Name", "level":"3" , "href" : "company_name.html"},
		{"name": "Executive Team", "level":"3" , "href" : "company_exec_team.html"},
		{"name": "Community Support", "level":"3" , "href" : "company_community.html"},
		{"name": "Contact Us", "level":"2" , "href" : "contacts.html"},
		{"name": "Enquiry Form", "level":"3" , "href" : "contact_form.html", "hidden":"true"},
		{"name": "Enquiry Received", "level":"3", "href":"contact_response.html", "hidden":"true"},
		{"name": "Careers", "level":"2" , "href" : "company_careers.html"}	
];

function init() {
	buildHeader();
	buildMenus();
	buildFooter();
	addPageEnd();
	populateVars();
	bindEvents();
	$(window).load(function() {
		if (isIndexPage) {
			$('body').fadeIn(700);
		} else {
			$('body').show();
		}
		if ($('#firstField').length>0) { $('#firstField').focus(); }
	});
}

function getSitePath() {
	if (window.location.hostname.toLowerCase()=='forums.calumo.com') {
		return '/demo/';
	}
	if (startsWith(window.location.pathname.toLowerCase(),'/c:/calumowebsite/')) {
		return 'file://c:/calumowebsite/live/';
	}
	return '/';
}

function bindEvents() {
	var me = this;
	$("#flags").bind("mouseover", function() { me.showContactsDropdown(); });
	$("#flags").bind("mouseout", function() { me.hideContactsDropdown(); });
}

function populateVars() {
	$('.mainPhone').html('+61 2 8985 7777');
	if (solutions!=null) {
		buildScenarios();
	}
}

function buildHeader() {
	$("#header").append('<div id="headeroptions"></div>'); // Removed <div id="search" style="display:none"><input id="searchinput" type="text" value="Search"/><input id="searchbutton" type="submit" value=""/></div>
	$("#header").append('<a class="blogLink" target="_blank" href="http://www.calumo.com/blog"></a>');
	$("#headeroptions").append('<div id="flagsLabel">Contact:</div><div id="flags"></div>');
	$("#header").append('<a href="'+sitePath+'index.html" id="logobutton"></a><table id="menu"><tr></tr></table>');
	createContactsDropdown();
}

function addPageEnd() {
	$("body").append("<div id='page_end'></div>");
}

function buildFooter() {
	if (isIndexPage) {
		$("#footer").addClass("indexPageFooter");
	}
	else {
		$("#footer").addClass("footerGradient");
	}
	$("#footer").append('<div id="footercontent"><div id="sitemap"></div></div>');
	var d = new Date();
	$("#footer").append('<div id="copyright">Copyright &copy;'+ d.getFullYear() +' CALUMO. All rights reserved</div>');
	$("#copyright").addClass("copyrightBorder");
	buildSiteMap();
}

function buildMenus() {
	var subMenu = $('#menu_panel');
	var mainMenu = $('#menu tr');
	var selectedMainItemIndex;
	var isIndexPage = subMenu.length==0 ? " style='visibility:hidden;'" : "";
	var tdClass = isIndexPage ? "" : " class='homeIcon'";
	mainMenu.append("<td" + tdClass + "><a id='homebutton' href='"+sitePath+"index.html'" + isIndexPage + ">&nbsp;&nbsp;</a></td>");

	// Find which main menu item the current page lives under
	for (index in menuItems) {
		if (menuItems[index].level=='1') {
			selectedMainItemIndex = index;
		}
		if ((sitePath+menuItems[index].href.toLowerCase())==currentPath) {
			break;
		}
	}
	
	// Populate main menu items, plus sub menu items for selected main menu item
	var mainItemFound = false;
	var tabIndex = 0;
	var tabClass="";
	for (index in menuItems) {
		var item = menuItems[index];
		var nextItem = menuItems[Number(index)+1];
		var nextHref = nextItem == null ? "":nextItem.href.toLowerCase();
		if (index == selectedMainItemIndex) {
			mainItemFound=true;
		}
		else if (mainItemFound && item.level=='1') {
			mainItemFound = false;
		}
		if (item.level=='1') {
			tabIndex++;
			if (index==selectedMainItemIndex && !isIndexPage) {
				tabClass="tab"+tabIndex;
				mainMenu.append("<td id='tab" + tabIndex + "' class='currenttab'>" + item.name + "</td>");
			} 
			else {
				mainMenu.append("<td id='tab" + tabIndex + "'><a href='" + sitePath + item.href + "'>" + item.name + "</a></td>");
			}
		}
		if (mainItemFound && !isIndexPage) {
			var element;
			if ((sitePath+item.href.toLowerCase()) == currentPath && item.level!='1' && item.href.toLowerCase()!=nextHref) {
				element = $("<div class='currentitem" + (item.level == '3' ? " indent":"") + "'>" + item.name + "</div>").appendTo(subMenu);
			}
			else if (item.hidden == null && item.level!='1') {
				element = $("<a " + (item.level == '3' ? "class='indent'":"") +  (item.href.indexOf("http:")==0 || item.href.toLowerCase().indexOf(".pdf")>=0 ? " target='_blank'" : "" ) + " href='" + sitePath + item.href + "'>" + item.name + "</a>").appendTo(subMenu);
			}
		}
	}
	if (tabClass!="") {
		$("#menu").addClass(tabClass);
		mainMenu.append("<td></td>"); // Adds some space to the right for the tab shadow
	}
	if ($('#menu_panel .currentitem').length==0) {
		$("<div class='currentitem'>VIP Area</div>").appendTo(subMenu);
	}
}

function initFormValidation() {
	highlightRequiredFields();
	$('.required').bind('keyup',function() { highlightRequiredFields();});
}

function highlightRequiredFields() {
	// Used for validation on entry forms
	$('.required').each(function() {
			var val = $(this).val();
			if (val.length<1) {
				$(this).addClass('missing');
			} else {
				$(this).removeClass('missing');
			}
		}
	);
}

function buildSiteMap() {
	containerElement = $('#sitemap');
	var groupElement;
	for (index in menuItems) {
		var item = menuItems[index];
		if (item.level==1) {
			groupElement = $("<div class='siteMapGroup'></div>").appendTo(containerElement);
		}
		if (item.level<3 && item.hidden == null) {
			var classHtml = item.level==1 ? "class='siteMapHeading'" : "";
			groupElement.append("<a " + classHtml + " href='" + sitePath + item.href + "'>" + item.name + "</a>");
		}
	}
}

function getPath() {
	var path = window.location.pathname.toLowerCase();
	if (path.substr(path.length-1,1)=="/") {  
		path = path + "index.html";
	}
	if (path.substr(0,4)=="/c:/") {  
		path = "file:/" + path;
	}	
	return path;
}

function createContactsDropdown() {
	if (flagsDropdown==null) {
		var html =  "\
			<div class='contactBox' style='display:none'>\n\
				<div class='contactTop'>Contact us:<p id='closeIfHoveringOverThisBit'></p></div>\n\
				<div class='contactMiddle'>\n\
					<table style='width:100%'>\n\
						<tr><td><a href='" + sitePath + "contacts.html'>Sydney</a></td><td style='color:black' class='phone'><span class='intcode'>+61</span> &nbsp;<span style='display:none;'>NoSkype</span>2 8985 7777</td></tr>\n\
						<tr><td><a href='" + sitePath + "contacts.html'>Melbourne</a></td><td class='phone'><span class='intcode'>+61</span> &nbsp;3 9865 6400</td></tr>\n\
						<tr class='contactBorderBottom'><td><a href='" + sitePath + "contacts.html'>Brisbane</a></td><td class='phone'><span class='intcode'>+61</span> &nbsp;7 3367 9300</td></tr>\n\
						<tr class='contactBorderTop contactBorderBottom'><td><a href='" + sitePath + "contacts.html'>North America</a></td><td class='phone'><span class='intcode'>&nbsp;+1</span> 877 572 <span style='display:none;'>NoSkype</span>4434</td></tr>\n\
						<tr class='contactBorderBottom contactBorderTop'><td colspan='2'><a href='" + sitePath + "contacts.html'>Office Details</a></td></tr>\n\
						<tr class='contactBorderTop'><td colspan='2'><a href='" + sitePath + "contact_form.html'>Submit an Enquiry</a></td></tr>\n\
					</table>\n\
				</div>\n\
				<div class='contactBottom'></div>\n\
			</div>\n\
";
		flagsDropdown = $(html).appendTo("#flags");
		var me = this;
		$("#closeIfHoveringOverThisBit").bind("mouseover", function(e) { me.hideContactsDropdown(e); });
	}
}

function showContactsDropdown() {
	flagsDropdown.show();
}

function hideContactsDropdown(e) {
	flagsDropdown.hide();
	if (e!=null)
		e.stopImmediatePropagation();
}

function getDayText (day) {
	var weekday=new Array(7);
	weekday[0]="Sunday";
	weekday[1]="Monday";
	weekday[2]="Tuesday";
	weekday[3]="Wednesday";
	weekday[4]="Thursday";
	weekday[5]="Friday";
	weekday[6]="Saturday";
	return weekday[day];
}
function getMonthText (m) {
	var month=new Array(12);
	month[0]="January";
	month[1]="February";
	month[2]="March";
	month[3]="April";
	month[4]="May";
	month[5]="June";
	month[6]="July";
	month[7]="August";
	month[8]="September";
	month[9]="October";
	month[10]="November";
	month[11]="December";
	return month[m];
}
function rightString(text,pos) {
	return text.substr(text.length - pos);
}

function startsWith(text,startsWith) {
	return text.substring(0,startsWith.length)==startsWith;
}

function disableSelection(target){ 
	if (typeof target.onselectstart!="undefined") //IE route 
	{ 
		target.onselectstart=function(){return false};
	}
	else if (typeof target.style.MozUserSelect!="undefined") //Firefox route 
	{
		target.style.MozUserSelect="none";
	}
	else //All other route (ie: Opera) 
	{
		target.onmousedown=function(){return false};
		target.style.cursor = "default";
	}
}

function validateForm(responsePage) {
	highlightRequiredFields();
	if ($('.missing').length>0) {
		alert('FORM INCOMPLETE.\n\nPlease complete the blue boxes.\n\n');
		$('.missing:first').focus();
		return false;
	}
	if ($('#captcha').length==0) {
		$("form").append("<center id='captcha_gray_background'><div id='captcha'><div class='title'>To ensure you are a real person...</div><span>How many rings in the CALUMO logo?</span><input type='input' id='captcha_question' /><input type='submit' value='OK' name='captcha_submit' id='captcha_submit' style='padding-bottom: 2px; border: 0px none; background-color: white;' class='subpageSmallButton3' /></div></center>");
		$('#captcha_question').focus();
	}
	else if ($('#captcha_gray_background').css('display')=='none') {
		$('#captcha_gray_background').show();
		$('#captcha_question').focus();
	}
	else {
		var a = $('#captcha_question').val().toUpperCase();
		var failed = (a != '3' && a != 'THREE');
		$('#captcha_gray_background').hide();
		if (failed) { 
			alert('Incorrect answer.');
		}
		else {
			var page = responsePage == null ? 'contact_response.html' : responsePage;
			$('#returnUrl').val(retUrlPath + page);
			return true;
		}
	}
	return false;
}

function getMapUrlFromAddress(address, embed) {
	if (address.indexOf(',')==-1) return "";
	return mapUrlPrefix + address.replace(/ /g, "+") + mapUrlSuffix + (embed==true ? "&output=embed" : "");
}
