// Definable Parameters
var HOME_WELCOME_TITLE = "Welcome to Allegan County";
var DRAFT_REVIEW_TEXT = "Draft (In Review)";
var HOME_LINK_LABEL = "Allegan County";
var BREAD_CRUMB_SEPARATOR = "<span class='pipe'> | </span>";
var IMAGE_DIR_NAME = "images";
var INDEX_FILENAME = "index.asp";
var BACK_ARROW = "&laquo; ";

// Definable Lookup maps (use commas to seperate each map object in array)
// Syntax: new TranslationMap("dirname", "The Real Title")
var departmentMap = new Array(
	
	// Main Areas
	new TranslationMap("living", "Living"),
	new TranslationMap("working", "Working"),
	new TranslationMap("visiting", "Visiting"),
	new TranslationMap("business", "Doing Business"),
	new TranslationMap("government", "Government")
);
var divisionMap = new Array(

	// County Administration Divisions
	new TranslationMap("desc", "Drainage, Erosion and Sediment Control")

);	

var filenameMap = new Array(
	//new TranslationMap("Some_Page.html", "New Title Here")
);

// Programmatic Constants (should not be modified, otherwise may break logic)
var SLASH_SEPARATOR = "/";
var DEPARTMENT_TRANSLATION = 0;
var DIVISION_TRANSLATION = 1;
var FILENAME_TRANSLATION = 2;

var TOP_TYPE = "top";
var LANDING_TYPE = "landing";
var DEPARTMENT_TYPE = "department";
var DIVISION_TYPE = "division";

var DEBUG_STRING = "#debug";
var DEBUG_FLASH_STRING = "#flashDebug";

/**
 * Parse location url into all necessary page attributes
 * 
 * Syntax of url parameter:
 *	http://[page context]/[department dir]/[division dir]/[file name]
 *
 * Example Usage:
 *	var pageInfo = new PageInfo(location.href);
 *	alert(pageInfo.value);
 *
 * @param url	The url to use as reference (should be populated by location.href)
 */
