/*************************************************************************
 *  Created and Copyright 2009 by Victor Purolnik.
 *  Feel free to contact me at: netsnake24@googlemail.com 
 *  
 *  This script is a plugin for jQuery. You need to download the jQuery
 *  library at http://www.jquery.com in order to use it.   
 *     
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    For the license, see: http://www.gnu.org/licenses/gpl.txt
*************************************************************************/

$.fn.addFavicon = function() {
	// Variables
	var scriptUrl = "/power/returnPage.php";
	var arrowUrl = "arrow_branch.png";
	
	// Preload arrow image to browser cache
	var arrowImg = new Image();
	arrowImg.src = arrowUrl;
	
	$(this).filter(function() {
		// Is it even /^https?\:\/\//?
		var hrefNow = $(this).attr("href");
		var pattern = new RegExp('^https?\:\/\/');
		hrefNow = pattern.test(hrefNow);
		return hrefNow;
		
	}).filter(function() {
		// Because it has problems with http://www. or http: only
		var hostName    = location.hostname.split("www.");
		var hostName    = hostName[ hostName.length-1 ];
		var hostNameNow = this.hostname.split("www.");
		var hostNameNow = hostNameNow[ hostNameNow.length-1 ];
		
		return hostNameNow != hostName;
		
	}).each(function() {
		var linkNow = $(this);
		$.ajax({
			url      : scriptUrl,
			type     : "GET",
			data     : { u : linkNow.attr("href") },
			success  : function(response) {
				response = response.split("||");
				linkNow.prepend("<img class=favicon src='"+ response[0] +"' />");
				linkNow.wrap("<nobr></nobr>");
			}
		});
		
	}).attr("target", "_blank");
}