/**************************************************************

	Script		: Widget
	Version		: 1.0
	Authors		: Pieter Soudan
	Desc		: a Widget Class for displaying modal iframes.
	Licence		: property of Red & Ivory

**************************************************************/

var Widget = new Class({
    Extends: ModalWindow,

    options: {
        endSize: {}
    },
    
    initialize: function(options){
        
        this.parent(options); 
        //set active state
        this.states['active'] = {
            'width': this.options.endSize.w,
            'height': this.options.endSize.h,
            'detach': true,
            'center': true,
            'overlay': true,
            'anim': true,
            'focus': false
        };
        this.options.endSize.w, this.options.endSize.h, true, true,true
        //set inactive state
        
        //this.pin(this.options.startSize.w, this.options.startSize.h);
        this.frame.addEvent('click', this.startWidget.bindWithEvent(this));
        this.frame.setStyle('cursor', 'pointer');
        this.addEvent('onStateChanged', this.widgetChanged.bind(this));
        this.fr = false;
	this.framed = false;
    },
    
    startWidget: function(event){
	
        this.marginY = window.getScrollTop();
        this.changeState('active', this.states[this.activeState].anim);
        this.frame.removeEvents('click');
        this.frame.setStyle('cursor', 'default');
    },
    
    widgetChanged: function(event){
        if(this.activeState == 'active') this.widgetShown();
        else this.widgetHidden();
    },
    
    widgetShown: function(event){
        this.overlay.addEvent('click', this.hideWidget.bindWithEvent(this));
        if(this.frame) {
	    if(this.ff) {
                
                this.ff.setStyle('visibility', 'hidden');
                this.ff.setStyle('display', 'none');
            }
	    if(this.framed) {
                this.fr.setStyle('visibility', 'visible');
                this.fr.setStyle('display', 'block');
            }
	    else {
		this.fr.injectInside(this.content);
		this.framed = true;
	    }
	}
	else {
            this.frame.addEvent('click', this.hideWidget.bindWithEvent(this));
            this.frame.setStyle('cursor', 'pointer');
        }
    },
    
    hideWidget: function(event){
	if(this.frame) {
	    if(this.ff) {
                this.ff.setStyle('visibility', 'visible');
                this.ff.setStyle('display', 'block');
            }
            this.resetSrc();
	    this.fr.setStyle('visibility', 'hidden');
            this.fr.setStyle('display', 'none');
	}
        this.changeState('start', this.states[this.activeState].anim);
        //this.setAll(this.options.startSize.w, this.options.startSize.h, this.options.startPos.x, this.options.startPos.y, true, true);
        this.overlay.removeEvents('click');
        this.frame.removeEvents('click');
    },
    
    widgetHidden: function(event){
        this.frame.addEvent('click', this.startWidget.bindWithEvent(this));
    },
    setFrame: function(src, w, h){
	this.fr = new Element('iframe',{'src': src, 'width':w, 'height': h,  'frameborder': "0", 'scrolling': 'no'});
        this.initialSrc=src;
    },
    
    resetSrc: function(){
        this.fr.src = this.initialSrc;
    }
    
    
});

