$AR = function() {
    var v = {
        root: '',
        fi: '',
        bi: '',
        page: 0,
        journal: 0
    };
    String.prototype.startsWith = function(str) {
        return (this.match("^"+str)==str);
    };
    return {
        log: function() {
            if (window.console && window.console.firebug) {
                console.log.apply(null, arguments);
            } else if (window.console && console.log) {
                console.log(arguments[0]);
            }
        },
        sv: function(k, v0) {
            v[k] = v0;
        },
        gv: function(k) {
            return v[k];
        },
        gi: function(s, r) {
            return s.replace(r, '');
        },
        nn: function(n) {
            var n2 = parseInt(n,0);
            if (isNaN(n2)) {
                n2 = 0;
            }
            return n2;
        },
        go: function(url) {
            document.location.href = url;
        },
        go2: function(p,w,h){
            window.open(
                p,
                '',
                'scrollbars=yes,toolbar=0,'+
                'left=30,top=30,resizable=yes,width='+w+
                ',height='+h
            );
        },
        ie_xml: function(xml) {  
            if (jQuery.browser.msie) {  
                var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");  
                xmlDoc.loadXML(xml);  
                xml = xmlDoc;  
            }  
            return xml;  
        },
        init: function(){
            $('.m-data').each(function(){
                $(this).data('ln',0);
                $AR.dat.init($AR.gi(this.id,'data'));
            });
            $('.m-data-filter-a-toggle').click($AR.dat.toggle);
            $('.m-data-btn-apply').click($AR.dat.fs);
            $('.m-data-filter-keyup').keyup($AR.dat.fs);
            $('.m-data-filter-change').change($AR.dat.fs);
            $('.m-data-filter-check').click($AR.dat.fs);
            $('.m-data-sort-select').live('change',$AR.dat.sort_select);
            $('.hoverable').live('mouseover mouseout', function(e) {
              if (e.type == 'mouseover') {
                $(this).addClass('hover');
              } else {
                $(this).removeClass('hover');
              }
            });
            $('.arnav').find('li').hover(function(){
                $(this).addClass('hover');
            },function(){
                $(this).removeClass('hover');
            });
            $('.comments-button').live('click', $AR.comments.clicked);
        },
        client: {}
    };
} ();

$AR.forms = {
    dependency: function(p,d){
        var $pd,
        pd = 'ardep_'+p+'__'+d+'',
        $p = $('select[name='+p+']'),
        $d = $('select[name='+d+']');
        $('body').append(
            '<select id="'+pd+'" style="display:none;"></select>'
        );
        $pd = $('#'+pd);
        $pd.html($d.html());
        $p.change(function(){
           $AR.forms.dep_change($p,$d,$pd);
        });
        $AR.forms.dep_change($p,$d,$pd);
    },
    dep_change: function($p,$d,$pd){
        var pv = $p.find('option:selected').attr('arval');
        $d.html('');
        $pd.find('option').each(function(){
            $AR.forms.dep_fill(this,$d,$pd,pv);
        });
    },
    dep_fill: function(obj,$d,$pd,pv){
        var pdv = $(obj).attr('arval'), 
            str = '';
        if (pdv.startsWith(pv+'_')||pdv=='') {
            if ($pd.find('option:selected').attr('arval')==pdv) {
                str = " SELECTED";
            }
            str = '<option value="'+$(obj).val()+'"'+str+'>'+$(obj).text();
            $d.append(str);
        }
    }
};

