// Set dropdown menus to inactive - onload makes them active

	var menuSystemActive = false;

// Set the window name

	window.name = "main";

// onload triggers

	function homepageTrigger() {
		menuSystemActive = true;
		buildBuyTabs();
		sifrStart();
		setupScroller();
		initClickablePanels();
	}
	
	function subpageTrigger() {
		menuSystemActive = true;
		buildBuyTabs();
		sifrStart();
		initClickablePanels();
	}


// siFR styling code
		
	var fixChars = function(str) {
		str=str.replace(/“/g,'"') ;
		str=str.replace(/”/g,'"') ;
		str=str.replace(/‘/g,'\'') ;
		str=str.replace(/’/g,'\'') ;
		return str ;
	}
	
	var fixCharsCapitals = function(str) {
		return fixChars(str).toUpperCase();
	}	
	
	var essai = { src: '/sifr/essai.swf'};
	sIFR.useStyleCheck = true;
	sIFR.activate(essai);

	function sifrStart() {
		
		sIFR.replace(essai, {
		  selector: '.atAGlanceHeader h1',
		  modifyContentString: fixChars,
		  wmode: 'transparent',
		  offsetTop: -4,
		  tuneHeight: -4,
		  css: '.sIFR-root { font-size: 36px; color: #ffffff; }'
		});
		
		sIFR.replace(essai, {
		  selector: '#contentArea h1.eventsTitle',
		  modifyContentString: fixChars,
		  wmode: 'transparent',
		  offsetTop: -4,
		  tuneHeight: -4,
		  css: {
		   '.sIFR-root':{ 'font-size': '24px', 'color': '#1f3666' },
		   'a': { 'text-decoration': 'underline', 'color': '#1f3666' },
		   'a:link': { 'color': '#1f3666','text-decoration': 'underline' },
		   'a:hover': { 'color': '#f39426','text-decoration': 'underline' }
		  }
		});
		
		sIFR.replace(essai, {
		  selector: '#contentArea h1',
		  modifyContentString: fixChars,
		  wmode: 'transparent',
		  offsetTop: -4,
		  tuneHeight: -4,
		  css: {
		   '.sIFR-root':{ 'font-size': '36px', 'color': '#1f3666' },
		   'a': { 'text-decoration': 'underline', 'color': '#1f3666' },
		   'a:link': { 'color': '#1f3666','text-decoration': 'underline' },
		   'a:hover': { 'color': '#f39426','text-decoration': 'underline' }
		  }
		});
		
		sIFR.replace(essai, {
		  selector: '#contentArea .genericForm h2',
		  modifyContentString: fixChars,
		  wmode: 'transparent',
		  offsetTop: -3,
		  tuneHeight: -3,
		  css: '.sIFR-root { font-size: 18px; leading: -4; color: #1f3666; }'
		});
		
		sIFR.replace(essai, {
		  selector: '.reviewListingContent h2',
		  modifyContentString: fixChars,
		  wmode: 'transparent',
		  offsetTop: -3,
		  tuneHeight: -3,
		  css: {
		   '.sIFR-root':{ 'font-size': '18px', 'leading': '-4', 'color': '#1f3666' },
		   'a': { 'text-decoration': 'underline', 'color': '#1f3666' },
		   'a:link': { 'color': '#1f3666','text-decoration': 'underline' },
		   'a:hover': { 'color': '#f39426','text-decoration': 'underline' }
		  }
		});
		
		sIFR.replace(essai, {
		  selector: '.perkPanel h2',
		  modifyContentString: fixChars,
		  wmode: 'transparent',
		  offsetTop: 0,
		  tuneHeight: -4,
		  css: {
		   '.sIFR-root':{ 'font-size': '22px', 'leading': '-4', 'color': '#1f3666' },
		   'a': { 'text-decoration': 'none', 'color': '#1f3666' },
		   'a:link': { 'color': '#1f3666','text-decoration': 'none' },
		   'a:hover': { 'color': '#c67415','text-decoration': 'underline' }
		  }
		});
		
		sIFR.replace(essai, {
		  selector: '#contentArea h2',
		  modifyContentString: fixChars,
		  wmode: 'transparent',
		  offsetTop: 0,
		  tuneHeight: -4,
		  css: {
		   '.sIFR-root':{ 'font-size': '22px', 'leading': '-4', 'color': '#1f3666' },
		   'a': { 'text-decoration': 'underline', 'color': '#1f3666' },
		   'a:link': { 'color': '#1f3666','text-decoration': 'underline' },
		   'a:hover': { 'color': '#f39426','text-decoration': 'underline' }
		  }
		});
		
		sIFR.replace(essai, {
		  selector: 'table caption',
		  modifyContentString: fixChars,
		  wmode: 'transparent',
		  offsetTop: -3,
		  tuneHeight: -3,
		  css: '.sIFR-root { font-size: 18px; leading: -10; color: #ffffff; }'
		});
		
		
		sIFR.replace(essai, {
		  selector: '#popupPaddedContent #pageTitleArea h1',
		  modifyContentString: fixChars,
		  wmode: 'transparent',
		  offsetTop: -4,
		  tuneHeight: -4,
		  css: '.sIFR-root { font-size: 30px; color: #ffffff; }'
		});
		
		sIFR.replace(essai, {
		  selector: '#popupPaddedContent .genericForm h2',
		  modifyContentString: fixChars,
		  wmode: 'transparent',
		  offsetTop: -3,
		  tuneHeight: -3,
		  css: '.sIFR-root { font-size: 18px; leading: -10; color: #1f3666; }'
		});
	}
	
