$(function() {
 $(document).ready(function(){
	$("#resetBookmarklet").click(function(){ $().setBookmarklet("reset"); });
	$(".jegenye input[type=checkbox], .jegenye input[type=radio]").change(function(){ $().setBookmarklet("set"); });
 });
});

$.fn.extend({
	setBookmarklet: function(m) {
		var defaultBookmarklet = "javascript:var%20b=document.body,w={%s%};if(b&&!document.xmlVersion){%20void(z=document.createElement('script'));void(z.src='http://tools.wamma.hu/torrent-search/wts.min.js?r='+Math.floor(Math.random()*1000));void(b.appendChild(z));}else{}",
			$bm = $("a#wts"),
			settings=[],
			torrentSites=[];			
		if (m=="set") {
			$(".jegenye ul.t").find("input[type=checkbox]:checked").each(function(){ torrentSites.push($(this).attr("rel")); });		
			if (torrentSites.length>0) { settings.push('t:["'+torrentSites.join('","')+'"]'); } else { alert("Legalább egy torrent keresőt válassz ki, különben smafu!"); return false; }
			$(".jegenye ul.s").find("input[name=s_search]:checked").each(function(){ if ($(this).attr("value")!="auto") { settings.push('s:"'+$(this).attr("value")+'"'); }	});	
			if ($("input#s_filter:checked").length==0) { settings.push("f:0"); }
			if ($("input#s_window:checked").length==0) { settings.push("w:0"); }
		}
		if (m=="reset") {
			document.getElementById("settingsform").reset();	
		}
		$bm.attr({href:defaultBookmarklet.replace("%s%",settings.join(","))}).effect("highlight",{},1000);
		$("p.bm span.h").fadeIn("fast",function(){setTimeout((function(){$("p.bm span.h").fadeOut("fast");}),1000);});
		$("input#bm").val(settings.join(","));
	}
});