$.fn.ExternalLinks = function() 
{
    $("a[rel]").each(function() 
	{
        if (this.getAttribute("rel").indexOf("external") > -1) {
            this.target = "_blank";
        }
    });
};

$.fn.BookmarkPage = function() 
{
	return this.each(function()
	{
		$(this).click(function()
		{
			if (window.sidebar) //Mozilla Firefox
			    window.sidebar.addPanel(window.document.title, window.document.location, "");
	        else
			    window.external.AddFavorite(window.document.location, window.document.title);
			return false;
	    });
	});
};

$.fn.EmailPage = function() 
{
	return this.each(function()
	{
		$(this).click(function()
		{
			var mailto = "mailto:?subject=Recommended Link from " + document.title;
			mailto += "&body=I saw this page on the " + document.title;
			mailto += " website. You should check it out at, " + location.href; 
			location.href = mailto;
			return false;
	    });
	});
};

$.fn.PrintPage = function() 
{
	return this.each(function()
	{
		$(this).click(function()
		{
			window.print();
			return false;
	    });
	});
};

$.fn.SearchBox = function(options) {
    var defaults = {
        initialText: "Enter keywords"
    };
    options = $.extend(defaults, options);
	
    return this.each(function() 
	{
        $(this).focus(function() 
		{
            if (this.value == options.initialText)
                this.value = "";
            else
                this.select();
        });
        $(this).blur(function() 
		{
            if (this.value == "")
                this.value = options.initialText;
        });
    });
};

$.fn.YouTubePlaylist = function(options) 
{
    var defaults = 
	{
		holderId: 'youtubepanel',
		playerHeight: '250',
		playerWidth: '300'
    };
    options = $.extend(defaults, options);
	
    return this.each(function() 
	{	
		function LoadVideo(youTubeID)
		{
			var autoPlay = (options.autoPlay) ? "&autoplay=1" : "";
			var fullScreen = (options.allowFullScreen) ? "&fs=1" : "";
				
		    var videoURL = "http://www.youtube.com/v/" + youTubeID + "&hl=en_GB&autoplay=0&fs=0&showinfo=0&rel=0&loop=0";
		    //var videoURLInternetExplorer = "http://www.youtube.com?p.swf?video_id=" + youTubeID + "&hl=en_GB&autoplay=0&fs=0&showinfo=0&rel=0&loop=0";
		
			var html = '';	
			html += "<object width=\"300\" height=\"250\">";
			html += "<param name=\"movie\" value=\"" + videoURL + "\"></param>";
			html += "<param name=\"allowFullScreen\" value=\"false\"></param>";
			html += "<param name=\"allowscriptaccess\" value=\"always\"></param>";
			html += "<embed src=\"" + videoURL + "\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"false\" width=\"300\" height=\"250\"></embed>";
			html += "</object>";
			
			$("#youtubepanel").html(html);
		}
			
		//Check videos have been selected for current page
		if ($(this).find('a')[0] != null)
		{		
			//Load first video by default
			var firstVideo = $(this).find('a')[0];
			var firstVideoID = $($(firstVideo).find('input')[0]).val();
			LoadVideo(firstVideoID);			
			$(firstVideo).addClass('selected');
		}
		else
		{
			$(this).addClass('displaynone');
		}
	
		//Load videos on click event
		var anchors = $(this).find('a');
        anchors.click(function()
		{
			var videoID = $($(this).find('input')[0]).val();
			LoadVideo(videoID);
			anchors.removeClass('selected');
			$(this).addClass('selected');
		});		
    });
};

$(document).ready(function() {
    jQuery().ExternalLinks();
});
