/***************************************************************
Global Variable Object
Detail 		:	An object for storing global variables
Extend		:	Object.extend(globalVars, {'property ' : value }) 
***************************************************************/
var global = new Class({
    initialize: function(date){
        this.date = date;
    }
});
globalVars = new global(Date())


/***************************************************************
Window Onload Manager (WOM) v1.0	http://www.netlobo.com/wom.html
Detail 		:	Manage multiple window.onload functions
Add			:	womAdd('function()')
***************************************************************/
function womGo(){
  for(var i = 0;i < woms.length;i++)
    eval(woms[i]);
}

function womAdd(func){
  woms[woms.length] = func;
}

var woms = new Array();
window.onload = function(){
	womGo();
}
/***************************************************************/


/***************************************************************
BrowserDetect v1.0       http://www.quirksmode.org/js/detect.html
Detail 		:	Detect the name, version, and platform of browser
			:	call via globalVars.detect
***************************************************************/
var BrowserDetect = {
        init: function () {
                this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
                this.version = this.searchVersion(navigator.userAgent)
                        || this.searchVersion(navigator.appVersion)
                        || "an unknown version";
                this.OS = this.searchString(this.dataOS) || "an unknown OS";
        },
        searchString: function (data) {
                for (var i=0;i<data.length;i++) {
                        var dataString = data[i].string;
                        var dataProp = data[i].prop;
                        this.versionSearchString = data[i].versionSearch || data[i].identity;
                        if (dataString) {
                                if (dataString.indexOf(data[i].subString) != -1)
                                        return data[i].identity;
                        }
                        else if (dataProp)
                                return data[i].identity;
                }
        },
        searchVersion: function (dataString) {
                var index = dataString.indexOf(this.versionSearchString);
                if (index == -1) return;
                return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
        },
        dataBrowser: [
                {
                        string: navigator.vendor,
                        subString: "Apple",
                        identity: "Safari"
                },
                {
                        prop: window.opera,
                        identity: "Opera"
                },
                {
                        string: navigator.vendor,
                        subString: "iCab",
                        identity: "iCab"
                },
                {
                        string: navigator.vendor,
                        subString: "KDE",
                        identity: "Konqueror"
                },
                {
                        string: navigator.userAgent,
                        subString: "Firefox",
                        identity: "Firefox"
                },
                {       // for newer Netscapes (6+)
                        string: navigator.userAgent,
                        subString: "Netscape",
                        identity: "Netscape"
                },
                {
                        string: navigator.userAgent,
                        subString: "MSIE",
                        identity: "Explorer",
                        versionSearch: "MSIE"
                },
                {
                        string: navigator.userAgent,
                        subString: "Gecko",
                        identity: "Mozilla",
                        versionSearch: "rv"
                },
                {       // for older Netscapes (4-)
                        string: navigator.userAgent,
                        subString: "Mozilla",
                        identity: "Netscape",
                        versionSearch: "Mozilla"
                }
        ],
        dataOS : [
                {
                        string: navigator.platform,
                        subString: "Win",
                        identity: "Windows"
                },
                {
                        string: navigator.platform,
                        subString: "Mac",
                        identity: "Mac"
                },
                {
                        string: navigator.platform,
                        subString: "Linux",
                        identity: "Linux"
                }
        ]

};
BrowserDetect.init()
Object.extend(globalVars, {'detect' : BrowserDetect } )
/***************************************************************/

/***************************************************************
FlashDetect v1.1		http://www.quirksmode.org/js/flash.html (modified from)
Detail 		:	Detects what version of/if Flash is installed 
			:	call via globalVars.detect	flash | flashVersion
***************************************************************/
var flashinstalled = 0;
var flashversion = 0;
if (navigator.plugins && navigator.plugins.length){
	x = navigator.plugins["Shockwave Flash"];
	if (x){
		flashinstalled = true;
		if (x.description){
			y = x.description;
			flashversion = y.charAt(y.indexOf('.')-1);
		}
	}else
		flashinstalled = 1;
		if (navigator.plugins["Shockwave Flash 2.0"]){
		flashinstalled = true;
		flashversion = 2;
	}
}else if (navigator.mimeTypes && navigator.mimeTypes.length){
	x = navigator.mimeTypes['application/x-shockwave-flash'];
	if (x && x.enabledPlugin)
		flashinstalled = true;
	else
		flashinstalled = false;
}else{
	for(var i=9; i>0; i--){
		try{
			var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
			flashversion = i;
			flashinstalled = true;
			break
		}
		catch(e){
		}
	}
}
Object.extend(globalVars.detect, {'flash' : flashinstalled, 'flashVersion' : flashversion } )
/***************************************************************/


