// Javascript for Central Plains

IsCrappyBrowser    = navigator.appVersion.indexOf('MSIE') != -1;
IsCrappyOldBrowser = navigator.appVersion.indexOf('MSIE 6.0') != -1;
IsOpera            = navigator.appName == 'Opera';


// Shuffles through random backgrounds

function randomHeader(Category) {
        if (Category == "homepage") NumFiles = 10;
        if (Category == "about_us") NumFiles = 4;
        if (Category == "communities") NumFiles = 19;
        if (Category == "links") NumFiles = 19;
        if (Category == "micp") NumFiles = 5;
        if (Category == "services") NumFiles = 5;
        if (Category == "tourism") NumFiles = 5;

  	var NumImgs = NumFiles - 1;
	var RandomNumber  = Math.random();
	var ImageNum      = JS_addLeadingZero(Math.round(RandomNumber * NumImgs)+1);
	var ImageFileName = MenuRoot + 'images/headers/' + Category + '/' + ImageNum + '.jpg';
	
	document.getElementById('HeaderImg').src = ImageFileName;
}


// Adds leading zero to a number
function JS_addLeadingZero(number) {
	var NewNumber;

	if (String(number).length == 1)
		NewNumber = ("0" + number);
	else
		NewNumber = number;

	return NewNumber;
}



// S2 testing site
if (document.location.href.match('secure2.pdcweb.net') != null)
	MenuRoot = 'http://secure2.pdcweb.net/www.centralplains.ca/';

// Published site
else if (document.location.href.match('http://centralplains.ca') != null || document.location.href.match('http://www.centralplains.ca') != null)
	MenuRoot = '/';

// Local
else
	MenuRoot = 'file:///D:/Customer Files/CF White Horse Plains/Central Plains Website/2009-05-26 www.centralplains.ca Actual/';

// Opens or closes menu item
function menu(Toggle, IDInitStr, CrappyBrowserMarginLeftOverride) {
	
	// Not just setting style for crappy browsers
	if (IDInitStr != null) {
		
		// Set style obj
		MenuStyleObj = document.getElementById('SubMenu_' + IDInitStr).style;
	
		// Reset the margin for crappy, proprietary browsers
		if (IsCrappyBrowser)
			MenuStyleObj.marginLeft = (CrappyBrowserMarginLeftOverride)?CrappyBrowserMarginLeftOverride + 'px':'120px';
		
		// Show/hide menu
		MenuStyleObj.display    = (Toggle)?'block':'none';
		MenuStyleObj.visibility = (Toggle)?'visible':'hidden';
	}
	
	// Set style for crappy browsers
	if (IsCrappyBrowser) {
		with (window.event.srcElement) {
			if (className != 'SubMenuContainer')
				style.color = (Toggle)?'#1E668E':'#000000';
		}
	}
}

/*
// For highlighting or unhighlighting a menu item
function setClass(Toggle, IDInitStr) {

	Obj = (typeof(IDInitStr) == 'object')?IDInitStr:document.getElementById('MainMenu_' + IDInitStr);
	
	with (Obj)
		className = (Toggle)?className + '_On':String(className).replace('_On', '');
}
*/

// Menu link
function MenuLink(LinkURL) {
	document.location.href = MenuRoot + LinkURL;
}

// Menu link in New Window
function MenuLinkNewWindow(url) {
  var params = 'resizable,menubar,location,status,scrollbars,width=850,height=600'
  popupWin = window.open(url, 'popup', params)
  popupWin.focus()
}

// Image to show in menu for offsite links
OffsiteLinkImage   = '<img src="' + MenuRoot + 'images/offsite_link.gif">';

// Map Link
function MapLink() {
  mapWin = openWindow(MenuRoot + 'images/CP_Region_Map_web_full_100527.gif', '820', '720')
}

// Open a new window of a certain size.
// Usage: <a href="javascript:openWindow('images/filename.jpg', '570', '220');"></a>

function openWindow(url, iwidth, iheight) {
  var params = 'resizable,width='+iwidth+',height='+iheight
  popupWin = window.open(url, 'popup', params)
  popupWin.focus()
}

function open1024Window(url) {
  var params = 'resizable,menubar,location,status,scrollbars,width=1024,height=768'
  popupWin = window.open(url, 'popup', params)
  popupWin.focus()
}



// Anti-Spider Email Addresses
// Use this code in the body:
//	<SCRIPT language=javascript>generateAntiSpiderEmail('user', 'domain', 'tld', 'display_text');</SCRIPT>
function generateAntiSpiderEmail(Username, DomainName, TLD, Text) {
	Address = Username + "&#64;" + DomainName + "." + TLD;

	if (!(Text)) {DisplayText = Address}
	else {DisplayText = Text};

	document.write("<a href=\"ma" + "il" + "to:" + Address + "\">" + DisplayText + "</a>");
}