// William & Mary Template
// Common utility scripts

// USER NAV
// Dropdown Script - adapted from Suckerfish
// http://www.htmldog.com/articles/suckerfish/dropdowns/ 
		
	set_user_nav_dropdown = function() {
	
	 	if (document.getElementById('user-nav-dropdown-wrapper')){
			
		       	var target_element = document.getElementById("user-nav-dropdown-wrapper");
			
			// set mouseover events
			
			target_element.onmouseover=function() {  
				this.className+=" sfhover";
				}
				
			target_element.onmouseout=function() { 
	
				this.className=this.className.replace(new RegExp(" sfhover\\b"), ""); 
				}
		}
			
	}
		
	// attach dropdown event to window onload (only works in IE)
	
	if (window.attachEvent) window.attachEvent("onload", set_user_nav_dropdown);
        window.onload = set_user_nav_dropdown;
	
// PRIMARY NAV, ADMISSION NAV, ETC.
// Image Rollover Script

	function set_rollover(img_element, normal_filename, rollover_filename) {
	
		// preload image ?				
		
		// set mouseover event
		
     	img_element.onmouseover = function() { this.src=rollover_filename; }
        img_element.onmouseout = function() { this.src=normal_filename; }		
		
	}
	
// HOMEPAGE IMPACT WIDGET

	function show_hide_module(module_id) {
	
		if (document.getElementById(module_id)){
			var target_element = document.getElementById(module_id);
	
			if (target_element.style.display=="block") {
				target_element.style.display = "none";
			}
			else {
				target_element.style.display = "block";		
			}
		
			return false;
		}
	
	}	


        // Used on the W&M Blog and Student Blog index pages.
        // Assumption: 
        //  - there is a div on the calling page named orderedContainer where the sorted divs will be moved

        function divIdSort(classtosort) {
		    var allDivs = document.getElementsByTagName("div");
		    var len = allDivs.length;
		    var sortDivArray = new Array();
                    var y = 0;
		    for(var x=0;x<len;x++){
			    if(allDivs[x].className == classtosort) {
				    // append to my class array
				    sortDivArray[y] = allDivs[x].id;
                                    y++;
			    }
		    }

		    sortDivArray.sort();
                    sortDivArray.reverse();
		    var len = sortDivArray.length;
                    var newlocation = document.getElementById("orderedContainer");
		    if (newlocation){
                            for(var x=0;x<len;x++){
			            var el = document.getElementById(sortDivArray[x]);
			            newlocation.appendChild(el);
		            }
                    }
        } 


function videoAnchor()
{
    var anchor= $(location).attr("hash");
    var cleanAnchor = anchor.substring(1);
    var arrAnchors = cleanAnchor.split("#");
    myAnchor = arrAnchors[0];
    if (myAnchor != "more" && myAnchor != "")
    {
        // First, make sure there is a match. If not, skip the hiding.
        var checkId = "vidcat-"+ myAnchor;
        var gwarn = 0;
        if (document.getElementById(checkId))
        {
            gwarn = 1;
        }
        if (gwarn)
        {
            var allDivs = document.getElementsByTagName("div");
            for (var i=0; i < allDivs.length; i++)
            {
                if (allDivs[i].className.indexOf("clear-tall") != -1)
                {
                    allDivs[i].style.display = 'none';
                }
                if (allDivs[i].className.indexOf("video-category") != -1)
                {
                    if (allDivs[i].id != checkId)
                    {
                        allDivs[i].style.display = 'none';
                    }
                }
            }
            var myPage = window.location.href;
            var myPageClean = myPage.substr(0,myPage.indexOf('#'));

            document.write('<div class="clear-tall">&nbsp;</div><div class="more-cat-link"><p><a href="'+myPageClean+'">More video categories...</a></p></div><div class="clear-tall">&nbsp;</div>');
        } // end if gwarn
    } // end if !=more
} // end function videoAnchor

// William & Mary Template
// Common utility scripts

// USER NAV
// Dropdown Script - adapted from Suckerfish
// http://www.htmldog.com/articles/suckerfish/dropdowns/ 
		
	set_user_nav_dropdown = function() {
	
	 	if (document.getElementById('user-nav-dropdown-wrapper')){
			
		       	var target_element = document.getElementById("user-nav-dropdown-wrapper");
			
			// set mouseover events
			
			target_element.onmouseover=function() {  
				this.className+=" sfhover";
				}
				
			target_element.onmouseout=function() { 
	
				this.className=this.className.replace(new RegExp(" sfhover\\b"), ""); 
				}
		}
			
	}
		
	// attach dropdown event to window onload (only works in IE)
	
	if (window.attachEvent) window.attachEvent("onload", set_user_nav_dropdown);
        window.onload = set_user_nav_dropdown;
	
// PRIMARY NAV, ADMISSION NAV, ETC.
// Image Rollover Script

	function set_rollover(img_element, normal_filename, rollover_filename) {
	
		// preload image ?				
		
		// set mouseover event
		
     	img_element.onmouseover = function() { this.src=rollover_filename; }
        img_element.onmouseout = function() { this.src=normal_filename; }		
		
	}
	
// HOMEPAGE IMPACT WIDGET

	function show_hide_module(module_id) {
	
		if (document.getElementById(module_id)){
			var target_element = document.getElementById(module_id);
	
			if (target_element.style.display=="block") {
				target_element.style.display = "none";
			}
			else {
				target_element.style.display = "block";		
			}
		
			return false;
		}
	
	}

function videoAnchor()
{
    var anchor= $(location).attr("hash");
    var cleanAnchor = anchor.substring(1);
    var arrAnchors = cleanAnchor.split("#");
    myAnchor = arrAnchors[0];
    if (myAnchor != "more" && myAnchor != "")
    {
        // First, make sure there is a match. If not, skip the hiding.
        var checkId = "vidcat-"+ myAnchor;
        var gwarn = 0;
        if (document.getElementById(checkId))
        {
            gwarn = 1;
        }
        if (gwarn)
        {
            var allDivs = document.getElementsByTagName("div");
            for (var i=0; i < allDivs.length; i++)
            {
                if (allDivs[i].className.indexOf("clear-tall") != -1)
                {
                    allDivs[i].style.display = 'none';
                }
                if (allDivs[i].className.indexOf("video-category") != -1)
                {
                    if (allDivs[i].id != checkId)
                    {
                        allDivs[i].style.display = 'none';
                    }
                }
            }
            var myPage = window.location.href;
            var myPageClean = myPage.substr(0,myPage.indexOf('#'));

            document.write('<div class="clear-tall">&nbsp;</div><div class="more-cat-link"><p><a href="'+myPageClean+'">More video categories...</a></p></div><div class="clear-tall">&nbsp;</div>');
        } // end if gwarn
    } // end if !=more
} // end function videoAnchor


/*** A and S Dropdown ***/
//jQuery.noConflict();

    try{
	var jqueryIsLoaded=jQuery;
	jQueryIsLoaded=true;
    }
    catch(err){
	var jQueryIsLoaded=false;
    }
    if(jQueryIsLoaded){
	jQuery(document).ready(function(){
	    if (jQuery("#departmentDropdownMenu").length > 0) {
		jQuery("#departmentDropdownMenu ul").hide();
		jQuery("#departmentDropdownMenu #deptAndProgHome").hover(function(){
		    jQuery("#departmentDropdownMenu ul").show();
		}, function(){
		    jQuery("#departmentDropdownMenu ul").hide();
		});
	    }
	    if (jQuery("#wm-photoset").length > 0) {
	    	jQuery("#wm-photoset").wmPhotoset();
	    }
	});
    }