/***************************************************************
FlashObject Class v1.0						
Detail 		:	Creates a new Flash Object and only inserts if client passes
				Flash/OS/Browser/Version test.
				
				Dependent on FlashDetect v1.1, Global Variable Object,
				Window Onload Manager, and Bowser Detect
				
				Redirect function not fully implemented.
				
	movie		:	the location of the SWF to be inserted
	id			:	the id to assign to the flash object
	width		:	the width attribute of the flash object
	height		:	the height attribute of the flash object
	failCase	:	the PLATFORM BROWSER VERSION that will fail
	bgcolor		:	the bgcolor attribute of the flash object
	quality		:	the quality attribute of the flash object
	align		:	the align attribute of the flash object
	wmode		:	the wmode attribute of the flash object
	menu		:	the menu attribute of the flash object
	redirectUrl	:	the URL to redirect to on fail
	flashvars	:	variables to pass to the flash object's root
	
	
	call in HTML:
	<script>
		
	flashBanner = new flashObject('focal.swf', 'banner', '750', '220', '7', 'Mac  Safari  all,  Mac  Mozilla  1.8', '#FFFFFF', 	 {wmode:'transparent', menu:'false'})
	</script>
	
	
***************************************************************/
var flashObject = new Class({
    initialize: function(movie, id, width, height, version, failCase, bgcolor, quality , align, wmode, menu, redirectUrl, flashvars){
        this.movie = 'http://www.qualityassuredservices.com/flash/'+ movie;
		this.id = id;
		this.width = width;
		this.height = height;
		this.version = parseFloat(version);
		this.failCase = failCase;
		this.bgcolor = bgcolor;
		this.quality = quality? quality : 'high';
		this.align = align? align : 'middle';
		this.wmode = wmode? wmode : 'opaque' ;
		this.menu = menu? menu : 'false';
		this.redirectUrl = redirectUrl;
		this.flashvars = flashvars;
		
		this.embed = 	'<object id="'+ this.id	+'-swf" type="application/x-shockwave-flash" data="'+ this.movie +'" width="'+ this.width +'" height="'+ this.height +'" align="'+ this.align +'">\n'
		this.embed +=		'<param name="movie" value="'+ this.movie +'" />\n'
		this.embed +=		'<param name="quality" value="'+ this.quality +'" />\n'
		this.embed +=		'<param name="bgcolor" value="'+ this.bgcolor +'" />\n'
		this.embed +=		'<param name="wmode" value="'+ this.wmode +'" />\n'
		this.embed +=		'<param name="menu" value="'+ this.menu +'" />\n'
		this.embed +=		'<param name="FlashVars" value="'+ this.flashvars +'" />\n'
		this.embed +=	'</object>'
		
		
		if(globalVars.detect.flash && globalVars.detect.flashVersion >= this.version){
			var fail = false;
			var test = new RegExp("(\\b"+globalVars.detect.OS+"\\b|\\ball\\b)\\W(\\b"+globalVars.detect.browser+"\\b|\\ball\\b)\\W("+globalVars.detect.version+"|\\ball\\b)","gi");
			
			if (!this.failCase.match(test)){
				womAdd('$(\''+this.id+'\').innerHTML = \''+this.embed.replace(/\n/g,"\\n")+'\'')
			}else{
				fail = true
			}
		}else{fail = true}
		
		if(fail && $(this.id+'-img')){$(this.id+'-img').style.visibility = "visible" ;}
		
		//alert(this.embed);
		//alert('Browser:'+globalVars.detect.browser+'\nVersion:'+globalVars.detect.version+'\nFlash Version:'+globalVars.detect.flashVersion + ' vs ' +version)
    }
});
/***************************************************************/


function stretchPage() {
/***************************************************************
Detail 		:	Resizes the page's spacer elemnet to ensure footer always renders at bottom of page
***************************************************************/
    var newheight = window.getScrollHeight() - ($('site-frame').offsetHeight - $('spacer').offsetHeight);
    if (newheight < 1) newheight = 1; // set to 1 because there are bugs in moz and konq when setting height to 0
    $('spacer').style.height = newheight + 'px';
}
womAdd('stretchPage()')
window.onresize = function() {
	stretchPage();
}
/***************************************************************/


function swapClass(e, el_class, forced){
/***************************************************************
Detail 		:	Swaps the class of (e) to/from (el_class) for UI feedback
Arguments	:	e 			= element or even to swap 
				el_class 	= className to swap
				forced		= forces addition of el_class
***************************************************************/
	if(e.className.indexOf(el_class) >= 0 || forced){
		e.className = e.className.replace(new RegExp(" "+el_class+"\\b"), "");
	}else{
		e.className+=" "+el_class;
	}
	return null;
}
/***************************************************************/


