/* -----------------------------------*/
/* --->>> onDOMReady Extension <<<----*/
/* -----------------------------------*/


Object.extend(Event, {
  _domReady : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;

    if (this._timer)  clearInterval(this._timer);
    
    this._readyCallbacks.each(function(f) { f() });
    this._readyCallbacks = null;
},
  onDOMReady : function(f) {
    if (!this._readyCallbacks) {
      var domReady = this._domReady.bind(this);
      
      if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", domReady, false);
        
        /*@cc_on @*/
        /*@if (@_win32)
            document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
            document.getElementById("__ie_onload").onreadystatechange = function() {
                if (this.readyState == "complete") domReady(); 
            };
        /*@end @*/
        
        if (/WebKit/i.test(navigator.userAgent)) { 
          this._timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState)) domReady(); 
          }, 10);
        }
        
        Event.observe(window, 'load', domReady);
        Event._readyCallbacks =  [];
    }
    Event._readyCallbacks.push(f);
  }
});

/* end onDomReady */


  function show_waiting(element_id) {
    if($(element_id))
      Effect.Appear(element_id,{duration:0.5,queue:'end'});
  }
  
  function hide_waiting(element_id) {
    if($(element_id))
      Effect.Fade(element_id,{duration:0.5,queue:'end'});
  }
  
  
  var Popup = {
    open: function(options)
    {
      this.options = {
        url: '#',
        width: 600,
        height: 500,
        name:"_blank",
        location:"no",
        menubar:"no",
        toolbar:"no",
        status:"yes",
        scrollbars:"yes",
        resizable:"yes",
        left:"",
        top:"",
        normal:false
      }
      Object.extend(this.options, options || {});
  
      if (this.options.normal){
          this.options.menubar = "yes";
          this.options.status = "yes";
          this.options.toolbar = "yes";
          this.options.location = "yes";
      }

      this.options.width = this.options.width < screen.availWidth?this.options.width:screen.availWidth;
      this.options.height=this.options.height < screen.availHeight?this.options.height:screen.availHeight;
      var openoptions = 'width='+this.options.width+',height='+this.options.height+',location='+this.options.location+',menubar='+this.options.menubar+',toolbar='+this.options.toolbar+',scrollbars='+this.options.scrollbars+',resizable='+this.options.resizable+',status='+this.options.status
      if (this.options.top!="")openoptions+=",top="+this.options.top;
      if (this.options.left!="")openoptions+=",left="+this.options.left;
      window.open(this.options.url, this.options.name,openoptions );
      // return false;
    }
  }

function handlePopUps(){
	document.getElementsByClassName("popup").each(function(item) {
		Event.observe(item, 'click', function(event){
			Popup.open({url:item.href});
			Event.stop(event);
		}, false);
     });
}

/* Sidebar User-menu */
  
function initUserMenu() {
    if ($('user-menu')) {
    	Element.addClassName($('user-menu'), "js");
    }
}

function initUserMenuLinks() {
	if ($('user-menu')) {
    	$('user-menu').getElementsByClassName("usermenu-link").each(function(item) {
    		Event.observe(item, 'click', function(event){clickUserMenu(event);}, false);
	     });    
	}
}

function clickUserMenu(event) {
	var pickedSection = Element.classNames(Event.element(event).parentNode);
	pickedSection.remove('area');                    
	Element.classNames($('usermenu-selection-list')).set('areas');                   
	Element.addClassName($('usermenu-selection-list'), pickedSection);
	/*Element.classNames($('usermenu-selection-list')).add(pickedSection);                   */
	pickedSection.add('area');
}

Event.onDOMReady(function() {initUserMenu(); });
Event.onDOMReady(function() {initUserMenuLinks(); });

/* Formatting helper-text */

function initFormattingHelp() {
	if($("formatting-help-link")) {
		Element.setStyle($("formatting-help-link"), {display: "block"});
		$("formatting-help-text").hide();
	}
}

function initFormattingHelpLink() {
	if($("formatting-help-link")) {
		Event.observe($("formatting-help-link"), 'click', function(event) {
			$("formatting-help-link").hide();
			Effect.BlindDown('formatting-help-text', {duration:1});
			Event.stop(event); // don't want the link to follow through if we've got JS.
		}, false);
	}
}

Event.onDOMReady(function() {initFormattingHelp(); });
Event.onDOMReady(function() {initFormattingHelpLink();} );
Event.onDOMReady(function() {handlePopUps();} );