// <![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;
    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(jQuery('#notes').length) return;
    var deb = jQuery('<div id="notes"></div>');
    jQuery("body").append(deb);
    jQuery('#notes').css({'position': 'fixed', 'width': '500px', 'overflow': 'auto', 'top': '0', 'z-index': '10000', 'padding-right': '5px', 'padding-bottom': '5px', 'left': (jQuery(window).width()-505)+'px'});
    jQuery('#notes').css({'position': (isie6 ? 'absolute' : 'fixed'), 'width': '500px', 'overflow': 'auto', 'top': '0', 'z-index': '10000', 'padding-right': '5px', 'padding-bottom': '5px', 'left': (jQuery(window).width()-505)+'px'});
    jQuery('#notes').css({'opacity' : '0.9'});
}
function set_notes(notes) {
    if(jQuery(window).height() < window.innerHeight)
        document.body.css({'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(jQuery('#notes').height() >= jQuery(window).height()-5)
        jQuery('#notes').css({'height': (jQuery(window).height()-5)+'px'});
    else
        jQuery('#notes').css({'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(jQuery('#note_'+m_id).length) m_id++;
    m_id = 'note_'+m_id;
    jQuery('#notes').append(jQuery('<div id="'+m_id+'"></div>'));
    jQuery('#'+m_id).css({'background': '#'+note_colors[type].bg, 'margin-top': '5px'});
    jQuery('#'+m_id).append(jQuery('<div id="close_'+m_id+'"></div>'));
    jQuery('#close_'+m_id).css({'float': 'right', 'font': 'bold 13px Arial', 'color': '#fff', 'background': 'url(http://www.bravotel.ro/skins/images/notification_close.gif) no-repeat', 'width': '16px', 'height': '20px', 'cursor': 'pointer'});
    jQuery('#close_'+m_id).bind('click', function() {
        jQuery(this).parent().hide();
        check_notes();
    });
    jQuery('#'+m_id).append(jQuery('<div id="title_'+m_id+'"></div>'));
    jQuery('#title_'+m_id).css(note_colors[type].ts);
    jQuery('#title_'+m_id).css({'margin-left': '20px', 'padding': '3px', 'width': '100px', 'text-align': 'center', 'text-transform': 'uppercase', 'font': 'bold 11px Arial'});
    jQuery('#title_'+m_id).html(title);
    jQuery('#'+m_id).append(jQuery('<div id="message_'+m_id+'"></div>'));
    jQuery('#message_'+m_id).css({'color': '#ffffff', 'font': '11px Arial', 'padding': '3px 6px 6px 6px'});
    jQuery('#message_'+m_id).html(message);
    check_notes();
    if(fadetime)
        setTimeout('jQuery(\''+m_id+'\').fadeOut(\'fast\');check_notes()', fadetime*1000);
}

function debug(el, functions) {
    show_note('D', 'Debug', debug_object(el, functions), 0, 0);
    //jQuery('#debug').scrollTop = jQuery('#debug').scrollHeight - jQuery('#debug').clientHeight;
}

function loader(id, stop) {
    if(id) {
        if(!jQuery(id)) return;
        if(jQuery(id).hasClassName('hidden'))
            jQuery(id).removeClassName('hidden');
        if(!jQuery(id).visible())
            jQuery(id).show();
        if(jQuery(id+'ajaxload'))
            jQuery(id+'ajaxload').remove();
        if(stop) {
            //jQuery(id).css({'cursor': jQuery(id).oldcursor});
            if(parseInt(jQuery(id).getStyle('height')) == 21)
                jQuery(id).css({'height': 'auto'});
            return;
        }
        //jQuery(id).oldcursor = jQuery(id).getStyle('cursor');
        //jQuery(id).css({'cursor': 'progress'});
        if(jQuery(id).height() < 21)
            jQuery(id).css({'height': '21px'});
        var offs = jQuery(id).cumulativeOffset();
        offs['left'] += parseInt(jQuery(id).getStyle('padding-left'));
        offs['top'] += parseInt(jQuery(id).getStyle('padding-top'));
        var deb = jQuery('<div id=""></div>')
        deb.id = id+'ajaxload';
        jQuery(document.body).append(deb);
        jQuery(id+'ajaxload').css({'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'});
        jQuery(id+'ajaxload').html('<img src="'+im+'ajax-loader.gif" alt="" style="vertical-align:middle"> '+loading);
        if(jQuery(id).height() >= jQuery(id+'ajaxload').height()+10) jQuery(id+'ajaxload').css({'margin-top': '5px'});
        if(jQuery(id).getWidth() >= jQuery(id+'ajaxload').getWidth()+10) jQuery(id+'ajaxload').css({'margin-left': '5px'});
        return;
    }
    if(!jQuery('#loading_div')) {
        var deb = jQuery('<div id=""></div>')
        deb.id = 'loading_div';
        jQuery(document.body).append(deb);
        jQuery('#loading_div').css({'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'});
        jQuery('#loading_div').html('<img src="'+im+'ajax-loader.gif" alt="" style="vertical-align:middle"> '+loading);
        //jQuery('#loading_div').setOpacity(0.9);
    }
    if(stop)
        jQuery('#loading_div').hide();
    else
        jQuery('#loading_div').show();
}

// ]]>
