/* Project Name Global Javascript Functions

By  - ISITE Design

Running jQuery in noConflict to avoid issues with CS code
*/
jQuery.noConflict();

//start the jQuery functions
jQuery(document).ready(function() {


	// apply automatic input clearing to the search input - add as needed
	//jQuery("#searchstring").inputClear();



    // add class to drop downs and buttons for IE <6
	if(document.all){

jQuery("#nav div.contact").css("bottom","9em");
	    jQuery("#nav li, button, #calendarnav .daynav td, #department-widget span, #search-more").hover(
	            function() {
					jQuery(this).addClass("over");
				},
	            function() {
					jQuery(this).removeClass("over");
				}
	    );
	}// if document.all
	
    
    // more search options
    jQuery('#search-more').show();
    //jQuery("#search-more").hoverIntent({
    //     sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
    //     interval:    300, // number = milliseconds for onMouseOver polling interval    
    //      timeout:     500, // number = milliseconds delay before onMouseOut    
    //      over: searchMoreShow, // function = onMouseOver callback (REQUIRED)    
    //     out: function () { return;} // function = onMouseOut callback (REQUIRED)    
    //});
    jQuery("#search-more").hover(searchMoreShow, function () {});
    jQuery('#search').hover(function () {}, searchMoreHide);

	// remove borders from radios
	jQuery("input[type='radio']").addClass("checkradio");

});// document ready / end jquery functions

function buildAccordian() {
    // Accordion nav opening and closing behavior
    jQuery('#nav ul.accordion > li > a').wrapInner("<span></span>");
    jQuery('#nav ul.accordion li').filter(":not(.on)").children('ul').hide().parent().addClass("off");
    jQuery('#nav ul.accordion li ul').siblings('a').click(function (e) {
        // If the anchor has a real href value, just go to the link (effectively disables the accordion behavior for this item)
        if(jQuery(this).attr('href') && jQuery(this).attr('href') != '' && jQuery(this).attr('href') != '#') return true;

        jQuery(this).siblings('ul').slideToggle(200, function () {
            if(jQuery(this).is(":visible")) {
                jQuery(this).parent().removeClass("off").addClass("on");
            } else {
                jQuery(this).parent().removeClass("on").addClass("off");
            }
            ieFixNavContact();
        });
        e.stopPropagation();
        return false;
    });
    ieFixNavContact();
    

}

function buildDropdown() {

    // Build the fancy department dropdown using the normal select box
	jQuery('#nav-departments fieldset').after('<div id="department-widget"></div>');
	jQuery('#nav-departments select').each(function(){itterateChildren(this, document.getElementById('department-widget'));});
    
    // Assign behavior to the dropdown    
	jQuery('#currentvalue, #dropdown').click(function(e){
		e.stopPropagation(); // don't fire the window click event when you're in here
		jQuery('#dropdown').is(':hidden') ? 	
            //jQuery('#dropdown').slideDown(300) : jQuery('#dropdown').slideUp(300);
            jQuery('#dropdown').show() : jQuery('#dropdown').hide();
	});
	jQuery(document).click(function(){
		if (jQuery('#dropdown').is(':visible')) { 
            //jQuery('#dropdown').slideUp('fast'); 
            jQuery('#dropdown').hide(); 
        }
	});
	jQuery('#nav-departments fieldset, #dropdown').hide();

}

function itterateChildren(elem, insertPoint) {
	jQuery(elem).children().each(function() { 
		if (this.id == 'default') {
			// this puts the selected option in the select into a span that will be visible as in a normal dropdown
			jQuery(insertPoint).append('<span id="currentvalue">'+jQuery(this).text()+'</span>');
			var parentUL = document.createElement('UL');
			parentUL.id = "dropdown";
			jQuery(insertPoint).append(parentUL);
		} else {
			//this itterates all the other children of the select, placing options in a's wrapped in li's
			//and placing the label of each optgroup in an li with no a; all options within optgroups are nested in
			//ul's nested in the optgroup's li.
			if (insertPoint.id == "department-widget") { insertPoint = document.getElementById('dropdown'); }
			if (this.tagName == "OPTION"){
				var current=jQuery(this);
				jQuery(insertPoint).append('<li><a href="'+current.attr('value')+'">'+current.text()+'</a></li>\n');
			} else if (this.tagName == "OPTGROUP") {
				var current = jQuery(this);
				var newUL = document.createElement('UL');
				jQuery(insertPoint).append('<li>'+current.attr('label')+'</li>');
				jQuery(insertPoint).children('li:last').append(newUL);
				itterateChildren(this, newUL);
			}
		}
	});
}

// Readjusts the position of the contact info div in the left column for ie6 
// (which has problems with bottom relative positioning). This is functional, but
// needs some visual work.
function ieFixNavContact() {
    if(jQuery.browser.msie && parseFloat(jQuery.browser.version) < 7) {
        jQuery('#nav div.contact').css('top', jQuery('#footer').offset().top - 130);
    }
}


function searchMoreShow() {
    //jQuery('#search ul.links').slideDown("normal");
    jQuery('#search ul.links').show();
}
function searchMoreHide() {
    //jQuery('#search ul.links').slideUp("normal");
    jQuery('#search ul.links').hide();
}

// clear search field on click
jQuery.fn.inputClear = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
};

// hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+ <http://cherne.net/brian/resources/jquery.hoverIntent.html>
(function(jQuery){jQuery.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=jQuery.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){jQuery(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;jQuery(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{jQuery(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);


function doconvertcase() //used for converting search strings to lowercase prior to search activation. Verity is case sensitive but will find all if string is converted to lowercase.
{
	newstring=document.theform.srchstring.value
	document.theform.srchstring.value = newstring.toLowerCase();
}
