$(document).ready(function(){
	
	//start dropdown behavior using the superfish plugin
	$('ul#topnav').superfish({
		autoArrows:    false,
	    dropShadows:   false,
	    delay:		   400
    });
    
    if($('#productbox').children().length == 0) {
    	$('#productbox').parent().hide();	
    }
    
    if($("#friendsend".length)){
    	$("#friendsend").toggle(function(){
    		$("#tellafriend").slideDown("slow");
    	},function(){
    		$("#tellafriend").slideUp("slow");
    	});
    }
        
    if($('#directoryexp').length) {
    	
    	//fixes EE's inability to sort categories alphabetically.
    	//if they fix it we can remove this on: researcher_directory.
    	$('#directoryexp').change(function(){
    	$("#facinform").attr('action', $("#facinform").attr("rel")+"/"+$(this).val());
			//console.log($(this).val());
		});
    	    	
    	var $selectlist = $('#directoryexp');
    	var $original = $selectlist.val();
    	var $options = $('option', $selectlist);
    	var expertise = [];
    	
    	$options.each(function(){
    		expertise.push({
    			val: $(this).val(),
    			text: $(this).text()
    		});
    		//console.log($(this).text());
		});
		
		expertise.sort(function(a, b){
		    if(a.text>b.text){
		        return 1;
		    }
		    else if (a.text==b.text){
		        return 0;
		    }
		    else {
		        return -1;
		    }
		});
		
		// loop through the sorted array and set the text/values to the options
	    for (var i = 0, l = expertise.length; i < l; i++) {
	        $($options[i]).val(expertise[i].val).text(expertise[i].text);
	    }
	
	    // set the selected value back
	    //$dd.val($original);
		
    }
	
	//$("a:external:not(#credits a, #topnav a, #header a, #footer a, h2 a, h3 a, #sharethis a)").addClass("extlink").attr("target", "_blank");
	//$("a[href^='http://otrec.us'], a[href^='http://www.otrec.us']").removeClass("extlink");
	//$("a[href$='.pdf']").addClass("pdflink");
	//$("a[href$='.doc']").addClass("doclink");
	//$("a[href$='.ppt']").addClass("pptlink");
	//$("a[href$='.xls']").addClass("xlslink");
	//$("a:mailto").addClass("maillink");	
	
	//faculty filter
	$("#facinform").submit(function(){
		
		var $query = "";
		var i = 0;
		
		$('input:checkbox:checked').each(function(){
			if (i != 0){$query += "|"}
			$query += $(this).val();
			i++;
		});
		
		if($('input:checkbox:checked').length != 0){
			
			$("#filter").val($query);
			
		}else{
			
			$("#filter").val("XX");
			
		}
		//alert($("#filter").val());
		
		//return false;
	})
	
	//hide fb tab
	if($("#fbbox").length){
		$("#fbbox").hide();
		
		$("#fbtab").click(function(){
			$("#twbox").slideUp("fast");
			$("#fbbox").slideDown("fast");
			$("#tabnav .current").removeClass("current");
			$(this).addClass("current");
			return false;
		});
		
		$("#twtab").click(function(){
			$("#fbbox").slideUp("fast");
			$("#twbox").slideDown("fast");
			$("#tabnav .current").removeClass("current");
			$(this).addClass("current");
			return false;
		});
	}
	
	//hide browse tab
	if($("#researchsearch").length){
		
		if($("#browseform").hasClass("first")){
			$("#researchsearch").hide();
		}else{
			$("#browseform").hide();
		}
		
		$("#stab").click(function(){
			$("#browseform").slideUp("fast");
			$("#researchsearch").slideDown("fast");
			$("#tabnav .current").removeClass("current");
			$(this).addClass("current");
			return false;
		});
		
		$("#btab").click(function(){
			$("#researchsearch").slideUp("fast");
			$("#browseform").slideDown("fast");
			$("#tabnav .current").removeClass("current");
			$(this).addClass("current");
			return false;
		});
		
	}
	
	//project page truncate abstract code
	if($("#showabstract").length){
		
		$("#showabstract").show();
		$("#abstractshort").show();
		$("#projectabstract").hide();
		
		$("#showabstract a").toggle(function(){
				//alert("open");
				$("#abstractshort").hide();
				$("#projectabstract").slideDown("slow");
				$(this).html("&uarr; collapse text");
				return false;
			},function(){
				//alert("close");
				$("#abstractshort").show();
				$("#projectabstract").slideUp("fast");
				$(this).html("&darr; expand text");
				return false;
		});	
		
	}

	//get number of images in group
	$num_imgs = $("#slideshow div div").length;
	i = 0;
	p = 0;
	
	$(window).load(function(){
		if ($num_imgs > 1){
			startShow();
		}
	});
	
	if ($num_imgs > 1){
		
		//do nav fixer
		$("#slideshownav li").each(function(){
			$(this).find("a").text($(this).index()+1);
		});
		
		$("#slideshownav li:first a").addClass("current");
		$("#slideshownav").show();
		
		//hide all images but the first one
		$("#slideshow div div").hide();
		$("#slideshow div div:first").show();
		
		$("#slideshow ul li a").click(function(){
		
			if( ! $(this).hasClass("current")){
					
				//stop current one
				$("#slideshow").stopTime("ishow");
				
				//start again from index provided by link
				i = $(this).text(); //get this index from link!
				i--;
				
				$("#slideshow div div.current").removeClass("current").fadeOut("slow");
				$("#slideshow div div:eq("+i+")").addClass("current").fadeIn("slow");
				//log("manually fade in index #" + i);
				
				//deal with links
				$("#slideshow a.current").removeClass("current");
				$(this).addClass("current");
				
				//start counter again
				startShow();
				
			}
			
			return false;
		
		});
	
	}//end if $num_imgs
	
	startShow = function(){
		//set timer to fade out and in images
		//after 4.5sec fade out CURRENT and fade in NEW
		$("#slideshow").everyTime(4500, "ishow", function(){
			
			if(i == $num_imgs-1){p=0;}else{p=i+1;}
			
			$("#slideshow div div:eq("+i+")").removeClass("current").fadeOut("slow");
			$("#slideshow div div:eq("+p+")").addClass("current").fadeIn("slow");
			//log("auto fade in index #" + i);
			
			//and links
			$("#slideshow a.current").removeClass("current");
			$("#slideshow ul a:eq("+p+")").addClass("current");
			
			if(i == $num_imgs-1){i=0;}else{i++;}
		})
	}

});
