/*
*
* admin.js
* http://carreirothestudio.com
* francis@francisdaigle.com
*
*/

$(document).ready(function() {

    // Uniform
    $("select, input").uniform();
    
    $('#uniform-servicesClear, #uniform-whatsNewClear').hover(
        function(){ $('span', this).addClass('clear'); },
        function(){ $('span', this).removeClass('clear'); }    
    );
    
    $('#uniform-whatsNewReset').hover(
        function(){ $('span', this).addClass('reset'); },
        function(){ $('span', this).removeClass('reset'); }    
    );
    
    $('#uniform-loginSubmit, #uniform-servicesSubmit, #uniform-whatsNewSubmit, #uniform-usersSubmit').hover(
        function(){ $('span', this).addClass('submit'); },
        function(){ $('span', this).removeClass('submit'); }    
    );
    
    $('.uploader').hover(
        function(){ $('.uploader .action').addClass('uploader'); },
        function(){ $('.uploader .action').removeClass('uploader'); }    
    );
    
    /********************/
	
	// IE 'haslayout()' fix
	if ( jQuery.support.inlineBlockNeedsLayout ) {
		$(".admin .whats-new #content #row-bottom .table-bottom #pagination .control").css("display", "inline");
		$(".admin .whats-new #content #row-bottom .table-bottom #pagination .control").css("zoom", "1");
	}
    
    // CKEditor config
    var config_a = 
    {
        height: 200,
        width: 420,
        linkShowAdvancedTab: false,
        scayt_autoStartup: false,
        enterMode : CKEDITOR.ENTER_P,
        toolbar:
        [ 
            { name: 'general', items : ['Bold', 'Italic', 'Underline', '-','Link', 'Unlink', '-', 'Cut','Copy','Paste','PasteText','PasteFromWord', 'SelectAll', '-','Undo','Redo' ] },
        ]
    };
    
    var config_b = jQuery.extend({}, config_a);
    config_b.width = 440;   
    
    $('.services #elements-content textarea').ckeditor(config_a);
    $('.whats-new #elements-content textarea').ckeditor(config_b);
    
    // Set table row colouring
    setTableRowClass('#services-table');
    setTableRowClass('#whats-new-table');
    
    // Set tableDnD
    servicesTableDnD();
    
    // Jquery date picker
    $('.whats-new #whatsNewDate').datepicker({ dateFormat: 'MM d, yy' }); 
    $('.whats-new #whatsNewDate').attr("readonly", true);
    
    $('#ui-datepicker-div').wrap('<div class="custom"></div>');
    
    // Colorbox
    if ($('.cbox').length) {
            $('.cbox').colorbox();
    }
    
    // Clear file upload
    $('#uniform-whatsNewReset').unbind('click').click(function(event){
        event.preventDefault();
        $('.uploader').remove();
        $('<input type="file" name="whatsNewImage" id="whatsNewImage">').insertAfter("#MAX_FILE_SIZE");
        $("#whatsNewImage").uniform();
        
       if ($('#whatsNewReset').attr("value") == 'REMOVE') {
            var whatsNewId = $('#whatsNewId').attr('value');
            var selected = $('#pagination ul').attr('id');
            var page = selected.split("-");
            $('.admin .whats-new #whats-new-table tbody').load('/admin/whatsNew/', { id: whatsNewId, page: page[1] },
                function() {
                    setTableRowClass('#whats-new-table');    
                }
            );
        }
    });
    
});

/********************/

// Table row colouring
function setTableRowClass(tableName) {
    var odd = '".admin ' + tableName + ' tbody tr:nth-child(odd)"';
    var even = '".admin ' + tableName + ' tbody tr:nth-child(even)"';
    
    $(odd).addClass("odd");
    $(even).addClass("even");
}

// TableDnD
function servicesTableDnD() {
    $('.services #services-table').tableDnD({
        onDragClass: "onDrag",
        onDrop: function(table, row) {
            var order = $.tableDnD.serialize(); 
            $('.services #services-table tbody').load('/admin/services', { order: order }, 
                function() { 
                    setTableRowClass('#services-table');
                    servicesTableDnD(); 
                }
            );
        }
    });
}

