//  @import com.hedgehoglab.application.view.BaseView;

function LabBox(scope) {

    if (!scope) scope = $.extend(scope, {height:200});
    BaseView.call(this, scope);
    var _this = this;
    var _modal = $('#modal_div');
    _modal.hide();

    var FADE_SPEED=200;

    this.on_cancel = function() {
        _this.hide();
    };
    
    this.on_confirm = function() {
        _this.hide();
    };
    

    if (!scope.buttons) scope.buttons = ['confirm','cancel'];

    var _element = $("<div id='facebox'><div class='popup Pane'>Error</div></div>");



    //  Fade out modal
    _modal.fadeOut(0);




	
    _this.on_template_ready = function() {

        if (scope.width) $(".body", _element).width(scope.width);
        if (scope.height) $(".body", _element).height(scope.height);
        _element.hide();
        $('body').append(_element);

        $(window).bind('resize', function() {
            setTimeout(function() {
                _element.x(($('body').width() - _element.width()) / 2)
                _element.y(($('body').height() - _element.height()) / 2);
            }, 10);

        });

        // Wildcard selector. Adds cancel function to all buttons
        $('a[class^=lb-]', _element).bind('click', function(e) {
            var tempID = $(this).attr('class').substring(3);
            _this.raise_event('lb_' + tempID + '_clicked', {});
            _this.on_cancel({hide:_this.hide});
            _element.fadeOut(FADE_SPEED, function(e) {});
            _modal.fadeOut(FADE_SPEED,function(){$(this).hide();});
            return false;
        });

        // Custom onclick for confirm button.
        $('a.lb-confirm', _element).unbind('click');
        $('a.lb-confirm', _element).bind('click', function(e) {
            _this.on_confirm({hide:_this.hide});
            _element.fadeOut(FADE_SPEED, function(e) {});
            _modal.fadeOut(FADE_SPEED,function(){$(this).hide();});
            return false;
        });
    };

    

    this.show = function(element, callback) {
        _modal.fadeIn(FADE_SPEED);
        $('.content', _element).html(element);
        _element.x(($('body').width() - _element.width()) / 2)
        _element.y(($('body').height() - _element.height()) / 2);
        _element.fadeIn(FADE_SPEED);

        callback($('.content', _element),$('.footer', _element));
    };

    this.hide = function(callback) {
        _element.fadeOut(FADE_SPEED, function(e) {
			if(callback) callback();
        });
        _modal.fadeOut(FADE_SPEED).hide();
        //_model.hide();
    };

    this.enable_button = function(button, enabled) {
		var but=String(button).replace(' ','-');
        if (enabled) {
            $("a.lb-" + but, _element).show();
        } else {
            $("a.lb-" + but, _element).hide();
        }
    };

    this.override_button_text = function(button, text) {
		var but=String(button).replace(' ','-');
        $('a.lb-' + but).html(text);
    };

	this.show_loading=function(message){
		$('.footer ul', _element).append('<li class="ajax-loading">'+message+'</li>');
	};
	
	this.hide_loading=function(){
		$('.footer ul li.ajax-loading', _element).remove();
	}

	_this.render_template_with_view('general/LabBox', {model:scope.buttons},{}, function(rendered_template) {
        _element = $(rendered_template);
        _this.on_template_ready();
    });
};