/*
A generic addEvent function
*/

function addEvent( obj, type, fn ) {
  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] );
  } else
    obj.addEventListener( type, fn, false );
}
function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}


/*
For show-hide function
*/

// The first function defines a custom show function which fixes the IE cleartype bug from blog.bmn.name 
(function($) {
	$.fn.customShow = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);

$(document).ready(function() {
  $(".hideable").before("<p class='showbox'><a href='#'>Read more.</a></p>");
  $(".hideable").append("<p class='hidebox'><a href='#'>Hide this.</a></p>");
  $(".hideable").hide();
  
  $("p.showbox").click(function() {
	$(this).next().customShow("fast");
	$(this).hide("fast");
	return false;
	});

  $("p.hidebox").click(function() {
	$(this).parent().hide("fast");
	$("p.showbox").customShow("fast");
	return false;
    });

});


/*
Create new window for new window function
*/
function openInNewWindow() {
	var newWindow = window.open(this.getAttribute('href'),'newWindow','height=700,width=900,left=100,top=100,resizable=yes,scrollbars=yes,location=yes,toolbar=yes,menubar=yes,status=yes');
    newWindow.focus();
    return false;
}

/*
Add the openInNewWindow function to the onclick event of links with a class name of "new-window"
*/
function getNewWindowLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById) {
		// Find all links
		var links = document.getElementsByTagName('a');
		var link;
		for (var i = 0; i < links.length; i++) {
			link = links[i];
			// Find all links with a class name of "new-window" and add image and add title info
			if (/\bnew\-window\b/.exec(link.className)) {
				link.title += "\n (opens in a new window)";
				link.onclick = openInNewWindow;
			var img = document.createElement("img");
			img.setAttribute("src", "images/new-win-icon.gif");
			img.setAttribute("alt", "(opens in a new window)");
			link.appendChild(img);
			}
		}
	}
}
addEvent(window, 'load', getNewWindowLinks);