// <![CDATA[
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;
var dev_mode = false;
var uid = 0;

function debug_object(object, functions, specials) {
    if(!object) return object;
    var res = '';
    switch(typeof(object)) {
        case 'object':
            res += 'Array (<br><div style="padding-left:15px">';
                var started = false;
                for(var i in object) {
                    if(typeof(object[i]) == 'function') {
                        if(functions) {
                            if(started) res += '<br>';
                            res += '['+i+'] => ';
                            res += object[i].toString().replace(/\{[\s\S]*/, '');
                            if(!started) started = true;
                        }
                    } else if(!functions) {
                        if(started) res += '<br>';
                        res += '['+i+'] => ';
                        if(typeof(object[i]) == 'object' && object[i]) {
                            try {
                                if(object[i].toString() == '[object Array]' || object[i].toString().match(/\[object\sObject\]/i))
                                    res += debug_object(object[i], functions, specials);
                                else
                                    res += object[i].toString();
                            } catch (e) { }
                        } else
                            res += object[i];
                        if(!started) started = true;
                    }
                }
            res += '</div>)<br>';
        break;
        default:
            //res += typeof(object[i]);
            if(specials)
                res += object.toString().replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br>').replace(/\[(\S+)\](.[^\]]*)?\[\/\1\]/g, '<$1>$2</$1>').replace(/\[(\S[^\]]+)\]/g, '<$1>');
            else
                res += object.toString().replace(/\n/g, '<br>').replace(/\[(\S+)\](.[^\]]*)?\[\/\1\]/g, '<$1>$2</$1>').replace(/\[br\]/g, '<br>');//replace(/\[(\S[^\]]+)\]/g, '<$1>');
        break;
    }
    return res;
}
function init_notes() {
    if($('notes')) return;
    var deb = new Element('div');
    deb.id = 'notes';
    $(document.body).appendChild(deb);
    $('notes').setStyle('position:'+(isie6 ? 'absolute' : 'fixed')+';width:500px;overflow:auto;top:0;z-index:10000;padding-right:5px;padding-bottom:5px;left:'+(document.viewport.getDimensions().width-505)+'px');
    $('notes').setOpacity(0.9);
}
function set_notes(notes) {
    if(document.viewport.getDimensions().height < window.innerHeight)
        document.body.setStyle('height:'+(window.innerHeight-17)+'px;');
    if(!notes.isJSON()) return;
    notes = notes.evalJSON();
    if(!notes) return;
    init_notes();
    notes.each(function(note) {
        show_note(note[0], note[1], debug_object(note[2], false, (note[0] == 'D' ? true : false)).replace(/\|quot\|/g, '"'), note[3], note[4]);
    });
}
function check_notes() {
    if($('notes').getHeight() >= document.viewport.getDimensions().height-5)
        $('notes').setStyle('height:'+(document.viewport.getDimensions().height-5)+'px');
    else
        $('notes').setStyle('height:auto');
}

function error(str) {
    show_note('E', 'Error', str, 10, 0);
}
function warning(str) {
    show_note('W', 'Warning', str, 10, 0);
}
function note(str) {
    show_note('N', 'Notification', str, 10, 0);
}

