$(function(){
    $('ul.kui-list .ui-state-default a, .kui-button a').live(
        'mouseover mouseout', function(event){
            if (event.type == 'mouseover') $(this).parent().addClass("ui-state-hover");
            else $(this).parent().removeClass("ui-state-hover");
        }
    );
    $('input.ui-state-default, a.ui-state-default').live(
        'mouseover mouseout', function(event){
            if (event.type == 'mouseover') $(this).addClass("ui-state-hover");
            else $(this).removeClass("ui-state-hover");
        }
    ).css( 'cursor', 'pointer' );
    $(".cancelBubble").bind("click", function(e) {
        e.stopPropagation();
        // = (MSIE) window.event.cancelBubble = true;
    });
    $("a.dialog, a.popup").live('click', function(){
        var link = $(this).attr('href'), title = $(this).attr('title'), iframe, dialog, close_dialog;
        if( link.indexOf('://') < 0)
            if( link.indexOf('?') >= 0 ) link += '&tmpl=component';
            else link += '?tmpl=component';
        if( !title ) title = 'Visualizar';
        iframe = $('<iframe width="100%" height="100%" scrolling="auto" marginWidth="0" marginHeight="0" frameBorder="0" src="' + link + '" />').load(function(){ 
            $('a', iframe[0].contentDocument).each(function(){ 
                if( !$(this).attr('target')) $(this).attr('target', '_top');
            })
        });
        dialog = $('<div title="'+title+'"></div>');
        close_dialog = function(e) {
            var clicked=$(e.target);
            if(clicked.is('.ui-dialog') || clicked.parents().is('.ui-dialog')){}
            else{$(dialog).dialog("close");}
        };
        $(document).bind('mousedown', close_dialog);
        dialog
            .append(iframe)
            .dialog({
                height: 500,
                width: kite.getSize() + 30,
                close: function(){ $(document).unbind('mousedown', close_dialog); },
                resizable: false,
                modal: true,
                buttons: {
                    "Fechar": function() { $(this).dialog("close"); }
                }
            });
        return false;
    });
});

var kite = {
    getSize: function(){ return content_width; },
    js_message: null,
    cleanMessage: function(force){
        if ( this.js_message )
            clearTimeout( this.js_message );

        if ( force )
            $('#kite_message').hide();
        else
            $('#kite_message').fadeOut();
    },
    message: function( text, type ){
        this.cleanMessage( true );

        var icon = '';
        var title = '';
        var classname = 'message'
        if ( type == 'warning' ) {
            classname = 'notice';
            title = 'Aviso';
        } else if ( type == 'error' ) {
            classname = 'error';
            title = 'Erro';
        } else if ( type == 'wait' ) {
            classname = 'notice';
            title = 'Aguarde';
        } else if ( type == 'info' ) {
            title = 'Info';
        } else if ( type == 'success' ) {
        }

        if ( title ) 
            title = title + ': ';

        $('html').prepend('<dl id="kite_message">'
                +'<dt class="message">'+title+'</dt>'
                +'<dd class="message '+classname+'">'
                +'<ul><li>'+title + text+'</li></ul>'
                +'</dd></dl>');

        $('#kite_message').fadeIn();
        if( type != 'wait' )
            this.js_message = setTimeout( function(){ kite.cleanMessage() }, 5000 );

        return this;
    },
    service: function(service, data, callback){
        return $.get('/index.php?kite_service='+service, data, callback);
    },
    module: function(id, func, data, callback){
        return $.post('/index.php?kite_service=module&module_id='+id+'&module_func='+func, data, callback);
    },
    register: function(id, data){
        var el = $('#' + id);
        for( x in data )
            el.data(x, data[x]);
        return this;
    }
}

jQuery.fn.outerHTML = function(s) {
    return (s)
        ? this.before(s).remove()
        : jQuery("<p></p>").append(this.eq(0).clone()).html();
}

jQuery.fn.moduleGet = function(func, data, callback){
    id = this.data('module_id');
    if( !id ){ alert( 'Módulo não registrado!' ); return this; }
    kite.module(id, func, data, callback);
    return this;
}
jQuery.fn.moduleSave = function(data, callback){
    return this.moduleGet('save', data, callback);
}
jQuery.fn.moduleSet = function(func, data, callback){
    return this.moduleGet('set' + func, data, callback);
}

var $wait = function(fn){ 
    $(function(){ setTimeout(fn, 1) });
};

