$(function(){
    $('a.mail').each(function(){
       $(this).attr('href', decodeMailAddr($(this).attr('href')));
    });
    
    $('a, input:button, button').each(function(){
        $(this).focus(function(){
            if(this.blur){
                this.blur();
            }
        });
    });

    $('a.externlink').each(function(){
        $(this).click(function(){
            window.open(this.href);
            return false;
        });
    });
    
    $('a.download').each(function(){
        var img = $('<span></span>');
        $(img).addClass('icon');
        $(img).addClass('downloadicon');
        $(img).addClass('nolazyload');
        $(this).after(img);
    });
    
    $('a.link, a.externlink').each(function(){
        var img = $('<span></span>');
        $(img).addClass('icon');
        $(img).addClass('linkicon');
        $(img).addClass('nolazyload');
        $(this).after(img);
    });
    
    $('#login').click(doLogin);
    $('#logoff').click(doLogoff);
    
    /* http://www.appelsiini.net/projects/lazyload */
    $('img:not(#pageheaderlogo, #ajaxloader, #coverimg, .nolazyload)').lazyload({
        placeholder : '/public/layout/ajaxcircle.gif',
        effect : 'fadeIn'
    });
    
    $(window).resize(function(){
        $("#cover").css("width", $(document).width());
        $("#cover").css("height", $(document).height());
        $("#dialogbox").css("left", ($(window).width() - $("#dialogbox").width()) / 2);
    });    
});

(function($){
    $.fn.serializeJSON = function(){
        var json = {};
        $.map($(this).serializeArray(), function(n, i){
        json[n['name']] = n['value'];
    });
    return json;
};
})(jQuery);

var clickQueue = new FuncQueue();

function FuncQueue(){
    this.queue  = [];
    this.add = function(func){
        if(typeof(func) != 'function'){
            return;
        }
        this.queue.push(func);
    }
    
    this.execute = function(){
        for(var i in this.queue){
            this.queue[i]();
        }
    }
}

function showDialogBox(title, msg){    
    $('#dialogboxcontent').html(msg);
    $('#dialogboxheader').html(title);
    
    $("#cover").css("width", $(document).width());
    $("#cover").css("height", $(document).height());
    $("#dialogbox").css("left", ($(window).width() - $("#dialogbox").width()) / 2);
    $("#cover").fadeTo('slow', 0.8);
    $("#dialogbox").fadeIn('slow'); 
}

function showOkCancelDialogBox(title, msg, callbackOK, callbackCancel){
    $('#btnDlgCancel').show();
    showDialogBox(title, msg);
    $('#btnDlgCancel').focus();
    
    var close = function(callback){
        $("#cover").fadeOut('slow');
        $("#dialogbox").fadeOut('slow', callback); 
        $('#btnDlgOK').unbind('click');
        $('#btnDlgCancel').unbind('click');
        $('#btnDlgCancel').fadeOut('slow');
    }
    
    $('#btnDlgOK').click(function(){
        close(callbackOK);
    });
    
    $('#btnDlgCancel').click(function(){
        close(callbackCancel);
    });
}

function showOKDialogBox(title, msg, callback){
    showDialogBox(title, msg);
    $('#btnDlgOK').focus();
    $('#btnDlgOK').click(function(){
        $("#cover").fadeOut('slow');
        $("#dialogbox").fadeOut('slow', callback); 
        $('#btnDlgOK').unbind('click');
    });
}

function decodeMailAddr(addr){
    var keycode = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var ret = '';
    for(var i = 0; i < addr.length; ++i){
        var schar = addr.substring(i, i + 1);
        var pos = keycode.indexOf(schar.toUpperCase());
        
        if(pos >= 0){
            pos = (pos + 14) % keycode.length;
            if(schar == schar.toUpperCase()){
                schar = keycode.substring(pos, pos + 1);
            }else{
                schar = keycode.substring(pos, pos + 1).toLowerCase();
            }
        }
        ret += schar;
    }
    return ret;
}

function ajaxAction(op, data, reloadReady){
    $('#ajaxloader').show();
    $('#pagecontent').css('height', $('#pagecontent').height());  
    $('#reloadcontent').fadeOut('slow', function(){
        $(this).load(window.location.pathname, {
            'ajax_xml' : true, 
            'do' : op,
            'data' : data}, function(){
                reloadReady();
                $('#ajaxloader').hide();
            }
        );
    });
}

function loadPageContent(){
    $('#reloadcontent').find('img').batchimageload({
        loadingCompleteCallback: function(){
            $('#pagecontent').css('height', '100%');  
            $('#reloadcontent').fadeIn('slow');
            clickQueue.execute();
        }
    });
}