function parseUrl(url) {
	// Strip out any existing anchor
	if (url.indexOf("#") > -1) {
		url = url.substring(0, url.indexOf("#"));
	}
	
	// Parse Root Context
	var firstPart = url.substring(0, url.indexOf("//"));
	var lastPart = url.substring(url.indexOf("//") + 2, url.length);
	if (lastPart.indexOf("/") == -1) {
		lastPart += "/";
	}
	var rootContext = firstPart + "//" + lastPart.substring(0, lastPart.indexOf("/"));
	
	var status = "";
	
	// Parse department and division directories
	var department = replaceNull(url.split("/")[3]);
	var division = replaceNull(url.split("/")[4]);
	
	status = "N/A";
	
	if (isIndex(department) || department.indexOf(".") > -1) {
		department = "";
	}
	
	// Parse filename
	var fileName = url.substring(url.lastIndexOf("/") + 1, url.length);
	if (fileName != null && fileName.indexOf(".") > -1) {
		if (fileName == division) { 
			division = "";
		} 
	} else {
		fileName = INDEX_FILENAME;
	}
	
	// Calculate image dir
	var imagePath = addSeparator(rootContext, SLASH_SEPARATOR) + 
		addSeparator(department, SLASH_SEPARATOR) + 
		addSeparator(division, SLASH_SEPARATOR) + IMAGE_DIR_NAME;
	
	// Title Translations
	var departmentTitle = translate(department, DEPARTMENT_TRANSLATION);
	var divisionTitle = translate(division, DIVISION_TRANSLATION);
	var pageTitle = translate(fileName, FILENAME_TRANSLATION);
	
	// Calculate Department & Division URLs
	var departmentUrl = "";
	if (department.length > 0) {
		departmentUrl = addSeparator(rootContext, SLASH_SEPARATOR) +
			addSeparator(department, SLASH_SEPARATOR);
	}
	
	var divisionUrl = "";
	if (departmentUrl.length > 0 && division.length > 0) {
		divisionUrl = departmentUrl + addSeparator(division, SLASH_SEPARATOR);
	}
	
	// Search Functionality
	if (department == "search") {
		if (fileName == "advanced.jsp") {
			pageTitle = "Advanced Search";
		} else {
			pageTitle = "Search";
		}
		
		department = "";
		division = "";
		fileName = "search.html";
	}
	
		
	// Parse Department / Division Navigation (breadcrumb)
	var navigation = "";
	var sep = BREAD_CRUMB_SEPARATOR;
	
	var type = DEPARTMENT_TYPE;
	
	if (exists(division)) {
		// << Department | Division
		navigation += "<a class='pageTitle' href=\"" + departmentUrl + "\">" + BACK_ARROW + replaceNbsp(departmentTitle) + "</a>";
		if (isIndex(fileName)) {
			navigation += sep + replaceNbsp(divisionTitle);
		} else {
			// << Department | Division | Page Title
			navigation += sep + "<a class='pageTitle' href=\"" + divisionUrl + "\">" + replaceNbsp(divisionTitle) + "</a>";
			navigation += sep + replaceNbsp(pageTitle);
		}
		type = DIVISION_TYPE;
	} else {
		if (exists(department)) {
			// Department
			if (!isIndex(fileName)) {
				navigation += "<a class='pageTitle' href=\"" + departmentUrl + "\">" + BACK_ARROW + replaceNbsp(departmentTitle) + "</a>";
			} else {			
				navigation += replaceNbsp(departmentTitle);
			}
			
			if (!isIndex(fileName)) {
				// << Department | Page Name
				navigation += sep + replaceNbsp(pageTitle);
			}
			
			if (isLandingPage(department)) {
					type = LANDING_TYPE;
			}
		} else {
			type = TOP_TYPE;
			if (isIndex(fileName)) {
				// Welcome To Allegan County
				navigation += HOME_WELCOME_TITLE;
			} else {
				// << Allegan County | Page Title
				navigation += "<a class='pageTitle' href=\"" + rootContext + "\">" + BACK_ARROW + replaceNbsp(HOME_LINK_LABEL) + "</a>";
				navigation += sep + replaceNbsp(pageTitle);
			}
		}
	}
	
	// Post-tweaks for proper search page breadcrumbs
	if (pageTitle == "Search") {
		fileName = "";
		url = rootContext + "/index.html";
		departmentUrl = "";
		departmentTitle = "";
		imagePath = rootContext + "/images";
		navigation = replaceNbsp(pageTitle);
	} else if (pageTitle == "Advanced Search") {
		fileName = "";
		url = rootContext + "/index.html";
		departmentUrl = "";
		departmentTitle = "";
		imagePath = rootContext + "/images";
		var simpleSearchUrl = rootContext + "/search/index.jsp";
		navigation = "<a class='pageTitle' href=\"" + simpleSearchUrl + "\">" + BACK_ARROW + replaceNbsp("Simple Search") + "</a>" + sep + replaceNbsp(pageTitle);
	}
	
	if (department == "MMWIP") {
		departmentTitle = DRAFT_REVIEW_TEXT;
		divisionTitle = "";
		pageTitle = "";
		navigation = DRAFT_REVIEW_TEXT;
	}
	
	// Contruct and return the populated PageAttributes object
	return new PageParams(url, rootContext,department,departmentUrl,departmentTitle,
		division,divisionUrl,divisionTitle,fileName,pageTitle,navigation,imagePath,type,status);
}

/**
 * PageParams Object
 *
 * Populated with parseUrl function and the current page URL (i.e. location.href)
 */
function PageParams(url,rootContext,department,departmentUrl,departmentTitle,division,
		divisionUrl,divisionTitle,fileName,pageTitle,navigation,imagePath,type, status) {
		
	this.url = url;
	this.rootContext = rootContext;
	this.department = department;
	this.departmentUrl = departmentUrl;
	this.departmentTitle = departmentTitle;
	this.division = division;
	this.divisionUrl = divisionUrl;
	this.divisionTitle = divisionTitle;
	this.fileName = fileName;
	this.pageTitle = pageTitle;
	this.navigation = navigation;
	this.imagePath = imagePath;
	this.type = type;
	
	this.value = "url = " + url +
		     "\nrootContext = " + rootContext + 
		     "\ndepartment = " + department + 
		     "\ndepartmentUrl = " + departmentUrl + 
		     "\ndepartmentTitle = " + departmentTitle + 
		     "\ndivision = " + division +
		     "\ndivisionUrl = " + divisionUrl +
		     "\ndivisionTitle = " + divisionTitle +
		     "\nfileName = " + fileName +
		     "\npageTitle = " + pageTitle +
		     "\nnavigation = " + navigation +
		     "\nimagePath = " + imagePath +
			 "\ntype = " + type +
			 "\nstatus = " + status;
}

/**
 * TranslationMap Object
 * Contains either a department, division, or page title lookup map item
 */
function TranslationMap(key, value) {
	this.key = key;
	this.value = value;
}

//////////////////////
// Utility fuctions //
//////////////////////
function isLandingPage(theValue) {
	theValue = theValue.toLowerCase();
	if (theValue == "living" || 
		theValue == "working" ||
		theValue == "visiting" ||
		theValue == "business" ||
		theValue == "government") {
		return true;
	}
	return false;
}

