function rollover(img){
	oldsrc = img.src;
	img.src = oldsrc.replace("1.", "2.");
}

function rollout(img){
	oldsrc = img.src;
	img.src = oldsrc.replace("2.", "1.");
}

/*****************
 * Custom dropdown - beginning
 *
 ****************/
 function custom_dropdowns(theid){
 	theform = document.getElementById(theid);
	
	if(!theform){
		return false;
	}
	
	var thechildren = theform.elements;
	var replacements = new Array();
	
	for(var i=0; i < thechildren.length; i++){
	
		if(thechildren[i].nodeName == "SELECT"){
			var childrennodes = thechildren[i].options;
			
			replacements[i] = "<ul>";
			
			for(var c = 0; c < childrennodes.length; c++){
				
					replacements[c] += "<li>" + childrennodes[c].value + "</li>";
				
			}
			
			replacements[i] += "</ul>";
			
		}
	}
	
 }
 
 function truncate_onchange(theid, limit){
 	var theselect = document.getElementById(theid);
	var newword = "";
	
	theselect.onblur = function(){
		
		this.thisid = this.selectedIndex;
		
		var thisword = this[this.thisid].text.length;
		
		this.orig = this[this.thisid].text;
		
		if(this.orig.length > limit){
			newword = this.orig.substring(0, limit);
			this[this.thisid].text = newword;
			
		}
	}
	
	
	theselect.onfocus = function(){
		if(this.orig != null && this.thisid != null){
			this[this.thisid].text = this.orig;
		}
	}
	
	theselect.focus();
	theselect.blur();
	
 }


/**********************
 * set up forms so they auto-populate with the label, and clear the default text when clicked
 * @params: id of form, array of default values
 *********************/
function setup_form(theid, thedefaults){
	
	theform = document.getElementById(theid);
		
	if(!theform){
		return false;
	}
	
	var thechildren = theform.elements;
	
	var childrennodes = "";
	
	for(var i = 0; i < thechildren.length; i++){
		if(thedefaults[thechildren[i].name] != null){
			thechildren[i].thedefault = (thedefaults[thechildren[i].name] != "") ? thedefaults[thechildren[i].name] : "";
			thechildren[i].onfocus = clearform;
			thechildren[i].onblur = populatefield;
			thechildren[i].onblur();
		}
	}
	
}

/* called by seupform() - clears an input field when clicked */
function clearform(){
	if(this.value == this.thedefault){
		this.value = "";
	}
}

/* called by setupform() - populates default text in input field */
function populatefield(){
	if(this.value == ""){
		this.value = this.thedefault;
	}else{
		if(this.value != this.thedefault){
		}
	}
}

/********
 * Check all form fields and empty any that contain default values
 *******/
function check_form(theform){
	for (var i=0;i<theform.length;i++){
		if(theform.elements[i].thedefault){
			if(theform.elements[i].value == theform.elements[i].thedefault){
				theform.elements[i].value = "";
			}
		}
	}
	return true;
}


/**********************
 *
 * THE NAVIGATION
 *
 **********************/
//http://www.learningjquery.com/2007/09/animated-scrolling-with-jquery-12
$(document).ready(function(){
  $('#nav a').click(function() {
	if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
	  var $target = $(this.hash);
	  $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
	  
		if ($target.length) {
			var targetOffset = $target.offset().top - 50;
			$('html,body').animate({scrollTop: targetOffset}, 500, 'easeOutQuad', nav_highlight);
	   
		 	var currentid = this.hash.slice(1);
			
		 	return false;
	  }
		
	}
	
	function nav_highlight(){
		$( "#nav li" ).each(
			function( i ){
			
				var thisid = this.id;
				
				if( thisid.replace("nav_", "") == currentid){
					$("#" + thisid).addClass ("current");
					//document.location = location.hostname + "#" + currentid;
				}else{
					$("#" + thisid ).removeClass("current");
				}
				
			}
		);
	}
	
  });
	
	var iebody=(document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
	
	// Get y position of each anchor
	var ys = new Array();
	ys["top"] = $('#top').offset().top;
	ys["partner"] = $('#partner').offset().top;
	ys["network"] = $('#network').offset().top;
	ys["contact"] = $('#contact').offset().top;
	ys["downloads"] = $('#downloads').offset().top;
	
	
	$(window).scroll(function(){
	
		var thisy = document.all? iebody.scrollTop : pageYOffset;
		
		
		$( "#nav li" ).each(
			function( i ){
				
				var thisid = this.id;
				
				
				if( (Math.abs(thisy - ys[thisid.replace("nav_", "")]) < 180)){
				$("#" + thisid).addClass ("current");
				}else{
					$("#" + thisid ).removeClass("current");
				}
				
			}
		);
		
	});

	
});
