var browser = navigator.appName;
var versionInfo = navigator.appVersion;
var versionNum = parseFloat(versionInfo);
var vendor = navigator.vendor;
var IE = "Microsoft Internet Explorer";
var IE6 = "MSIE 6.0";
var IE7 = "MSIE 7.0";
var safari = "Apple Computer, Inc.";
var imagePath = "images/";
var classAttribute = classAttr();   //Get the appropriate class attribute to set. IE and Firefox use different attributes for class in the DOM.


/***** Begin preload images *****/
var origImg = [];	//Array of images for rollovers
origImg[0] = '';

var overImg = [];	// Array of images to preload
overImg[0] = '';

var preloadFlag = false;
function preloadImages() {
	for (i=0; i<overImg.length; i++) {
		var images = [];
		images[i] = new Image();
		images[i].src = imagePath + overImg[i];
	}
	preloadFlag = true;
}
/***** End preload images *****/

/***** Begin image rollover funtion *****/
// This function grabs the src attribute and replaces "off" with "on" (for mouseover) or "on" with "off (for mouseout) in the attribute string. KM
function rollover(event) {
	var etype = event.type;
	var imgSrc = getObjectAttribute(this,"src");
	var newImgSrc;
	
	if (etype == 'mouseover'){
	    newImgSrc = imgSrc.replace('off.','on.','gi');
		this.setAttribute('src',newImgSrc);
	}
	
	else if (etype == 'mouseout') {
	    newImgSrc = imgSrc.replace('on.','off.','gi');
		this.setAttribute('src',newImgSrc);
	}
}

// IE 6 transparent PNG rollover function. KM
function rolloverPNG(event) {
	var etype = event.type;
	var imgSrc = getObjectAttribute(this,"name");
	var newImgSrc;
	
	if (etype == 'mouseover'){
	    newImgSrc = imgSrc.replace('off.','on.','gi');
		this.setAttribute('src',newImgSrc);
	}
	
	else if (etype == 'mouseout') {
	    newImgSrc = imgSrc.replace('on.','off.','gi');
		this.setAttribute('src',newImgSrc);
	}
}

/* Function to initalize rollover */
function initRollovers() {
    var rollovers = getElementsByClass('rollover', null, null); //Get elements with a class of "rollover"

    if (rollovers.length > 0) {		//If there is at least one element with a class "rollover"
        for (var i = 0; i < rollovers.length; i++) {	//Loop through all elements with class "rollover"
            var classValue = rollovers[i].getAttribute(classAttribute);

            if ((versionInfo.match(IE6) !== null) && (classValue.match('tpng') !== null)) { //If the browser is IE6 and a transparent png				
                addEvent(rollovers[i], 'mouseover', rolloverPNG); //Attach mouseover event
                addEvent(rollovers[i], 'mouseout', rolloverPNG); //Attach mouseout event				
            }
            else {
                addEvent(rollovers[i], 'mouseover', rollover); //Attach mouseover event
                addEvent(rollovers[i], 'mouseout', rollover); //Attach mouseout event
            }
        }
    }
}

// When function needs to be added inline (e.g. dynamically added content) KM
function rolloverInline(imgId,type) {
	var imgObj = _gel(imgId);
	var imgSrc = getObjectAttribute(imgObj,'src');
	var newImgSrc;	

	if (type == 'over'){
	    newImgSrc = imgSrc.replace('off.','on.','gi');
		imgObj.setAttribute('src',newImgSrc);
	}

	

	else if (type == 'out') {
	    newImgSrc = imgSrc.replace('on.','off.','gi');
		imgObj.setAttribute('src',newImgSrc);
	}
}
/***** End image rollover function *****/

