function ___getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {    
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {    // all except Explorer
        if(document.documentElement.clientWidth){
            windowWidth = document.documentElement.clientWidth; 
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }    
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else { 
        pageHeight = yScroll;
    }
    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){    
        pageWidth = xScroll;        
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
    return arrayPageSize;
}
/**
 / THIRD FUNCTION
 * getPageScroll() by quirksmode.com
 *
 * @return Array Return an array with x,y page scroll values.
 */
function ___getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {     // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;    
    }
    arrayPageScroll = new Array(xScroll,yScroll);
    return arrayPageScroll;
}
function showLoading() {
    if(setk>0) {
        arrPageScroll = ___getPageScroll();
        arrPageSizes = ___getPageSize();
        $('#loading').css({
            top:    arrPageScroll[1] + (arrPageSizes[3] / 2) - ($('#loading').innerHeight()/2),
            left:    arrPageScroll[0] + (arrPageSizes[2] / 2) - ($('#loading').innerWidth()/2)
        });
    }
}
function getExt(f) {
    f = String(f);
    fs = f.split(".");
    ext = fs[fs.length-1];
    if(ext.length>4) return true;
    if ((ext && /^(jpg|gif|jpeg|png|bmp)$/i.test(ext))){
        $('#loading').html("<span class=t01><a href='javascript:closeLoad();'>[đóng lại]</a></span><div class=t02><img ondblclick='closeLoad();' src='"+f+"' title='Double click to close'></div>");
        imgs = 1;
        showLoading();
        return false;
    }
    if ((ext && /^(mp3|wma|wmv|dat|avi)$/i.test(ext))){
        $('#loading').html("<span class=t01><a href='javascript:closeLoad();'>[đóng lại]</a></span><div class=t02 style='background:url(images/hp.jpg) center'><a href='"+f+"' target=_blank title='Click vào để download media này về'>Link download</a><br><embed type=application/x-mplayer2 transparentatstart=1 wmode=transparent width=300 src='"+f+"'></div>");
        showLoading();
        return false;
    }
    if (! (ext && /^(html|htm|text|php)$/i.test(ext))){
        if(String(f).search(baseurl)==-1) {
            showError(0, f);
            return false;
        }
        $('#loading').html("<span class=t01><a href='javascript:closeLoad();'>[đóng lại]</a></span><div class=t02>File này không thể xem trực tiếp từ website<br>Xin vui lòng click vào link bên dưới để tải về<a href='"+f+"'>"+f+"</a></div>");
        showLoading();
        return false;
    }
    return true;
}
function showError(e, i) {
    if(flg==1) return false;
    op = "<span class=t03><a href='"+i+"'>Thử tải dữ liệu lại theo cách cổ điển</a></span>";
    switch(e) {
        case 2:
        errortext = "Quá thời gian kết nối";
        break;
        case 404:
        errortext = "Trang này không tồn tại";
        break;
        case 0:
        if(String(i).search(baseurl)==-1) {
            errortext = "Click vào đây để vào tiếp tục truy cập vào <a href='"+i+"' target=_blank>"+i+"</a>";
            op = '';
        } else {
            errortext = "Kết nối thất bại";
        }
        break;
        default:
        errortext = "Không rõ nguyên nhân lỗi";
        break;
    }
    $('#loading').html("<span class=t01><a href='javascript:closeLoad();'>[đóng lại]</a></span><div class=t02>"+errortext+op+"</div>");
    showLoading();
}
function closeLoad(){
    $("#loading").empty();
    $("#loading").css({display:'none'});
    $("#mainboard2").fadeTo(200,1, function(){
        setk = 0;
        imgs = 0;
    });
}

setk = 0;
temp_url = '';
flg = 0;
imgs = 0;
function pageClick(ob) {
    if(String(temp_url)==String(ob)) return false;
    $("#mainboard2").fadeIn(1);
    $("#mainboard2").fadeTo(200, 0.4, function(){
        $("#loading").fadeIn(1, function(){
            $("#loading").html('<img src="images/loadsys.gif"> Đang tải dữ liệu');
            showLoading();
            getData(ob);
        });
        setk = 1;
        flg = 0;
    });
    return false;
}
function getData(d) {
    //mytime = setTimeout("alert('Quá thời gian kết nối')", 30000);
    //$("#loading").load("http://www.google.com");
    if(!getExt(d)) return false;
    if(String(d).search(baseurl)==-1) {
        showError(0, d);
        return false;
    }
    myReq = $.ajax({
        url: d+"&site=nohead",
        success: function(data,t00, t01) { 
            if(t01.status!=200) {
                showError(t01.status, d); 
                return false;       
            }
            $.address.value(String(d).replace(baseurl+'DKA/', ''));
            $('#mainboard2').html(data);
            $("#mainboard2").fadeTo(500,1, function(){
                document.getElementById("loading").style.display="none";
                setk = 0;
            });
            temp_url = d;
            clearTimeout(myTime);
            arrPageScroll = ___getPageScroll();
            if(arrPageScroll[1]>255) {
                $("body").animate({scrollTop:255}, 500).delay(200);
            }
        },
        error:function(t03) {
            showError(t03.status, d);
            clearTimeout(myTime);
        }
    });
    var myTime = setTimeout("showError(2, '"+d+"'); flg=1; myReq.abort();", 15000);
}

$.address.change(function(event) {
    if(event.value!=''&&event.value!='/'&&setk==0) pageClick(baseurl+"DKA"+event.value);
});
$(window).scroll(function(){
    if(imgs == 0)
    showLoading();
});
$(window).resize(function(){
    showLoading();
});