// add a getElementByClass function for browsers that don't support getElementsByClassName
		
	// getElementsByClass(class string, optional DOM node, optional html tag)
	function getElementsByClass(searchClass,node,tag) {
		var classElements = new Array();
		if ( node == null )
			node = document;
		if ( tag == null )
			tag = '*';
		var els = node.getElementsByTagName(tag);
		var elsLen = els.length;
		var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
		for (i = 0, j = 0; i < elsLen; i++) {
			if ( pattern.test(els[i].className) ) {
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
	}
	
	
// panel link clicker

	var allClickPanels = new Array();

	function initClickablePanels() {
		if(document.getElementsByClassName) {
			findClickPanels = document.getElementsByClassName("clickablePanel");
		} else {
			findClickPanels = getElementsByClass("clickablePanel",document,"div");
		}
		if(findClickPanels.length > 0) {
			for(i=0;i<findClickPanels.length;i++) {
				allClickPanels[i] = new clickPanel(findClickPanels[i],i);
			}
		}
	}
	
	function clickPanel(getPanel, getIndex) {
		this.panel = getPanel;
		this.index = getIndex;
		if(this.panel.getElementsByTagName('A')[0]) {
			// get link details
			this.panel.newLocation = this.panel.getElementsByTagName('A')[0].href;
			this.panel.newTarget = this.panel.getElementsByTagName('A')[0].target;
			// set behaviour
			this.panel.onclick = function() {
				if(this.newTarget == "_blank") {
					window.open(this.newLocation);
				} else {
					window.location = this.newLocation;
				}
			}
			// cancel link action
			this.panel.getElementsByTagName('A')[0].onclick = function() {return false};
			// set cursor style
			this.panel.style.cursor = "pointer";
		}
	}


// Browser detection

	if(navigator.userAgent.indexOf("Opera")!=-1) GetBrowserType = "opera"
	else if(navigator.appName == "Microsoft Internet Explorer") GetBrowserType = "ie"
	else if(navigator.appName == "Netscape") GetBrowserType = "ns";
	
	safariMode = false;
	if(navigator.userAgent.indexOf("AppleWebKit")!=-1) safariMode = true;
	
	GetBrowserPlatform = "";
	if(navigator.userAgent.indexOf("Mac_PowerPC")!=-1 || navigator.userAgent.indexOf("Macintosh")!=-1) GetBrowserPlatform = "mac";


// Menu Code

	storeMenuIndex = null;
	storeButtonState = null;
	storeThisElement = null;
	menuActive = false;
	menuTimeout = null;
	
	function getMenuClass(src,hoverState) {
		// Get the number of the menu item we are changing
		if(hoverState) return "menuOver"
		else {
			if(storeButtonState) return "menuOn"
			else return "menuOff";
		}
	}
	
	function showMenu(menuIndex,buttonState,thisElement) {
		if(menuSystemActive) {
			cancelHideMenu();
			if(menuActive && menuIndex!= storeMenuIndex) doHideMenu();
			if(!menuActive) {
				storeMenuIndex = menuIndex;
				storeButtonState = buttonState;
				storeThisElement = thisElement;
				thisElement.className = getMenuClass(thisElement,true);
				
				// work out menu bar width
				menuAreaWidth = 0;
				for(i=0;i<MenuTotal;i++) {
					menuAreaWidth += document.getElementById('menu'+i).offsetWidth;
				}
				
				if(SubMenuStrings[menuIndex]) {
					
					// Work out menu position
					SubMenuWidth = document.getElementById('dropDownMenu').offsetWidth;
					MenuOffset = document.getElementById('headerArea').offsetLeft + document.getElementById('headerAreaCentral').offsetLeft + document.getElementById('menuContainer').offsetLeft;
					ButtonOffset = thisElement.offsetLeft;
					xPos = ButtonOffset + MenuOffset;
					MenuAlign = "alignLeft";
					//align div
					if((ButtonOffset + SubMenuWidth) > menuAreaWidth) {
						// menu position aligned right
						ButtonOffset = thisElement.offsetLeft + thisElement.offsetWidth;
						xPos = (ButtonOffset + MenuOffset) - (SubMenuWidth -= 29);
						MenuAlign = "alignRight";
					} else {
						xPos += 11;
					}
				
					// Write menu links
					buildMenu = "<DIV class='" + MenuAlign + "'><dl>" + SubMenuStrings[menuIndex] + "</dl></DIV>";
					document.getElementById('dropDownMenu').innerHTML = buildMenu;
					
					// Set position of menu + make visible
					if(GetBrowserType == "ns" || GetBrowserType == "opera") {
						document.getElementById('dropDownMenu').style.left = xPos + "px";
					} else {
						document.getElementById('dropDownMenu').style.posLeft = xPos;
					}
					document.getElementById('dropDownMenu').style.zIndex = 100;
					document.getElementById('dropDownMenu').style.visibility = "visible";
				
				}
				menuActive = true;
			}
		}
	}
	
	function hideMenu() {
		if(menuSystemActive) {
			refreshMenu();
			menuTimeout = setTimeout("doHideMenu();",200);
		}
	}
	
	function doHideMenu() {
		if(menuSystemActive && storeThisElement != null) {
			storeThisElement.className = getMenuClass(storeThisElement,false);
			document.getElementById('dropDownMenu').style.visibility = "hidden";
			storeMenuIndex = null;
			storeButtonState = null;
			storeThisElement = null;
			menuActive = false;
		}
	}
	
	function cancelHideMenu() {
		if(menuSystemActive) {
			clearTimeout(menuTimeout);
			menuTimeout = null;
		}
	}
	
	function doNoLink() {
		if(menuSystemActive) {
			clearTimeout(menuTimeout);
		}
	}
	
	function refreshMenu() {
		if(menuSystemActive && safariMode) document.getElementById('dropDownMenu').style.zIndex += 1;
	}
	
	
// Buy panel tabs
	var tabOn = 0;
	var tabTextOn = "";
	var allTabDivs = new Array;
	var allTabTitles = new Array;
	
	function buildBuyTabs() {
		if (document.getElementById("buyPanelTabs") && document.getElementById("buyPanelTabbedContentArea")) {
			// Get location query (to check for turning tabs on and off)
			var queryArray = new Array;
			queryArray = document.location.search.substring(1).split("&");
			if (queryArray.length > 0) {
				for (var i in queryArray) {
					// Look for tab numbers in URL
					if (queryArray[i].toLowerCase().indexOf("buytabnum") >= 0) {
						var querySplit = new Array;
						querySplit = queryArray[i].split("=");
						if (querySplit.length > 0) { tabOn = parseInt(querySplit[1]) - 1; break; }
					}
					// Look for tab names in URL
					if (queryArray[i].toLowerCase().indexOf("buytabname") >= 0) {
						var querySplit = new Array;
						querySplit = queryArray[i].split("=");
						if (querySplit.length > 0) { tabTextOn = querySplit[1].toLowerCase(); break; }
					}
				}
			}
			// Look for a text match in the divs and set tabOn if necessary
			var myElements = document.getElementsByTagName("DIV");
			storeCount = 0;
			for (i=0; i<myElements.length; i++) {
				if (myElements[i].className == "buyPanelTabbedContent" || myElements[i].className == "buyPanelTabbedContent hidden") {
					// store this div element + title text
					allTabDivs[storeCount] = myElements[i];
					if(document.all) {
						allTabTitles[storeCount] = myElements[i].getElementsByTagName("H3")[0].innerText.toString();
					} else {
						allTabTitles[storeCount] = myElements[i].getElementsByTagName("H3")[0].textContent.toString();
					}
					// check to see whether title is in url query
					if (tabTextOn != "" && tabTextOn == allTabTitles[storeCount].toLowerCase()) tabOn = storeCount;
					storeCount += 1;
				}
			}
						
			// Create tab button area
			document.getElementById("buyPanelTabs").innerHTML = "<div id=\"buyPanelTabs\"></div>";
			document.getElementById("buyPanelTabs").style.display = "block";
			// Hide all content bar active tab content
			for (i=0; i < allTabDivs.length; i++) {
				if(i == tabOn) {
					allTabDivs[i].className = "buyPanelTabbedContent";
				} else {
					allTabDivs[i].className = "buyPanelTabbedContent hidden";
				}
			}
			// Build tabs
			var myTabs = "<dl>";
			for (i=0; i < allTabDivs.length; i++) {
				if (i == tabOn) {
					myTabs += "<dd id='tab" + i + "' class='tabOn'>";
				} else {
					myTabs += "<dd id='tab" + i + "' class='tabOff'>";
				}
				myTabs += "<a href='javascript:doBuyTab(" + i + ");'>" + allTabTitles[i] + "<\/a><\/dd>";
			}
			myTabs += "</dl>";
			document.getElementById("buyPanelTabs").innerHTML = myTabs;
		}
	}


	function doBuyTab(getTab) {
		// check tab is different from current active tab
		if(getTab != tabOn) {
			// hide old content and change old tab class
			allTabDivs[tabOn].className = "buyPanelTabbedContent hidden";
			document.getElementById("tab"+tabOn).className = "tabOff";
			// show new content and change new tab class
			allTabDivs[getTab].className = "buyPanelTabbedContent";
			document.getElementById("tab"+getTab).className = "tabOn";
			// store new tab
			tabOn = getTab;
		}
	}