function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
    }

}

function popup_help_full(helpid, x, y, width, height) {
    if (x == 0) x = 50;
    if (y == 0) y = 50;
    if (width == 0) width = 585;
    if (height == 0) height = 520;
    winOptions = "location=no,toolbar=yes,menubar=no,resizable=yes,scrollbars=yes,height=" + height + ",width=" + width;

    if (navigator.appName == "Netscape")
        winOptions += ",screenX=" + x + ",screenY=" + y;
    else
        winOptions += ",left=" + x + ",top=" + y;

    window.open("/Help/PopUp.aspx?help_id=" + helpid, "pop", winOptions);
}

/* get, set, and delete cookies */
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
	
function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}
	
function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

/*	Scrolly stuff	*/
function autoScrollY()
{
	var currentY = currentYOffSet();
	
	//	only scroll if currenty == 0 to stop refresh bug
	if (currentY == 0 && window.scrollBy) {

		var strQs = '';
		var intScrollY = 0;
		if (window.location && window.location.search) {
			strQS = window.location.search;
			intScrollY = extractQueryTerm (strQS, 'Y');
		}
		if (intScrollY > 0) {
			window.scrollBy (0, intScrollY);
		}
	}
}

function extractQueryTerm (strQS, strName)
{
	var intStart;
	var intEnd;
	var intHashEnd;
	var intAmpEnd;
	var intQstnEnd;
	var strSearch;
	var strValue;

	strQS += "#";
	strSearch = strName + "=";
	intStart = (strQS.indexOf (strSearch)) + strSearch.length;

	intHashEnd = strQS.indexOf ("#", intStart);
	intAmpEnd = strQS.indexOf ("&", intStart);
	intQstnEnd = strQS.indexOf ("?", intStart);

	intEnd = intHashEnd;

	if ((intQstnEnd != -1) && (intQstnEnd < intEnd)) {
		intEnd = intQstnEnd;
	}
	if ((intAmpEnd != -1) && (intAmpEnd < intEnd)) {
		intEnd = intAmpEnd;
	}

	strValue = strQS.substr (intStart, (intEnd - intStart));
	return strValue;
}

function currentYOffSet () {
	var pxY;
	if (document.documentElement && document.documentElement.scrollTop) {
		pxY = document.documentElement.scrollTop;	
	} else if (window.pageYOffset) { 
		pxY = window.pageYOffset;	
	} else if (document.body) {
		pxY = document.body.scrollTop;
	}
	else {
		pxY = 0;
	}
	return pxY;
}

function persistYOffSet (link)
{
	var pxY = currentYOffSet();
	return link.href + '&Y=' + pxY;	
}

function redirectY (link) {

	if (window.location && this && link ) {

		window.location = persistYOffSet (this);

		//	stop normal link action
		link.returnValue = false;
		if (link.preventDefault) {
			link.preventDefault();
		}
		return false;
	}
}

function hookUpLinks (linksToHookUp) {
	var links = linksToHookUp.split(",");
	for (var i=0;i<links.length;i++) {
		if (document.getElementById(links[i])) {
		    jQuery("#" + links[i]).click(redirectY);
		}
	}
}
/* Reset attribute search fields */
function resetSearch()
{
   resetSearch("sidebarSearch")
}
/* Reset attribute search fields */
function resetSearch(nameOfFormToReset)
{
	var myForm = document.getElementById(nameOfFormToReset);
	if (!myForm || !myForm.reset || !myForm.elements) 
		return;

    myForm.reset();
    
	var arrElems = myForm.elements;
	if (!arrElems) 
		return;
		
    // Reset all input fields    
    for( var i = 0 ; i < arrElems.length ; i++){

		switch (arrElems[i].type.toLowerCase()) {
			case  "text":
			case  "password":
			case  "textarea":
				arrElems[i].value="";
				break;
			case  "radio":
			case  "checkbox":
				arrElems[i].selected = false;
				arrElems[i].checked = false;
				break;
			default:
				arrElems[i].selectedIndex = 0;
				break;
		}
		
		if(arrElems[i].onchange) {
			arrElems[i].onchange();
		}
    }
}

function changeLayout(description) {
    var i, a;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
        // ignore any without a title attribute or not alternative style
        var title = a.getAttribute("title");
        if (title == "" || title == null) {
            continue;
        } else {
            if (a.getAttribute("title") == description) { a.disabled = false; }
            else if (a.getAttribute("title") != "default") { a.disabled = true; }
        }
    }
}

