﻿window.rapidwizard_is_loaded=true;

function RapidWizard(userconfig) {
	this.userconfig=userconfig;
	this.hosts = ["4shared.com", "megaupload.com", "megashare.com", "yousendit.com", "filesmonster.com", "rapidshare.de", "up-file.com", "kewlshare.com", "mediafire.com", "transferbigfiles.com", "ul.to", "easy-share.com", "letitbit.net", "netgull.com", "yourfilehost.com", "freakshare.net", "filesrack.com", "filefactory.com", "uploaded.to", "filefront.com", "rapidshare.com", "bonpoo.com", "ugotfile.com", "megashares.com", "netload.in", "uploading.com", "storage.to", "vip-file.com", "enterupload.com", "hotfile.com", "badongo.com", "midload.com", "depositfiles.com", "uploadbox.com", "sendspace.com", "sendfile.to", "ziddu.com", "zshare.net", "file-rack.com", "filebox.com", "spread-it.com", "gigasize.com", "novaup.com", "data.hu", "addat.hu", "fileop.com", "extabit.com", "loadfiles.in", "multiupload.com", "sharingmatrix.com", "shareflare.net", "extrashare.us", "save.am", "fileserve.com", "filesonic.com"];
	this.hosts_regex = ["4shared\\.com", "megaupload\\.com", "megashare\\.com", "yousendit\\.com", "filesmonster\\.com", "rapidshare\\.de", "up\\-file\\.com", "kewlshare\\.com", "mediafire\\.com", "transferbigfiles\\.com", "ul\\.to", "easy\\-share\\.com", "letitbit\\.net", "netgull\\.com", "yourfilehost\\.com", "freakshare\\.net", "filesrack\\.com", "filefactory\\.com", "uploaded\\.to", "filefront\\.com", "rapidshare\\.com", "bonpoo\\.com", "ugotfile\\.com", "megashares\\.com", "netload\\.in", "uploading\\.com", "storage\\.to", "vip\\-file\\.com", "enterupload\\.com", "hotfile\\.com", "badongo\\.com", "midload\\.com", "depositfiles\\.com", "uploadbox\\.com", "sendspace\\.com", "sendfile\\.to", "ziddu\\.com", "zshare\\.net", "file\\-rack\\.com", "filebox\\.com", "spread\\-it\\.com", "gigasize\\.com", "novaup\\.com", "data\\.hu", "addat\\.hu", "fileop\\.com", "extabit\\.com", "loadfiles\\.in", "multiupload\\.com", "sharingmatrix\\.com", "shareflare\\.net", "extrashare\\.us", "save\\.am", "fileserve\\.com", "filesonic\\.com"];
	this.alive_link_png = 'http://ejfelibagoly.hu/images/check.png';
	this.dead_link_png = 'http://ejfelibagoly.hu/images/check_no.png';
	this.unknown_link_png = 'http://ejfelibagoly.hu/images/unknown.gif';
	this.links = [];
	
	this.re_link2 = new RegExp('(https?://(?:[A-Z0-9]\.)*(?:'+this.hosts_regex.join('|')+')[-()A-Z0-9áéíóöőúüűÁÉÍÓÖŐÚÜŰ+&@#/%?=~_|!:,.;\s ]*[A-Z0-9áéíóöőúüűÁÉÍÓÖŐÚÜŰ+&@#/%=~_|\s ])', "ig");
	this.re_link3 = new RegExp('https?://(?:[A-Z0-9]\.)*(?:'+this.hosts_regex.join('|')+')[-()A-Z0-9áéíóöőúüűÁÉÍÓÖŐÚÜŰ+&@#/%?=~_|!:,.;\s]*[A-Z0-9áéíóöőúüűÁÉÍÓÖŐÚÜŰ+&@#/%=~_|\s ]', "i");
	this.re = new RegExp('^(https?://([A-Z0-9]\.)*('+this.hosts_regex.join('|')+')[-()A-Z0-9áéíóöőúüűÁÉÍÓÖŐÚÜŰ+&@#/%?=~_|!:,.;\s ]*[A-Z0-9áéíóöőúüűÁÉÍÓÖŐÚÜŰ+&@#/%=~_|\s ])$', "ig");
	
	this.load(userconfig);
}

