var isopera = false;
var ischrome = false;
var isff = false;
var isie = false;
var isie6 = false;
var isie7 = false;
var isie8 = false;
if(navigator.userAgent.split('Opera').length == 2) isopera = true;
else if(navigator.userAgent.split('Chrome').length == 2) ischrome = true;
else if(navigator.userAgent.split('Firefox').length == 2) isff = true;
else if(navigator.userAgent.split('MSIE').length == 2) {
    isie = true;
    var bv = parseInt(navigator.userAgent.split('MSIE')[1]);
    if(bv <= 6) isie6 = true;
    else if(bv == 7) isie7 = true;
    else if(bv == 8) isie8 = true;
}
var d = document;
function debug(el, func, infunc) {
    if(!$('debug')) {
        var deb = d.createElement('div');
        deb.id = 'debug';
        deb.style.maxHeight = '400px';
        deb.style.width = '500px';
        deb.style.overflow = 'auto';
        deb.style.position = isie6 ? 'absolute' : 'fixed';
        deb.style.left = (d.viewport.getDimensions().width-503)+'px';
        deb.style.top = '0';
        deb.style.backgroundColor = '#444444';
        deb.style.color = '#ffffff';
        deb.style.font = '11px Arial';
        deb.style.borderLeft = '2px solid #ffffff';
        deb.style.zIndex = '10000';
        $(d.body).appendChild(deb);
        $('debug').setOpacity(0.9);
    }
    var type = typeof(el);
    var sel = '';
    switch(type) {
        case 'object':
            for (var i in el)
                if(i != 'channel' && el[i] && (typeof(el[i]) != 'function' || func)) {
                    sel += i;
                    if(infunc || typeof(el[i]) != 'function') sel += ' : '+el[i].toString().replace(/\n/g, '<br>');
                    sel += '<br>';
                }
        break;
        case 'undefined':
            sel = 'undefined';
        break;
        case 'boolean':
        case 'number':
        case 'string':
        case 'function':
        default:
            sel = el.toString().replace(/\n/g, '<br>');
        break;
    }
    if(!sel) sel = '&nbsp;';
    $('debug').innerHTML += ($('debug').innerHTML.length ? '<br>' : '')+sel;
    $('debug').scrollTop = $('debug').scrollHeight - $('debug').clientHeight;
}

window.onscroll = function() {
    if(!isie6) return;
    var dim = d.viewport.getScrollOffsets();
    if($('debug'))
        $('debug').style.top = (dim.top+5)+'px';
    if($('popload'))
        $('popload').style.top = dim.top+'px';
}

function setHeights() {
    window.onresize = function() {};
    popSize();
    setTimeout('window.onresize = setHeights',100);
}

function popInit(nobg) {
    if($('popload')) $('popload').remove();
    var dim = d.viewport.getDimensions();
    var deb = d.createElement('div');
    deb.id = 'popload';
    deb.style.position = isie6 ? 'absolute' : 'fixed';
    if(!nobg) {
        if(isie6) {
            //deb.style.backgroundColor = "#000000";
            //deb.style.backgroundColor = "transparent";
            //deb.style.backgroundImage = "url("+im+"blank.gif)";
            //deb.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+im+"overlay_dark.png',sizingMethod='scale')";
        } else
            deb.style.backgroundImage = 'url('+im+'overlay_dark.png)';
    }
    deb.innerHTML = '<div id="popdiv"></div><img id="popim" src="'+im+'loading_dark.gif" alt="please wait..."><div id="popclose"><img src="'+im+'close.png" alt="Close"><div id="popcloset">'+closet+'</div></div>';
    $(d.body).appendChild(deb);
    $('popload').setStyle('z-index:100;left:0;top:'+(isie6 ? d.viewport.getScrollOffsets().top : 0)+'px;width:'+dim.width+'px;height:'+dim.height+'px');
    $('popclose').setStyle('position:'+(isie6 ? 'absolute' : 'fixed')+';display:none;'+((isie6 || isie7) ? 'width:60px;' : '')+'cursor:pointer;margin-top:3px;margin-left:-2px');
    $('popclose').down().setStyle('display:block;float:right;padding:3px 4px 4px 4px;');
    $('popcloset').setStyle('font-size:12px;color:'+(isie ? 'black' : 'white')+';float:left;text-shadow:black 1px 1px 1px;display:none;float:left');
    $('popim').setStyle('padding-top:'+parseInt(dim.height/2 - 16)+'px;padding-left:'+parseInt(dim.width/2 - 16)+'px');
    $('popdiv').setStyle('overflow:auto;float:left;display:none;');
    $('popclose').onmouseover = function() { $('popcloset').show();popSize();};
    $('popclose').onmouseout = function() { $('popcloset').hide(); popSize();};
    $('popclose').onclick = popClose;
}

function popSize() {
    if(!$('popload')) return;
    var dim = d.viewport.getDimensions();
    $('popdiv').setStyle('height:auto;width:auto');
    //if(!window.popsize)
        //window.popsize = {width: $('popdiv').getWidth(), height: $('popdiv').getHeight()}
    //var ph = window.popsize.height;
    //var pw = window.popsize.width;
    var ph = $('popdiv').getHeight();
    var oldph = ph;
    var pw = $('popdiv').getWidth();
    var oldpw = pw;
    var ml = parseInt(dim.width/2 - (pw/2));
    var mt = parseInt(dim.height/2 - (ph/2));
    if(mt < 0 && ml < 0) {
        mt = 0;
        ml = 0;
        ph = dim.height+17;
        pw = dim.width+17;
    } else if(mt < 0) {
        mt = 0;
        ph = dim.height;
        pw += 17;
    } else if(ml < 0) {
        ml = 0;
        ph += 17;
        pw = dim.width;
    }
    $('popdiv').setStyle('margin-left:'+(isie6 ? parseInt(ml/2) : ml)+'px;margin-top:'+mt+'px;height:'+ph+'px;width:'+pw+'px');
    $('popclose').setStyle('left:'+(pw+ml-$('popclose').getWidth()-(oldph > ph ? 17 : 0))+'px;top:'+mt+'px');
}

function popLoad(html) {
    if(!$('popload')) return;
    $('popim').hide();
    if(typeof(html) == 'object') {
        $('popdiv').appendChild(html);
    } else {
        $('popdiv').update(html);
    }
    popSize();
    $('popdiv').show();
    $('popclose').show();
}

function popClose() {
    $('popload').remove();
}

function popPage(pag, style) {
    if(!pag) return;
    new Ajax.Request(pag, {
        method:'get',
        onSuccess: function(transport) {
            var response = transport.responseText;
            popLoad('<div style="'+style+'">'+response+'</div>');
        },
        onLoading: function() {
            popInit();
        }
    });
}
