function externalLinks() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i];
   var rel = anchor.getAttribute("rel");
   if (anchor.getAttribute("href") && rel)
	if(anchor.getAttribute("rel").match(/\bexternal\b/)) 
     anchor.target = "_blank";
 } 
} 
YAHOO.util.Event.addListener(window, "load", externalLinks, window);
YAHOO.util.Event.addListener(window, "load", PageHeight, window);


function elementHasClass(element, className)
{
	return element.className==className
				|| element.className.indexOf(className+' ')!=-1
				|| element.className.indexOf(' '+className)!=-1;
}

function addClassToElement(element, className)
{
	if(!elementHasClass(element, className))
	{
		if(element.className!="")
			element.className+=" "+className;
		else
			element.className=className;
	}
}


function removeClassFromElement(element, className)
{
	element.className = element.className.replace(new RegExp("^((?:.*\\s)?)"+className+"((?:\\s.*)?)$"), "$1$2");
}

function setupSelectOnFocusTextBox(textboxId, defaultText)
{
  var textbox = document.getElementById(textboxId);
  textbox.defaultText= defaultText;
  textbox.onfocus = function()
  {
    if(this.value == this.defaultText)
      this.value = "";
    else
      this.select();
  }
  textbox.onblur = function()
  {
  	if(this.value == "")
  		this.value = this.defaultText;
  }
}

function setupDropDown(parentElementId, dropDownElementId)
{
  var parentElement = document.getElementById(parentElementId);
  var dropDownElement = document.getElementById(dropDownElementId);
  parentElement.dropDownElement = dropDownElement;
  parentElement.onmouseover = function()
  {
  	/*if(parseFloat(navigator.appVersion.split("MSIE")[1]) < 8){
	    this.dropDownElement.style.left = '-1px';
	    this.dropDownElement.style.top = '22px';
	} else {*/
		if(this.className == 'last') {
			this.dropDownElement.style.left = '-109px'; 
		} else {		
			this.dropDownElement.style.left = '-1px'; 
		}
		this.dropDownElement.style.top = '22px';
	//}
    removeClassFromElement(this.dropDownElement, "subnavmainhidden");
  }
  parentElement.onmouseout = function() 
  {
    addClassToElement(this.dropDownElement, "subnavmainhidden");
  }
}
 

function setupPopup(emailFriendLinkId, emailFriendFormId)
{
	var emailFriendLink = document.getElementById(emailFriendLinkId);
	var emailFriendForm = document.getElementById(emailFriendFormId);
	
	emailFriendLink.onclick = function()
	{
		if(elementHasClass(emailFriendForm, 'invisible')) {
			removeClassFromElement(emailFriendForm, 'invisible');
		}
		else {
			addClassToElement(emailFriendForm, 'invisible');
		}
	}
}

function setupDraw(drawElementId, openCloseLinkId, distanceToMove)
{
    var animationDuration = 0.25;
    var drawElement = document.getElementById(drawElementId);
    var openCloseElement = document.getElementById(openCloseLinkId);
    
    var openAnimation = new YAHOO.util.Motion(drawElementId, {points: { by: [0, distanceToMove] } }, animationDuration);
    var closeAnimation = new YAHOO.util.Motion(drawElementId, {points: { by: [0, -distanceToMove] } }, animationDuration);
    
    openCloseElement.openAnimation = openAnimation;
    openCloseElement.closeAnimation = closeAnimation;
    openCloseElement.drawIsOpen = false;
    openCloseElement.onclick = function()
    {
        if(this.drawIsOpen)
        {
            //Close the draw
            this.closeAnimation.animate();
            this.innerHTML = "Open Search";
            this.title = "Open the search bar";
        }
        else
        {
            //Open the draw
            this.openAnimation.animate();
            this.innerHTML = "Close Search";
            this.title="Close the search bar";
        }
        this.drawIsOpen = !this.drawIsOpen;
        return false;
    }
}

function setupExpandOnClick(expandingElementId, openCloseLinkId, noun)
{
    var animationDuration = 0.25;
    var expandingElement = document.getElementById(expandingElementId);
    var openCloseElement = document.getElementById(openCloseLinkId);
    
    var openAnimation = new YAHOO.util.Anim(expandingElementId, {height: { to: 0 } }, animationDuration);
    var closeAnimation = new YAHOO.util.Anim(expandingElementId, {height: { to: 0 } }, animationDuration);
    
    expandingElement.style.height = "0";
    
    openCloseElement.openAnimation = openAnimation;
    openCloseElement.closeAnimation = closeAnimation;
    expandingElement.closeAnimation = closeAnimation;
    openCloseElement.expandingElement = expandingElement;
    openCloseElement.expanderIsOpen = false;
    openCloseElement.onclick = function()
    {
        if(this.expanderIsOpen) {
        	this.closeAnimation.animate();
        } else {
            //Open the expander
            this.expandingElement.style.top = (this.expandingElement.offsetParent.offsetTop + 140) + 'px';
            this.expandingElement.style.left = (this.expandingElement.offsetParent.offsetLeft) + 'px';
            this.expandingElement.style.visibility = "hidden";
            this.expandingElement.style.height = "auto";
            var expanderHeight = this.expandingElement.offsetHeight;
            this.expandingElement.style.height = "0";
            this.expandingElement.style.visibility = "visible";
            this.openAnimation.attributes.height = { to : expanderHeight };
            this.openAnimation.animate();
        }
        this.expanderIsOpen = !this.expanderIsOpen;
        return false;
    }    
}