RapidWizard.prototype.load = function(userconfig) {
	rapidwiz = this;
	rapidwiz.loadJquery(function(noconflict){
		if(noconflict)
			jQuery.noConflict();
		
		jQuery.fn.textNodes = function() {
			var ret = [];
			this.contents().each(function() {
				var fn = arguments.callee;
				if(this.nodeType == 3) {
					ret.push(this);
				} else if(this.nodeType==1) {
					if(
					this.tagName.toLowerCase()=='script'
					|| this.tagName.toLowerCase()=='head'
					|| this.tagName.toLowerCase()=='iframe'
					|| this.tagName.toLowerCase()=='textarea'
					|| this.tagName.toLowerCase()=='option'
					|| this.tagName.toLowerCase()=='style'
					|| this.tagName.toLowerCase()=='title'
					|| this.tagName.toLowerCase()=='a'
					){
						//alert(this.tagName);
					} else {
						jQuery(this).contents().each(fn);
					}
				}
			});
			//return jQuery(ret);
			return ret;
		}
		
		ondomready = function() {
			rapidwiz.autolink();
			if(userconfig.Onload) {
				rapidwiz.checkLinks();
			}
		}
		/*
		if(false) {
            ondomready();
        } else {
            jQuery(ondomready);
		}
		*/
		
		jQuery(ondomready);
	});
}

RapidWizard.prototype.loadJquery = function(fn) {
	var s = document.createElement('script');
	s.setAttribute('type','text/javascript');
	s.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js');
	
	noconflict=false;
	
	if(typeof jQuery=='undefined')
		document.getElementsByTagName('head')[0].appendChild(s);
	
	if(typeof $=='undefined')
		noconflict=true;
	
	var checkload = function() {
		if(typeof jQuery=='undefined')
			setTimeout(checkload, 10)
		else
			fn(noconflict);
	}
	checkload();
}


RapidWizard.prototype.autolink = function() {
	// Crude fix for when text is already a link name
	rapidwiz=this;
	
	// Ignore Tags
	//ignore = ['a', 'head', 'script', 'style', 'title', 'option', 'textarea'];
	
	jQuery.each(jQuery(rapidwiz.userconfig.Selector).textNodes(), function(i, node){
		text = node.nodeValue;
		if(rapidwiz.re_link3.test(text)){
			newNode=document.createElement('span');
			text=jQuery('<div/>').text(text).html();
			newNode.innerHTML=text.replace(rapidwiz.re_link2, '<a href="$1" onclick="return false;" style="line-height:1.5em; text-decoration:none;">$1</a>');
			node.parentNode.replaceChild(newNode, node);
		}
	});
}


RapidWizard.prototype.checkLink = function(anchor, linknum) {
	rapidwiz=this;
	jQuery.getJSON (
		"http://avsptracker.eu/check.php?url="+encodeURIComponent(anchor.href)+"&callback=?",
		function(data) {
			if(data.data==1){
                if(!rapidwiz.userconfig.OverrideDefault) {
					jQuery(anchor).append('<img src="'+rapidwiz.alive_link_png+'" style="padding:0px; margin-left:3px; border:0px; height:16px; width:16px;" />').attr("title", "Link OK");
                }
                if(rapidwiz.userconfig.OnCheck) {
					rapidwiz.userconfig.OnCheck('alive', anchor);
                }
			} else if(data.data==0) {
                if(!rapidwiz.userconfig.OverrideDefault) {
					jQuery(anchor).append('<img src="'+rapidwiz.dead_link_png+'" style="padding:0px; margin-left:3px; border:0px; height:16px; width:16px;" />')
					.attr("title", "Link Dead").css("textDecoration", "line-through");
                }
                if(rapidwiz.userconfig.OnCheck) {
					rapidwiz.userconfig.OnCheck('dead', anchor);
                }
			} else {
                if(!rapidwiz.userconfig.OverrideDefault) {
					jQuery(anchor).append('<img src="'+rapidwiz.unknown_link_png+'" style="padding:0px; margin-left:3px; border:0px; height:16px; width:16px;" />').attr("title", "Link status unknown!");
                }
                if(rapidwiz.userconfig.OnCheck) {
					rapidwiz.userconfig.OnCheck('unknown', anchor);
                }
			}
		}
	);
}

RapidWizard.prototype.checkLinks = function() {
	rapidwiz = this;
	linknum=1;
	jQuery(rapidwiz.userconfig.Selector).find("a").each(function(i){
		if(this.href.match(rapidwiz.re)) {
			if(linknum==1) {
				linknum++;
			}
			rapidwiz.checkLink(this, linknum);
		}
	});
}