//if ($("#phf").width() > 410) { $("#phf").addClass("d"); }
//$("#phr1").click(function(){ $(this).parent().fadeOut("fast", function(){ $("#lbox").fadeIn("fast"); }); });
$("#lbox_m").val("enter your email address");
$("#lbox_m").blur(function(){
    if ($(this).val() == "") {
        $(this).val("enter your email address");
    }
});
$("#lbox_m").focus(function(){
    if ($(this).val() == "enter your email address") {
        $(this).val("");
    }
});

$("#lbox").fadeIn("fast");
$("#lbox .s3 a").click(function(){ $("#lbox .s3").hide(); $("#lbox .s1").show(); });
$("#lbox .s6 a").click(function(){ $("#lbox .s6").hide(); $("#lbox .s4").show(); });
$("#lbox_r").click(function(){ $("#lbox .s4").hide(); $("#lbox .s1").show(); });
$("#lbox .s7 a").click(function(){ $("#lbox .s7").hide(); $("#lbox .s4").show(); });
$("#lbox_f").click(function(){ $("#lbox .s1").hide(); $("#lbox .s4").show(); });
$("#lbox").submit(function(){
	if ($("#lbox .s1").css("display") == "none") {
		retriveMail();
		return false;
	}
	$("#lbox .s1").hide();
	$("#lbox .s2").show();
	$.post(site_path + site_lang + "/" + l_url,
			{mail: $("#lbox_m").val(),
			pass: $("#lbox_p").val(),
			user: $("#lbox_u").val()},
			function(d){
				$("#lbox .s2").hide();
				if (d == "2" || d == "3") {
					$("#lbox .s" + d).show();
				} else {
					document.location=d;
	}});
	return false; 
});

$(".frm_reply").click(function(e){
	$(e.target).html() == 'Reply' ? $(e.target).html('Close') : $(e.target).html('Reply');
	$(e.target).next().slideToggle();
});

$("#homelow #sup3a .backLink").click(function(){ $("#homelow .signup").hide(); $("#homelow #sup1").show(); });
$("#homelow #sup3b .backLink").click(function(){ $("#homelow .signup").hide(); $("#homelow #sup1").show(); });
$("#homelow #sup4a .backLink").click(function(){ $("#homelow .signup").hide(); $("#homelow #sup1").show(); });
$("#homelow #sup4b .backLink").click(function(){ $("#homelow .signup").hide(); $("#homelow #sup1").show(); });
$("#homelow #sup5a .backLink").click(function(){ $("#homelow .signup").hide(); $("#homelow #sup1").show(); });
$("#homelow #sup5b .backLink").click(function(){ $("#homelow .signup").hide(); $("#homelow #sup1").show(); });
$("#homelow #sup6 .backLink").click(function(){ $("#homelow .signup").hide(); $("#homelow #sup1").show(); });
$("#supForm").submit(function(){
	$("#homelow .signup").hide();
	$("#homelow #sup7").show();
	$.post(site_path + site_lang + "/" + s_url,
			{mail: $("#signupMail").val(),
			password: $("#signupPass").val(),
			password2: $("#signupPass").val(),
			name: $("#signupName").val()},
			function(d){
				$("#homelow .signup").hide();
				$("#homelow #sup" + d).show();
	});
	return false; 
});

$("#signupName").val("choose your username");
$("#signupName").blur(function(){
    if ($(this).val() == "") {
        $(this).val("choose your username");
    }
});
$("#signupName").focus(function(){
    if ($(this).val() == "choose your username") {
        $(this).val("");
    }
});

$("#signupMail").val("enter your valid email address");
$("#signupMail").blur(function(){
    if ($(this).val() == "") {
        $(this).val("enter your valid email address");
    }
});
$("#signupMail").focus(function(){
    if ($(this).val() == "enter your valid email address") {
        $(this).val("");
    }
});

function retriveMail()
{
	$("#lbox .s4").hide();
	$("#lbox .s2").show();
	$.post(site_path + site_lang + "/" + f_url,
			{mail: $("#lbox_m2").val(), user: $("#lbox_u").val()},
			function(d){
				$("#lbox .s2").hide();
				$("#lbox .s" + d).show();
	});
	return false;
}

$("#lbox_f2").click(retriveMail);

function showLoader() { $("#loading").css("top", ($(window).scrollTop() + ($(window).innerHeight() / 2) - 100) + 'px'); $("#loading").show(); }
function hideLoader() { $("#loading").hide(); }

