var pd={
	activeLayer:"",
/**************************************************************************************************
** Initialize Scripts - is this a browser that understands DOM?
**************************************************************************************************/
	scriptInit:function() {
		if (!document.getElementById) {
			return false;
		}else{
			return true;
		}
	},

/************************************************************************************************
** Set up Event Listener - the script that allows us to use the addEvent call below
*************************************************************************************************/

	addEvent:function(elm, evType, fn, useCapture) {
		if (elm.addEventListener) {
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	},
	
/***********************************************************************************************
** helper functions:
** $: get object
** getElementsByClass: does what the function name says
** changeClass: add, remove, swap, check classes for an object
************************************************************************************************/

	$:function() {
		var elements = new Array();
		for (var i = 0; i < arguments.length; i++) {
			var element = arguments[i];
			if (typeof element == 'string'){
				if(document.getElementById(element)){
					element = document.getElementById(element);
				}
			}
			if (arguments.length == 1){
				if (typeof element == 'object'){
					return element;
				}else{
					return false;
				}
			}
			if (typeof element == 'object'){
				elements.push(element);
			}
		}
		return elements;
	},
	
	getElementsByClass:function(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;
	},
	
/*****************************************************************************************
** This function takes four parameters:
** a defines the action you want the function to perform.
** O the object in question.
** c1 the name of the first class
** c2 the name of the second class
**
** Possible actions are:
** swap replaces class c1 with class c2 in object o.
** add adds class c1 to the object o.
** remove removes class c1 from the object o.
** check tests if class c1 is already applied to object o and returns true or false.
******************************************************************************************/
	
	changeClass:function(a,o,c1,c2){
		switch(a){
			case 'swap':
				o.className=!pd.changeClass('check',o,c1)?o.className.replace(c2,c1) : o.className.replace(c1,c2);
			break;
			case 'add':
				if (!pd.changeClass('check',o,c1)){o.className+=o.className ? ' ' + c1 : c1;}
			break;
			case 'remove':
				var rep=o.className.match(' ' + c1) ? ' ' + c1 : c1;
				o.className = o.className.replace(rep,'');
			break;
			case 'check':
				return new RegExp('\\b' + c1 + '\\b').test(o.className)
			break;
		}
	},
	
/*******************************************************************************
** find position of a layer
*******************************************************************************/
	
	findPos:function(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curleft,curtop];
	},

	//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
	getPageScroll:function(){

		var yScroll;

		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}
	
		arrayPageScroll = new Array('',yScroll) 
		return arrayPageScroll;
	},



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
	getPageSize:function(){
	
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	},
	
	hideLayer:function(obj){
		obj.style.position="absolute";
		obj.style.left = "-999em";
		obj.style.top = "-999em";
	},
	
	openOctoflexPopup:function(obj){
		var obj = pd.$(obj);
		var arrayPageSize = pd.getPageSize();
		var arrayPageScroll = pd.getPageScroll();
				
		// position div and make sure that the top and left values are not negative
		var popupTop = arrayPageScroll[1]-50;
		var popupLeft = ((arrayPageSize[0]/2 -20 - 300));
		var popupLeft =530;
		
		obj.style.top = (popupTop < 0) ? "0px" : popupTop + "px";
		obj.style.left = (popupLeft < 0) ? "0px" : popupLeft + "px";
	},
	
	initOctoflexPopup:function(){
		pd.refPoint = pd.$('octoImage');
		pd.octoflexLayers = pd.getElementsByClass('popupLayer');
		pd.octoflexLayersNum = pd.octoflexLayers.length;
		var imagemapAreas = pd.getElementsByClass('octoSchemaArea');
		for (var i = 0;i<imagemapAreas.length;i++){
			obj = pd.$(imagemapAreas[i].id+ '_layer');
			upLink = pd.$(imagemapAreas[i].id+ '_up');
			pd.hideLayer(obj);
			pd.hideLayer(upLink);
			
			obj.onclick = function(e){
				e = (e) ? e : ((window.event) ? window.event : "");
				var elem = (e.target) ? e.target : e.srcElement;
				var className = elem.className;
				if(elem.className!="innerPopupLink") pd.hideLayer(this);
			}
			
			obj.closeButton = pd.getElementsByClass('btClose',obj,'a').id;
			obj.closeButton = pd.$(this.id+'_close');
			obj.closeButton.parentObj = obj;
			obj.closeButton.onclick = function(){
				pd.hideLayer(this.parentObj);
				return false;
			}
			
			imagemapAreas[i].onclick = function(){
				var arrayPageSize = pd.getPageSize();
				var arrayPageScroll = pd.getPageScroll();
				obj = pd.$(this.id+ '_layer');
				if (pd.activeLayer == "") pd.activeLayer = obj;
				pd.hideLayer(pd.activeLayer);
				pd.activeLayer = obj;
				//alert(this.id + '_layer');
				//alert(obj.style.left);
				//obj.style.left = pd.findPos(pd.refPoint)[0]+"px";
				//obj.style.top = pd.findPos(pd.refPoint)[1]+"px";
				
				
				
				// center div and make sure that the top and left values are not negative
				var popupTop = arrayPageScroll[1]-50;
				var popupLeft = ((arrayPageSize[0]/2 -20 - 300));
				var popupLeft =530;
				/*console.log("arrayPageScroll[1]: " + arrayPageScroll[1]);
				console.log("obj.style.height: " + obj.style.height);
				console.log("popupTop: " + popupTop);
				console.log("popupLeft: " + popupLeft);*/
		
				obj.style.top = (popupTop < 0) ? "0px" : popupTop + "px";
				obj.style.left = (popupLeft < 0) ? "0px" : popupLeft + "px";
				
				
				
				
				return false;
			}
		}
	},
	
	
	fixPng:function(){
		var arVersion = navigator.appVersion.split("MSIE")
		var version = parseFloat(arVersion[1])

		if ((version >= 5.5) && (document.body.filters)) 
		{
		   for(var i=0; i<document.images.length; i++)
		   {
		      var img = document.images[i]
		      var imgName = img.src.toUpperCase()
		      if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
		      {
		         var imgID = (img.id) ? "id='" + img.id + "' " : ""
		         var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		         var imgStyle = "display:inline-block;" + img.style.cssText 
		         if (img.align == "left") imgStyle = "float:left;" + imgStyle
		         if (img.align == "right") imgStyle = "float:right;" + imgStyle
		         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
		         var strNewHTML = "<span " + imgID + imgClass + imgTitle
		         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
		         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
		         img.outerHTML = strNewHTML
		         i = i-1
		      }
		   }
		}
	},
	
	initMainNav:function(){
		
		if (!document.getElementsByTagName) return false;
		var sfEls = pd.$("nav").getElementsByTagName("li");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
				this.style.zIndex=8200; //this line added to force flyout to be above relatively positioned stuff in IE
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	},
	
	
/**************************************************************************
**	main init function
** add functions you want to execute on load
***************************************************************************/

	init:function(){
		if(pd.scriptInit){
			if(pd.$('nav')&& (window.attachEvent)) pd.initMainNav();
		}
	}
}
pd.addEvent(window, 'load', pd.init, false);

