$(function() {
	debug = true;
	$.refreshAnim = true;


	$.youtubeInit = function() {
		$("#mainContent").find("a").filter('.WBYoutube').each(function(){
			var $this = $(this), url = $this.attr('href').split('v=')[1];

			$this.replaceWith('<div style="text-align: center; margin: auto;" class="yt_player">\n\
								<object type="application/x-shockwave-flash" style="width:540px; height:435px;" data="http://www.youtube.com/v/'+url+'?fs=1&hd=1">\n\
									<param name="movie" value="http://www.youtube.com/v/'+url+'?fs=1&hd=1" />\n\
									<param name="allowFullScreen" value="true" />\n\
								</object>\n\
							   </div>');
		});

		var $at = $("#articleTwits");
		if ($at.length) {
			$at.find("a").filter('.WBYoutubeComment').each(function(){
				var $this = $(this), url = $this.attr('href').split('v=')[1];

				$this.replaceWith('<div style="text-align: center; margin: auto;" class="yt_player">\n\
									<object type="application/x-shockwave-flash" style="width:540px; height:435px;" data="http://www.youtube.com/v/'+url+'?fs=1&hd=1">\n\
										<param name="movie" value="http://www.youtube.com/v/'+url+'?fs=1&hd=1" />\n\
										<param name="allowFullScreen" value="true" />\n\
									</object>\n\
								   </div>');
			});
		}

		$("#widget_comments").find("a").filter('.WBYoutubeComment').each(function(){
		var $this = $(this), url = $this.attr('rel');

		$this.replaceWith('<div class="WBYoutubeCommentEmbed" rel="'+url+'" style="text-align: center; margin: auto;">\n\
							<object type="application/x-shockwave-flash" style="width:448px; height:361px;" data="http://www.youtube.com/v/'+url+'?fs=1&hd=1">\n\
								<param name="movie" value="http://www.youtube.com/v/'+url+'?fs=1&hd=1" />\n\
								<param name="allowFullScreen" value="true" />\n\
							</object>\n\
						   </div>');
		});
	}

	loading = function (status) {
		if (status) {
			$("#loader").css({ opacity: 1, display: "block" }).stop().animate({ opacity: 1 }, 200, "swing");
		  	if ($.refreshAnim && !$.browser.msie) {
			  $("#mainContent").stop().animate({ opacity: 0 }, 200, "swing");
		  	}
		} else {
		 	$("#loader").stop().animate({ opacity: 0 }, 200, "swing", function () {
				$(this).css({ opacity: 0, display: "none" });
   		 	});
		  	if ($.refreshAnim && !$.browser.msie) {
			  $("#mainContent").stop().animate({ opacity: 1 }, 200, "swing");
		  	} else {
		  		$.refreshAnim = true;
		  	}
		}
	}

	$.linkInit = function() {
		$("#mainContent").find('a').each(function() {
			var $this = $(this), href = $this.attr('href');

			if ((href.search($.base) < 0) && (href[0] != '#')) $this.attr('target', '_blank');
		});
	}

	initFunctions = function (remoteLinkContainer) {
		$.linkInit();
		$.youtubeInit();
	}

	initWbPage = function () {
		initFunctions();
	}

	checkIE6 = function () {
	    return ($.browser.msie && parseInt($.browser.version)<7);
	}

	checkIE = function () {
	    return ($.browser.msie);
	}

	addTag = function (str) {
		var selection = "";
		var value = $("#commentText").val();
		var range = $("#commentText").getSelection();
		var before = value.substring(0, range.start);
		var after = value.substring(range.end, value.length);

		if ((range.text.length > 0) || (str != "img" && str != "url")) {
			if ((str == "url" || str == "img") && range.text.substr(0,7) != "http://" ) {
				range.text = "http://" + range.text;
			}
			var trailing = "";
			if (range.text[range.length-1] == " ") {
				range.text = range.text.substring(0, range.length-1);
				trailing = " ";
			}
    		var selection = "["+str+"]" + range.text + "[/"+str+"]";
			$("#commentText").val(before + selection + trailing + after);
		} else if (str == "url") {
			if (selection = prompt("Adja meg az urlt!","http://")) {
				$("#commentText").val(before + "[url]" + selection + "[/url] " + after);
			}
		} else if (str == "img") {
			if (selection = prompt("Adja meg a kép url-t!","http://")) {
				$("#commentText").val(before + "[img]" + selection + "[/img] " + after);
			}
		}
	};

	htmlToBBCode = function (s) {
		s = $.trim(s);

		function rep(re, str) {
			s = s.replace(re, str);
		};

		rep(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[url=$1]$2[/url]");
		rep(/<font.*?color=\"(.*?)\".*?class=\"codeStyle\".*?>(.*?)<\/font>/gi,"[code][color=$1]$2[/color][/code]");
		rep(/<font.*?color=\"(.*?)\".*?class=\"quoteStyle\".*?>(.*?)<\/font>/gi,"[quote][color=$1]$2[/color][/quote]");
		rep(/<font.*?class=\"codeStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[code][color=$1]$2[/color][/code]");
		rep(/<font.*?class=\"quoteStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[quote][color=$1]$2[/color][/quote]");
		rep(/<font.*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[color=$1]$2[/color]");
		rep(/<font>(.*?)<\/font>/gi,"$1");
		rep(/<img.*?src=\"(.*?)\".*?>/gi,"[img]$1[/img]");
		rep(/<img.*?src=\"(.*?)\".*?\/>/gi,"[img]$1[/img]");
		rep(/<span class=\"codeStyle\">(.*?)<\/span>/gi,"[code]$1[/code]");
		rep(/<span class=\"quoteStyle\">(.*?)<\/span>/gi,"[quote]$1[/quote]");
		rep(/<strong class=\"codeStyle\">(.*?)<\/strong>/gi,"[code][b]$1[/b][/code]");
		rep(/<strong class=\"quoteStyle\">(.*?)<\/strong>/gi,"[quote][b]$1[/b][/quote]");
		rep(/<em class=\"codeStyle\">(.*?)<\/em>/gi,"[code][i]$1[/i][/code]");
		rep(/<em class=\"quoteStyle\">(.*?)<\/em>/gi,"[quote][i]$1[/i][/quote]");
		rep(/<u class=\"codeStyle\">(.*?)<\/u>/gi,"[code][u]$1[/u][/code]");
		rep(/<u class=\"quoteStyle\">(.*?)<\/u>/gi,"[quote][u]$1[/u][/quote]");
		rep(/<\/(strong|b)>/gi,"[/b]");
		rep(/<(strong|b)>/gi,"[b]");
		rep(/<\/(em|i)>/gi,"[/i]");
		rep(/<(em|i)>/gi,"[i]");
		rep(/<\/u>/gi,"[/u]");
		rep(/<u>/gi,"[u]");
		rep(/<br \/>/gi,"\n");
		rep(/<br\/>/gi,"\n");
		rep(/<br>/gi,"\n");
		rep(/<p>/gi,"");
		rep(/<\/p>/gi,"\n");
		rep(/&nbsp;/gi," ");
		rep(/&quot;/gi,"\"");
		rep(/&lt;/gi,"<");
		rep(/&gt;/gi,">");
		rep(/&amp;/gi,"&");
		rep(/&undefined;/gi,"'"); // quickfix
		return s;
	}


	// USERNAME AND PASSWORD FIELD TEXT HIDER
	$(".usernameInput").unbind().bind('click focus change', function() {
		if ($(this).val() == "Felhasználónév") {
			$(this).val("");
		}
	});

	$(".usernameInput").blur(function() {
		if ($.trim($(this).val()) == "") {
			$(this).val("Felhasználónév");
		}
	});

	$(".passwordInput").unbind().bind('click focus change', function() {
		if ($(this).val() == "jelszo") {
			$(this).val("");
		}
	});

	$(".passwordInput").blur(function() {
		if ($.trim($(this).val()) == "") {
			$(this).val("jelszo");
		}
	});

	initWbPage();
});

// Google Search engine
if (typeof google !== "undefined") {
	google.load('search', '1');
	function OnLoad() {
		// create a search control
		var searchControl = new google.search.SearchControl();
		searchControl.setLinkTarget(google.search.Search.LINK_TARGET_SELF);
		searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
		searchControl.setSearchCompleteCallback(null, function() {
			$("#searchContent").show();
		});


		var drawOptions = new google.search.DrawOptions();
		drawOptions.setSearchFormRoot(document.getElementById("searchBox"));
		drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);


		var searcher = new google.search.WebSearch();
		var searcherOptions = new google.search.SearcherOptions();
		searcherOptions.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
		searcher.setSiteRestriction("009267545418342554126:xdxhyd3afe0");
		searcher.setUserDefinedLabel("Keresési találatok");
		searchControl.addSearcher(searcher, searcherOptions);

		searchControl.draw(document.getElementById("searchContent"), drawOptions);

		$("#searchBox").find('.gsc-search-button').val('Keresés');
	}
	google.setOnLoadCallback(OnLoad, true);
}
