/*
	Copyright (c) 2004-2006, The Dojo Foundation
	All Rights Reserved.

	Licensed under the Academic Free License version 2.1 or above OR the
	modified BSD license. For more information on Dojo licensing, see:

		http://dojotoolkit.org/community/licensing.shtml
*/



dojo.provide("wlp.widget.WLPTitlePane");
dojo.require("dojo.widget.ContentPane");
dojo.require("dojo.widget.*");
dojo.require("dojo.html.style");
dojo.require("dojo.lfx.*");
dojo.widget.defineWidget("wlp.widget.WLPTitlePane", dojo.widget.ContentPane, {labelNodeClass:"", containerNodeClass:"", timer:"", e:"", iin:true, out:true, label:"", open:false, url_href:"",target:"", templateString:"<div dojoAttachPoint=\"domNode\">\n<div dojoAttachPoint=\"labelNode\" dojoAttachEvent=\"onclick: onLabelClick; onmouseout: onLabelOut; onmouseover: onLabelOver;\"></div>\n<div dojoAttachPoint=\"containerNode\"></div>\n</div>\n", postCreate:function () {
	if (this.label) {
    if(this.url_href=="") {
      this.labelNode.appendChild(document.createTextNode(this.label));
		} else {
    var link = document.createElement('a');
        link.href=    this.url_href;
        link.title=   this.label;
        if (this.target != "")
          link.target=  this.target;
        link.appendChild( document.createTextNode(this.label) );
        this.labelNode.appendChild(link);
		}
	}
	if (this.labelNodeClass) {
		dojo.html.addClass(this.labelNode, this.labelNodeClass);
	}
	if (this.containerNodeClass) {
		dojo.html.addClass(this.containerNode, this.containerNodeClass);
	}
	if (!this.open) {
		dojo.html.hide(this.containerNode);
	}
	if (this.widgetId == this.e){
		this.openMenu();}
  
	wlp.widget.WLPTitlePane.superclass.postCreate.apply(this, arguments);
}, 
onLabelClick:function () {
  var me= this;
  var state = this.open;
  if((me.children!=null)&&(me.children.length == 0)) {
    for (var child_num=0; child_num < this.myChildren.length; child_num++) {
      var child=this.myChildren[child_num];
      var widgetChild = dojo.widget.createWidget("wlp:WLPTitlePane", 
            {id:child.id,labelNodeClass:child.labelNodeClass,label:child.label,url_href:child.url_href,target:child.target, myChildren:child.children});
      me.addChild(widgetChild);
    }
  }
  if(this.children.length!=0 || me.widgetId == 'toggleAll'){
  if((me.widgetId != 'toggleAll')) {
    // toggleAll not detected : open / close one node 
    if(state) {
      this.closeNode();
    } else {
      this.openNode();
    }
  } else {
    // toggleAll detected : open / close all nodes
    dojo.lang.forEach(this.parent.children, function (brother) {
      if(state)
        brother._close(true);
      else 
        brother._open(true);
    });
  }}
}, 
onLabelOver:function () {
	dojo.html.addClass(this.labelNode, "over");
  var a = this;
  var b = this;
  var start = true;
  while(start){
    try{
      b = a.parent;
      c = b.parent;
      a = b;
    }catch(err){
      start = false;
      a.iin = !a.iin;  
      a.timeur();  
    }
  }
}, 
onLabelOut:function () {
	dojo.html.removeClass(this.labelNode, "over");
  var a = this;
  var b = this;
  var start = true;
  while(start){
    try{
      b = a.parent;

      c = b.parent;

      a = b;
    }catch(err){

      start = false;
      a.out = !a.out;  
      a.timeur();  
    }
  }
}, 
timeur:function () {
	 
    if (this.iin ^ this.out == 0){
      this.timer = dojo.lang.setTimeout(this, "openMenu",5000);
    }else{
      dojo.lang.clearTimeout(this.timer);
    }

},
quickOpenMenu:function () {
  var myElement = dojo.widget.byId(this.e);
      if(myElement != undefined) {
        var myElement1 = myElement.parent; 
      if(myElement1!=null) {
        var myElement2 = myElement1.parent; 
      if(myElement2!=null) {
        var myElement3 = myElement2.parent;        
      if(myElement3!=null) { 
        myElement3.containerNode.style.display = "block";
        myElement3.labelNode.className += " " + myElement3.labelNode.className+"-selected";
        myElement3.open=true;
      }
      myElement2.containerNode.style.display = "block";
      myElement2.labelNode.className += " " + myElement2.labelNode.className+"-selected";
      myElement2.open = true;
      }
      myElement1.containerNode.style.display = "block";
      myElement1.labelNode.className += " " + myElement1.labelNode.className+"-selected";
      myElement1.open = true;      
      }
      myElement.containerNode.style.display = "block";
   	  myElement.labelNode.className += "-focused";
      myElement.open = true;
    }
},
openMenu:function () {

	var myElement = dojo.widget.byId(this.e);
    if(myElement != undefined) {

      dojo.lang.hitch(myElement, myElement.openNode());
      if(myElement.parent!=null) {
    	dojo.lang.hitch(myElement.parent, myElement.parent.openNode());
      if(myElement.parent.parent!=null) {
        dojo.lang.hitch(myElement.parent.parent, myElement.parent.parent.openNode());
      if(myElement.parent.parent.parent!=null) { 
        dojo.lang.hitch(myElement.parent.parent.parent, myElement.parent.parent.parent.openNode());
      }}}
    }
},
closeNode:function() {
  var me= this;
        // node is open : 
        // if one of its brother is open (= menu expanded), close brothers and leave node open
        // else close this node
        var opened_brother=false;
        dojo.lang.forEach(this.parent.children, function (brother) {
          if((brother != me ) && (brother.open)) opened_brother = true;
        });
        if(opened_brother) {
          dojo.lang.forEach(this.parent.children, function (brother) {
            if(brother != me) brother._close(true);
          });
        } else this._close(true);
},

openNode:function() {
  var me= this;
      // node is closed : open this node, close the others
      if(this.parent != null) 
      {
        dojo.lang.forEach(this.parent.children, function (brother) {
          if(brother != me) brother._close(true);
        });
      }
	if(this.children.length!=0)
	this._open(false);
},


_open:function(recursif) {
  		if(!this.open) 
        dojo.lfx.wipeIn(this.containerNode, 250).play(); 
  		this.open = true;
  		dojo.html.addClass(this.labelNode, this.labelNodeClass+"-selected");
      if(recursif) {
        dojo.lang.forEach(this.children, function (child) {
          if(child.widgetType == 'WLPTitlePane')
            	child._open(true);
        });      
      }
        
},

_close:function(recursif) {
      if(this.open) 
      dojo.lfx.wipeOut(this.containerNode, 250).play();
  		this.open = false;
  		dojo.html.removeClass(this.labelNode, this.labelNodeClass+"-selected");
  		
      if(recursif) {
        dojo.lang.forEach(this.children, function (child) {
          if(child.widgetType == 'WLPTitlePane')
            	child._close(true);
        });      
      }
},


setLabel:function (label) {
	this.labelNode.innerHTML = label;
}});