function setupExpander(expandingElementId, openCloseLinkId, noun)
{
	if(parent.document.URL.indexOf('print') > 0)
		return;
    var animationDuration = 0.25;
    var expandingElement = document.getElementById(expandingElementId);
    var openCloseElement = document.getElementById(openCloseLinkId);
    
    var openAnimation = new YAHOO.util.Anim(expandingElementId, {height: { to: 0 } }, animationDuration);
    var closeAnimation = new YAHOO.util.Anim(expandingElementId, {height: { to: 0 } }, animationDuration);
    
    expandingElement.style.height = "0";
    
    openCloseElement.openAnimation = openAnimation;
    openCloseElement.closeAnimation = closeAnimation;
    expandingElement.closeAnimation = closeAnimation;
    openCloseElement.expandingElement = expandingElement;
    openCloseElement.expanderIsOpen = false;
    openCloseElement.onmouseout = function()
    {
	    this.expandingElement.style.visibility = "hidden";
    	this.expandingElement.style.height = "0";
    	
    }
    openCloseElement.onmouseover = function()
    {
        
            //Open the expander
            this.expandingElement.style.top = (this.expandingElement.offsetParent.offsetTop + 140) + 'px';
            this.expandingElement.style.left = (this.expandingElement.offsetParent.offsetLeft) + 'px';
            this.expandingElement.style.visibility = "hidden";
            this.expandingElement.style.height = "auto";
            var expanderHeight = this.expandingElement.offsetHeight;
            this.expandingElement.style.height = "0";
            this.expandingElement.style.visibility = "visible";
            this.openAnimation.attributes.height = { to : expanderHeight };
            this.openAnimation.animate();
        
        //this.expanderIsOpen = !this.expanderIsOpen;
        return false;
    }
    expandingElement.onmouseover = function()
    {
    	this.style.visibility = "visible";
    	this.style.height = "auto";
    }
    
    expandingElement.onmouseout = function()
    {
	    this.style.visibility = "hidden";
    	this.style.height = "0";
    }
}

function setupAutoCompleteBox(inputId, containerId, wrapperId, selectedCourseLinkId, defaultText)
{
	YAHOO.util.Connect.asyncRequest('GET', 'http://pgstudy.nottingham.ac.uk/school/autocompletehandler.ashx', 
	{ 
		success: function(evt) { 
			var responseObj = eval('['+evt.responseText+']');
			var dataSource = new YAHOO.util.LocalDataSource(responseObj);
			dataSource.responseSchema = {fields : ["name", "id", "url"]};
			var input = YAHOO.util.Dom.get(inputId);
			var wrapper = YAHOO.util.Dom.get(wrapperId);
			var link = YAHOO.util.Dom.get(selectedCourseLinkId);
			var autoCompleterConfig = { allowBrowserAutocomplete:true, forceSelection:false, queryMatchContains:true };
			var autoCompleter = new YAHOO.widget.AutoComplete(input, containerId, dataSource, autoCompleterConfig);			
  			input.defaultText= defaultText;
			
			autoCompleter.itemSelectEvent.subscribe(function(type, args) {
		        var selectedItemData = args[2];
		        window.location = selectedItemData[2];
		    });
		    autoCompleter.textboxKeyEvent.subscribe(function(){
		        YAHOO.util.Dom.removeClass(wrapper, "courseselected");
		    });
		    autoCompleter.textboxBlurEvent.subscribe(function(textbox) {
		    	if(input.value == "")
			  		input.value = input.defaultText;
		    });
		    autoCompleter.textboxFocusEvent.subscribe(function(type, args) {
		    	if(input.value == input.defaultText)
      				input.value = "";
    			else
      				input.select();			
		    });
		}, 
		failure: function(o) { 
			alert('Failed to retrieve course data'); 
		} 
	},null);
}

function setupCourseSelector(data, inputId, suggestionContainerId, selectedCourseHolderId, selectedCourseLinkId, wrapperId)
{
    var dataSource = new YAHOO.util.LocalDataSource(data); 
    dataSource.responseSchema = {fields : ["name", "id", "url"]};
    
    var wrapper = YAHOO.util.Dom.get(wrapperId);
    var input = YAHOO.util.Dom.get(inputId);
    var link = YAHOO.util.Dom.get(selectedCourseLinkId);
    var autoCompleterConfig = { allowBrowserAutocomplete:false, forceSelection:true, queryMatchContains:true };
    var autoCompleter = new YAHOO.widget.AutoComplete(input, suggestionContainerId, dataSource, autoCompleterConfig);
    
    var selectedCourseHolder = YAHOO.util.Dom.get(selectedCourseHolderId);
    
    autoCompleter.itemSelectEvent.subscribe(function(type, args) {
        var selectedItemData = args[2];
        selectedCourseHolder.value = selectedItemData[1];
        link.href = selectedItemData[2];
        YAHOO.util.Dom.addClass(wrapper, "courseselected");
        input.select();
    });
    autoCompleter.textboxKeyEvent.subscribe(function(){
        YAHOO.util.Dom.removeClass(wrapper, "courseselected");
    });
    YAHOO.util.Event.addListener(input, "focus", function(){this.select();});
}

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function PageHeight()
{
	var item = document.getElementById('sitewrapper');
	item.style.height = getDocHeight();
}