function hover(el) {
/***************************************************************
Detail 		:	Permits hover state for nav elements
***************************************************************/
	if ($(el)){
	var els = $(el).getElementsByTagName("li");
	for (var i=0; i<els.length; i++) {
		el=els[i]
		if (el.className.indexOf("category") >= 0){
			el.onmouseover=function() {
				//alert('hover')
				if (this.className.indexOf("hover") < 0){
				this.className+=" hover";}
			}
			if (globalVars.detect.browser == "Explorer"){
				el.onmouseleave=function() {
					this.className=this.className.replace(new RegExp(" hover\\b"), "");
				}
			}else {
				el.onmouseout=function(e) {
					this.className=this.className.replace(new RegExp(" hover\\b"), "");
				}
			}
		}
	}
	}
}
womAdd('hover("nav")')
womAdd('hover("user-nav")')
/***************************************************************/


function focusElements(){
/***************************************************************
Detail 		:	Permits UI feedback for focused form input
Requires	:	swapClass()
***************************************************************/
	var els = document.getElementsByTagName("input");
	for (var i=0; i<els.length; i++) {
		el=els[i]
		if(el.className == "") el.className = "default";
		el.onfocus=function() {
			swapClass(this, "focus");
		}
		el.onblur=function() {
			swapClass(this, "focus", true);
		}
	}
}
womAdd('focusElements()')
/***************************************************************/


function clearTest(){
/***************************************************************
Detail 		:	Clears all text fields of default values
***************************************************************/
	var els = document.getElementsByTagName("input");
	for (var i=0; i<els.length; i++) {
		el=els[i]
		if (el.value && el.className.indexOf('iform') < 0 && el.type == 'text'){
			temp_var = el.value
			el.onclick=function() {
				//alert(temp_var)
				if (this.value == temp_var){
				this.value='';
				}
			}
			/*el.onblur=function() {
				//alert(temp_var)
				if (this.value == ''){
				this.value=temp_var;
				}
			}*/
		}
	}
}
womAdd('clearTest()')
/***************************************************************/


function linkRelationships() {
/***************************************************************
Detail 		:	provides a target="_blank" to all rel="external" links for XHTML strict compatability
			:	also loads all rel="ajax" into modal element
			:	also hides/reveals layers in a multi layerd container
***************************************************************/
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");
	for (i=0; i < anchors.length; i++){
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel")){
															   
			if(anchor.getAttribute("rel").indexOf("external") >= 0 ){
				anchor.target = "_blank";
			 }
			 
			if(anchor.getAttribute("rel").indexOf("ajax") >= 0 ){
				var url = anchor.href
				anchor.onclick = function(e){
					(new Event(e)).stop()
					modalCall(url.replace(/(.aspx|.htm|.html|.asp)((\?)(.*))?/, '$1?ajax=true&$4'))
				};
			 }
			 if(anchor.getAttribute("rel").indexOf("swap") >= 0 ){
				anchor.onclick = function(e){
					(new Event(e)).stop()
					swapLayer(this.title)
					this.blur();
				};
			 }
		}
	}
}

womAdd('linkRelationships()')
/***************************************************************/


/***************************************************************
Detail 		:	Swaps layers in multiple layer container
***************************************************************/
swapLayer = function(key){
	var els = $('information').getElementsByTagName("li");
	for (var i=0; i < els.length; i++) {
		var el=$(els[i])
		if (el.getFirst().title == key){
			if (el.className.indexOf('active') < 0) {el.className+= " active"}
			$(key).style.display = "block";
		}else{
			el.className=el.className.replace(new RegExp(" ?active\\b"), "");
			$(el.getFirst().title).style.display = "none"
		}
		$(el.getFirst().title).getFirst().style.display = "none"
	}
}

swapLayerTest = function(){		// if a particular layer was referenced in the URL that layer is revealed
	var str = document.location.toString()
	var key = 'overview'
	var idx = str.indexOf('#')+1
	if (str.indexOf('#') > 0){
		key = str.substr(idx)
	}
	womAdd('swapLayer("'+key+'")')
}
/***************************************************************/


/***************************************************************
Detail 		:	creates FX for modal interaction
***************************************************************/
var motion = new Class({
    initialize: function(open, close){
        this.open = open;
        this.close = close;
		this.onComplete = '';
    }
});

var modalFX = new motion();

modalInit = function(){

	Object.extend(globalVars, {'spacerHeight' : $('spacer').style.height} )
	
	modalFX.open = function() {
		$('modal').setOpacity(1);
		$('modal').setStyles({
			display: 'block', 
			width: '100%',
			height: window.getScrollHeight() + 'px'
		});
		
		$('modal-frame').setStyles({top: window.getScrollTop() + 'px'});
		
		var x = parseInt(globalVars.detect.version) >= 7 && globalVars.detect.browser == 'Explorer' ? 600 :  $('modal-frame').scrollHeight ;
		$('modal-frame').effects({duration:500}).start({'height':[0, x + 36 ] }).addEvent('onComplete', function() {
			$('spacer').style.height = Math.max( x - window.getHeight() + 60 + $('spacer').scrollHeight, $('spacer').scrollHeight ) + 'px';
			
			$('modal').setStyles({height: window.getScrollHeight() + 'px'});
		});
	}
	
	modalFX.close = function() {
		$('modal-frame').effects({duration:500}).start({'height':[0] }).addEvent('onComplete', function() {
			$('modal').setStyle('display', 'none');
			$('spacer').style.height = globalVars.spacerHeight
		});
		
	}
};