function add_friend()
{
	showLoader();
	var id = $(this).attr("rel");
	$.post(site_path + site_lang + "/_aff/", {id: id}, function(d){
		hideLoader();
		$("a.aff[rel='" + id +"']").each(function(){
			var p = $(this);
			while (!$("a.rff", p.parent()).length) {
				var p = p.parent();
			}	
			if (p.parent().hasClass("l")) {
				$("a.aff", p.parent()).parent().css("display", "none");
				$("a.rff", p.parent()).parent().css("display", "inline");
			} else {
				$("a.aff", p.parent()).hide();
				$("a.rff", p.parent()).show();
			}
		});
		alert(d);
	});
	return false;
}
function remove_friend ()
{
	showLoader();
	var id = $(this).attr("rel");
	$.post(site_path + site_lang + "/_rff/", {id: id}, function(d){
		hideLoader();
		$("a.aff[rel='" + id +"']").each(function(){
			var p = $(this);
			while (!$("a.rff", p.parent()).length) {
				var p = p.parent();
			}	
			if (p.parent().hasClass("l")) {
				$("a.dff", p.parent()).parent().css("display", "none");
				$("a.rff", p.parent()).parent().css("display", "none");
				$("a.cff", p.parent()).parent().css("display", "none");
				$("a.aff", p.parent()).parent().css("display", "inline");
			} else {
				$("a.dff", p.parent()).hide();
				$("a.rff", p.parent()).hide();
				$("a.cff", p.parent()).hide();
				$("a.aff", p.parent()).show();
			}
		});
		alert(d);
	});
	return false;
}
function confirm_friend()
{
	showLoader();
	var id = $(this).attr("rel");
	$.post(site_path + site_lang + "/_cff/", {id: id}, function(d){
		hideLoader();
		$("a.aff[rel='" + id +"']").each(function(){
			var p = $(this);
			while (!$("a.rff", p.parent()).length) {
				var p = p.parent();
			}	
			if (p.parent().hasClass("l")) {
				$("a.cff", p.parent()).parent().css("display", "none");
				$("a.dff", p.parent()).parent().css("display", "none");
				$("a.rff", p.parent()).parent().css("display", "inline");
			} else {
				$("a.cff", p.parent()).hide();
				$("a.dff", p.parent()).hide();
				$("a.rff", p.parent()).show();
			}
		});
		alert(d);
	});
	return false;
}

$("a.aff").click(add_friend);
$("a.rff").click(remove_friend);
$("a.dff").click(remove_friend);
$("a.cff").click(confirm_friend);

$("a.bgm").click(function(){
	var p = $(this).parent();
	showLoader();
	$.post(site_path + site_lang + "/_bgm", {id: $(this).attr("rel")}, function(){
		hideLoader();
		$("a.bgm", p).css("display", "none");
		$("a.agm", p).css("display", "inline");
	});
	return false;
});
$("a.agm").click(function(){
	var p = $(this).parent();
	showLoader();
	$.post(site_path + site_lang + "/_agm", {id: $(this).attr("rel")}, function(){
		hideLoader();
		$("a.agm", p).css("display", "none");
		$("a.bgm", p).css("display", "inline");
	});
	return false;
});

$("a.agg").click(function(){
	showLoader();
	var id = $(this).attr("rel");
	$.post(site_path + site_lang + "/_agg/", {id: id}, function(d){
		hideLoader();
		$("a.agg[rel='" + id +"']").each(function(){
			var p = $(this);
			while (!$("a.rgg", p.parent()).length) {
				var p = p.parent();
			}	
			if (p.parent().hasClass("l")) {
				$("a.agg", p.parent()).parent().css("display", "none");
				$("a.rgg", p.parent()).parent().css("display", "inline");
			} else {
				$("a.agg", p.parent()).hide();
				$("a.rgg", p.parent()).show();
			}
		});
		alert(d);
	});
	return false;
});
$("a.rgg").click(function(){
	showLoader();
	var id = $(this).attr("rel");
	$.post(site_path + site_lang + "/_rgg/", {id: id}, function(d){
		hideLoader();
		$("a.agg[rel='" + id +"']").each(function(){
			var p = $(this);
			while (!$("a.rgg", p.parent()).length) {
				var p = p.parent();
			}	
			if (p.parent().hasClass("l")) {
				$("a.rgg", p.parent()).parent().css("display", "none");
				$("a.agg", p.parent()).parent().css("display", "inline");
			} else {
				$("a.rgg", p.parent()).hide();
				$("a.agg", p.parent()).show();
			}
		});
		alert(d);
	});
	return false;
});
$("a.rgg2").click(function(){
	showLoader();
	var p = $(this).parent();
	var id = $(this).attr("rel");
	$.post(site_path + site_lang + "/_rgg/", {id: id}, function(d){
		hideLoader();
		p.css("display", "none");
		alert(d);
	});
	return false;
});

