/**
 * Digitalus Framework
 * 
 * LICENSE
 * 
 * This source file is subject to the new BSD license that is bundled with this
 * package in the file LICENSE.txt. If you did not receive a copy of the license
 * and are unable to obtain it through the world-wide-web, please send an email
 * to info@digitalusmedia.net so we can send you a copy immediately.
 * 
 * @category Digitalus Framework
 * @package Dig_Jquery
 * @copyright Copyright (c) 2009, Digitalus Media USA (digitalusmedia.net)
 * @license New BSD License
 * @author: Forrest Lyman
 * 
 */

jQuery.dig = {
    ui: {
        init: function()
        {
            $('.striped p:odd').addClass('ui-state-default ui-corner-all');
            // zend framework displays a dt element (held open with &nbsp;) in the form whether a label is specified or not. there must be some reason, so i am
            // just going to hide it with jQuery
            $('dt').each(function(){
                if($(this).html() == '&nbsp;') {
                    $(this).hide();
                }
            });
            $('.delete').click( function(){
                var message = 'Are you sure you want to permanently remove this ' + $(this).attr('title') + '?';
                if(! confirm(message)) {
                    return false;
                }
            });

            $('.ajaxLink').live('click', function(){
                jQuery.dig.ui.disable();
                $.post(
                    $(this).attr('href'),
                    null,
                    function(response) {
                        jQuery.dig.ui.enable();
                        alert(response.message);
                    },
                    'json'
                    );
            });
            $('.toggle_content a.handle').live('click', function(){
                $(this).closest('.toggle_content').children().toggle();
                return false;
            });
            $('.toggle_content').each(function(){
                $(this).append('<a href="#" class="handle">Less</a>');
                $(this).children().hide();

                var errorCount = $(this).find('ul.errors').size();
                var errorMessage = '';
                if(errorCount > 0) {
                    errorMessage = '<span class="errors">Contains Errors (' + errorCount + ')</span>';
                }

                $(this).prepend('<a href="#" class="handle">More</a>' + errorMessage);
            }); 
            $.dig.ui.message.init();
        /*
            $('.expandable').each(function(){
                var id = $(this).attr('id');
                $(this).after('<a href="#" class="expand" rel="' + $(this).attr('id') + '>Expand<a>' );
            });
            $('.expand').live('click', function(){
                var expandable = $('#' + $(this).attr('rel'));
                expandable.addClass('expanded');
                return false;
            });
            */
        },
        disable: function() {
            this.get().fadeTo('fast', 0.3);
        },
        enable: function() {
            this.get().fadeTo('fast', 1);
        },
        get: function()
        {
            return $('#page_wrapper_inner');
        },
        message: {
            init: function() {
                if($('ul.errors').size() > 0) {
                    var message = 'Your form has errors and could not be processed. Please review the form for more details.';
                    $.dig.ui.message.add(message, 'critical');
                }

                $('.errors').addClass('ui-state-error');
                $('.messages').live('click',function(){
                    $.dig.ui.message.hide();
                });
            },
            add: function(message, type) {
                $.dig.ui.message.clear();
                var error = '<li><span class="' + type + '"></span>' + message + '</li>';
                var messageContainer = $.dig.ui.message.get();
                if(messageContainer.size() > 0) {
                    messageContainer.append(error).show();
                } else {
                    $('#page').prepend('<ul class="messages ui-widget-content ui-state-highlight ui-corner-all">' + error + '</ul>');
                }
            },
            clear: function () {
                $.dig.ui.message.get().empty();
            },
            hide: function() {
                $.dig.ui.message.get().fadeOut();
            },
            get: function() {
                return $('ul.messages');
            }
        }
    },
    editor: {
        init: function()
        {
            if(! $.dig.editor.exists()) {
                return false;
            }
            this.clean();
            $('.ckeditor').each(function(){
                var name = $(this).attr('name');
                CKEDITOR.replace( name );
            })
        },
        load: function(itemId)
        {
            $.dig.ui.disable();
            $('#main_form').load('/digCms/item/edit', {
                id: itemId
            }, function(){
                $.dig.ui.enable();
                $.dig.ui.init();
                $.dig.editor.init();
                $.initForm();
            });
        },
        clean: function()
        {
            if(! $.dig.editor.exists()) {
                return false;
            }
            for(var name in CKEDITOR.instances){
                delete CKEDITOR.instances[name];
            };
        },
        exists: function() {
            if(typeof CKEDITOR != "undefined") {
                return true;
            }
            return false;
        }
    },
    admin: {
        init: function() {
            var pageHeight = $(window).height();
            var headerHeight = $("#header").height();
            var sidebarHeight = pageHeight - headerHeight - 50;
            $('#admin #sidebar').height(sidebarHeight);
        }
    }
} 

$.dig.ui.init();
$.dig.editor.init();

