$(document).ready(function() {
    String.prototype.trim = function() {
	str = this.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
    	    if (/\S/.test(str.charAt(i))) {
        	str = str.substring(0, i + 1);
    		break;
    	    }
	}
	return str;
    };

    var ipw = new iKeyboard($('#i_passwd'));
    $('#i_passwd').val('');

    $('#link_lostpw').click(function() {
	$('.mainbox,.infobox').slideUp(function() {
	    window.location = "/lostpassword"
	})
    });


    $('input[id^=i_],textarea').focus(function(){
        $(this).toggleClass("input_highlight");
    }).blur(function(){
        $(this).toggleClass("input_highlight");
    });

    (function($){
        $.fn.btnWorking = function(){
            if($(this).hasClass("btnworking")) {
                $(this).removeClass("btnworking");
                $(this).removeAttr("disabled");
                var img = $(this).children("img");
                img.attr("src", img.attr("tag"));
                img.removeAttr("tag");
            }
            else {
                $(this).addClass("btnworking");
                $(this).attr("disabled", "disabled");
                var img = $(this).children("img");
                img.attr("tag", img.attr("src"));
                img.attr("src", "/portal/img/loadingbtn.gif");
            }
        }
    })(jQuery);

    (function($) {
        $.fn.buttonState = function(state) {
            if(state) {
                $(this).removeClass('btndisabled');
                $(this).removeAttr("disabled");
            } else {
                $(this).addClass('btndisabled');
                $(this).attr("disabled", "disabled");
            }
        }

        $.fn.delayedKeyup = function(initStr, callback) {
            var obj = $(this);
            obj.data('timeout', []);

            obj.val(initStr);
            $(this).css("color", "#cccccc");

            obj.click(function(){
                if($(this).val().trim() == initStr)
                    $(this).val('');

                $(this).css("color", "");
            });

            obj.blur(function(){
                if($(this).val().trim() == '') {
                    $(this).val(initStr);
                    $(this).css("color", "#cccccc");
                }
            });

            obj.keyup(function(){
                $.each(obj.data('timeout'), function(i, t) {clearTimeout(t);});
                var a = obj.data('timeout');
                a.push(setTimeout(function(){if(obj.val() != initStr) callback();}, 1500));
                obj.data('timeout', a);
            });
        }
    })(jQuery);



    function flashLogin() {
    	setTimeout("$('.mainbox').css('margin-left', '10px')", 75);
    	setTimeout("$('.mainbox').css('margin-left', '-10px')", 150);
    	setTimeout("$('.mainbox').css('margin-left', '10px')", 225);
    	setTimeout("$('.mainbox').css('margin-left', '-10px')", 300);
    	setTimeout("$('.mainbox').css('margin-left', '10px')", 375);
    	setTimeout("$('.mainbox').css('margin-left', '-10px')", 450);
    	setTimeout("$('.mainbox').css('margin-left', '10px')", 525);
    	setTimeout("$('.mainbox').css('margin-left', '-10px')", 600);
    }

    $('#btn_login').click(function() {
	$(this).btnWorking();
	$.post("index.jsp", {id: $('#i_id').val(), passwd: $('#i_passwd').val()}, function(data) {
	    var json = $.parseJSON(data);
    
	    switch(json.response) {
		case "FLD":
		    flashLogin();
		    break;
		case "SIGNUP":
		    $.errorBox("Hozzáférés még nem aktív", "A belépés előtt aktiválnod kell hozzáférésedet, melyet az e-mailben kapott aktiváló link segítségével tudsz megtenni.");
		    break;
		case "SUSPEND":
		    $.errorBox("Hozzáférés megtagadva", "Hozzáférésedet határozatlan ideig korlátoztuk. Ennek okáról, és a visszaállításról bővebb információkat kapsz <a class=\"a\" href=\"http://atw.co.hu/kapcsolat\">ügyfélszolgálatunkon</a>.");
		    break;

		case "BLK":
		    $.errorBox("Hozzáférés megtagadva", "Túl sok sikertelen belépési kísérlet miatt hozzáférésedet 15 percre inaktiváltuk.");
		    break;

		case "XFR":
		    $('.mainbox').slideUp('fast', function() {
			window.location = json.param;
		    });
		    
		    break;
	    }

	    $('#btn_login').btnWorking();
	});
    });

    $('#i_id,#i_passwd').focusin(function() {
	$(this).css("background", "#ffffff");
    }).blur(function() {
	if($(this).attr('id') == "i_id" && $(this).val().trim() == "")
	    $(this).css("background", $(this).data("bg"));
	else if($(this).val().trim() == "" && $('#i_passwd_keyboard:visible').length == 0)
	    $(this).css("background", $(this).data("bg"));
    });


    $('#i_id').data("bg", "url(/login/img/iadmin_id.png) no-repeat 5px 6px #ffffff");
    if($('#i_id').val() == '')
	$('#i_id').css("background", $('#i_id').data("bg"));
    $('#i_passwd').data("bg", "url(/portal/img/iadmin_pw.png) no-repeat 5px 6px #ffffff");
    $('#i_passwd').css("background", $('#i_passwd').data("bg"));
    
    setTimeout("$('.mainbox,.infobox').slideDown()", 500);
});