$AR.lib = { /* member library */
    aff: 0,
    uc_id: '',
    uc_code: '',
    qerror: '',
    qsize: 0,
    fquota: 0,
    undo_o: null,
    lock: 0,
    lock_o: null,
    lock_e: null,
    last: -1,
    lock_key: false,
    lock_ent: false,
    mode: '',
    libmode: 0,
    profile: 0,
    get_lock: function() {
        this.lock += 1;
        return this.lock - 0;
    },
    get_file: function(id) {
        if ($AR.lib.libmode==1) {
            window.opener.arUpdateImageField(id);
            window.close();
        } else if ($AR.lib.libmode==2) {
            window.opener.$AR.ap.update_image(id);
            window.close();
        } else {
            window.open('file.asp?id='+id);
        }
    },
    get_ff: function(obj) {
        try {
            return obj.id.replace(/[0-9]*/g, '');
        } catch(ex) {
            return '';
        }
    },
    get_selected: function(){
        var ffs = [];
        $('.arlib-con-folder-selected').add(
            '.arlib-con-file-selected'
        ).each(
            function(){
                ffs.push(this.id);
            }
        );
        return ffs;
    },
    init: function(profile, fquota, uc_id, uc_code, libmode, aff) {
        $AR.lib.profile = profile;
        $AR.lib.fquota = fquota;
        $AR.lib.uc_id = uc_id;
        $AR.lib.uc_code = uc_code;
        $AR.lib.libmode = libmode;
        $AR.lib.load_content(aff, 1);

        //crumb listeners
        $('.arlib-a-crumb').live('click', $AR.lib.crumb_click);
        
        //upload listeners
        $('.arlib-q-btn').hover(
            function(){$(this).addClass('arlib-q-btn-hover');},
            function(){$(this).removeClass('arlib-q-btn-hover');}
        );
        $('.arlib-q-start').click(function() {
            $AR.lib.q_start();
            return false;
        });
        $('.arlib-q-stop').click(function() {
            swfu.cancelQueue();
            $AR.lib.file_hide(
                $('#arlib_q .arlib-q-item')
            );
            $AR.lib.qsize = 0;
            $('.arlib-con-screen').hide();
            $AR.lib.state('empty');
            return false;
        });
        $('.arlib-a-item-cancel').live('click',
        function() {
            var it = $(this).parent(
            '.arlib-q-item'
            )[0];
            var size = $(it).data('size');
            swfu.cancelUpload(it.id.replace('arlQ_', ''));
            $AR.lib.qsize -= size;
            $AR.lib.file_hide($(it));
            if (swfu.getStats().files_queued > 0) {
                $AR.lib.q_status();
            } else {
                $('.arlib-con-screen').hide();
                $AR.lib.state('empty');
            }
            return false;
        });

        //content listeners
        $('.arlib-con-folder').live('dblclick', this.folder_dblclick);
        $('.arlib-con-file').live('dblclick', this.file_dblclick);
        $('.arlib-con-item').live('click', this.item_click);
        $('.arlib-con-folder').live('mouseover', this.mouseover);
        $('.arlib-con-folder').live('mouseout', this.mouseout);
        $('.arlib-con-file').live('mouseover', this.mouseover);
        $('.arlib-con-file').live('mouseout', this.mouseout);

        //tree listeners
        $('.arlib-tree-folder').live('dblclick', this.tree_dblclick);
        $('.arlib-tree-folder').live('click', this.tree_click);
        $('.arlib-tree-folder').live('mouseover', this.tree_mouseover);
        $('.arlib-tree-folder').live('mouseout', this.tree_mouseout);

        //content buttons
        $('.arlib-btn').hover(
            function() {$(this).addClass('arlib-btn-hover');},
            function() {$(this).removeClass('arlib-btn-hover');}
        ).click($AR.lib.btn_click);

        //keyboard listeners
        $(document).keydown($AR.lib.keydown);
    },
    init2: function(data) {
        $('.arlib-con-item input').keyup(
            $AR.lib.item_key
        ).blur($AR.lib.item_blur);
        if ($AR.lib.mode != 'uploading') {
            $AR.lib.clear();
            $AR.lib.state('empty');
        }
        try {
            var aff_new = $AR.nn($('#arlib_new_id')[0].value);
            if (aff_new > 0) {
                var $o = $('#aff' + aff_new);
                $AR.lib.lock_o = $o[0];
                $AR.lib.lock_e = null;
                $o.addClass('arlib-con-folder-selected');
                $AR.lib.next2($AR.lib.get_lock(), 'item_click');
            }
        } catch(ex) {}
    },
    undo: function() {
        try {
            var $o = $($AR.lib.undo_o);
            $o.find('input')[0].value = $o.data('old');
        } catch(ex) {}
        $AR.lib.undo_o = null;
    },
    del: function(){
        if (confirm(
            'Are you sure you wish to permanently ' +
            'delete the selected items? This ' +
            'action cannot be undone.'
        )) { 
            $.post(
                'ajax.asp',
                {
                    'ajax': 'lib_action',
                    'action': 'delete',
                    'profile': $AR.lib.profile,
                    'affCurrent': $AR.lib.aff,
                    'ffs': $AR.lib.get_selected().join(",")
                },
                function(){$AR.lib.reload();}
            );
        }
    },
    reload: function(){
        $AR.lib.load_content($AR.lib.aff, 0);
    },
    move: function(){
        if ($AR.lib.mode=='move2') {
            var id = $AR.gi(
                $('.arlib-tree-folder-selected')[0].id,
                'arlib_tree'
            );
            $.post(
                'ajax.asp',
                {
                    'ajax': 'lib_action',
                    'action': 'move',
                    'profile': $AR.lib.profile,
                    'dest': id,
                    'ffs': $AR.lib.get_selected().join(",")
                },
                $AR.lib.pb_move
            );
        } else {
            $AR.lib.state('move');
            $('#arlib_preview').hide();
            $('#arlib_tree').html('').show().load(
                'ajax.asp?ajax=lib_action&action=tree&profile=' +
                $AR.lib.profile
            );
        }
    },
    clear: function() {
        $('#arlib_preview').hide();
        $('#arlib_tree').hide().html('');
        $AR.lib.lock = 0;
        $AR.lib.lock_o = null;
        $AR.lib.lock_e = null;
        $AR.lib.lock_key = false;
        $AR.lib.last = -1;
        $AR.lib.mode = '';
        $('.arlib-con-item').removeClass(
        'arlib-con-file-selected'
        ).removeClass(
        'arlib-con-folder-selected'
        ).removeClass(
        'arlib-con-folder-hover'
        ).removeClass(
        'arlib-con-file-hover'
        );
        $('.arlib-con-text').show();
        $('.arlib-con-edit').hide();
    },
    next1: function(action, obj, evt) {
        lock = $AR.lib.get_lock();
        $AR.lib.lock_o = obj;
        $AR.lib.lock_e = evt;
        setTimeout('$AR.lib.next2(' + lock + ',"' + action + '")', 250);
    },
    next2: function(lock, action) {
        if (lock != $AR.lib.lock) {
            return false;
        }
        $AR.lib.get_lock();
        var $o = $($AR.lib.lock_o);
        var e = $AR.lib.lock_e;
        var shift = false,
        ctrl = false;
        try {
            shift = e.shiftKey;
            ctrl = (e.ctrlKey || e.altKey);
        } catch(ex) {}
        var index = $('.arlib-con-item').index($o[0]);
        switch (action) {
            case 'item_click':
                var last = $AR.lib.last;
                var ffc = ($o[0].id.indexOf('aff') > -1) ? 'folder': 'file';
                if ($AR.lib.mode == 'edit') {
                    // locked
                    return false;
                } else if (shift && last > -1) {
                    // multi
                    var mn = Math.min(last, index);
                    var mx = Math.max(last, index);
                    var $g = $('.arlib-con-item').slice(
                    mn,
                    mx + 1
                    );
                    $('.arlib-con-item').removeClass(
                    'arlib-con-folder-selected'
                    ).removeClass(
                    'arlib-con-file-selected'
                    );
                    $g.filter('.arlib-con-folder').addClass(
                    'arlib-con-folder-selected'
                    );
                    $g.filter('.arlib-con-file').addClass(
                    'arlib-con-file-selected'
                    );
                    $AR.lib.state('multi');
                } else if (ctrl) {
                    //multi
                    $o.toggleClass('arlib-con-' + ffc + '-selected');
                    $AR.lib.last = index;
                    $AR.lib.state('multi');
                } else if (
                $o.hasClass('arlib-con-folder-selected') ||
                $o.hasClass('arlib-con-file-selected')
                ) {
                    // edit
                    $('.arlib-con-item').removeClass(
                    'arlib-con-folder-selected'
                    ).removeClass(
                    'arlib-con-file-selected'
                    ).removeClass(
                    'arlib-con-folder-hover'
                    ).removeClass(
                    'arlib-con-file-hover'
                    );
                    $o.addClass('arlib-con-' + ffc + '-selected');
                    $o.children('.arlib-con-text').hide();
                    $o.children('.arlib-con-edit').show();
                    var $in = $o.find('input')[0];
                    $in.select();
                    $in.focus();
                    $AR.lib.last = index;
                    $AR.lib.undo_o = $o[0];
                    $AR.lib.state('edit');
                } else {
                    $('.arlib-con-item').removeClass(
                    'arlib-con-folder-selected'
                    ).removeClass(
                    'arlib-con-file-selected'
                    );
                    $o.addClass('arlib-con-' + ffc + '-selected');
                    $AR.lib.last = index;
                    $AR.lib.state('selected');
                    if (ffc == 'file') {
                        $AR.lib.preview($o[0]);
                    }
                }
                break;
            case 'tree_click':
                $('.arlib-tree-folder').removeClass(
                    'arlib-tree-folder-selected'
                );
                $o.addClass('arlib-tree-folder-selected');
                $AR.lib.state('move2');
                break;
        }
        return false;
    },
    keydown: function(e) {
        var mode = $AR.lib.mode;
        var esc = {'selected':0,'move':0,'move2':0};
        if (mode in esc && e.keyCode == 27) {
            $AR.lib.clear();
            $AR.lib.state('empty');
        }
        if ($AR.lib.mode == 'selected' && e.keyCode == 46) {
            $AR.lib.del();
        }
    },
    mouseover: function() {
        if ($AR.lib.mode != '') {return false;}
        if ($(this).hasClass('arlib-con-folder')) {
            $(this).addClass('arlib-con-folder-hover');
            if ($(this).hasClass('arlib-folder-up')) {
                $(this).addClass('arlib-folder-up-hover');
            }
        } else {
            $AR.lib.preview(this);
        }
        return false;
    },
    mouseout: function() {
        if ($AR.lib.mode != '') {
            return;
        }
        if ($(this).hasClass('arlib-con-folder')) {
            $(this).removeClass('arlib-con-folder-hover');
            $(this).removeClass('arlib-folder-up-hover');
        } else {
            $(this).removeClass('arlib-con-file-hover');
            $('#arlib_preview').hide();
        }
        return;
    },
    tree_mouseover: function() {
        $(this).addClass('arlib-tree-folder-hover');
    },
    tree_mouseout: function() {
        $(this).removeClass('arlib-tree-folder-hover');
    },
    preview: function(obj) {
        var id = $AR.gi(obj.id, 'af');
        $(obj).addClass('arlib-con-file-hover');
        $('#arlib_preview_img').attr(
        'src', 'file.asp?type=preview&id=' + id
        );
        $('#arlib_preview').show();
    },
    format: function(n) {
        var kb = 1024;
        var mb = 1024 * kb;
        var gb = 1024 * mb;
        if (n < kb) {return n + " B";}
        if (n < mb) {return Math.round(n * 10 / kb) / 10 + " KB";}
        if (n < gb) {return Math.round(n * 10 / mb) / 10 + " MB";}
        return Math.round(n * 10 / gb) / 10 + " GB";
    },
    q: function(f) {
        $('#arlib_q').append(
        '<div id="arlQ_' +
        f.id +
        '" class="arlib-q-item">' +
        '<div class="arlib-q-item-bar">' +
        '<div class="arlib-q-item-fill">' +
        '</div></div>' +
        '<div class="arlib-q-item-name">' +
        f.name +
        '</div>' +
        '<a href="." class="arlib-a-item-cancel">X</a>' +
        '<div class="arlib-q-item-status">' +
        $AR.lib.format(f.size) +
        '</div></div>'
        );
        $it = $('#arlQ_' + f.id);
        $it.data('size', f.size);
        if (f.size > $AR.lib.fquota) {
            $it.addClass('arlib-file-big');
        }
        $AR.lib.qsize += f.size;
        $AR.lib.state('ready');
    },
    progress: function(f, bl, bt) {
        var p = Math.floor(100 * bl / bt);
        $('#arlQ_' + f.id + ' .arlib-q-item-fill'
        ).width(p + '%');
        $('#arlQ_' + f.id + ' .arlib-q-item-status'
        ).html('' + $AR.lib.format(bl) + '/' + $AR.lib.format(bt));
    },
    crumb_click: function() {
        id = $AR.gi(this.id, 'arlib_crumb');
        $AR.lib.load_content(id, 1);
        return false;
    },
    tree_click: function(e) {
        $AR.lib.next1('tree_click', this, e);
        return false;
    },
    tree_dblclick: function(e) {
        $AR.lib.get_lock();
        $('.arlib-tree-folder').removeClass(
            'arlib-tree-folder-selected'
        );
        $(this).addClass('arlib-tree-folder-selected');
        $AR.lib.state('move2');
        $AR.lib.move();
        return false;
    },
    item_click: function(e) {
        $AR.lib.next1('item_click', this, e);
        return false;
    },
    item_blur: function() {
        $AR.lib.blur_obj = this;
        setTimeout('$AR.lib.item_blur2()', 150);
    },
    item_blur2: function() {
        var obj = $AR.lib.blur_obj,
            $item = $(obj).parents('.arlib-con-item'),
            old = $item.data('old'),
            ff = $AR.lib.get_ff($item[0]),
            id = $item[0].id.replace('aff', '').replace('af', '');
        if (obj.value == old) {
            $AR.lib.clear();
            $AR.lib.state('empty');
        } else {
            $.post(
            'ajax.asp',
            {
                'ajax': 'lib_action',
                'action': 'rename',
                'profile': $AR.lib.profile,
                'affCurrent': $AR.lib.aff,
                'ff': ff,
                'id': id,
                'name': obj.value
            },
            $AR.lib.pb_rename
            );
        }
    },
    item_key: function(e) {
        if ($AR.lib.lock_key) {
            $AR.lib.lock_key = false;
            return false;
        }
        switch (e.keyCode) {
        case 27:
            $AR.lib.undo();
            this.blur();
            break;
        case 13:
            if ($AR.lib.lock_ent) {
                $AR.lib.lock_ent = false;
                return false;
            }
            this.blur();
            break;
        }
        $AR.lib.lock_ent = false;
        return false;
    },
    folder_dblclick: function() {
        if ($AR.lib.mode == 'edit') {
            return false;
        }
        $AR.lib.get_lock();
        id = $AR.gi(this.id, 'aff');
        $AR.lib.load_content(id, 1);
        return false;
    },
    file_dblclick: function() {
        $AR.lib.get_lock();
        id = $AR.gi(this.id, 'af');
        $AR.lib.get_file(id);
        return false;
    },
    file_done: function(f) {
        $AR.lib.qsize -= f.size;
        $AR.lib.file_hide($('#arlQ_' + f.id));
        $AR.lib.q_status();
        $AR.lib.reload();
    },
    file_hide: function($i) {
        $i.fadeTo(1000, 0.1).slideUp(function() {
            $(this).remove();
        });
    },
    btn_click: function() {
        var $o = null;
        try {
            $o = $($AR.lib.lock_o);
        } catch(ex) {}
        switch ($AR.gi(this.id, 'arlib_btn_')) {
        case 'create':
            $AR.lib.load_content($AR.lib.aff, 0, 'new');
            break;
        case 'cancel':
            $AR.lib.undo();
            $AR.lib.clear();
            $AR.lib.state('empty');
            break;
        case 'choose':
            $AR.lib.get_file($AR.gi($o[0].id, 'af'));
            break;
        case 'open':
            $AR.lib.load_content($AR.gi($o[0].id, 'aff'), 1);
            break;
        case 'rename':
            if ($AR.lib.mode == 'selected') {
                $AR.lib.lock_e = null;
                $AR.lib.next2($AR.lib.get_lock(), 'item_click');
            }
            break;
        case 'del':
            $AR.lib.del();
            break;
        case 'move':
            $AR.lib.move();
            break;
        case 'download':
            $AR.lib.get_file($AR.gi($o[0].id, 'af'));
            break;
        }
        return false;
    },
    success: function(f, data) {
        if (data.indexOf('error: ')>-1) {
            $AR.lib.qerror += data.replace('error: ','-- ').replace('\n','') +
            '\n';
        }
    },
    select: function(){
        if ($AR.lib.mode=='edit') {
            return true;
        }
        return false;
    },
    q_start: function() {
        $AR.lib.mode = 'uploading';
        $AR.lib.qerror = '';
        var fq = $AR.lib.fquota,
            available = $('#arlib_available').val()-0;
        $bigs = $('.arlib-q-item').filter(function() {
            return $(this).data('size') > fq;
        });
        if ($bigs.length > 0 && fq > 0) {
            alert('Please remove any files from your queue that are larger than ' + $AR.lib.format(fq));
            return false;
        }
        if (swfu.getStats().files_queued > 0) {
            if (available < $AR.lib.qsize) {
                alert(
                    'Your upload queue exceeds the available space ' +
                    'in your library.\n Please remove some files ' +
                    'and try again.'
                );
            } else {
                swfu.startUpload();
            }
        }
        return false;
    },
    q_done: function(n) {
        if (swfu.getStats().files_queued < 1) {
            if ($AR.lib.qerror != '') {
                alert(
                    'At least one file failed to upload:\n' +
                    $AR.lib.qerror
                );
            };
            $AR.lib.qsize = 0;
            $('.arlib-con-screen').hide();
            $AR.lib.state('empty');
        }
    },
    q_status: function(t, m) {
        $s = $('#arlib_q_message');
        if (arguments.length == 0) {
            t = '';
        };
        if (t.length > 0) {
            $s.html(t);
        } else {
            try {
                n = swfu.getStats().files_queued;    
            } catch(ex){
                n = 0;
            }
            t = n + ' item';
            if (n != 1) {
                t += 's';
            }
            if (n > 0) {
                t += ', ' + $AR.lib.format($AR.lib.qsize) + ' Total';
            }
            $s.html(t);
        }
    },
    state: function(s) {
        var last = $AR.lib.last;
        var ff = $AR.lib.get_ff($AR.lib.lock_o);
        var gui = {
            open: false,
            choose: false,
            rename: false,
            move: false,
            download: false,
            del: false,
            create: false,
            cancel: false
        };
        if (s.slice(0,4)!='move') {
            $('#arlib_tree').hide().html('');
        };
        switch (s) {
        case 'empty':
            $('.arlib-q-add').removeClass('arlib-q-add-hide');
            $('.arlib-q-start').hide();
            $('.arlib-q-stop').hide();
            $('#arlib_btns').show();
            $('#arlib_crumb_on').show();
            $('#arlib_crumb_off').hide();
            $AR.lib.q_status();
            gui.create = true;
            break;
        case 'ready':
            $('.arlib-q-add').removeClass('arlib-q-add-hide');
            $('.arlib-q-start').show();
            $('.arlib-q-stop').show();
            $('#arlib_btns').hide();
            $('#arlib_crumb_on').hide();
            $('#arlib_crumb_off').show();
            $('#arlib_screen').hide();
            $('.arlib-con-screen').show();
            $AR.lib.clear();
            $AR.lib.q_status();
            break;
        case 'multi':
            $AR.lib.mode = 'selected';
            $('#arlib_preview').hide();
            gui.move = true;
            gui.del = true;
            gui.cancel = true;
            break;
        case 'paused':
            break;
        case 'selected':
            $AR.lib.mode = 'selected';
            $('.arlib-con-item').removeClass(
            'arlib-con-folder-hover'
            ).removeClass(
            'arlib-con-file-hover'
            );
            if (ff == 'aff' && last > -1) {
                gui.open = true;
            }
            if (ff == 'af' && last > -1) {
                gui.choose = true;
                gui.download = true;
            }
            gui.rename = true;
            gui.move = true;
            gui.del = true;
            gui.cancel = true;
            $('.arlib-q-add').addClass('arlib-q-add-hide');
            break;
        case 'edit':
            $AR.lib.mode = 'edit';
            var $o = $($AR.lib.lock_o);
            $AR.lib.undo_o = $o[0];
            $o.data('old', $o.find('input')[0].value);
            gui.rename = true;
            gui.cancel = true;
            break;
        case 'move':
            $AR.lib.mode = 'move';
            gui.cancel = true;
            break;
        case 'move2':
            $AR.lib.mode = 'move2';
            gui.cancel = true;
            gui.move = true;
            break;
        }
        for (var i in gui) {
            try {
                var $o2 = $('#arlib_btn_' + i);
                if (gui[i]) {
                    $o2.show();
                } else {
                    $o2.hide();
                }
            } catch(ex) {}
        }
    },
    pb_rename: function(data) {
        var d = data.replace(/[^0-9]*/g, '');
        if (d == '0') {
            $AR.lib.reload();
        } else {
            $AR.lib.lock_key = true;
            var t = '';
            switch (d) {
            case '1':
                t = 'An item with the name you specified ' +
                'already exists. Please choose a different name.';
                break;
            case '3':
                t = 'Invalid file extension. Please choose a ' +
                'different name.';
                break;
            default:
                t = 'An unspecified error occurred. Rename failed.';
            }
            alert(t);
            $AR.lib.lock_ent = true;
            var $in = $($AR.lib.lock_o).find('input')[0];
            $in.select();
            $in.focus();
            setTimeout('$AR.lib.lock_key = false;',300);
        }
    },
    pb_move: function(data){
        var d = data.replace(/[^0-9]*/g, '');
        if (d == '0') {
            $AR.lib.reload();
        } else {
            $AR.lib.lock_key = true;
            var t = '';
            switch (d) {
            case '1':
                t = 'One or more of the selected folders cannot be ' +
                'moved to the destination folder.';
                break;
            case '2':
                t = 'The destination folder already contains an item ' +
                ' with the same name as one of the items you are moving.';
                break;
            default:
                t = 'An unspecified error occurred. Move failed.';
            }
            alert(t);
        }
    },
    load_content: function(aff, crumb, action) {
        this.aff = aff;
        var act = (arguments.length > 2) ? '&action=' + action: '';
        if (swfu) {
            swfu.setPostParams({
                "aff": '' + aff,
                "uc_id": $AR.lib.uc_id,
                "uc_code": $AR.lib.uc_code
            });
        }
        $('#arlib_con').load(
            'ajax.asp?ajax=lib_content&profile='+
            $AR.lib.profile+'&affCurrent=' + aff + act,
            $AR.lib.init2
        );
        if (crumb == 1) {
            $('#arlib_crumb').load(
                'ajax.asp?ajax=lib_crumb&profile='+
                $AR.lib.profile+'&affCurrent=' + aff
            );
        };
        return false;
    }
};
$AR.dat = { /* data module */
    tShow: null,
    tFilter: null,
    tLookup: null,
    init: function(pm){
        if ($.cookie('data_'+pm)=='show') {
            $AR.dat.toggle2(pm,'show');
        } else if ($.cookie('data_'+pm)=='hide') {
            $AR.dat.toggle2(pm,'hide');
        }
        $AR.dat.loading(pm);
        $('.m-data-btn-download').live('click',function(){
            $AR.dat.download(pm);
        });
        $('#data'+pm).find('.m-data-results').load(
            'ajax.asp?ajax=data&pm='+pm, function(){
                $AR.dat.resize(pm);
            }
        );
    },
    gi: function(obj){
        return $AR.gi($(obj).parents('.m-data')[0].id,'data');
    },
    toggle: function(){
        var pm = $AR.dat.gi(this);
        if ($(this).hasClass('m-data-filter-a-hide')) {
            $AR.dat.toggle2(pm,'hide');
        } else {
            $AR.dat.toggle2(pm,'show');
        }
        return false;
    },
    toggle2: function(pm,sh){
        if (sh=='show') {
            $('#data'+pm).find('.m-data-filter-off').hide();
            $('#data'+pm).find('.m-data-filter-on').show();
            $.cookie('data_'+pm,'show');
        } else {
            $('#data'+pm).find('.m-data-filter-off').show();
            $('#data'+pm).find('.m-data-filter-on').hide();
            $.cookie('data_'+pm,'hide');
        }
    },
    fs: function(){
        var pm = $AR.dat.gi(this);
        if ($(this).hasClass('m-data-btn-apply')) {
            clearTimeout($AR.dat.tShow);
            $AR.dat.fss(pm);
        }
        else {
            clearTimeout($AR.dat.tShow);
            $AR.dat.tShow = setTimeout("$AR.dat.fss('"+pm+"')",1500);
        }
    },
    fss: function(pm){
        var $f = $('#data'+pm),
            ser = $f.find('form').serialize();
        $AR.dat.loading(pm);
        $.post('ajax.asp?ajax=data',ser,function(data){
            $f.find('.m-data-results').html(data);
            $AR.dat.resize(pm);
        });
    },
    page: function(pm,pg){
        var $f = $('#data'+pm);
        $AR.dat.loading(pm);
        $.post('ajax.asp?ajax=data',
            {'fdpg':pg, 'pm': pm},
            function(data){
                $f.find('.m-data-results').html(data);
                $AR.dat.resize(pm);
            }
        );
    },
    sort: function(pm,s){
        var $f = $('#data'+pm);
        $AR.dat.loading(pm);
        $.post('ajax.asp?ajax=data',
            {'sort':s, 'pm': pm},
            function(data){
            $f.find('.m-data-results').html(data);
            $AR.dat.resize(pm);
        });
    },
    sort_select: function(){
        var pm = $AR.dat.gi(this);
        $AR.dat.sort(pm,$(this).val());
    },
    loading: function(pm){
        var $div = $('#data'+pm).find('.m-data-results'),
            h = $div.height();
        $div.html(
            '<div class="m-data-loading">loading. . .</div>'
        ).height(h);
    },
    resize: function(pm){
        var $div = $('#data'+pm).find('.m-data-results');
        $div.css("height", "auto");
    },    
    download: function(pm){
        document.location.href='ajax.asp?ajax=data_download&pm='+pm;
    }
};
$AR.chart = { /* chart module */
    grid: {},
    init: function(pm){
        $(document).ready(function(){
            $AR.chart.fetch(pm);
        });
    },
    fetch: function(pm){
        // todo: filter/date parameters
        $.getJSON('ajax.asp?ajax=chart&pm='+pm, function(data) {
            $('#json_response').html(data);
            $AR.chart.grid[pm] = data.grid;
            $AR.chart.draw(pm);
        });
    },
    draw: function(pm){
        // todo: real columns
        // todo: different visualizations
        // todo: width, height, title
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Thing1');
        data.addColumn('number', 'Thing2');
        data.addRows($AR.chart.grid[pm]);
        var chart = new google.visualization.PieChart(
            document.getElementById('chart'+pm)
        );
        chart.draw(data, {width: 400, height: 240, is3D: true, title: 'Chart Yo'});
    }
};
$AR.ap = { /* member pages */
    ap_id: 0,
    as_id: -1,
    profile_id: 0,
    $page: null,
    $comments: null,
    $form: null,
    $edit: null,
    init: function(profile_id,ap_id,as_id,edit){
        this.profile_id = profile_id;
        if ($AR.nn(window.location.hash.replace('#',''))==0) {
            window.location.hash = as_id;
        }
        this.ap_id = ap_id;
        this.$page = $('.m-ap-page');
        this.$comments = $('.m-ap-comments');
        this.$form = $('.m-ap-comments-form');
        this.$edit = $('.ap-edit-ajax');
        $('.ap-a').live('click', $AR.ap.clicked);
        $('.ap-input').live('keyup', $AR.ap.keyup);
        this.$comments.load('ajax.asp?ajax=comments&ap='+ap_id);
        this.$form.load('ajax.asp?ajax=comment-form&ap='+ap_id);
        if (edit==1) {
            this.load_edit(ap_id,0,'');
        }
        this.hash_monitor();
    },
    keyup: function(e){
        if (e.keyCode==13) {
            if ($('.ap-edit-section-form').length>0) {
                $AR.ap.click_save_section();
            } else if ($('.ap-edit-page-form').length>0) {
                $AR.ap.click_save_page();                
            }
        }
    },
    hash_monitor: function() {
        var as_id = $AR.nn(window.location.hash.replace('#',''));
        if (as_id != $AR.nn($AR.ap.as_id)) {
            $AR.ap.as_id = as_id;
            $AR.ap.load_page();
        }
        setTimeout('$AR.ap.hash_monitor();',1000);
    },
    load_page: function(){
        $AR.ap.$page.load( //todo: phase out profile_id
            'ajax.asp?ajax=ap-page'+
                '&profile='+$AR.ap.profile_id+
                '&ap='+$AR.ap.ap_id+
                '&as='+$AR.ap.as_id,
                function(){
                    $('#pageScreen').height(
                        Math.max($(window).height(),$('body').height())
                    );    
                }
        );
    },
    load_edit: function(ap_id,as_id,action){
        $AR.ap.$edit.load( //todo: phase out profile_id
            'ajax.asp?ajax=ap-edit'+
                '&action='+action+
                '&profile='+$AR.ap.profile_id+
                '&ap='+ap_id+
                '&as='+as_id,
            $AR.ap.load_edit_ready
        );
    },
    load_edit_ready: function(){
        var $ps = $('#pageScreen');
        $AR.ap.update_image(-1);
        if ($ps.is(':visible')) {
            $('.ap-edit-outer').show();
            return;
        }
        $ps.slideDown(300,function(){
            $('.ap-edit-outer').fadeIn(300);
        });
    },
    refresh: function(){
        var search = window.location.search;
        window.location.href = window.location.pathname +
            '?a=1&ap='+$AR.ap.ap_id+'#'+$AR.ap.as_id;
        if (search.indexOf('edit')==-1) {
            window.location.reload();
        }
    },
    clicked: function(){
        if ($(this).hasClass('ap-edit-a')) {
            return $AR.ap.click_edit_page();
        } else if ($(this).hasClass('ap-edit-item-a')) {
            return $AR.ap.click_edit_item(this);            
        } else if ($(this).hasClass('ap-edit-exit-a')) {
            return $AR.ap.click_edit_exit();
        } else if ($(this).hasClass('ap-nav-a')) {
            return $AR.ap.click_nav(this);
        } else if ($(this).hasClass('ap-edit-nav-a')) {
            return $AR.ap.click_edit_nav(this);
        } else if ($(this).hasClass('ap-edit-info-a')) {
            return $AR.ap.click_edit_info();
        } else if ($(this).hasClass('ap-add-page-a')) {
            $AR.ap.ap_id = -1;
            $AR.ap.as_id = 0;
            return $AR.ap.click_edit_info();
        } else if ($(this).hasClass('ap-add-section-a')) {
            $AR.ap.as_id = -1;
            $AR.ap.load_edit($AR.ap.ap_id,$AR.ap.as_id,'asEdit');
            return false;
        } else if ($(this).hasClass('ap-edit-page-submit-a')) {
            return $AR.ap.click_save_page();
        } else if ($(this).hasClass('ap-edit-page-cancel-a')) {
            return $AR.ap.click_edit_page();
        } else if ($(this).hasClass('ap-edit-section-submit-a')) {
            return $AR.ap.click_save_section();
        } else if ($(this).hasClass('ap-edit-section-cancel-a')) {
            return $AR.ap.click_edit_page();
        } else if ($(this).hasClass('ap-delete-a')) {
            return $AR.ap.click_delete_page();
        } else if ($(this).hasClass('ap-thumbs-a')) {
            return $AR.ap.click_thumb(this);
        } else if ($(this).hasClass('ap-delete-section-a')) {
            return $AR.ap.click_delete_section(this);
        } else if ($(this).hasClass('ap-edit-section-a')) {
            return $AR.ap.click_edit_section(this);
        } else if ($(this).hasClass('ap-open-library-a')) {
            return $AR.ap.click_open_library();
        } else if ($(this).hasClass('ap-remove-image-a')) {
            $AR.ap.update_image(0);
            return false;
        }
    },
    click_nav: function(obj){
        $AR.ap.as_id = $(obj).attr('href').replace('#','');
        $AR.ap.load_page();
    },
    click_edit_page: function(){
        $AR.ap.load_edit($AR.ap.ap_id,0,'');
        return false;
    },
    click_edit_exit: function(){
        $AR.ap.refresh();
        return false;
    },
    click_edit_item: function(obj){
        var as_id = $AR.gi($(obj).parents('.ap-item')[0].id,'ap_item');
        $AR.ap.load_edit($AR.ap.ap_id,$AR.ap.as_id,'asEdit');
        return false;
    },
    click_edit_nav: function(obj){
        $AR.ap.ap_id = $(obj).attr('href').replace('#','');
        $AR.ap.as_id = 0;
        window.location.hash = '';
        $AR.ap.load_page();
        $AR.ap.load_edit($AR.ap.ap_id,0,'');
        return false;
    },
    click_edit_info: function(){
        $AR.ap.load_edit($AR.ap.ap_id,0,'apEdit');
        return false;
    },
    click_edit_section: function(obj){
        $AR.ap.as_id = $AR.gi($(obj).parents('.ap-edit-section')[0].id,
        'ap_edit_section');
        $AR.ap.load_edit($AR.ap.ap_id,$AR.ap.as_id,'asEdit');
        $AR.ap.load_page();
    },
    click_save_page: function(){
        var ser = $('.apForm').serialize();
        $.post('ajax.asp',ser,function(data){
            $AR.ap.ap_id = data;
            $AR.ap.load_edit($AR.ap.ap_id,0,'');
            $AR.ap.load_page();
        });
        return false;
    },
    click_save_section: function(){
        var ser = $('.apForm').serialize();
        $.post('ajax.asp',ser,function(data){
            window.location.hash = data;
            $AR.ap.as_id = data;
            $AR.ap.load_edit($AR.ap.ap_id,0,'');
            $AR.ap.load_page();
        });
        return false;
    },
    click_delete_page: function(){
        if (confirm('Are you sure? Deletion cannot be undone.')) {
            $.post(
                'ajax.asp',
                {'ajax': 'ap-edit', 'action': 'apDelete', 'ap': $AR.ap.ap_id},
                function(data){
                    $AR.ap.ap_id = data-0;
                    $AR.ap.load_edit($AR.ap.ap_id,0,'');
                    $AR.ap.load_page();
            });
        }
        return false;
    },
    click_delete_section: function(obj){
        var as_id = $AR.gi($(obj).parents('.ap-edit-section')[0].id,
        'ap_edit_section');
        if (confirm('Are you sure? Deletion cannot be undone.')) {
            $.post(
                'ajax.asp',
                {
                    'ajax': 'ap-edit',
                    'action': 'asDelete',
                    'ap': $AR.ap.ap_id,
                    'as': as_id
                },
                function(data){
                    $AR.ap.as_id = data-0;
                    $AR.ap.load_edit($AR.ap.ap_id,0,'');
                    $AR.ap.load_page();
            });        
        }
        return false;
    },
    click_open_library: function(){
        $AR.go2(
            'library.asp?profile='+$AR.ap.profile_id+
            '&libraryMode=2&af='+$('#apEditImage').val(),
            780,
            560
        );
        return false;
    },
    click_thumb: function(obj){
        var as = obj.id.replace('_next','').replace('_previous','');
        $AR.as_id = $AR.gi(as,'ap_thumbs');
        $AR.ap.load_page();
    },
    update_image: function(id){
        $('.ap-edit-image').load(
            'ajax.asp?ajax=ap-filename&profile='+$AR.ap.profile_id+
            '&ap='+$AR.ap.ap_id+'&as='+$AR.ap.as_id+'&af='+id
        );
    }
};
$AR.comments = { /* comments */
    clicked: function(){
        var $f = $(this).parents('form'),
            action='';
        if ($(this).hasClass('preview')) {
            action='preview';
        } else if ($(this).hasClass('post')) {
            action='post';
        } else if ($(this).hasClass('edit')) {
            action='edit';
        } else if ($(this).hasClass('cancel')) {
            action='cancel';
        }
        $f.find('[name=action]').val(action);
        $.post(
            'ajax.asp',
            $f.serialize(),
            function(data){
                $AR.ap.$form.html(data);
                if ($AR.ap.$form.find('.comments-reload').length>0) {
                    $AR.ap.$comments.load(
                        'ajax.asp?ajax=comments&ap='+$AR.ap.ap_id);
                }
            }
        );
    }
};

