// Set top search box coloring
var searchBox = new YAHOO.util.Element('searchBox');
searchBox.on('focus', function(e) { searchBox.set('value', ''); searchBox.setStyle('color', '#000'); });

// Open a new window
function newWin(loc, width, height, name, opt) {
	var width = width ? width : 850;
	var height = height ? height : 500;
	var opt = opt ? opt : 'width=' + width + ',height=' + height + ',toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes';
	var name = name ? name : 'pewaOpenWin'; 
	var newWin = window.open(loc, name, opt);
	newWin.focus();
	return newWin;
}




// Various methods to work with the global javascript array catSeriesList - which holds all information about categories and related series

// Get an array with series names in it
function getSeries() {
	var seriesArr = [];
	if(catSeriesList) {
		for(var category in catSeriesList) {
			for(var x = 0; x < catSeriesList[category].series.length; ++x) {
				seriesArr[seriesArr.length] = catSeriesList[category].series[x];
			}
		}
	}
	var sortFn = function(a, b) {
		return a.seriesName > b.seriesName ? 1 : -1;
	};
	seriesArr.sort(sortFn);
	return seriesArr;
}

// Filter the series retrieval function above to just get the series name
function getSeriesNamesOnly() {
	var seriesArr = getSeries();
	var seriesNamesArr = [];
	for(var x = 0; x < seriesArr.length; ++x) {
		seriesNamesArr[seriesNamesArr.length] = seriesArr[x].seriesName;
	}
	return seriesNamesArr;
}

// Get an array containing series from a specific category id
function getSeriesByCategoryId(categoryId) {
	var seriesArr = [];
	if(catSeriesList) {
		var catSeries = catSeriesList[categoryId];
		if(catSeries) {
			catSeries = catSeries.series;
			for(var x = 0; x < catSeries.length; ++x) {
				seriesArr[seriesArr.length] = catSeries[x];
			}
		}
	}
	var sortFn = function(a, b) {
		return a.seriesName > b.seriesName ? 1 : -1;
	};
	seriesArr.sort(sortFn);
	return seriesArr;	
}

// Get category by ID
function getCategoryByCategoryId(categoryId) {
	if(catSeriesList) {
		return catSeriesList[categoryId];
	}
}

// Get series by ID
function getSeriesBySeriesId(seriesId) {
	if(catSeriesList) {
		var seriesArr = getSeries();
		for(var x = 0; x < seriesArr.length; ++x) {
			if(seriesArr[x].seriesId == seriesId) {
				return seriesArr[x];
			}
		}
	}
}

// Get an array containing states from a specific country
function getStatesInCountry(country) {
	var statesArr = [];
	if(countryStateList && countryStateList[country]) {
		statesArr = countryStateList[country];
	}
	return statesArr;	
}



// Read query string params with javascript, must initialize it by calling function below
var queryString = {};
function parseQueryString() {
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for(var i = 0; i < parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0, pos);
			var val = parms[i].substring(pos+1);
			queryString[key] = unescape(val);
		}
	}
}



// Update all Popup links and download links to open up in a new window
function updatePopupLinks() {
	var allLinks = document.getElementsByTagName('a');
	var linkCount = allLinks.length;
	for(var i = 0; i < linkCount; ++i) {
		var theLink = new String(allLinks[i].href);
		if(YAHOO.util.Dom.hasClass(allLinks[i], 'popUp') || YAHOO.util.Dom.hasClass(allLinks[i], 'downloadLink')) {
			YAHOO.util.Event.addListener(allLinks[i], 'click', function(theLink) {
																			return function(e) {
																					YAHOO.util.Event.preventDefault(e);
																					newWin(theLink, 800, 600);
																					return false;
																			}
																		}(theLink));
		}
	}
	allLinks = null;
}


YAHOO.util.Event.addListener(window, "load", updatePopupLinks);




// If the favorite menu is on this page, wire-up the drop down link box
function activateFavoriteMenu(url, title) {
	var favMenu = document.getElementById('favoriteMenu');
	if(favMenu) {
		var favDropLink = favMenu.getElementsByTagName('a')[0];
		var favUl = favMenu.getElementsByTagName('ul')[0];
		if(favDropLink && favUl) {
			var favLink = favUl.getElementsByTagName('a')[0];
			favLink.href = 'javascript:Favorites.add("' + url + '", "' + title + '");';
			var favoriteMenuTimeout = null;
			var closeMenu = function () {
				favoriteMenuTimeout = setTimeout(function() {
														favUl.style.display = 'none';
														},
														1000
													);
			}
			var openMenu = function() {
								if(favoriteMenuTimeout != null) {
									clearTimeout(favoriteMenuTimeout);
								}
								favUl.style.display = 'block';
							};
			YAHOO.util.Event.addListener(favDropLink, "mouseover", openMenu);
			YAHOO.util.Event.addListener(favUl, "mouseover", openMenu);
			YAHOO.util.Event.addListener(favDropLink, "mouseout", closeMenu);
			YAHOO.util.Event.addListener(favUl, "mouseout", closeMenu);
			YAHOO.util.Event.addListener(favLink, "click", function() { this.innerHTML = 'Favorite saved!'; this.style.textDecoration = 'none'; this.style.fontWeight = 'bold'; closeMenu(); });
		}
	}
}



// Error manager class
var Errors = function() {
	
	var errors = [];
	var introMsg = "Please correct the following errors:\n\n";
	this.length = 0;
	
	this.add = function(msg) {
		errors.push(msg);
		this.length++;
	}
	
	this.setIntro = function(intro) {
		introMsg = intro;	
	}
	
	this.display = function() {
		if(this.length > 0) {
			var msgArr = [];
			for(var i = 0; i < errors.length; ++i) {
				msgArr.push((i + 1) + ') ' + errors[i]);
			}
			alert(introMsg + msgArr.join("\n") + "\n");
		}
	}
	
}

