String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}
String.prototype.endsWith = function(str) {
    return (this.match(str+"$")==str);
}
String.prototype.startsWith = function(str) {
    return (this.match("^"+str)==str);
}
Array.prototype.remove = function(e) {
    for(var i=0; i < this.length; i++) {
        if (this[i] == e) {
            this.splice(i,1);
            return;
        }
    }
}
function init() {
    $(".orp-check").focus(function() { 
        buff_check(this, false);
    }).blur(function() { 
        buff_check(this, true);
    }).before('<div class="orp-bubble" style="display:none;"></div>');
    $('.orp-bubble').each(function() {
        var p = $(this).parent();
        $(this).css({
            marginLeft:(p.find('input').outerWidth() + p.find('label').outerWidth() - 10)
        });
    });
    $('a.menu-site-map').hover(
        function() {
            buff_map.show(this);
        }
        );
    $('.menu-site-map').parent().hover(null, buff_map.hide);
    $("div.orp-linkable").click(buff_utils.innerClick);
    $(".orp-event").hover(buff_event.over, buff_event.out);
    $(".orp-gallery-item").click(buff_gallery.click);
    $(".orp-gallery-selector").each(buff_gallery.init);
    $(".orp-gallery-intro-box").click(buff_utils.innerClick);
    $(".news-item").click(buff_utils.innerClick);
    $('.orp-ticker').oTicker();
    $(".orp-animate").each(function() {
        new buff_Animation(this)
    });
    //$(".orp-events .orp-event").click(buff_utils.innerClick);
    $(window).resize(buff_resize);
    $(document).keyup(function(e) {
        if (e.keyCode == 27) buff_panel.close();
    });
    buff_cal.init();
    if (typeof(orpPageTag) != 'undefined') {
        if (orpPageTag.length > 0) {
            $('div.navigation a.' + orpPageTag).addClass('selected');
        }
    }
    $('.orp-igallery').each(
        function() {
            new orp_Gallery(this);
        }
        );
    $(".focus, .orp-focus").focus();
}
buff_resize = function() {
    $(".keep-centred").each(function() {
        buff_panel.centre($(this))
    });
}
buff_vote = function(q,a) {
    $("#poll-box").load("script-vote?q=" + q + "&a=" + a);
}
buff_check = function(item, doCheck) {
    var box = $(item);
    var parent = box.parent();
    var bubble = $('.orp-bubble', parent);
    $(bubble).css({
        marginLeft:(parent.find('input').outerWidth() + parent.find('label').outerWidth() - 10)
    });
    if (!doCheck) {
        parent.removeClass("bad").addClass("focus");
            
        msg = '';
        switch (item.name) {
            case "name" :
                msg = 'Must be at least five letters';
                break;
            case "email" :
                msg = 'Required';
                break;
            case "pass" :
                msg = 'Must be at least six characters';
                break;
            case "user" :
                msg = 'Must be at least five letters';
                break;
            case "pass2" :
                msg = 'Must match above';
                break;
        }
        if (msg.length > 0) {
            $('.orp-bubble', parent).show();
            $('.orp-bubble', parent).text(msg);
        }
    } else {
        parent.removeClass("focus").removeClass("bad");
        var val = box.val().trim();
        var err = false;
        var msg = '';
        switch(item.name) {
            case "name" :
                if (val.length < 5) {
                    err = true;
                    msg = 'Must be at least five letters';
                }
                break;
            case "email" :
                err = !val.match(/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/);
                msg = 'Check address';
                    
                break;
            case "pass" :
                if (val.length < 6) {
                    err = true;
                    msg = 'Must be at least six characters';
                }
                break;
            case "user" :
                if (val.length < 5) {
                    err = true;
                    msg = 'Must be at least five letters';
                }
                break;
            case "pass2" :
                if (val != $("#pass").val().trim()) {
                    err = true;
                    msg = 'Does not match above';
                }
        }
        if (err) {
            parent.addClass("bad");
            $('.orp-bubble', parent).text(msg).show();
        } else {
            $('.orp-bubble', parent).hide();
        }
    }
}
buff_map = {
    show : function(parent) {
        if (!buff_map.div) {
            buff_map.getDiv(parent).show();
            buff_map.div.html("LOADING MAP");
            buff_map.div.load(buildUrl("script-site-map"));
        } else {
            buff_map.div.show();
        }
        var off = $(parent).offset();
        //orp_frag.overlay.css("left", off.left + $(item).outerWidth() - orp_frag.overlay.outerWidth());
        buff_map.div.css("left", off.left - 300); // move to top left
        buff_map.div.css("top", off.top + 22);
    },
    hide : function() {
        if (buff_map.div) buff_map.div.hide();
    },
    getDiv : function(parent) {
        if (!buff_map.div) {
            $(parent).parent().append("<div id=\"orp-site-map\"></div>");
            buff_map.div = $("#orp-site-map");
        }
        return buff_map.div;
    }
}
/*buff_url = function(url) {
    var d = new Date();
    if (url.indexOf("?") == -1) {
        url += "?";
    } else {
        url += "&";
    }
    return url + "d=" + d.getTime();
}*/
buff_event = {
    over : function() {
        $("span.description", this).show();
    },
    out : function() {
        $("span.description", this).hide();
    }
}
buff_gallery = {
    click : function() {
        var panel = new orp_Panel({
            className:'orp-user-popup',
            shadeClick:true
        });
        buff_gallery.panel = panel;
        panel.html('' +
            '<div id="orp-gallery-popup-controls">' +
            '<a href="javascript:buff_gallery.prev();" class="orp-gallery-prev">PREV</a>' +
            '<div class="orp-gallery-summary"></div>' +
            '<a href="javascript:buff_gallery.next();" class="orp-gallery-next">NEXT</a></div>' +
            '<div id="orp-gallery-popup-image"></div>');
        buff_gallery.div = $("#orp-gallery-popup-image");
        //buff_img.div.css("min-height", buff_img.div.height() + "px");
        //buff_img.div.css("min-width", buff_img.div.width() + "px");
            
            
        buff_gallery.selector = $(this).parent();
        buff_gallery.current = $(this);
            
        buff_gallery.loadImage();
    },
    calcSummary : function() {
        $(".orp-gallery-summary").html(buff_gallery.current.data("count") + " of " + buff_gallery.selector.data("total"));
    },
    loadHandler : function(response, status, request) {
        if (status != "success") {
            error("failed to load " + request);
        } else {
            var h1 = buff_gallery.div.height();
            var h2 = $("img", buff_gallery.div).attr("height");
            if (h1 > h2) {
                $("img", buff_gallery.div).css("padding-top", (h1 - h2)/2 + "px");
            }
        }
    //PopupManager.resized(buff_gallery.panel);
    },
    init : function() {
        var total = 0;
        $(".orp-gallery-item", this).each(
            function() {
                total++;
                $(this).data("count", total);
            });
        $(this).data("total", total);
    },
    next : function() {
        buff_gallery.shift(1);

    },
    prev : function() {
        buff_gallery.shift(-1);
    },
    shift : function(i) {
        var a = buff_gallery.current.data("count");
        var b = buff_gallery.selector.data("total");
        a += i;
        if (a < 1) a = b;
        if (a > b) a = 1;
        buff_gallery.current = $("div:nth-child(" + a + ")", buff_gallery.selector);
        buff_gallery.loadImage();
    },
    loadImage : function() {
        buff_gallery.div.html('<div class="orp-gallery-waiting">IMAGE LOADING</div>');
        buff_gallery.div.load("script-gallery-image?id=" + $(buff_gallery.current).attr("rel") + "&w=500", buff_gallery.loadHandler);
        buff_gallery.calcSummary();
    }
}
buff_panel = {
    centre : function(div) {
        if (div == null) div = buff_panel.div;
        var ww = $(window).width();
        var wh = $(window).height();
        var pw = $(div).width();
        var ph = $(div).height();
        var left = (ww - pw)/2;
        var top = (wh - ph)/2;
        if (left < 20) left = 20;
        if (top < 20) top = 20;
        top = 30;
        div.css({
            left: left,
            top: top
        });
        buff_panel.moved();
    },
    moved : function() {
        if (buff_panel.div == null) return;
        var offset = buff_panel.div.offset();
        buff_panel.closeBut.css({
            left: offset.left + buff_panel.div.outerWidth() - buff_panel.closeBut.outerWidth(),
            top: offset.top
        });
    },
    popup : function(div) {
        if (buff_panel.div != null) {
            buff_panel.div.hide();
            buff_panel.div = null;
        }
        if (buff_panel.shade == null) {
            $('body').append('<div id="orp-back-shade"></div>');
            buff_panel.shade = $('#orp-back-shade');
            buff_panel.shade.click(buff_panel.clickShade);
        }
        if (buff_panel.closeBut == null) {
            $('body').append('<div id="orp-panel-close-but"><a href="javascript:buff_panel.close();">CLOSE</a></div>');
            buff_panel.closeBut = $("#orp-panel-close-but");
        }
        buff_panel.closeBut.show();
        buff_panel.div = div;
        div.show();
        buff_panel.shade.show();
        buff_panel.moved();
    },
    close : function() {
        if (buff_panel.div != null) {
            buff_panel.div.hide();
            buff_panel.shade.hide();
            buff_panel.closeBut.hide();
            buff_panel.div = null;
        }
    },
    clickShade : function() {
        buff_panel.close();
    }
}
buff_comments = {
    edit : function(id) {

    },
    editId : 0,
    click : function(e, id) {
        buff_comments.cancel();
        buff_comments.editId = id;
        var p = null;
        $('.orp-comment-parent').each(function() {
            if ($(this).attr('orp:cid') == id) p = $(this); 
        });
        if (p == null) return;
        buff_comments.parent = p;
        $(".orp-comment-edit a", p).hide();
        buff_comments.previousContent = $('.orp-comment', p).html();
        var txt = '';
        $('.orp-comment p', p).each(function() {
            txt = txt + $(this).text() + "\n"; 
        });
        //var txt = $(".orp-comment", p).html();
        txt = txt.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
        
        //txt = txt.replace(/<br>/g, "\n");
        $(".orp-comment", p).html('<textarea>' + txt + '</textarea><div class="oui-button-bar"><div class="oui-inner"><a href="javascript:buff_comments.update();" class="oui-button">Update</a></div></div>');
        $(".orp-comment textarea", p).focus();
    },
    cancel : function() {
        if (buff_comments.editId > 0) {
            $(".orp-comment", buff_comments.parent).html(buff_comments.previousContent);
            buff_comments.editId = 0;
        }
    },
    update : function() {
        var txt = $(".orp-comment textarea", buff_comments.parent).val();
        $.ajax({
            url:'script-comment-update',
            type: 'POST',
            data:{
                id: buff_comments.editId,
                comment:txt
            },
            contentType: "application/x-www-form-urlencoded; charset=UTF-8"
        });
        txt = txt.replace(/\n/g, "</p><p>");
        txt = '<p>' + txt + '</p>';
        $(".orp-comment-edit a", buff_comments.parent).css({
            display:'inline-block'
        });
        $(".orp-comment", buff_comments.parent).html(txt);
        buff_comments.editId = 0;
    },
    loaded : 0,
    moreButton : null,
    showMore : function(e, limit) {
        buff_comments.moreButton = $(e);
        buff_comments.moreButton.hide();
        buff_comments.loaded += limit;
        var extra = $('<div><div class="orp-waiting">Loading</div></div>');
        $('#discuss-parent').append(extra);
        var loader = new buff_Loader({
            url:'script-discussions-load-more', 
            data:{
                offset:buff_comments.loaded
            }, 
            onComplete:buff_comments.moreComplete, 
            onError:buff_comments.moreError,
            e:extra, 
            msg:'<span class="orp-waiting">Loading older discussion topics</span>'
        })
    },
    moreComplete : function() {
        buff_comments.moreButton.show();
        $("div.orp-linkable", this).click(buff_utils.innerClick);
    },
    moreError : function() {
        $(this).html('<div class="orp-error">Failed to load more discussions</div>');
        buff_comments.moreButton.show();
    }
    
}
var buff_cal = {
    init : function() {
        var picker = $("#orp-calendar-picker");
        if (picker.length > 0) {
            var table = '<table><tr><td class="prev-month">-</td><td class="month" colspan="5">Month</td><td class="next-month">+</td></tr><tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>';
            for(var i=0; i < 6; i++) {
                table += '<tr class="week"><td></td><td></td><td></td><td></td><td></td><td></td><td></td>';
            }
            table += '</table>';
            picker.html(table);
            var d = new Date($('input[name=year]').val(),$('input[name=month]').val()-1,$('input[name=day]').val());
            buff_cal.pickerDate = d;
            buff_cal.selectedDate = d;
            buff_cal.pickerDiv = picker;
            buff_cal.redrawCalendar();

            $('tr.week td').click(buff_cal.pickerClick).hover(buff_cal.pickerOver, buff_cal.pickerOut);
            $('td.prev-month', picker).click(buff_cal.prevClick);
            $('td.next-month', picker).click(buff_cal.nextClick);
        }
    },
    formLoaded : function() {
        $('form#orp-user-event').submit(buff_cal.submitEvent);
    //$('#orp-user-event-cancel').click(buff_cal.cancelEvent);
    },
    redrawCalendar : function() {
        var d = buff_cal.pickerDate;
        var picker = buff_cal.pickerDiv;
        var first = new Date(d.getFullYear(), d.getMonth(), 1);
        $('td.month', picker).html(buff_cal.months[d.getMonth()] + " " + d.getFullYear());
        first = new Date(first.getFullYear(), first.getMonth(), 1-first.getDay());
        $('tr.week', picker).each(function() {
            $(this).removeClass('myweek');
            if (first.getTime() == buff_cal.selectedDate.getTime()) $(this).addClass('myweek');
            $('td', this).each(function() {
                $(this).html(first.getDate()).data('date', first).removeClass('not');
                if (first.getMonth() != d.getMonth()) $(this).addClass('not');
                first = new Date(first.getFullYear(), first.getMonth(), first.getDate()+1);
            });
        });
    },
    pickerOver : function() {
        $(this).parent().addClass('hover');
    },
    pickerOut : function() {
        $(this).parent().removeClass('hover');
    },
    pickerClick : function() {
        var d = $(this).data('date');
        window.location = window.location.pathname + "?group=" + $('input[name=group]').val() + "&y=" + d.getFullYear() + "&m=" + (d.getMonth() + 1) + "&d=" + d.getDate();
        return false;
    },
    prevClick : function() {
        buff_cal.incMonth(-1);
        return false;
    },
    nextClick : function() {
        buff_cal.incMonth(1);
        return false;
    },
    incMonth : function(inc) {
        var d = buff_cal.pickerDate;
        buff_cal.pickerDate = new Date(d.getFullYear(), d.getMonth() + inc, 1);
        buff_cal.redrawCalendar();
    },
    addEventForm : null,
    addEvent : function(pid, pdate) {
        var panel = new orp_Panel({
            className:'orp-user-popup'
        });
        panel.load(buildUrl('script-edit-event', {
            pid:pid,
            date:pdate
        }), buff_cal.formLoaded);
    },
    editEvent : function(id) {
        var panel = new orp_Panel({
            className:'orp-user-popup'
        });
        panel.load(buildUrl('script-edit-event', {
            id:id
        }), buff_cal.formLoaded);
    },
    submitEvent : function() {
        var form = $('#orp-user-event');
        var status = $('p.status', form);
        if (status.length == 0) {
            form.append('<p class="status"></p>');
            status = $('p.status', form);
        }
        status.html("Submitting your event");
        form.ajaxSubmit({
            error: buff_cal.eventFailed,
            success : buff_cal.eventSent
        });
        return false;
    },
    eventFailed : function(request, error) {
        $('#orp-user-event p.status').html(request.responseText);
    },
    eventSent : function(result) {
        var form = $('#orp-user-event');
        var status = $('p.status', form);
        status.html("form sent, refreshing page");
        buff_utils.reload();
    },
    months : ['January','February','March','April','May','June','July','August','September','October','November','December']
}
var buff_utils = {
    showMore : function(e) {
        $("div.orp-more", $(e).parent()).show();
        $(e).hide();
    },
    innerClick : function() {
        var a = $('a', this);
        if (a.length > 0) {
            document.location = a.attr('href');
        }
    },
    reload : function() {
        document.location.reload(true);
    }
}
function orp_Panel(params) {
    var me = this;
    var cross = null;
    if (params == null) params = {};
    var closedCallback = params.closed;
    var loadedCallback = null;
    var zIndex = orp_MaxIndex(popupList) + 10;
    var noShade = (params.shade === false);
    var below = params.below;
    if (popupList.length == 0) {
        $('.orp-player object').hide();
    }
    popupList.push(me);
    var level = popupList.length;
    if (params.className == null) params.className = 'orp-panel orp-standard';
    var shade = null;
    if (!noShade) {
        shade = $('<div></div>');
        shade.css({
            position:'fixed',
            width:'100%',
            height:'100%',
            opacity:0.5,
            filter:'alpha(opacity=50)',
            left:0,
            top:0,
            'background-color':'#000',
            'z-index':zIndex
        });
        shade.click(close);
    }
    var panel = $('<div><p class="orp-loading">LOADING</p></div>');
    panel.addClass(params.className);
    if (params.id != null) panel.attr('id', params.id);
    panel.css({
        position:'absolute',
        'z-index':zIndex + 5
    });
    $(window).resize(resized);
    if (params.cross !== false) {
        cross = $('<a href="javascript:;" class=\"orp-popup-cross\">&nbsp;</a>');
        cross.css({
            position:'absolute',
            'z-index':zIndex + 6
        });
        cross.click(close);
    }
    if (shade != null) $('body').append(shade);
    $('body').append(panel);
    if (cross != null) $('body').append(cross);
    resized();
    this.resized = resized;
    this.close = close;
    this.load = load;
    this.empty = empty;
    this.append = append;
    this.html = html;
    this.zIndex = zIndex;
    this.panel = panel;
    this.cross = cross;
    this.prepend = prepend;
    this.setBelow = setBelow;
    this.css = panel.css;
    this.outerHeight = panel.outerHeight;
    this.outerWidth = panel.outerWidth;

    function resized() {
        var ww = $(window).width();
        var wh = $(window).height();
        var pw = panel.outerWidth();
        var ph = panel.outerHeight();
        var left = (ww - pw)/2;
        var top = (wh - ph)/2;
        if (left < 20) left = 20;
        if (top < 20) top = 20;
        top = 30 + level * 20;
        top = top + $(window).scrollTop();
        if (below != null) {
            var boffset = $(below).offset();
            left = boffset.left;
            top = boffset.top;
            var topmax = $(window).scrollTop() + $(window).height() - 10;
            if ((top + panel.outerHeight()) > topmax) top = topmax-panel.outerHeight();
        }
        panel.css({
            left:left,
            top:top
        });
        if (cross != null) {
            cross.css({
                left:left + panel.outerWidth() -14,
                top:top - 10
            });
        }
    }
    function setBelow(value) {
        this.below = value;
        resized();
    }
    function load(url, callback) {
        loadedCallback = callback;
        panel.load(url, loadHandler);
    }
    function loadHandler(response, status, request) {
        if (status == "success") {
            $('.orp-focus', panel).focus();
            if ($('ul.orp-tabs', panel).length > 0) {
                new orp_Tabs(this);
            }
            resized();
            if (loadedCallback != null) {
                loadedCallback.call(this, true, response);
            }
        } else {
            close();
            error(response);
            if (loadedCallback != null) loadedCallback.call(this, false);
        }
    }
    function close() {
        $(window).unbind('resize', resized);
        popupList.remove(me);
        if (popupList.length == 0) {
            $('.orp-player object').show();
        }
        panel.remove();
        if (shade != null) shade.remove();
        if (cross != null) cross.remove();
        if (closedCallback != null) {
            var callback = closedCallback;
            closedCallback = null;
            callback.call(this);
        }
    }
    function html(code) {
        panel.html(code);
        resized();
    }
    function append(nodes) {
        panel.append(nodes);
        resized();
    }
    function prepend(nodes) {
        panel.prepend(nodes);
        resized();
    }
    function css(params) {
        panel.css(params);
    }
    function empty() {
        panel.empty();
    }
}
function error(msg, title) {
    buff_error.open(msg, title);
}
var buff_error = {
    panel : null,
    closed : function() {
        buff_error.panel = null;
    },
    open : function(msg, title) {
        if (title == null) title = 'Error';
        if (buff_error.panel == null) {
            buff_error.panel = new orp_Panel({
                closed:buff_error.closed
            });
            buff_error.panel.html('<h1>' + title + '</h1><p>' + msg + '</p>');
        } else {
            buff_error.panel.append('<p>' + msg + '</p>');
        }
    }
}
function orp_MaxIndex(list) {
    var index = 0;
    for(var i = 0; i < list.length; i++) {
        index = Math.max(index, list[i].zIndex);
    }
    return index;
}
var popupList = new Array();
var PopupManagerOLD = {
    highPanel : 0,
    darkness : 0.5,
    total : 0,
    list : new Array(),
    init : function(params) {
        $(window).resize(PopupManager.resizeAll);
        if (params == null) return;
    },
    getPanel : function(params) {
        if (params == null) params = {};
        if (params.className == null) params.className = 'orp-panel orp-standard';
        var shade = $('<div></div>');
        PopupManager.highPanel++;
        PopupManager.total++;
        shade.css({
            position:'fixed',
            width:'100%',
            height:'100%',
            opacity:PopupManager.darkness,
            filter:'alpha(opacity=' + (PopupManager.darkness * 100) + ')',
            left:0,
            top:0,
            'background-color':'#000',
            'z-index':PopupManager.highPanel*10
        });
        var panel = $('<div><p class="orp-loading">LOADING</p></div>');
        if (params.className != null) panel.addClass(params.className);
        if (params.id != null) panel.attr('id', params.id);
        panel.css({
            position:'absolute',
            'z-index':PopupManager.highPanel*10 + 5
        });
        panel.data('shade', shade);
        panel.data('level', PopupManager.highPanel);
        panel.data('closed', params.closed);
        shade.data('panel', panel);
        if (params.shadeClick == true) shade.click(PopupManager.clickShade);
        var cross = $('<a href="javascript:;" onclick="PopupManager.closeButton(this);" class=\"orp-popup-cross\">&nbsp;</a>');
        cross.css({
            position:'absolute',
            'z-index':PopupManager.highPanel*10 + 6
        });
        if (params.cross === false) cross.hide();
        panel.data("cross", cross);
        cross.data("panel", panel);
        $('body').append(shade).append(cross).append(panel);
        PopupManager.resized(panel);
        PopupManager.list.push(panel);
        return panel;
    },
    hideCross : function(panel) {
        panel.data("cross").hide();
    },
    showCross : function(panel) {
        panel.data("cross").show();
    },
    clickShade : function() {
        PopupManager.close($(this).data('panel'));
    },
    resized : function(panel) {
        var ww = $(window).width();
        var wh = $(window).height();
        var pw = panel.outerWidth();
        var ph = panel.outerHeight();
        var left = (ww - pw)/2;
        var top = (wh - ph)/2;
        if (left < 20) left = 20;
        if (top < 20) top = 20;
        top = 30 + panel.data('level') * 20;
        top = top + $(window).scrollTop();
        panel.css({
            left:left,
            top:top
        });
        var cross = panel.data('cross');
        if (cross != null) {
            cross.css({
                left:left + panel.outerWidth() -14,
                top:top - 10
            });
        }
    },
    resizeAll : function() {
        var list = PopupManager.list;
        if (list.length == 0) return;
        for(var i=0; i < list.length; i++) {
            PopupManager.resized(list[i]);
        }
    },
    closeButton : function(e) {
        var cross = $(e);
        PopupManager.close(cross.data('panel'));
        return false;
    },
    close : function(panel) {
        var cross = panel.data('cross');
        var shade = panel.data('shade');
        var closed = panel.data('closed');
        removeElement(PopupManager.list, panel);
        /*var h = 0;
        for(var i = 0; i < PopupManager.list.length; i++) {
            var l = PopupManager.list[i].data('level');
            if (l > h) h = l;
        }
        PopupManager.highPanel = h;*/
        if (cross != null) cross.remove();
        if (shade != null) shade.remove();
        if (panel != null) panel.remove();
        PopupManager.total--;
        if (PopupManager.total == 0) PopupManager.highPanel = 0;
        if (closed != null && closed != false) closed();
    },
    load : function(panel, url, callback) {
        panel.data('callback', callback);
        panel.load(url, PopupManager.loadHandler);
    },
    loadHandler : function(response, status, request) {
        var callback = $(this).data('callback');

        if (status == "success") {
            // OKAY!
            PopupManager.resized($(this));
            $('.orp-focus', this).focus();
            if (callback != null) callback.call(this, true, response);
        } else {
            PopupManager.close($(this));
            error(response);
            //$(this).html('<div class="orp-error"><h3>Error</h3><p>' + request.responseText + '</p></div>');
            if (callback != null) callback.call(this, false);
        }
    }
};
var buff_menu = {
    opened : null,
    element : null,
    openDrop : function(event, e, id) {
        if (event.preventDefault) event.preventDefault();
        event.cancelBubble = true;
        if (event.stopPropagation) event.stopPropagation();
        if (e == buff_menu.element) {
            buff_menu.close();
        } else {
            if (buff_menu.opened) buff_menu.close();
            buff_menu.element = e;
            $(buff_menu.element).addClass('orp-menu-open');
            $('body').bind('click', buff_menu.windowClicked);
            buff_menu.opened = $('#' + id).show();
        }
    },
    close : function() {
        if (buff_menu.opened) buff_menu.opened.hide();
        $(buff_menu.element).removeClass('orp-menu-open');
        buff_menu.element = null;
        buff_menu.opened = null;
        $('body').unbind('click', buff_menu.windowClicked);
    },
    windowClicked : function() {
        buff_menu.close();
    }
}
function buff_Loader(iargs) {
    var args = iargs;
    var cancelled = false;
    var e = args.e;
    if (!args.params) args.params = {};
    if (args.data) args.params = args.data;
    var url = buildUrl(args.url, args.params);
    if (args.msg) e.html(args.msg);
    e.load(url, loadHandler);
    if (args.onSentRequest != null) args.onSentRequest.call(e);
    this.cancel = cancel;
    function loadHandler(response, status) {
        if (status != 'success') {
            if (args.onError != null) {
                args.onError.call(this, response, args.errorMsg);
            } else {
                if (!args.quiet) {
                    error(response, args.errorMsg);
                    e.html('<div class="orp-error">' + response + "</div>");
                }
            }
        } else {
            if (args.onComplete != null) args.onComplete.call(this, response, args.completeArg);
            if (args._onComplete) args._onComplete.call(this, response, args._completeArg);
        }
    }
    function cancel() {
        cancelled = true;
    }
}
function buff_Animation(ie) {
    var e = $(ie);
    if (e.attr('orp:delay') != null) {
        setTimeout(animate, Number(e.attr('orp:delay')));
    } else {
        animate();
    }
    function animate() {
        switch(e.attr('orp:animation')) {
            case 'slideup' :
                e.slideUp();
                break;
        }
    }
}
function removeElement(list, e) {
    for(var i=0; i < list.length; i++) {
        if (list[i] == e) {
            list.splice(i,1);
            return;
        }
    }
}
function buildUrl(path, params) {
    if (params == null) {
        params = {};
    }
    params.rand = new Date().getTime();

    var first = true;
    for(var key in params) {
        if (first) {
            path += "?";
        } else {
            path += "&";
        }
        first = false;
        path += key + "=" + encodeURIComponent(params[key]);
    }
    return path;
}
var buff_follow = {
    follow : function(id,e) {
        var p = $(e).parent();
        new buff_Loader({
            url:'script-follow', 
            data:{
                id:id
            }, 
            onComplete:buff_follow.followComplete, 
            completeArg:p, 
            e:p, 
            msg:'<span class="orp-follow-action orp-waiting">Waiting</span>'
        });
    },
    followComplete : function(e) {
        
    },
    unfollow : function(id,e) {
        var p = $(e).parent();
        new buff_Loader({
            url:'script-unfollow', 
            data:{
                id:id
            }, 
            onComplete:buff_follow.unfollowComplete, 
            completeArg:p, 
            e:p, 
            msg:'<span class="orp-follow-action orp-waiting">Waiting</span>'
        });
    },
    unfollowComplete : function(e) {
        
    }
}
$(document).ready(init);
