function PrepareSelectedButton(el){
  var ba = new BrowserAbstraction();
  var downUrl = el.getAttribute("BtnDownUrl");
  if(downUrl=="") downUrl = null;
  var _selected = ba.toBoolean(el.getAttribute("BtnSelected"));
  
  if(_selected==null && downUrl!=null){
    var l = new String(window.location.pathname);
    _selected = downUrl!=null && downUrl!="" && l.indexOf(downUrl, 0)==0;
  }
  el.setAttribute("BtnSelected", _selected);
  return this._selected;
}

function TDButton(td){
  this.element = td;
  this.ba = new BrowserAbstraction();
  this.downUrl = td.getAttribute("BtnDownUrl");
  this._selected = td.getAttribute("BtnSelected");
  if(this._selected!=null && this._selected!="")
    this._selected = (this._selected=="0" || this._selected=="false") ? false : true;
    
  this._opened = this.ba.toBoolean(td.getAttribute("BtnOpened"));
    
  this.cssName = td.getAttribute("BtnCssName");
}


TDButton.prototype.generate = function () {
	var editor = this;
  this.reg = new RegExp("( ?"+this.cssName+"(_u|_o|_d))");
  this.isSelected();

  if(this.cssName){
    var css = this.cssName + (this._selected ? '_d' : '_u');
    this.ba._addClass(this.element, css);
  }
  
  if(!this._selected && this.cssName){
    this.ba._addEvent(this.element, 'mouseover', function (){editor.mOver();});
    this.ba._addEvent(this.element, 'mouseout', function (){editor.mOut();});
    this.ba._addEvent(this.element, 'click', function (){editor.mClick();});
  }
  
  if(this.element.getAttribute("BtnSubMenu")){
    if(this._opened==null)
      this._opened = this._selected;
    
    var cc = window.document.getElementById(this.element.getAttribute("BtnSubMenu"));
    if(cc)
      cc.style.display = this._opened ? 'block' : 'none';
  }

  
}

TDButton.prototype.isSelected = function (){
  if(this._selected==null || this._selected==""){
    var l = new String(window.location.pathname);
    this._selected = this.downUrl!=null && this.downUrl!="" && l.indexOf(this.downUrl, 0)==0;
  }
      
  return this._selected;
}

TDButton.prototype.mOver = function (){
  this.element.className = this.element.className.replace(this.reg, ' '+this.cssName+'_o');
}

TDButton.prototype.mOut = function (){
  this.element.className = this.element.className.replace(this.reg, ' '+this.cssName+'_u');
}

/*TDButton.prototype.mClick = function (){
  var col = this.element.getElementsByTagName('A');
  if(col.length>0){
    window.location = col[0].href;
  }
}*/

TDButton.prototype.mClick = function (){
  var col = this.element.getElementsByTagName('A');
  if(col.length>0){
  	if(col[0].target&&col[0].target!='undefined') {
  		window.open(col[0].href,col[0].target);
  	}
  	else {
  		window.location = col[0].href;
  	}
  }
}


function ImgButton(td){
  this.element = td;
  this.overSuffix = '_o';
  this.upSuffix = '_u';
  this.downSuffix = '_d';
  
  this.ba = new BrowserAbstraction();
  this.downUrl = td.getAttribute("BtnDownUrl");
  this._selected = td.getAttribute("BtnSelected");
  if(this._selected!=null && this._selected!="")
    this._selected = (this._selected=="0" || this._selected=="false") ? false : true;

  var tmp = new Boolean(td.getAttribute("BtnUpIsDown"));
  if(tmp==true) {
    this.overSuffix = this.downSuffix;
  }

  tmp = new RegExp("(.*)(_u|_o|_d)(\.[a-z]{3,})$");
  tmp = tmp.exec(this.element.src);
  this.imgSrcUp = tmp[1]+this.upSuffix+tmp[3];
  this.imgSrcOver = tmp[1]+this.overSuffix+tmp[3];
  this.imgSrcDown = tmp[1]+this.downSuffix+tmp[3];
  
  this.generate();
}


ImgButton.prototype.generate = function () {
	var editor = this;
  this.isSelected();

  if(!this._selected){
    this.ba._addEvent(this.element, 'mouseover', function (){editor.mOver();});
    this.ba._addEvent(this.element, 'mouseout', function (){editor.mOut();});
  }	else {
    this.element.src = this.imgSrcDown;
  }

  
}

ImgButton.prototype.isSelected = function (){
  if(this._selected==null || this._selected==""){
    var l = new String(window.location.pathname);
    //this._selected = this.downUrl!=null && this.downUrl!="" && l.indexOf(this.downUrl, 0)==0;
    var re = new RegExp("^"+this.downUrl);
    this._selected = this.downUrl!=null && this.downUrl!="" && l.match(re);
  }
      

  return this._selected;
}

ImgButton.prototype.mOver = function (){
  this.element.src = this.imgSrcOver;
}

ImgButton.prototype.mOut = function (){
  this.element.src = this.imgSrcUp;
}


function PopUpMenu(o){
  this.el = o;
  this.ba = new BrowserAbstraction();
  
}

