// Menu control

function displayMenuPages(section, page) {
	
	// Change the selected section
	if (section>0) $('#MenuSection'+section).toggleClass("MenuSectionItem MenuSectionItemSelected");
	if (thisSection>0) $('#MenuSection'+thisSection).toggleClass("MenuSectionItem MenuSectionItemSelected");

	thisSection=section;	
	
	// Change the displayed pages
	if (section>0) {
		var html = '';
		numPages = MenuSections[section]['numsubs'];
		for (var i=1;i<=numPages;i++) {
			// If this is the current page, change the style
			if (page == MenuPages[section][i]['name'])
				html += "<a href='"+MenuPages[section][i]['menupagelink']+"'><span class='MenuPageItemSelected'>"+MenuPages[section][i]['displaytext']+"</span></a>";
			else
				html += "<a href='"+MenuPages[section][i]['menupagelink']+"'><span class='MenuPageItem'>"+MenuPages[section][i]['displaytext']+"</span></a>";
		}
		$('#MenuPages').html(html);
	}
	
	// Refresh the Cufon fonts
	Cufon.replace('.MenuSectionItem, .MenuSectionItemSelected, .MenuPageItem, .MenuPageItemSelected', {
		hover: true,
		hoverables: { span: true }
	});
}


function displayMenuSections(numSections, thisSection, thisPage) {

	sectionId = 0;
	for (var i=1;i<=numSections;i++) {
		// If not subpages, link directly to first page. Otherwise link to function which displays the subpages
		if (MenuSections[i]['numsubs'] == 0) {
			$('#MenuSections').append("<a href='"+MenuSections[i]['link']+"'><span class='MenuSectionItem' id='MenuSection"+i+"'>"+MenuSections[i]['displaytext']+"</span></a>");
		} else {
			$('#MenuSections').append("<span class='MenuSectionItem' id='MenuSection"+i+"' onClick='displayMenuPages("+i+",\""+thisPage+"\")'>"+MenuSections[i]['displaytext']+"</span>");
		}
		if (MenuSections[i]['title'] == thisSection) sectionId = i;
	}
	displayMenuPages(sectionId, thisPage);
}	

// Check for mobile browser
function isMobileBrowser() {
	if (
		(navigator.userAgent.indexOf('iPhone') != -1) || 
		(navigator.userAgent.indexOf('iPad') != -1) ||	
		(navigator.userAgent.indexOf('Blackberry') != -1) || 
		(navigator.userAgent.indexOf('Android') != -1)
		) 
		return(true);
	else
		return(false);
}

// Open pop-up window
function openWin(URL){
    aWindow=window.open(URL,"","width=850,height=650,status=yes,scrollbars=yes,toolbar=no,menubar=no,resizable=yes,top=1,left=1");
}
function openRadioWin(URL){
    aWindow=window.open(URL,"","width=220,height=250,status=no,scrollbars=no,toolbar=no,menubar=no,resizable=no,top=1,left=1");
}

