$(document).ready(function(){ 
	
	$("#tabContainer").tabs( {fx: { opacity: 'toggle', duration: 'fast' } });	
	
	$('#news ul').jcarousel({
        auto: 15, 
        wrap: 'last'
    });
    	
});


// Drag and drop
var widgets = {
    
    jQuery : $,
    
    settings : {
        columns : '.column',
        widgetSelector: '.widget',
        
        /* If you don't want preferences to be saved change this value to
            false, otherwise change it to the name of the cookie: */
        saveToCookie: 'ascham-widget-preferences'
    },

    init : function () {		
        this.sortWidgets();
        this.makeSortable();
    },
    
    getWidgetSettings : function (id) {
        var $ = this.jQuery,
            settings = this.settings;
        return settings.widgetDefault;
    },
    
    
    makeSortable : function () {
        var widgets = this,
            $ = this.jQuery,
            settings = this.settings;
        
        $(settings.widgetSelector).css({
            cursor: 'move'
        }).mousedown(function (e) {
            $(settings.widgetSelector).css({width:''});
            $(this).parent().css({
                width: $(this).parent().width() + 'px'
            });
        }).mouseup(function () {
            if(!$(this).parent().hasClass('dragging')) {
                $(this).parent().css({width:''});
            } else {
                $(settings.columns).sortable('disable');
            }
        });

        $(settings.columns).sortable({
            items: settings.widgetSelector,
            connectWith: $(settings.columns),
            placeholder: 'widget-placeholder',
            forcePlaceholderSize: true,
            revert: 300,
            delay: 100,
            opacity: 0.8,
            containment: 'document',
            start: function (e,ui) {
                $(ui.helper).addClass('dragging');
            },
            stop: function (e,ui) {
                $(ui.item).css({width:''}).removeClass('dragging');
                $(settings.columns).sortable('enable');
                /* Save prefs to cookie: */
                widgets.savePreferences();
            }
        });
    },
    
    savePreferences : function () {
        var widgets = this,
            $ = this.jQuery,
            settings = this.settings,
            cookieString = '';
            
        if(!settings.saveToCookie) {return;}
        
        /* Assemble the cookie string */
        $(settings.columns).each(function(i){
            cookieString += (i===0) ? '' : '|';
            $(settings.widgetSelector,this).each(function(i){
                cookieString += (i===0) ? '' : ';';
                /* ID of widget: */
                cookieString += $(this).attr('id') + ',';             
            });
        });
        $.cookie(settings.saveToCookie,cookieString,{
            expires: 10
            //path: '/'
        });
    },
    
    sortWidgets : function () {
        var widgets = this,
            $ = this.jQuery,
            settings = this.settings;  
    
        /* Read cookie: */
        var cookie = $.cookie(settings.saveToCookie);
        if(!settings.saveToCookie||!cookie) {
            /* Get rid of loading gif and show columns: */
            $('#columns').css({background:'none'});
        	$(settings.columns).css({visibility:'visible'});
            return;
        }
        
        /* For each column */
        $(settings.columns).each(function(i){ 
            var thisColumn = $(this),
                widgetData = cookie.split('|')[i].split(';');
    
            $(widgetData).each(function(el){
										
                if(!this.length) { return; }
                	
                var thisWidgetData = this.split(','),
                    clonedWidget = $('#' + thisWidgetData[0]);      
                               
                $('#' + thisWidgetData[0]).remove();
                $(thisColumn).append(clonedWidget);
            });	
        });
					
        /* All done, remove loading gif and show columns: */
        $('#columns').css({background:'none'});
        $(settings.columns).css({visibility:'visible'});
    }  
};

widgets.init();
