var EXTRAS = {
    
	// Event listener by Scott Andrew (www.scottandrew.com):
	addEvent : function(obj, evType, fn, useCapture){
		if (obj.addEventListener){
			obj.addEventListener(evType, fn, useCapture);
			return true;
		} 
		else if (obj.attachEvent){
			var r = obj.attachEvent("on"+evType, fn);
			return r;
		} 
		else {
			return false;
		}
	}, 
	
	// Method adapted from Dan Pupius (pupius.co.uk):
	getElementsByClass : function(className,node) {
		if(!node) node=document;
		var refTags = document.all ? node.all : node.getElementsByTagName("*");
		var retVal = new Array();
		for(var z=0;z<refTags.length;z++) {
			if(refTags[z].className == className) 
			retVal.push(refTags[z]);
		}
		return retVal; 
	}
  
}


// adds 1 or more elements to an array (IE only)
if(!Array.prototype.push)
{
	Array.prototype.push =  function()
	{
		var i;
		for(i=0; j=arguments[i]; i++) this[this.length] = j;
		return this.length;
	}
}

// ==============================================================
// Crossbrowser Get computed styles 
// (c)2004 Sergi Meseguer http://zigotica.com/ under CC license:
// http://creativecommons.org/licenses/by-sa/2.0/
// ==============================================================
var  CSS = {

	setStyle : function(node,value) {
		if(typeof(node)=="string")  node = document.getElementById(node);
		node.className = value;
	},
	
	getStyle : function(node,rule){
		if(typeof(node)=="string")  node = document.getElementById(node);
		if(rule=="width") return CSS.getW(node);
		else if(rule=="height") return CSS.getH(node);
		else {
			if (window.getComputedStyle) return window.getComputedStyle(node, null)[rule];
			else return node.currentStyle[rule];
		}
	},

	getH : function(node){
		if(typeof(node)=="string")  node = document.getElementById(node);
		return node.offsetHeight;
	},

	getW : function(node){
		if(typeof(node)=="string")  node = document.getElementById(node);
		return node.offsetWidth;
	},
    
    opacityEffect : function(node, oStart, oEnd, mSec){
        if(typeof(node)=="string") node = document.getElementById(node);
        var speed = Math.round(mSec / 100);
        var timer = 0;
        
        if (oStart > oEnd) {
            for (var i=oStart; i>=oEnd; i--) {
                setTimeout("CSS.setOpacity('" + node.id + "'," + i + ")",(timer*speed));
                timer++;
            }
        } else {
            for (var i=oStart; i<=oEnd; i++) {
                setTimeout("CSS.setOpacity('" + node.id + "'," + i + ")",(timer*speed));
                timer++;
            }
        }
        //setTimeout("CSS.removeNode('"+ node.id + "')",(timer*speed));
    },
    
    removeNode : function(node) {
        if(typeof(node)=="string") node = document.getElementById(node);
        node.parentNode.removeChild(node);
    },
    
    setOpacity : function(node,opacity) {
        if(typeof(node)=="string") node = document.getElementById(node).style;
        node.opacity = (opacity / 100);
        node.MozOpacity = (opacity / 100);
        node.KhtmlOpacity = (opacity / 100);
        node.filter = "alpha(opacity=" + opacity + ")"; 
    }
}

/* BeWeB Js class */
var  BeWeB = {

  init : function() {
  
    /* Afegir confirmació als botons d'eliminar! */
    btns = document.getElementsByTagName('button');
    for (var i=0;i<btns.length;i++) {
      if (btns[i].baseClass) break;
      var cls = btns[i].className.split(' ');
      for (var j=0;j<cls.length;j++) {
        if ( cls[j] != "del") continue;
        btns[i].onclick = function() {
          return confirm(strings['confirm_question']);
        }
      }
    }
  
    /* Hover style pels botons i inputs  */
    var inputs = document.getElementsByTagName('input');
    for(var i=0;i<inputs.length;i++) {
      if (inputs[i].type != "checkbox")
        BeWeB.setHoverEffect(inputs[i]);
    }
    
    /* Opacity effect pels missatges */
    if ( (obj = document.getElementById("BeWeB_warn"))    != null) {
        setTimeout("CSS.opacityEffect('" + obj.id + "',100,0,1000)",2000);
    }
    if ( (obj = document.getElementById("BeWeB_success")) != null) {
        setTimeout("CSS.opacityEffect('" + obj.id + "',100,0,1000)",2000);
    }
    
    /* Doubleclick stuff! */
    var tbls = EXTRAS.getElementsByClass('module_table');
    for(var i=0;i<tbls.length;i++) {
        var rows = tbls[i].getElementsByTagName('tr');
        for (var j=0;j<rows.length;j++) {
            rows[j].onclick = function() {
                // Select / deselect
                var inps = this.getElementsByTagName("input");
                for (var i=0;i<inps.length;i++) {
                    if (inps[i].type != "checkbox" )
                        continue;
                    inps[i].checked = !inps[i].checked;
                }
            }
            rows[j].ondblclick = function() {
                // Try to edit item!
                as = this.getElementsByTagName("a");
                for (var i=0;i<as.length;i++) {
                    if (as[i].className != "btn edit")
                        continue;
                    location.href = as[i].href;
                    break;
                }
            }
            rows[j].baseClass = rows[j].className;
            rows[j].onmouseover = function() {
                this.className = this.baseClass + " hover";
            }
            rows[j].onmouseout = function() {
                this.className = this.baseClass;
            }
        }
        var inps = tbls[i].getElementsByTagName('input');
        for(var j=0;j<inps.length;j++) {
            if (inps[j].type != "checkbox") continue;
            inps[j].onclick = function() {
                this.checked = !this.checked;
            }
        }
    }
    
    /* Inicialitzem els checkboxes de "capçalera" (que seleccionen
     * o des-seleccionen la columna sencera */
    var chks = EXTRAS.getElementsByClass('chkhdr');
    for(var i=0;i<chks.length;i++) {
        chks[i].onclick = function() {
            chks = this.form.getElementsByTagName("input");
            for (var i=0;i<chks.length;i++) {
                if (  chks[i].type == "checkbox") {
                    chks[i].checked = this.checked;
                }
            }
            if (document.all) this.checked = !this.checked;
        }
    }
    
  },
  
  setHoverEffect : function(e) {
    e.hasFocus = 0;
    e.onmouseover = function() {
      if (this.className.indexOf("_hover") < 0) 
        this.className = this.className + '_hover';
    }
    e.onmouseout = function() {
      if (!this.hasFocus && this.className.indexOf("_hover") > 0) {
        this.className = this.className.substring(0,this.className.indexOf("_hover"));
      }
    }
    e.onfocus = function() {
      this.hasFocus = 1;
      if (this.className.indexOf("_hover") < 0) 
        this.className = this.className + '_hover';
    }
    e.onblur = function() {
      this.hasFocus = 0;
      if (!this.hasFocus && this.className.indexOf("_hover") > 0) {
        this.className = this.className.substring(0,this.className.indexOf("_hover"));
      }
    }
    e.onclick = function() {
      if (this.value.indexOf("btn_") > 0) {
        btns = document.getElementsByTagName("button");
        for(var i=0;i<btns.length;i++) {
          btns[i].innerHTML = this.innerHTML;
        }
        //alert(this.value.substring(this.value.indexOf("btn_")+4,this.value.indexOf("btn_")+7));
        //this.form.action.innerHTML = this.value.substring(this.value.indexOf("btn_")+4,this.value.indexOf("btn_")+7);
      }
    }
  }
  
}

/** Add initialize event */
EXTRAS.addEvent(window, 'load', BeWeB.init);
