function externalLinks() { 
	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a"); 
	for (var i = 0; i < anchors.length; i++) { 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank"; 
		}
	}
}

if (window.addEventListener) {
	window.addEventListener('load', externalLinks, false); 
} else if (window.attachEvent) { 
	window.attachEvent('onload', externalLinks);
}

function popup(pageUrl, width, height) {
	var left = screen.width / 2 - width / 2;
	var top = screen.height / 2 - height / 2;
	
	window.open(pageUrl, '', 'width=' + width + ',height=' + height + ',top=' + top + ',left=' + left + ",scrollbars=yes,resizable=no");
}

function getUrlFragment() {
	if (window.location.hash != undefined &&
		window.location.hash != '') {
		var hasHash = window.location.hash.indexOf('#');
		if (hasHash == -1) {
			return window.location.hash;
		} else if (window.location.hash.length > 1) {
			return window.location.hash.substr(hasHash + 1);
		} else {
			return false;
		}
	}
	return false;
}

// two string methods from http://www.ditchnet.org/wp/2005/04/04/i-want-my-javalang/

/**
 *  String convenience method to trim leading and
 *  trailing whitespace.
 *  @returns string
 */
String.prototype.trim = function() { 
	return this.replace(/^\s+|\s+$/g, '');
};

/**
 *  String convenience method for checking if the
 *  end of this string equals a given string.
 *
 *  @returns boolean
 *  @throws IllegalArgumentException for parameters
 *                          not of type String
 */
String.prototype.endsWith = function (s) {
    if ('string' != typeof s) {
        throw('IllegalArgumentException: Must pass a ' +
            ' string to String.prototype.endsWith()');
    }
    var start = this.length - s.length;
    return this.substring(start) == s;
};

/* cookie methods from http://www.quirksmode.org/js/cookies.html */

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

$(document).ready(function() {
	$('a > img.resizeThumbnail').click(function() {
		$('div.highlightImage').remove();
		var href = $(this).parent().attr('href');
		$theImg = $('<img src="' + href + '" style="border: 2px solid white" alt="" />');
		$bg = $('<div style="cursor: pointer; position: absolute; left: 0; top: 0; width: 100%; height: 100%; background-color: black;" />').css('opacity', '0.5');
		$(window).scroll(function() {
			$bg.css('top', $(this).scrollTop() + "px");
		});
		$bg.css('top', $(window).scrollTop() + "px");
		$img = $('<div style="cursor: pointer; position: absolute; left: 0; top: 100px; width: 100%; text-align: center;" class="highlightImage"></div>');
		$img.append($theImg);
		$close = $('<a href="#" style="position: absolute; margin-left: -24px; margin-top: 6px;"><img src="/images/close.png" alt="" width="18" height="18" /></a>');
		$theImg.after($close);
		$('body').append($bg);
		$('body').append($img);
		$bg.click(function() {
			$('.highlightImage').remove();
			$(this).remove();
			return false;
		});
		$img.click(function() {
			$bg.remove();
			$(this).remove();
			return false;
		});
		return false;
	});
});
