var providers = {
    'hc' : 'Hostelsclub',   'gom': 'Gomio',
    'hm' : 'Hostelmania',   'iwb': 'InstantWorldBooking',
    'hrs': 'HRS',           'rtg': 'RatesToGo',
    'adv': 'HotelAdvisor',  'hde': 'Hotel.de',
    'hb' : 'Hostelbookers', 'hi' : 'HIHostels',
    'esc': 'Escapio',       'rec': 'Reconline',
    'bp' : 'BudgetPlaces',  'hu' : 'HostelUnion',
    'ht' : 'HostelTimes',   'ct' : 'Cultuzz',
    'lmg': 'BookingMarkets','du' : 'Dummy',
    'hw' : 'Hostelworld',   'h24': 'Hostels247',
    'etb': 'EasyToBook',    'bnw': 'Book Now App'
};
var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
var isMSIE = /*@cc_on!@*/false;



function submenu(menu_item, tab) {
    if (menu_item) {
        $(menu_item.parentNode).getElements('.active').removeClass('active');
        $(menu_item).addClass('active');
    }

    var subs = $$('div.' + 'subm');
    for (var i = 0; i < subs.length; i++) {
        $(subs[i]).setStyle('display', 'none');
    }
    $('subm_' + tab).setStyle('display', 'block');
}

function submenu_check() {
    var qString = document.location.search.substr(1).parseQueryString();

    if (qString.menu) {
        if ($('subm_' + qString.menu)) submenu(null, qString.menu);
    }
}



function show_overlay() {
    if (!$('overlay')) {
        var overlay = new Element('div', {
            'id': 'overlay',
            'styles': {
                'position': 'fixed'
            }
        });
        // IE6 fixed position workaround
        if (IE6) {
            var height = '1500';
            var els = $('body').getElements('.bigyen');
            if (els.length) {
                height = $(els[0]).getCoordinates();
                height = height.top + height.height + 20;
            }
            overlay = new Element('div', {
                'id': 'overlay',
                'styles': {
                    'position': 'absolute',
                    'height': height + 'px'
                }
            });
        }
        overlay.fade('hide');
        $('body').appendChild(overlay);
        overlay.fade(0.7);
        
        if(IE6)
            $('body').getElements('select').fade(0);
    }
}

function hide_overlay() {
    if ($('overlay')) {
        $('overlay').fade('out');
        $('overlay').dispose();
        
        if(IE6)
            $('body').getElements('select').fade(1);
    }
}

function hide_stuff(o) {
    $(o).setStyle('display', 'none');
}

function show_stuff(o) {
    $(o).setStyle('display', 'block');
}

function show_throbber(status_message) {
    if($('throbber')) return false;

    var throbber = new Element('div', {
        'id': 'throbber'
    });
    var throbberImg = new Element('img', {
        'src': '/img/throbber.gif'
    });
    var throbberMsg = new Element('div', {
        'html': status_message
    });

    throbber.adopt(throbberImg).adopt(throbberMsg);
    throbber.setStyle('width', ($('body').getSize().x - 50) + 'px');
    throbber.setStyle('top', ($('body').getScroll().y + 150) + 'px')

    $('body').appendChild(throbber);
}

function hide_throbber() {
    //  document.body.unspin();
    if ($('throbber')) {
        $('throbber').dispose();
    }
}



function goto(loc, newWindow) {
    if (newWindow) {
        window.open(loc);
    }
    else {
        location.href = loc;
    }
}

function changeProperty() {
    show_overlay();
    $('body').adopt(new Element('div', { 'id':'rmlst_wrapper', 'class':'centerbox' }));
    new Request.HTML({
        url: '/ajax/property_list.jsxt',
        onSuccess: function (txt, xml) { checkError(txt) },
        onFailure: show_fail_msg,
        update: $('rmlst_wrapper')
    }).post();
}

function selectProperty(pid) {
    $('rmlst_wrapper').dispose();
    show_throbber();

    new Request({
        url: '/ajax/set_property.jsxt',
        onSuccess: function (txt, xml) {
            if(checkError(txt)) return; window.location.reload();
        },
        onFailure: show_fail_msg
    }).post({'pid':pid});
}

function loginOverlay() {
    $('body').adopt(new Element('div', { 'id':'login_overlay_wrapper', 'class':'centerbox' }));

    new Request.HTML({
        url: '/ajax/login_overlay.jsxt',
        update: $('login_overlay_wrapper'),
        onSuccess: function (txt, xml) {
            $('login_form_overlay_redirect').set('value', window.location.pathname);
            if(checkError(txt)) return;
            window.location.reload();
        },
        onFailure: show_fail_msg
    }).post();
}

function show_fail_msg(debugTxt) {
    hide_throbber();
    show_overlay();
    
    var winScroller = new Fx.Scroll(window);
    winScroller.toTop();

    
    var fmsg = new Element('div', {
        'class': 'centerbox',
        'html': '<b>Ooops! An error has occured!</b><br />This could be due to '+
                'a problem with your network connection. In case of a problem ' +
                'on our side the support team is notified ' +
                'automatically. Please try again later.'
    });
    $('body').appendChild(fmsg);

    if (debugTxt) send_debug(debugTxt);
}

function send_debug(debugTxt) {
    new Request.HTML({
        url: "/ajax/debug.jsxt"
    }).post({
        msg: debugTxt
    });
}

function send_log(debugTxt) {
    new Request.HTML({
        url: "/ajax/debug.jsxt?log=true"
    }).post({
        msg: debugTxt
    });
}

function hasValue(o) {
    var allOk = true;

    o.each(function (item, index) {
        if ($(item).get('value') == '') allOk = false;
    });

    return allOk;
}


function keepAlive() {
    new Request.HTML({
        url: "/ajax/keepalive.jsxt"
    }).post();
}

function checkError(txt, error) {
    if(txt == 'Session Timeout') {
        hide_throbber();
        show_overlay();
        
        var winScroller = new Fx.Scroll(window);
        winScroller.toTop();

        loginOverlay();
        return true;
    }
    
    if(txt == null) {
        show_fail_msg();
        return true;
    }
    
    return false;
}

// Prevent browser without console from throwing JS errors
if (!window.console) {
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}


window.addEvent('domready', function () {
    submenu_check();
});



