

$.ifixpng.pixel = '/img/pixel.gif'

// Swedish calendar
$.fullCalendar.monthNames = ['Januari','Februari','Mars','April','Maj','Juni','Juli','Augusti','September','Oktober','November','December']
$.fullCalendar.monthAbbrevs = ['Jan','Feb','Mar','Apr','Maj','Jun','Jul','Aug','Sep','Okt','Nov','Dec']
$.fullCalendar.dayNames =  ['Söndag','Måndag','Tisdag','Onsdag','Torsdag','Fredag','Lördag']
$.fullCalendar.dayAbbrevs = ['Sön','Mån','Tis','Ons','Tor','Fre','Lör']


$(function() {




    $('.calendar').fullCalendar({
	    weekStart: 1,
	    buttons: {today: 'Idag'},
        eventClick: function(calEvent, jsEvent) {
            window.location.href = calEvent.uri
        },
        eventSources: [
            '/event/calendar_events'
        ],
        eventRender: function(calEvent, element) {
            element.tooltip({
                track: true,
                bodyHandler: function() {
                    return $(element).find('.event-title').html()
                }                    
            })
        }
	});
    
    
        
    $("table.sortable").each(function() {
        var sortList = [];
        if(this.id && $.cookie('tablesorter_' + this.id)) {
            var sortList = eval($.cookie('tablesorter_' + this.id))
        }
        
        $(this).tablesorter({
            sortList: sortList,
            widgets: ['zebra'],
            widthFixed: true,
            textExtraction: function(td) {
                var text = $(td).attr('trav:value') || $(td).find('h2').html() || $(td).html() || ''
                return text
            }
        }).bind('sortEnd', function(e) {
            if(e.target.id) {
                var sortList = '[['+e.target.config.sortList[0]+']]';  // [col_num, sort_order 0|1]
                $.cookie('tablesorter_' + e.target.id, sortList)
            }
        })
    })        
    

    $('form.remotevalidate').remoteValidate()
    // $('.uploader').uploader()    
    // $('textarea.richtext').ckeditor()
    
    // $('input[type="password"]').each(function(){
    //     this.focus(); 
    // })
    
    
    // ===========
    // = ifixpng =
    // ===========
    $('#logo, #footer, .pngfix').ifixpng()
    
    // ==========
    // = Popbox =
    // ==========
    $('.popbox').popbox()
})


function set_user(user_id) {
    // $.cookie('user_id', user_id, {expires: 365})
    window.location = '/authenticate_user?user_id=' + user_id
}

function edit(id, className, path) {
    var o = {
        'id': id,
        'className': className,
        'name': '',
        'paths': [path]
    }
    var rec = new Acm.Node.RecordFactory(o)
    
    var a = new Acm.NodeEditPanel({
        nodeRecord: rec,
        renderTo: document.getElementById('edit'),
        layout: 'border',
        // autoHeight: true,
        height: 400
    })
    a.createAndPopulate('sv_SE', 'draft')
    return false
}

function create(createPath, className) {
    var tabs = new Acm.NodeEditTabs({
        // layout: 'fit',
        // listeners: {
        //     'change': this.onFormChange,
        //     scope: this
        // },
        buttons: [{
            text: 'Spara', 
            handler: function() {
                tabs.form.submit({
                    url: '/acm/node/create?lang=sv_SE&parent='+createPath+'&name=test&className='+className,
                    success: function(form, action) {
                        alert('Saved')
                    },
                    failure: function(form, action) {
                        alert('Failed')
                    }
                })
            }
        }],
        renderTo: document.getElementById('create'),
        height: 400
    })
    
    // Populate
    var rec = new Ext.data.Record({
        path: createPath, 
        className: className
    })
    tabs.create(rec)
    return false
}

function del(id) { // "delete" is a reserved word
    if(confirm('Delete?')) {
        $.post('/acm/node/delete/' + id, {}, function(data, status, xmlhttp) {
            debugger
            window.location.href = data['location']
        }, 'json')
    }
}