PopUpMenu.prototype.generate = function () {
	var editor = this;
  this.ba._addEvent(this.element, 'mouseover', function (){editor.mOver();});
  this.ba._addEvent(this.element, 'mouseout', function (){editor.mOut();});
}
// browser identification

function BrowserAbstraction(){
  
}

BrowserAbstraction.agt = navigator.userAgent.toLowerCase();
BrowserAbstraction.is_ie	   = ((BrowserAbstraction.agt.indexOf("msie") != -1) && (BrowserAbstraction.agt.indexOf("opera") == -1));
BrowserAbstraction.is_opera  = (BrowserAbstraction.agt.indexOf("opera") != -1);
BrowserAbstraction.is_mac	   = (BrowserAbstraction.agt.indexOf("mac") != -1);
BrowserAbstraction.is_mac_ie = (BrowserAbstraction.is_ie && TDButton.is_mac);
BrowserAbstraction.is_win_ie = (BrowserAbstraction.is_ie && !TDButton.is_mac);
BrowserAbstraction.is_gecko  = (navigator.product == "Gecko");


// event handling

BrowserAbstraction.prototype.toBoolean = function(val) {
  if(val==null)
    return null;
  if(val=="")
    return null;
  return (val=="0" || val=="false") ? false : true;
};

BrowserAbstraction.prototype._addEvent = function(el, evname, func) {
	if (BrowserAbstraction.is_ie) {
		return el.attachEvent("on" + evname, func);
	} else {
		return el.addEventListener(evname, func, false);
	}
};

BrowserAbstraction.prototype._addEvents = function(el, evs, func) {
	for (var i in evs) {
		this._addEvent(el, evs[i], func);
	}
};

BrowserAbstraction.prototype._removeEvent = function(el, evname, func) {
	if (this.is_ie) {
		el.detachEvent("on" + evname, func);
	} else {
		el.removeEventListener(evname, func, true);
	}
};

BrowserAbstraction.prototype._removeEvents = function(el, evs, func) {
	for (var i in evs) {
		this._removeEvent(el, evs[i], func);
	}
};

BrowserAbstraction.prototype._stopEvent = function(ev) {
	if (BrowserAbstraction.is_ie) {
		ev.cancelBubble = true;
		ev.returnValue = false;
	} else {
		ev.preventDefault();
		ev.stopPropagation();
	}
};

BrowserAbstraction.prototype._removeClass = function(el, className) {
	if (!(el && el.className)) {
		return;
	}
	var cls = el.className.split(" ");
	var ar = new Array();
	for (var i = cls.length; i > 0;) {
		if (cls[--i] != className) {
			ar[ar.length] = cls[i];
		}
	}
	el.className = ar.join(" ");
};

BrowserAbstraction.prototype._addClass = function(el, className) {
	// remove the class first, if already there
	this._removeClass(el, className);
	el.className += " " + className;
};

BrowserAbstraction.prototype._hasClass = function(el, className) {
	if (!(el && el.className)) {
		return false;
	}
	var cls = el.className.split(" ");
	for (var i = cls.length; i > 0;) {
		if (cls[--i] == className) {
			return true;
		}
	}
	return false;
};

BrowserAbstraction.getBoundingBox = function(el) {
	if (BrowserAbstraction.is_ie) {
		//return el.getBoundingClientRect();
    var box = {top:0, left:0, right:0, bottom:0, height:0, width:0}
	  box.top=el.offsetTop;
	  box.left=el.offsetLeft;
	  
	  box.width=el.clientWidth;
	  box.height=el.clientHeight;

	  while(el = el.offsetParent){
      box.top+=el.offsetTop;
	    box.left+=el.offsetLeft;
	  }

	  box.right = box.left+box.width;
	  box.bottom = box.top+box.height;
		return box;		
	} else {
    var box = {top:0, left:0, right:0, bottom:0, height:0, width:0}
	  box.top=el.offsetTop;
	  box.left=el.offsetLeft;
	  
	  box.width=el.clientWidth;
	  box.height=el.clientHeight;

	  while(el = el.offsetParent){
      box.top+=el.offsetTop;
	    box.left+=el.offsetLeft;
	  }

	  box.right = box.left+box.width;
	  box.bottom = box.top+box.height;
		return box;
	}
};


TDButton.InitAll = function() {
  var el = document.getElementsByTagName("TD");
  for(var i=0; i<el.length; i++){
    
    if(el[i].getAttribute("BtnCssName")){
      el[i].oBtn = new TDButton(el[i]);
      el[i].oBtn.generate();
    }
  }
  
  var el = document.getElementsByTagName("IMG");
  for(var i=0; i<el.length; i++){
    
    if(el[i].getAttribute("BtnDownUrl") || el[i].getAttribute("BtnSelected") || el[i].getAttribute("BtnUpIsDown")){
      //new ImgButton(el[i]).generate();
      new ImgButton(el[i]);
    }
  }  
}

	if (BrowserAbstraction.is_ie) {
		window.attachEvent("onload", function(){TDButton.InitAll();});
	} else if (BrowserAbstraction.is_opera) {
		window.document.addEventListener("load", function(){TDButton.InitAll();}, false);
	} else {
	  window.addEventListener("load", function(){TDButton.InitAll();}, true);
	}


