﻿function getViewport() {
    var w=$(window),d=$(document)
    return [w.width(), w.height(), d.scrollLeft(), d.scrollTop() ];
};
var loadingTimer,loadingFrame=1,fo,fl,list,bod;
$(document).ready(function(){
if(!fo){
    fo= $('<div id="fancy_overlay"></div>');
    fl= $('<div class="fancy_loading" id="fancy_loading"><div></div></div>');
    bod=$("body");
    bod.append(fo).append(fl);
    fo.css('opacity', '0.8');
}
if(location.hash){
    if(location.hash=="#lots"){
        showLoad($("#infoasterix a"),"#lots");
    }
    else if(location.hash=="#body"){
        showLoad($("a",$("#acceptRules").next()));
    }
}
});
function animateLoading() {
    if (!fl.is(':visible')){
        clearInterval(loadingTimer);
        fo.hide();
        return;
    }
    $("> div",fl).css('top', (loadingFrame * -40) + 'px');
    loadingFrame = (loadingFrame + 1) % 12;
};
function showLoading(){
    fo.show();
    clearInterval(loadingTimer);
    var pos = getViewport();
    fl.css({'left': ((pos[0] - 40)*0.5 + pos[2])+"px", 'top': ((pos[1] - 40)*0.52 + pos[3])+"px"}).show();
    loadingTimer = setInterval(animateLoading, 66);
};
function showLoad(self,html){
    showLoading();
    if(!list){
    var href=self.attr("href");
    if(html){
        href=href.substr(0,href.indexOf("#"));
        location.hash=html;
    }
    else{
        location.hash="body";
    }
    try{
    $.ajax({
        type : 'GET',
        url  : href,
        data : {},
        success: function(result) {
            var htm;
            if(html){
               htm=$(html,result).parent();
               $(html,htm).remove();
            }
            else{
               htm=$("#content",result);
            }
            list= $("<div><div style=\"overflow:auto;padding:20px;margin:10px 0;height:420px;color:#666\" class=\"regl\">"+htm.html()+"</div></div>");
            cl=$("<div style=\"text-align:right;cursor:pointer\" class=\"link\"><a href=\"#\">"+$("#txtclo",result).html()+" &nbsp;&nbsp;&nbsp;X</a></div>");
            cl.click(function(){list.hide();fl.hide();});
            var pos = getViewport();
            list.prepend(cl).css({padding:"10px",backgroundColor:"#fff",textAlign:"left",width:"800px",height:"480px",position:"absolute",zIndex:30,top:((pos[1] - 480)*0.5 + pos[3])+"px",left: ((pos[0] - 800)*0.5 + pos[2])+"px"});
            $("> div",fl).hide();
            bod.append(list);
        },
        error:function(){
            alert("Error... Please retry...");
            fl.hide();
        }
    });
    } catch(e){
        if(e.number==-2146828218){
            window.location.reload(true);
        }
        else{
            alert("Error... Please retry...");
            fl.hide();
        }
    }
    }
    else{
        list.show();
    }
}
var initWindo = function(){
    if(list){
        var pos = getViewport();
        list.css({top:((pos[1] - 480)*0.5 + pos[3])+"px",left: ((pos[0] - 800)*0.5 + pos[2])+"px"});
    }
}
$(window).resize(initWindo);