/***** Begin pop up functions *****/
function popWindow(href,title,sb,width,height) {
	window.open(href,title,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars='+ sb +',resizable=0,width='+ width +',height='+ height +',top=150,left=150');
}
/***** End pop up functions *****/

/***** Begin expandable nav function *****/
/* Function to swap background images */
function navBGSwap(event) {	
	var id = getTarget(event).id;	//Get the id of the target of the mouse event
	var etype = event.type;			//Get the type of event, i.e. mouseover or mouseout
	var c = classAttr();			//Get which class attribute to set
	
	if (etype == 'mouseover'){													//If the event was a mousover
		if(_gel(id).getAttribute(c) == "current_off"){								//If the class is "current_off" ("current_off" will be the class of the currently expanded navigation)
			_gel(id).setAttribute(c,'current_on');									//Set the class to "current_on"
		}
		else if((_gel(id).getAttribute(c) == "off") || (!_gel(id).getAttribute(c))){	//If the class is "off" ("off" will be the class of non-expanded navigation; second conditional is to fix IE bugginess)
			_gel(id).setAttribute(c,'on');											//Set the class to "on"
		}
	}
	
	else if (etype == 'mouseout') {												//If the event was a mousout
		if(_gel(id).getAttribute(c) == "current_on"){								//If the class is "current_on" ("current_on" will be the class of the currently expanded navigation)
			_gel(id).setAttribute(c,'current_off');								//Set the class to "current_off"
		}
		else if ((_gel(id).getAttribute(c) == "on") || (!_gel(id).getAttribute(c))){	//If the class is "on" ("on" will be the class of non-expanded navigation; second conditional is to fix IE bugginess)
			_gel(id).setAttribute(c,'off');
		}
	}	
	else {	//If neither of the previous two conditions are met, do nothing.
	}
}
/* Function to expand the sub link navigation */
function navExpand(event) {	
	var current_open = getElementsByClass('current_off', null, 'span')[0];							//Get the currently open (expanded) navigation
	var all_uls = _gel('nav_tertiary').getElementsByTagName('ul')[0].getElementsByTagName('ul');		//Get all sub link ul elements
	var id = getTarget(event).id;																	//Get the id of the target of the mouse event
	var expand_ul = _gel(id).parentNode.getElementsByTagName('ul')[0];									//Get the ul element that should be expanded/collapsed when click event occurs
	var c = classAttr();																			//Get which class attribute to set
		
	if (expand_ul){																					//If there is a ul element to be expanded/collapsed
		if (expand_ul.style.display == "block") {													//If the ul is expanded
			expand_ul.style.display = "none";														//Collapse the ul on click
			_gel(id).setAttribute(c,'on');																//Set the class of the clicked target to "on"
		}
		
		else {																						//Else if there is no ul that is expanded			
			for(var i=0;i<all_uls.length;i++){														//Loop all sub link ul elements
				all_uls[i].style.display = "none";													//Collapse all sub link ul elements
			}
			expand_ul.style.display = "block";														//Expand the ul element
			_gel(id).setAttribute(c,'current_on');														//Set the class of the clicked target to "current_on"
			current_open.setAttribute(c, 'off');													//Set the class of the currently expanded navigation to "off"
		}
	}
	
}
/* Function to expand particular navigation on page load */
function navExpandOnload() {
	var c = classAttr();																			//Get which class attribute to set
	if(expand_num){																					//If the variable "expand_num" exists
		var expandID = 'li' + expand_num;															//Set the id of the navigation to expand
		_gel(expandID).parentNode.getElementsByTagName('ul')[0].style.display = "block";				//Expand the proper navigation based on it's id
		_gel(expandID).setAttribute(c,'current_off');													//Set the class to "current_off" of the expanded navigation
	}
}

/* Function to initialize the expandable navigation */
function initNavExpand() {
    var c = classAttr();

    if (_gel('nav_tertiary')) {
        var link_items = _gel('nav_tertiary').getElementsByTagName('ul')[0].getElementsByTagName('li'); //Get li items withing the tertiary nav section.
        var j = 1;

        for (var i = 0; i < link_items.length; i++) {	//Loop through all the li items

            if ((link_items[i].parentNode.getAttribute(c) != 'sublinks') && (link_items[i].getElementsByTagName('ul')[0])) {  //Check to make sure that the li is not within the sub links ul
                addEvent(link_items[i].getElementsByTagName('span')[0], 'mouseover', navBGSwap); //Attach mouseover event
                addEvent(link_items[i].getElementsByTagName('span')[0], 'mouseout', navBGSwap); //Attach mouseout event
                addEvent(link_items[i].getElementsByTagName('span')[0], 'click', navExpand); 	//Attach click event
                link_items[i].getElementsByTagName('span')[0].setAttribute('id', 'li' + j++); //Ad an id to the span tag
                link_items[i].getElementsByTagName('span')[0].setAttribute(c, 'off'); 		//Set a class attribute
            }
        }

    }
}
/***** End expandable nav function *****/

/***** Begin tab change functions *****/
function tabClick(event){    
    var tabs = getElementsByClass("tab",null,null);	//Get tabs
    var tabSections = getElementsByClass("tab-section",null,null);   //Get tab body sections
    	
	for(var t=0;t<tabs.length;t++){ //Loop through all tabs
	    var tabObj = tabs[t];
	    var tabSectionObj = tabSections[t];
	    	    
	    if(tabObj == this){ //Change clicked tab on
            tabObj.setAttribute(classAttribute, "tab selected");
            tabSectionObj.style.display = "block";
	    }
	    else {	//Change other tabs off	        
            tabObj.setAttribute(classAttribute, "tab");
            tabSectionObj.style.display = "none";		    
	    }
	}
}

function tabHover(event){ 
    var etype = event.type;
	
	if (this.getAttribute(classAttribute) != "tab selected"){
	    if (etype == "mouseover"){
		    this.setAttribute(classAttribute,"tab hover");
	    }
    	
	    else if (etype == "mouseout") {
		    this.setAttribute(classAttribute,"tab");
	    }
	}
}

function initTabbing() {
    var tabs = getElementsByClass("tab", null, null); //Get elements with a class of "tab"

    if (tabs.length > 0) {		//If there is at least one element with a class "tab"
        for (var i = 0; i < tabs.length; i++) {	//Loop through all elements with class "tab"
            addEvent(tabs[i], "click", tabClick); //Attach click event
            addEvent(tabs[i], "mouseover", tabHover); //Attach over event
            addEvent(tabs[i], "mouseout", tabHover); //Attach out event
        }
    }
}
/***** End tab change functions *****/

/***** Begin accordion expand functions *****/      
function accordionClick(){  
	for(var t=0;t<accordionHeaders.length;t++){ //Loop through all tabs
	    var accordionHeaderObj = accordionHeaders[t];
	    var accordionSectionObj = accordionSections[t];
	    var iconObj = icons[t];
	    var imgSrc = iconObj.getAttribute("src");
	    
	    if(accordionHeaderObj == this){ 
            if(accordionSectionObj.style.display == "none"){    //Open accordion
                accordionSectionObj.style.display = "block";
                accordionSectionObj.setAttribute(classAttribute,"accordion-section open"); 
                
                newImgSrc = imgSrc.replace("_collapsed","_expanded","gi");
		        iconObj.setAttribute("src",newImgSrc);
            }
            else {  //Close accordion
                accordionSectionObj.style.display = "none";
                accordionSectionObj.setAttribute(classAttribute,"accordion-section"); 
                
                newImgSrc = imgSrc.replace("_expanded","_collapsed","gi");
		        iconObj.setAttribute("src",newImgSrc);
            }
	    }
    }
}

function accordionHover(event){
	var etype = event.type;
	var id = getTarget(event).id;	
	var iconObj = _gel(id).getElementsByTagName("img")[0];
	var imgSrc = iconObj.getAttribute("src");
	var newImgSrc;
	
	if (etype == 'mouseover'){
	    newImgSrc = imgSrc.replace('off.','on.','gi');
		iconObj.setAttribute('src',newImgSrc);
	}
	
	else if (etype == 'mouseout') {
	    newImgSrc = imgSrc.replace('on.','off.','gi');
		iconObj.setAttribute('src',newImgSrc);
	}
}
    
function expandAll(){    
    for(var t=0;t<accordionSections.length;t++){ //Loop through all sections    
        var iconObj = icons[t];
        var imgSrc = iconObj.getAttribute("src");
        
        newImgSrc = imgSrc.replace("_collapsed","_expanded","gi");
		iconObj.setAttribute("src",newImgSrc);
        accordionSections[t].style.display = "block";
    }
}

function collapseAll(){    
    for(var t=0;t<accordionSections.length;t++){ //Loop through all sections
        var iconObj = icons[t];
        var imgSrc = iconObj.getAttribute("src");
        
        newImgSrc = imgSrc.replace("_expanded","_collapsed","gi");
		iconObj.setAttribute("src",newImgSrc);
        accordionSections[t].style.display = "none";
    }
}

//These variables need to be placed at the bottom of any page with accordion divs.
//var accordionHeaders = getElementsByClass("accordion-header",null,null);	//Get accordion headers
//var accordionSections = getElementsByClass("accordion-section",null,null);   //Get accordion sections
//var icon = getElementsByClass("expand-icon",null,null);   //Get accordion sections
//onloadfunctions.push(initAccordionClick());
function initAccordion() {

    if (accordionHeaders.length > 0) {		//If there is at least one element with a class "accordion-header"
        for (var i = 0; i < accordionHeaders.length; i++) {	//Loop through all elements with class "accordion-header"
            addEvent(accordionHeaders[i], "click", accordionClick); //Attach click event
            addEvent(accordionHeaders[i], "mouseover", accordionHover); //Attach mouseover event
            addEvent(accordionHeaders[i], "mouseout", accordionHover); //Attach mouseover event
        }
    }

    if (accordionSections.length > 0) {		//If there is at least one element with a class "accordion-section"
        for (var i = 0; i < accordionSections.length; i++) {	//Loop through all elements with class "accordion-section"
            if (accordionSections[i].getAttribute(classAttribute).indexOf("open") == -1) {
                accordionSections[i].style.display = "none"; //Set display to none
            }
        }
    }

    if (icons.length > 0) {		//If there is at least one element with a class "expand-icon"
        for (var i = 0; i < icons.length; i++) {	//Loop through all elements with class "expand-icon"
            addEvent(icons[i], "mouseover", rollover); //Attach mouseover event
            addEvent(icons[i], "mouseout", rollover); //Attach mouseover event
        }
    }
}
/***** End accordion expand functions *****/

/***** Begin show/hide Flash navigation function *****/
function flashNavigation() {
	var fullFlashNav = _gel('flashNav');
	
	if (fullFlashNav.style.height == "400px"){
		fullFlashNav.style.height="25px";	
	}
	
	else {
		fullFlashNav.style.height="400px";
	}
}
/***** End show/hide Flash navigation function *****/

/***** Begin clear value functions *****/
// This function can be used to add a focus event to any input with a class of "clear-value". Needs jquery.js
function initClearValue(UpdatePanelID) {
    var selector = ".clear-value";
    var input = $(selector);

    if (input.length > 0) {
        for (var i = 0; i < input.length; i++) {           
            $(input[i]).focus(function() {
                $(this).val("");
                $(this).removeClass("clear-value");
                $(this).unbind("focus");
            });
        }
    }
}
/***** End clear value functions *****/

//requires JQuery
$().ready(function() {
    initRollovers();
});

var isOn = false; //for image toggle and zoom
$(function swapImage() {
	$("#zoom-thumb-container a").click(function() {
		$(".jqzoom").attr("href", $(this).attr("rel"));
		$("#largeImg")[0].src = $(this).attr("href");
		if(isOn == true){
			$('#zoomLink').click();																 
		}
		if($(this).attr("rel") == ""){
			$("#zoom-btn-container").css("visibility","hidden");
		}else{
			$("#zoom-btn-container").css("visibility","visible");
		}
		/*
		$(".thumb_detail a").removeClass("active");
		$(this).addClass("active");
		$(this).siblings("a.thumbText").addClass("active");*/
		return false;
	});
});