womAdd('modalInit()')
/***************************************************************/


function modalCall(url){	
/***************************************************************
Detail 		:	calls ajax modal element
***************************************************************/
	document.body.style.cursor = 'wait'; //in case the ajax takes a while to load
	new Ajax(url, {
			method: 'post', 
			update: $('modal-content')
		}).request().addEvent('onComplete', function() {
			document.body.style.cursor = 'auto';
			modalFX.open();
	}); 
}
/***************************************************************/

/***************************************************************
Detail 		:	Creates a styled list to represent the Select Menu for forms
***************************************************************/
function selectReplacement(obj) {
  obj.className += ' replaced';
  var ul = document.createElement('ul');
  ul.className =  'selectReplacement';
  ul.id = obj.id + '-ul';
  var opts = obj.options;
  var selectedOpt = (!obj.selectedIndex) ? 0 : obj.selectedIndex;
  for (var i=0; i<opts.length; i++) {
	var li = document.createElement('li');
	var txt = document.createTextNode(opts[i].text);
	li.appendChild(txt);
	li.className = ''
	li.selIndex = i;
	li.selectID = obj.id;
	li.onclick = function() {
	  selectMe(this);
	};
	if (i == selectedOpt) {
	  Object.extend(globalVars, {'selectedOpt' : li } );
	  li.className += ' selected';
	  li.onclick = function() {
		generateObjClick(this)
	  };
	}
	if (window.attachEvent) {
	  li.onmouseover = function() {
		this.className += ' hover';
	  };
	  li.onmouseout = function() {
		this.className = 
		  this.className.replace(new RegExp(" hover\\b"), '');
	  };
	}
	ul.appendChild(li);
  }
  obj.onfocus = function() {
	ul.className += ' selectFocused';
  };
  obj.onblur = function() {
	ul.className = 'selectReplacement';
  };
  obj.onchange = function() {
	var idx = this.selectedIndex;
	selectMe(ul.childNodes[idx])
  };
  obj.onkeypress = obj.onchange;
  obj.parentNode.insertBefore(ul,obj);
  
  document.onmousedown = function(event){
	var event = new Event(event);
	if (event.target.parentNode.className){
		if (event.target.parentNode.className.indexOf('selectReplacement') < 0 && $$('.selectOpen')){
		selectMe(globalVars.selectedOpt)
		}
	}
	}
}

function selectMe(obj) {
  globalVars.selectedOpt = obj;
  var ul = obj.parentNode
  var lis = obj.parentNode.getElementsByTagName('li');
  for (var i=0; i<lis.length; i++) {
	if (lis[i] != obj) {
	  lis[i].className = lis[i].className.replace(new RegExp("selected\\b", "g"), '');
	  lis[i].onclick = function() {
		selectMe(this);
	  };
   } else {
	  setVal(obj.selectID, obj.selIndex, ul.id);
	  obj.className+=' selected';
	  obj.parentNode.className = 
		obj.parentNode.className.replace(new RegExp(" selectOpen\\b"), '');
	  if($('active-option')){ $('active-option').remove(); $('last-option').id = '';}
	  obj.onclick = function() {
		generateObjClick(obj)
	  };
	
	}
  }
}

function generateObjClick(obj){
	obj.parentNode.className += ' selectOpen';
	var li = document.createElement('li');
	var txt = document.createTextNode(obj.innerHTML);
	li.appendChild(txt);
	li.id = 'active-option';
	$(li).onclick = function() {
	  selectMe(obj);
	};
	$(obj.parentNode).getLast().id = 'last-option'
	$(li).injectBefore($(obj.parentNode).getFirst());		
	obj.onclick = function() {
	  selectMe(obj);
	};
}

function setVal(objID,val,ulID) {
  var obj = $(objID);
  var change = (obj.selectedIndex == val)? false : true;
  obj.selectedIndex = val;
  
  if (ulID.indexOf('prodSort') == 0 && change){
	document.body.style.cursor = 'wait';
 	$(ulID).parentNode.submit();
  }
}
function setForm() {
  var s = document.getElementsByTagName('select');
  for (var i=0; i<s.length; i++) {
	if (s[i].className.indexOf('set') >= 0){ selectReplacement(s[i]); }
  }
}

womAdd('(document.all && !window.print) ? null : setForm();')