/*
 Prototype & Scriptaculous Extensions.
 Written by Eyesore, Inc or taken from various sources on the internet (marked).
*/

/*
 Extends the in-place editor with support for empty text. Modified from scriptaculous wiki
*/
Ajax.InPlaceEditorWithEmptyText = Class.create(Ajax.InPlaceEditor, {

  initialize : function($super, element, url, options) {
    if( options == null ) options = {};
    if (!options.emptyText)        options.emptyText      = "click to edit...";
    if (!options.emptyClassName)   options.emptyClassName = "inplaceeditor-empty";
    $super(element, url, options);
    this.checkEmpty();
  },

  checkEmpty : function() {
    if (this.element.innerHTML.length == 0 && this.options.emptyText) {
      this.element.appendChild(
          new Element("span", { className : this.options.emptyClassName }).update(this.options.emptyText)
        );
    }
  },

  getText : function($super) {
    if (empty_span = this.element.select("." + this.options.emptyClassName).first()) {
      empty_span.remove();
    }
    return $super();
  },

  leaveEditMode : function($super, transport) {
    this.checkEmpty();
    return $super(transport);
  }
});


/*
 * Pretty linked bubbles
 */
var Bubble = Class.create({
    trigger: null,
    bubble: null,
    appearDuration: 0.5,
    hoverDuration: 0.5,
    fadeDuration: 1.2,
    _timer: null,
    _fading: false,
    _mouse_out_trigger: true,
    _mouse_out_bubble: true,
    
    initialize: function( trigger, bubble ){
        this.trigger = trigger;
        this.bubble = bubble;
        
        this.bubble.hide();
        this.hook();
    },
    
    hook: function(){
        this.trigger.on('mouseover',function(){
            this._mouse_out_trigger = false;
            this.appear();
        }.bind(this));
        
        this.trigger.on('mouseout',function(){
            this._mouse_out_trigger = true;
            this.fade();
        }.bind(this));
        
        this.bubble.on('mouseover',function(){
            this._mouse_out_bubble = false;
            this.appear();
        }.bind(this));
        
        this.bubble.on('mouseout',function(){
            this._mouse_out_bubble = true;
            this.fade();
        }.bind(this));
    },
    
    appear: function(){
        if( this._timer ) clearTimeout( this._timer );
        if( !this._fading ){
            this.bubble.appear({
                duration: this.appearDuration,
                queue: { position: 'end', scope: this.bubble.id, limit: 2 }
            });
        } else {
            this.stopEffects();
            this.bubble.setOpacity( 1.0 );
            this.bubble.show();
        }
    },
    
    fade: function(){
        if( this._timer ) clearTimeout( this._timer );
        this._timer = setTimeout( function(){
            if( this._mouse_out_trigger && this._mouse_out_bubble ){
                this._fading = true;
                this.bubble.fade({
                    duration: this.fadeDuration,
                    queue: { position: 'end', scope: this.bubble.id, limit: 2 },
                    afterFinish: function(){ this._fading = false }.bind(this)
                });
            }}.bind(this),
            1000 * this.hoverDuration
        );
    },
    
    stopEffects: function(){
        Effect.Queues.get(this.bubble.id).invoke('cancel');
        this._fading = false;
    }
});


/*
 * A simple pop-up script.
 * Be sure to include popup.css
 */

var Popup = Class.create({
    popup: null,
    popupwrapper: null,
    popupoverlay: null,
    width: undefined,
    
    initialize: function( content, width ){
        Popup.current_popup = this;
        this.buildContainer();
        this.setContent(content);
        this.resize( width );
        this.hook();
        this.appear();
    },
    
    buildContainer: function(){
        if( $('PopupWrapper') != null ) $('PopupWrapper').remove();
        
        var popupoverlay = Builder.node('div', {'id':'PopupOverlay'}, ' ');
        var popup = Builder.node('div', {'id':'Popup'});
        var popupwrapper = Builder.node('div', {'id': 'PopupWrapper'}, [popup] );
        popupoverlay.setOpacity(0.8);
        popupoverlay.hide();
        popupwrapper.hide();
        
        var body = $$('body')[0];
        var dim = this.getPageSize();
        popupoverlay.setStyle({height: dim[1] + 'px'});
        
        body.appendChild(popupoverlay);
        body.appendChild(popupwrapper);
        
        this.popupoverlay = popupoverlay;
        this.popupwrapper = popupwrapper;
        this.popup = popup;
    },
    
    setContent: function( content ){
        this.popup.update( content );
        Event.fire( document, 'popup:loaded', this );
    },
    
    resize: function( width ){
        if( width != undefined ) this.width = width;
        if( this.width == undefined ) return;
        
        this.popup.setStyle({width: width + 'px'});
    },
    
    appear: function(){
        new Effect.BlindDown( this.popupoverlay, {
            afterFinish: function(){
                this.popupwrapper.appear();
            }.bind(this)
        });
    },
    
    hide: function(){
        this.popupwrapper.fade({
            'afterFinish': function(){
                this.popupwrapper.remove();
            }.bind(this)
        });
        new Effect.BlindUp( this.popupoverlay, {
            'afterFinish': function(){
                this.popupoverlay.remove();
            }.bind(this)
        });
    },
    
    hook: function(){
        /*
        this.popupwrapper.on('click', function(e){
            e.stop();
            this.hide();
        }.bind(this));
        
        this.popup.on('click', function(e){
            e.stop();
        });*/
    },
    
    /* Stolen from lightbox2 */
    getPageSize: function() {
	        
	    var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}
});

Popup.current_popup = null;
Popup.hide = function(){
    this.current_popup.hide();
}
