
var Ctrl_Dialog = Class.extend({
    
    init: function(options) {
        this.options = jQuery.extend({
            'autoOpen' : false,
            'contents' : null
        }, options);
    
        this.options.dialogClass = 'ctrl-dialog ' + this.options.dialogClass;
        
        var id = 'ctrl-dialog' + new Date().getTime();
        this.elem = $('<div id="'+id+'"></div>');
        $('body').append(this.elem);
        
        if(null !== this.options.contents) {
            if(!(this.options.contents instanceof jQuery))
                    this.options.contents = $(this.options.contents);
            
            this.elem.append(this.options.contents.show());
        }
        
        this.elem.dialog(this.options);
        this.window = this.elem.closest('.ui-dialog');
        
    },
    
    open: function(){ this.elem.dialog('open'); },
    close: function(){ this.elem.dialog('close'); },
    
    destroy: function() {
        this.elem.dialog('destroy').remove();
    }
    
});

var Ctrl_Dialog_Lightbox = Ctrl_Dialog.extend({
    open: function(url){
        var that = this;
        
        var i = new Image();
        i.src = url;
        this.elem.empty().append(i);
        
        
        i.onload = function(){
            that.elem.dialog('option', 'width', this.width).dialog('option', 'height', this.height);
            that.elem.dialog('open');
        };
        
       
        
        //this.elem.css();
    }
});

var Ctrl_Dialog_Ajax = Ctrl_Dialog.extend({
    init: function(options, src) {
        this.src = src;
        this._isLoaded = false;
        options = jQuery.extend({
            'autoLoad': true
        }, options);
        options.dialogClass = options.dialogClass? 'ctrl-dialog-ajax ' + options.dialogClass : 'ctrl-dialog-ajax';
        this._super(options);
      
        if(this.options.autoLoad && this.src)
            this.load();
    },
    
    load: function(callback, force) {
        var that = this;
        
        if(!this._isLoaded || force) {
            this.elem.empty().load(this.src, function(){ 
                if(typeof callback == 'function') {
                    that.isLoaded = true;
                    callback.call(that);
                }
            });        
        }
    },
    
    setSrc: function(src, callback) {
        this.src = src;
        this.load(callback, true);
        
        return this;
    }
    
});

var Ctrl_Dialog_Iframe = Ctrl_Dialog.extend({
    
    init: function(options, src) {
        options = options || {};
        options.dialogClass = options.dialogClass? 'ctrl-dialog-iframe ' + options.dialogClass : 'ctrl-dialog-iframe';
        this._super(options);
        
        if(src != undefined)
            this.setSrc(src);
        

    },
    
    getIframe: function() {
        if(this.iframe == undefined) {
            this.iframe = this.elem.find('iframe');
            if(this.iframe.length == 0) {
                this.iframe = $('<iframe frameborder="0"></iframe>');
                this.elem.append(this.iframe);
            }
        }
        
        return this.iframe;
    },
    
    setSrc: function(src, forceRefresh) {

        if(src != this.getIframe().attr('src') || forceRefresh === true)
            this.getIframe().attr('src', src);
        
        return this;
    },
    
    refresh: function() { var ifm = this.getIframe(); ifm.attr('src', ifm.attr('src')); return this; }
    
});