function translate(key, transType) {
	var map = null;
	
	if (transType == DEPARTMENT_TRANSLATION) {
		map = departmentMap;
	} else if (transType == DIVISION_TRANSLATION) {
		map = divisionMap;
	} else if (transType == FILENAME_TRANSLATION) {
		map = filenameMap;
	}
	
	for (var i=0;i<map.length;i++) {
		var item = map[i];
		if (item.key == key) {
			return item.value;
		}
	}
	
	if (transType == FILENAME_TRANSLATION) {
		// No match found, translate special filename character (if not index page)
		if (!isIndex(key)) {		
			key = key.replace(/\$n/g, "&");
			key = key.replace(/_/g, " ");
			key = key.replace(/\$qq/g, "\"");
			key = key.replace(/\$q/g, "?");
			key = key.replace(/\$c/g, ",");
			// Remove Extension
			key = key.substring(0, key.lastIndexOf("."));
			
			return key;
		}
		
		// This is an index page, don't do anything with it
		return "";
	}
	
	return key;
}

/**
 * Check if the given file name is an index file
 */
function isIndex(fileName) {
	if (fileName == null || fileName.toLowerCase() != INDEX_FILENAME) {
		return false;
	}
	
	return true;
}

function addSeparator(theValue, sep) {
	if (theValue != null && theValue!= "") {
		theValue += sep;
	}
	return theValue;
}

function replaceNbsp(theValue) {
	if (theValue != null) {
		theValue = theValue.replace(/ /g, "&nbsp;");
	}
	return theValue;
}

function replaceNull(theValue) {
	if (theValue == null) {
		theValue = "";
	}
	return theValue;
}

function exists(theValue) {
	if (theValue == null || theValue.length == 0) {
		return false;
	}
	return true;
}

///////////////////////////////////////////////////////////////////////////////////////////////

function loadPageContent() {
	var url = location.href;
	
	var debug = false;
	
	if (url.indexOf(DEBUG_STRING) > -1) {
		debug = true;
	}
	
	if (url.indexOf("#") > -1) {
		url = url.substring(0, url.indexOf("#"));
	}
	
	var pageParams = parseUrl(url);
	if (debug) {
		populate("target_flash", getDebugCode(pageParams.value));
	} else {
		populate("target_flash", getFlashMarkup(pageParams));
	}
	populate("target_deptHeirarchyNav", pageParams.navigation);
		
	// (Main template divs)
	// target_weather
	// target_dailyPhoto
	
	MM_preloadImages('/images/ExpressConnect_over.gif');
}

function populate(targetContentAreaId, content) {
	var targetObj = findObj(targetContentAreaId, document);
	if (targetObj == null) {	
		//alert("Temporary Warning:  Page " + location.href + " doesn't currently have a div called " + targetContentAreaId);
	} else {
		document.getElementById(targetContentAreaId).innerHTML = content;
	}
}

/**
 * Get the flash markup for a given PageAttribute object
 */
function getFlashMarkup(pageParams) {
	var department = pageParams.departmentTitle;
	var division = pageParams.divisionTitle;
	var type = pageParams.type;
				
	// Debug
	var debug = false;
	if (location.href.indexOf(DEBUG_FLASH_STRING) > -1) {
		debug = true;
	}
		
	// Render markup
	var html = 
		'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"\n' +
		'	codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"\n' +
		'	width="770" height="140" hspace="0" vspace="0">\n\n' +
		'	<param name="allowScriptAccess" value="sameDomain" />\n' +
		'	<param name="movie" value="/images/TopHeaderImages.swf">\n' +
		'	<param name="quality" value="high">\n' +
		'	<param name="flashvars" value="department=' + escape(department) + '&division=' + escape(division) + '&type=' + escape(type) + '">\n' +
		'	<embed\n' +
		'		src="/images/TopHeaderImages.swf?department=' + escape(department) + '&division=' + escape(division) + '&type=' + escape(type) + '"\n' +
		'		width="770" height="140" hspace="0" vspace="0" quality="high" allowScriptAccess="sameDomain"\n' +
		'		pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash">\n' +
		'	</embed>\n' +
		'</object>';
	
	if (debug) {
		return getDebugCode(html);
	}
	return html;
}

function getDebugCode(theValue) {
	var debugStartTag = '<textarea rows=18 cols=121>';
	var debugEndTag = '</textarea>';
	return debugStartTag + theValue + debugEndTag;
}
	
// Utility functions for finding page objects, using the DOM, and working with layers
function findObj(n, d) { 
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);



function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}





