// Clear Search
function clearDefault(el) {
if (el.defaultValue==el.value) el.value = ""
}

// Adds a class focus to input text when focused
function focusfix(selector, className) {$(selector).focus(function() {$(this).addClass(className);});
// Removes class when focus is lost
$(selector).blur(function() {$(this).removeClass(className);});}

jQuery(document).ready(function($) {focusfix('input.text', 'focus');});
jQuery(document).ready(function($) {focusfix('textarea', 'focus');});


/* ------- useful links ------- */

startFormsList = function() {
   if (document.all&&document.getElementById) {
        navRoot = document.getElementById("links");
        for (i=0; i<navRoot.childNodes.length; i++) {
            node = navRoot.childNodes[i];
            
				if (node.nodeName=="div") {                
                node.onmouseover=function() {
                    this.className+=" over"; 
                    ulNode = this.childNodes[2] ;
                    if(ulNode) {
                        if(ulNode.nodeName == "UL") {           
                            ulPos       = findPos(ulNode);      
                            head        = document.getElementById("header");
                            headWid     = head.offsetWidth;
                            bWid        = ((document.body.offsetWidth - headWid)/2) > 0 ? (document.body.offsetWidth - headWid)/2 : 0;
                            if(document.getElementById("selectBlock") != null){                        
                                selBl               = document.getElementById("selectBlock")
                                selPos              = findPos(selBl);
                                selBl.className     = "active";
                                selBl.style.width   = ulPos[2];
                                selBl.style.left    = ulPos[0] - bWid;
                                ht                  = ulPos[3] - selPos[1] - 70;
                                if (ht >= 0 ) {
                                    if(ht >= 32){
                                        ht = 32; 
                                    }
                                    selBl.style.height = ht;
                                } else {                            
                                    selBl.style.height = 0;
                                }   
                            }
                         }
                     }
                }
                
                node.onmouseout=function() {
                    this.className=this.className.replace(" over", "");
                    
                    if(document.getElementById("selectBlock") != null){
                        selBl = document.getElementById("selectBlock")
                        selBl.className = "inactive";
                        selBl.style.width = 0;
                        selBl.style.height = 0;
                        selBl.style.left = 0;
                    }
                }
            }
        }
    }
}

window.onload=startFormsList;


toggleLinks= function(){
    el = document.getElementById("links");
    if(el.className != "over"){
        el.className="over";        
    }
    else {
        el.className="";
    }
}


/*--icons--*/


/* Icons */
$(document).ready(function(){

	$("a[@href$=pdf]").addClass("pdf");
	$("a[@href$=xls]").addClass("xls");
	$("a[@href$=doc]").addClass("doc");
	$("a[@href$=ppt]").addClass("ppt");
	$("a[@href$=com/]").addClass("web");
	
});


/* Image Caption */

//Get Element by Class Name from: http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}


//Function for converting title tags on images into caption

function addCaption(xClass) {
	var allImages = getElementsByClassName(document, "img", xClass);
	for ( var i=0; i < allImages.length; i++) {
		var imageCaption = document.createTextNode(allImages[i].title);
		var imageContainer = document.createElement("div");
		var imagePara = document.createElement("p");
		var imageWidth = allImages[i].getAttribute("width");
		var spareSpan = document.createElement("span"); //This adds an extra span. Useful for curved corners
		imagePara.appendChild(imageCaption);
		allImages[i].parentNode.insertBefore(imageContainer, allImages[i]);
		imageContainer.appendChild(allImages[i]);
		if ( allImages[i].title != "" ) {
		imageContainer.appendChild(imagePara); 
		}
		imageContainer.appendChild(spareSpan);		
		imageContainer.className = xClass
		spareSpan.className = "spareSpan"
		allImages[i].className = "imgCaption"
		imageContainer.style.width = imageWidth + "px";
		
    }
}

// Runs all the listed functions on the loading of the window

window.onload=function(){
addCaption("imgLeft");
addCaption("imgRight");
}

/* New Window */
$(function(){
$('a.external').click(function(){
window.open(this.href);
return false;
});
});

// Filetype icons and external links
$(document).ready(function() {	
		
	// Add pdf icons to pdf links
	$("a[href$='.pdf']").addClass("pdf").attr("target", "_blank");
	$("a[href$='.doc']").addClass("doc").attr("target", "_blank");
	$("a[href$='.ppt']").addClass("ppt").attr("target", "_blank");
	
	// Add email icons to email links
	$("a[href^='mailto:']").addClass("email");

	//Add external link icon to external links - 
	$('a').filter(function() {
		//Compare the anchor tag's host name with location's host name
	    return this.hostname && this.hostname !== location.hostname;
	  }).addClass("external").attr("target", "_blank");	
	 
});