var note_colors = {
    E: {bg: 'd72323', ts: 'background:#b50000;color:#ff8b8b'}, 
    W: {bg: 'ff9600', ts: 'background:#f66e00;color:#ffce88'}, 
    D: {bg: '666666', ts: 'background:#444444;color:#999999'}, 
    N: {bg: '88ba00', ts: 'background:#5ca400;color:#e0ff8c;'}
};
function show_note(type, title, message, fadetime, m_id) {
    init_notes();
    //var m_id = 0;
    if(!m_id)
        while($('note_'+m_id)) m_id++;
    m_id = 'note_'+m_id;
    var deb = new Element('div');
    deb.id = m_id;
    $('notes').appendChild(deb);
    $(m_id).setStyle('background:#'+note_colors[type].bg+';margin-top:5px;');
    deb = new Element('div');
    deb.id = 'close_'+m_id;
    $(m_id).appendChild(deb);
    $('close_'+m_id).setStyle('float:right;font:bold 13px Arial;color:#fff;background:url('+im+'notification_close.gif) no-repeat;width:16px;height:20px;cursor:pointer;');
    $('close_'+m_id).onclick = function() {
        if(this.id.substring(11).length == 10)
            new Ajax.Request('ajax/close_notification/'+this.id.substring(11), {method:'get'})
        $(this).up().hide();
        check_notes();
    };
    deb = new Element('div');
    deb.id = 'title_'+m_id;
    $(m_id).appendChild(deb);
    $('title_'+m_id).setStyle(note_colors[type].ts+';margin-left:20px;padding:3px;width:100px;text-align:center;text-transform:uppercase;font:bold 11px Arial;');
    $('title_'+m_id).innerHTML = title;
    deb = new Element('div');
    deb.id = 'message_'+m_id;
    $(m_id).appendChild(deb);
    $('message_'+m_id).setStyle('color:#fff;font:11px Arial;padding:3px 6px 6px 6px;');
    $('message_'+m_id).innerHTML = message;
    $(m_id).appendChild(deb);
    check_notes();
    if(fadetime)
        setTimeout('$(\''+m_id+'\').fade();check_notes()', fadetime*1000);
}

function debug(el, functions) {
    if(!$('content')) dev_mode = true;
    if(!dev_mode) return;
    show_note('D', 'Debug', debug_object(el, functions), 0, 0);
    //$('debug').scrollTop = $('debug').scrollHeight - $('debug').clientHeight;
}

function loader(id, stop) {
    if(id) {
        if(!$(id)) return;
        if($(id).hasClassName('hidden'))
            $(id).removeClassName('hidden');
        if(!$(id).visible())
            $(id).show();
        if($(id+'ajaxload'))
            $(id+'ajaxload').remove();
        if(stop) {
            //$(id).setStyle('cursor:'+$(id).oldcursor);
            if(parseInt($(id).getStyle('height')) == 21)
                $(id).setStyle('height:auto');
            return;
        }
        //$(id).oldcursor = $(id).getStyle('cursor');
        //$(id).setStyle('cursor:progress');
        if($(id).getHeight() < 21)
            $(id).setStyle('height:21px');
        var offs = $(id).cumulativeOffset();
        offs['left'] += parseInt($(id).getStyle('padding-left'));
        offs['top'] += parseInt($(id).getStyle('padding-top'));
        var deb = new Element('div');
        deb.id = id+'ajaxload';
        $(document.body).appendChild(deb);
        $(id+'ajaxload').setStyle('border:1px solid #7c90b8;background:#8C9EC3;padding:3px 10px 3px 7px;color:#fff;font:bold 12px Arial;position:absolute;top:'+offs['top']+'px;left:'+offs['left']+'px;z-index:100');
        $(id+'ajaxload').update('<img src="'+im+'ajax-loader.gif" alt="" style="vertical-align:middle"> '+loading);
        if($(id).getHeight() >= $(id+'ajaxload').getHeight()+10) $(id+'ajaxload').setStyle('margin-top:5px');
        if($(id).getWidth() >= $(id+'ajaxload').getWidth()+10) $(id+'ajaxload').setStyle('margin-left:5px');
        return;
    }
    if(!$('loading_div')) {
        var deb = new Element('div');
        deb.id = 'loading_div';
        $(document.body).appendChild(deb);
        $('loading_div').setStyle('border:1px solid #7c90b8;background:#8C9EC3;padding:7px 20px 7px 10px;color:#fff;font:bold 12px Arial;position:'+(isie6 ? 'absolute' : 'fixed')+';top:10px;left:10px;z-index:10000');
        $('loading_div').update('<img src="'+im+'ajax-loader.gif" alt="" style="vertical-align:middle"> '+loading);
        //$('loading_div').setOpacity(0.9);
    }
    if(stop)
        $('loading_div').hide();
    else
        $('loading_div').show();
}

Event.observe(window, 'scroll', function() {
    if(!isie6 || !$('notes')) return;
    var dim = document.viewport.getScrollOffsets();
    $('notes').style.top = (dim.top+5)+'px';
});

Event.observe(window, 'unload', function() {
    loader();
});

Event.observe(window, 'resize', function() {
    if(!$('notes')) return;
    $('notes').setStyle('left:'+(document.viewport.getDimensions().width-508)+'px');
});

// ]]>