function attachWallRemoveHandler()
{
	$("a.rmw").unbind("click");
	$("a.rmw").click(function(){
		var p = $(this).parent();
		var id = $(this).attr("rel");
		showLoader();
		$.post(site_path + site_lang + "/_rmw", {id: id}, function(){
			hideLoader();
			--wall_last;
			--wall_max;
			$(".wall_l").html(wall_last);
			$(".wall_m").html(wall_max);
			p.hide();
		});
		return false;
	});
}
attachWallRemoveHandler();
$("#wall_p").submit(function(){
	showLoader();
	$.post(site_path + site_lang + "/_wpost/", 
		{t: $("input[name='t']", $(this)).val(),
		 h: $("input[name='h']", $(this)).val(),
		 txt: $("input[name='tekst']", $(this)).val()},
		 function(d){
			 hideLoader();
			 if (d == '1') {
				 var l = wall_url + "/p#wall";
				 if (document.location == l) {
				     document.location.reload();
				 } else {
					 document.location = l;
				 }
			 } else {
				 alert('The post text can\'t be empty!'); 
			 }
	});
	return false;
});
$("#posts div.t a").click(function(){
	if (wall_last >= wall_max) {
		return false;
	}
	showLoader();
	$.getJSON(site_path + site_lang + "/_gposts?t=" + $(this).attr("rel").substr(0, 1)
			  + "&h=" + $(this).attr("rel").substr(1) + "&s=" + wall_last, 
		function(d){ 
			hideLoader();
			if (d[0] > 0) {
				$("#posts").append(d[1]);
				wall_last = d[0];
				$(".wall_l").html(wall_last);
				n = wall_max - wall_last;
				if (n <= 0) {
					$("#posts div.t a").hide();
				} else if (n < $("#posts div.t a span").html()) {
					$("#posts div.t a span").html(n);
				}
				attachWallRemoveHandler();
			}
			});
});

//var _v = "show";
//var _c = "hide";
$(".bol").click(function(){
    var t = $(this);
    var x = t.parent();
    var p = $(".bol_", x);
    var visible = 0;
    
    p.slideToggle("fast");
    if(x.hasClass('box_visible')){
    	x.removeClass('box_visible');
    }
    else {
    	x.addClass('box_visible');
    	visible = 1;
    }
	$.get(site_path + "index.php", {a: "_sbx", k: t.attr("id").substr(3), v: visible});
    //var s = $("span", t);
    //s.text(s.text() == _v ? _c : _v);
});
$(".flst li").hover(function(){ 
    $(".i", this).fadeIn("fast");
 },function(){ 
	 $(".i", this).fadeOut("fast");
 });
$(".plst li").hover(function(){ 
    $(".i", this).fadeIn("fast");
 },function(){ 
     $(".i", this).fadeOut("fast");
 });

$("#mnu_ssrch").click(function(){
    $("#srchbox").slideToggle();
    return false;
});

$("#srch_h").submit(function(){
    var s = $("#srch_hq").val();
    
    
    if(s.length < 2) {
        alert("The minimum number of search characters is 2!");
    } else {
    	document.location = $('[@name=srch_r]:checked').val() + "?q=" + unicodeEscape(s);
    }
    return false;
});

$(".c_opt").change(function(){
$(this).css("background-image", this.options[this.selectedIndex].style.backgroundImage); 
}) ;