(function($) {

    var browseShown = false;

    $(document).ready(function() {

        $("#LoginLink").click(function() { showLoginBox(); return false; });
        $("#LoginClose").click(hideLoginBox);
        $("#BrowseDropDown").click(function(e) { e.stopPropagation(); e.preventDefault(); showBrowseBox(); return false; });
        fixNavbarStates();

    });

    function fixNavbarStates() {
        $("#SiteNavigation li a span").mousedown(function(e) {
            if (e.target.nodeName != "EM") {
                $(this).parent().addClass("active");
            }
        });

        $("#SiteNavigation li a span").bind("mouseup mouseout", function(e) {
            if (e.target.nodeName != "EM") {
                $(this).parent().removeClass("active");
            }
        });

        $("#SiteNavigation li #BrowseDropDown").mousedown(function(e) {
            $(this).addClass("active");
        });
        $("#SiteNavigation li #BrowseDropDown").bind("mouseup mouseout", function(e) {
            $(this).removeClass("active");
        });
    }


    function showLoginBox() {
        $("#LoginModalBox").bgIframe();
        $("#LoginModalBox").css("display", "block");
        $("#LoginModalBox").click(function(e) {
            e.stopPropagation();
        });
        $("#modal_email").focus();
        $(document).click(hideLoginBox);
    }

    function hideLoginBox(e) {
        if (e.button != 2) {
            $("#LoginModalBox").css("display", "none");
            $(document).unbind("click", hideLoginBox);
        }
    }

    function showBrowseBox() {
        if ($("#BrowseLinksBox").css("display") == "block") {
            $(document).click();
        } else {
            // position it in the correct place
            var position = $(".BrowseLinkItem").position();
            var ulPosition = $("#SiteNavLinks").position();
            var divNav = $("#SiteNavigation").position();

            var top = position.top + divNav.top + $(".BrowseLinkItem").height() + 2;
            var left = position.left + ulPosition.left + divNav.left - 1;
            $("#BrowseLinksBox").css("top", top);
            $("#BrowseLinksBox").css("left", left);

            $("#BrowseLinksBox").bgIframe();
            $("#BrowseLinksBox").css("display", "block");
            // set the browse box to the hover state
            $(".BrowseLinkItem").addClass("PermanentHover");
            $(document).click(hideBrowseBox);
        }

    }

    function hideBrowseBox(e) {
        if (e.button != 2) {
            $("#BrowseLinksBox").css("display", "none");
            $(".BrowseLinkItem").removeClass("PermanentHover");
            $(document).unbind("click", hideBrowseBox);
        }
    }

})(jQuery);

function AjaxLoadScript(url) {

    jQuery.ajax({
        type: 'GET',
        url: url,
        cache: true,
        dataType: 'script',
        data: null
    }); 

}

/* Some convenience functions for adding dates and times. */

/* Date extensions 
 **********************/

Date.prototype.add = function(milliseconds){
	var newdate = Date.now();
	newdate.setTime(this.getTime() + milliseconds);
	return newdate;
}

Date.prototype.subtract = function(milliseconds){
	var newdate = Date.now();
	newdate.setTime(this.getTime() - milliseconds);
	return newdate;
}

// 12am midnight
Date.prototype.startOfDay = function(){
	return new Date(this.getFullYear(), this.getMonth(), this.getDate());
}

// 1 millisecond before midnight.
Date.prototype.endOfDay = function(){
	var sod = this.startOfDay();
	return sod.add((1).day()).subtract(1);
}

Date.now = function(){
	return new Date();
}

Date.shortMonthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
Date.shortDayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];

/* Number extensions
 **********************/

Number.prototype.seconds = function(){ return this * 1000 }
Number.prototype.second = function(){ return this.seconds() }

Number.prototype.minutes = function(){ return this * (60).seconds() }
Number.prototype.minute = function(){ return this.minutes() }

Number.prototype.hours = function(){ return this * (60).minutes() }
Number.prototype.hour = function(){ return this.hours() }

Number.prototype.days = function(){ return this * (24).hours() }
Number.prototype.day = function(){ return this.days() }

Number.prototype.weeks = function(){ return this * (7).days() }
Number.prototype.week = function(){ return this.weeks() }

Number.prototype.fortnights = function(){ return this * (2).weeks() }
Number.prototype.fortnight = function(){ return this.fortnites() }

Number.prototype.months = function(){ return this * (30).days() }
Number.prototype.month = function(){ return this.months() }

Number.prototype.years = function(){ return this * (365.25).days() }
Number.prototype.year = function(){ return this.years() }

Number.prototype.from = function(date){ return date.add(this) }
Number.prototype.fromNow = function(){ return this.from(Date.now()) }

Number.prototype.before = function(date){ return date.subtract(this) }
Number.prototype.ago = function(){ return this.before(Date.now()) }
