// JavaScript Document


// $Id: common.js,v 1.12 2007/10/19 19:26:23 jgee Exp $

//////////////////////////////////////////////////////////////////
//
// Sets Navigation Mouseover effects
//
///////////////////////////////////////////////////////////////////

var current_nav; // corresponds to the current page
var active_nav; // corresponds to the element with mouse focus

function set_nav_behavior(el)
{
    var parts,img_name,sub_parts,ext,file_basename,newImg,sbnv;
    
    if(el.nodeName == 'IMG')
    {
        el.normal = el.getAttribute('src');
        parts = el.normal.split('/');
        img_name = parts.pop();
        sub_parts = img_name.split('.');
        ext = sub_parts.pop();
        file_basename = sub_parts.join();
        
        file_basename = file_basename.split('_');
        if(file_basename[file_basename.length-1] == 'over')
        {
            file_basename.pop();
            file_basename = file_basename.join('_');
            current_nav = el;
            el.over = parts.join('/') + '/' + file_basename + '.' + ext;
        }
        else
            el.over = parts.join('/') + '/' + sub_parts.join('_') + '_over' + '.' + ext;
        el.timeout = false;
        
        el.sbnv=false;
        if(sbnv=_get(file_basename+'_subnav'))
        {
            el.sbnv = sbnv.id;
            sbnv.on = false;
            sbnv.parent = el;
            
            sbnv.onmouseover = function()
            {
                if(this.parent.timeout)
                    clearTimeout(this.parent.timeout);
            }
            sbnv.onmouseout = function()
            {
                var obj = this.parent;
                if(current_nav != obj)
                    obj.timeout = setTimeout(function(){obj.countdown.apply(obj)},500);
            }
        }
        
        // preload images
        newImg = new Image();
        newImg.src = el.over;
        
        el.onmouseover = function()
        {
            var was_active;
            
            if(active_nav && active_nav != this)
            {
                was_active = active_nav;
                active_nav = this;
                was_active.countdown();
            }
            else
                active_nav = this;
            
            if(this != current_nav)
            {
                this.setAttribute('src',this.over);
            }
            else
                this.setAttribute('src',this.normal);
            
            if(this.sbnv)
                _get(this.sbnv).style.display = 'block';
            
            if(this.timeout)
            {
                clearTimeout(this.timeout);
            }
        }
        el.onmouseout = function()
        {
            var obj;
               
            obj = this;
            
            if(current_nav != this)
            {
                this.timeout = setTimeout(function(){obj.countdown.apply(obj)},500);
            }
        }
        el.countdown = function()
        {
            if(this.sbnv)
                _get(this.sbnv).style.display = 'none';
            this.setAttribute('src',this.normal);
            clearTimeout(this.timeout);
            
            if(this != current_nav && this == active_nav)
            {
                current_nav.onmouseover();
            }
            if(this == current_nav)
                this.setAttribute('src',this.over);
        }
    }
}

function set_element_as_active(el) {
    if(el.nodeName == 'IMG') {
        /*
        if(isDescendentOf(_get('main_nav'),el)) {
            src = el.getAttribute('src');
            parts = src.split('.');
            ext = parts.pop();
            el.setAttribute('src',parts.join('.') + '_over.' + ext);
        }*/
    }else {
        if(!isDescendentOf(_get('breadcrumb'),el)) {
            old = el.parentNode.innerHTML;
            nw = '<span class="active_page">' + old + '</span>';
            el.parentNode.innerHTML = nw;
            //alert(el.parentNode.innerHTML);
        }
    }
}

links = document.getElementsByTagName('a');

for(i=0; i < links.length; i++) {
    
    href = links[i].getAttribute('href');
    if(
       0
    ) {
        set_element_as_active(links[i].firstChild);
    }
    //inner = links[i].firstChild;
}

//walk(_get('primary_nav'),set_nav_behavior);
if(current_nav)
    current_nav.onmouseover();

    
function displayById(id, status) {
	var thisObj = document.getElementById(id);
	
	if(!is_object(thisObj))
	{
	   return;
	}
	
	if (status == 'show') {
		thisObj.style.display = '';		
	}
	if (status == 'hide') {
		thisObj.style.display = 'none';
	}
}

//open link in new window
function externalLinks() {
	if (!document.getElementsByTagName) {
		return;
	}
	var anchors = document.getElementsByTagName("a");
	
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
		 	anchor.target = "_blank";
		}
	}
}