$(".c_all").click(function(){
	$("input[type='checkbox']", $(this).parent().parent()).attr("checked", "checked");
	return false;
});
$(".c_none").click(function(){
    $("input[type='checkbox']", $(this).parent().parent()).attr("checked", "");
    return false;
});
function getSelectedMsgsStr(el)
{
	var p = el.parent().parent();
	if (el.attr("rel") == "all") {
		return "all";
    } else {
        var c = getSelectedMsgs(el);
        if (c.length) {
            g = true;
            return c.map(function(){ return $(this).val(); }).get().join(",");
        }
    }
    return false;
}
function getSelectedMsgs(el)
{
    return $("input[type='checkbox'][checked]", el.parent().parent());
}
function getMsgsType(el)
{
	switch (el.attr("id")) {
	   case "inbox":
		   return "i";
		   break;
		   
	   case "trash":
		   return "d";
		   break;
		   
	   case "sent":
		   return "s";
		   break;
	}

	return "n";
}
$(".msg_read").click(function(){
	var el = $(this);
	var p = el.parent().parent();
    var msgs = getSelectedMsgsStr(el);
	
	if (msgs != false) {
        showLoader();
        $.get(site_path + "index.php",
        {a: "set_read_msg", msgs: msgs, type: getMsgsType(p)},
        function(){
            hideLoader();
            if (msgs == "all") {
            	$("td.u", p).removeClass("u").addClass("r");
            } else {
            	getSelectedMsgs(el).each(function(){
                    $("td.u", $(this).parent().parent()).removeClass("u").addClass("r");
                    });
            }
            });
    }
    return false;
});
$(".msg_unread").click(function(){
    var el = $(this);
	var p = $(this).parent().parent();
    var msgs = getSelectedMsgsStr(el);
    
    if (msgs != false) {
        showLoader();
        $.get(site_path + "index.php",
        {a: "set_unread_msg", msgs: msgs, type: getMsgsType(p)},
        function(){
            hideLoader();
            if (msgs == "all") {
                $("td.r", p).removeClass("r").addClass("u");
            } else {
            	getSelectedMsgs(el).each(function(){
                    $("td.r", $(this).parent().parent()).removeClass("r").addClass("u");
                    });
            }
            });
    }
    return false;
});
$(".msg_del").click(function(){
    var el = $(this);
	var p = $(this).parent().parent();
    var msgs = getSelectedMsgsStr(el);
    
    if (msgs != false) {
        showLoader();
        $.get(site_path + "index.php",
        {a: "set_del_msg", msgs: msgs, type: getMsgsType(p)},
        function(){
            var l = site_path + site_lang + "/messages/d";
            if (document.location == l) {
                document.location.reload();
            } else {
            	document.location = l;
            } });
    }
    return false;
});
$(".msg_restore").click(function(){
    var el = $(this);
	var p = $(this).parent().parent();
    var msgs = getSelectedMsgsStr(el);
    
    if (msgs != false) {
        showLoader();
        $.get(site_path + "index.php",
        {a: "set_restore_msg", msgs: msgs},
        function(){
            var l = site_path + site_lang + "/messages/d";
            if (document.location == l) {
                document.location.reload();
            } else {
            	document.location = l;
            } });
    }
    return false;
});
$(".msgd_del").click(function(){
	var el = $(this);
	var id = el.attr("rel");
	var type = id.substr(0, 1);
	id = id.substr(1);
	if (id != false) {
		showLoader();
		$.get(site_path + "index.php",
				{a: "set_del_msg", msgs: id, type: type},
				function(){
						document.location = site_path + site_lang + "/messages/d";
				});
	}
	return false;
});
$(".msgd_restore").click(function(){
	var el = $(this);
	var id = el.attr("rel").substr(1);
	if (id != false) {
		showLoader();
		$.get(site_path + "index.php",
				{a: "set_restore_msg", msgs: id},
				function(){
						document.location.reload();
				});
	}
	return false;
});
function attachMsgLoadHandlers()
{
	$(".load_msg").click(function(){
	    showLoader();
	    $(this).parent().parent().parent().load(site_path + "index.php",
	            {a: "load_msgs", id: $(this).attr("rel")}, function(){ hideLoader(); attachMsgLoadHandlers(); });
	    return false;
	});
}
attachMsgLoadHandlers();

function unicodeEscape (s) { if (s == "") { return ""; } var i = 0; var url = ""; var c; var s2 = escape(s); while (i < s2.length) { c = s2.substr(i, 1); if (c == "%") { s3 = s2.substr(i + 1, 1); if (s3 == "u") { url += "|" + s2.substr(i + 1, 5); i += 6; } else { url += "|u00" + s2.substr(i + 1, 2); i += 3; } } else { url += c; i++; } } return url; }