/*  Html Widgets , version 1.0.0
 *  (c) 2007 Harish Chauhan <harish_282@hotmail.com>
 *
 *  widgets is freely distributable under the terms of an GPL license.
 *  Extention to prototype.js
 *
/*--------------------------------------------------------------------------*/

if(!HKCWidget) var HKCWidget = {};

HKCWidget.Button = Class.create();
 HKCWidget.Button.prototype = {
 		
   initialize: function(container, options) {
      this.container            = $(container);
      this.text					= "Submit";
      this.title				= "Submit";
      this.onclick				= null;
      this.content				= null;
      this.type					= null;
      this.icon					= null;
      this.id					= null;
      this._guid				= null;
      
      this._state				= "normal";
      
      this.normalClass			= 'hbtn-normal'
      this.overClass			= 'hbtn-over'
      this.downClass			= 'hbtn-down'
      
      this.setOptions(options);
      this._attachBehaviors();
      
      return this;
   },
   
   setOptions: function(options) {
   	this.label = options.label;
	this.title = options.title;
	this.id	   = options.id;	
	this._guid = options._guid;	
	
	if(options.type != undefined)
	{this.type = options.type;}

	if(options.icon != undefined)
	{this.icon = options.icon;}
	
	if(options.border==0)
	{
      this.normalClass			= 'hbtnb-normal'
      this.overClass			= 'hbtnb-over'
      this.downClass			= 'hbtnb-down'
	}
		
   	this.onclick = options.onclick;
   },   
   
   _attachBehaviors: function() {
   	
   	var self = this;
   	
   	var table = document.createElement('table');
   	table.setAttribute("cellpadding","0");
   	table.setAttribute("cellspacing","0");
	//table.style.display = 'inline'
   	
   	var tbody = document.createElement('tbody');
   	var tr = document.createElement('tr');
   	var td = document.createElement('td');
   	this.content = document.createElement('div');
   	this.content.className = this.normalClass;
   	this.content.setAttribute("unselectable","on");
   	
   	var img = '';
   	if(this.icon != null && document.all)
   		img = "<img src='"+this.icon+"' border=0 style='vertical-align:middle;margin-top:-2px;'>&nbsp;";
	else if(this.icon != null)
   		img = "<img src='"+this.icon+"' border=0 style='vertical-align:middle;margin-top:-4px;'>&nbsp;";
   	
   	this.content.innerHTML = '<div class="hbtn-right">'+
			'<div class="hbtn-left">'+
   			'<div unselectable="on" title="'+this.title+'" class="hbtn-text" >'+img+this.label+'</div>'+
       		'</div>'+
       	'</div>';
   	
   	td.appendChild(this.content);
   	tr.appendChild(td);
   	tbody.appendChild(tr);
   	table.appendChild(tbody);
   	
   	this.content.id = this.id;
	this.content.onclick = function (e) {
		self._guid.click();
			//this.onclick;	
	}
	this.content.onmouseover = function(e)
							{
								if(self.type == 'push' && self.state =='down') return;
								
								self.state = 'over';
								this.className = self.overClass;
							}	
	this.content.onmouseout = function(e)
								{
									if(self.type == 'push' && self.state =='down') return;	
									self.state = 'out';
									this.className = self.normalClass;
								}	
	this.content.onmousedown = function(e)
								{ 
									//alert(self.type);
									if(self.type == 'push' && self.state =='down')
									{
										self.state = 'pushup';
										return;	
									}
									
									self.state = 'down';
									this.className = self.downClass;
								}	
	this.content.onmouseup = function(e)
								{ 
									if(self.type == 'push' && self.state =='down') return;	
									
									if(self.type == 'push' && self.state =='pushup') 
									{
										this.className = self.overClass;
										return;	
									}
										
									
									self.state = 'up';
									this.className = self.normalClass;
								}	
	
   	this.container.appendChild(table);
   }
  
 };

function hkc_convert_btn(classname)
{
	var els;
	
	if(classname != undefined)
		els = document.getElementsByClassName(classname);
	else
		els = document.getElementsByTagName("input");
	
	for(var i=0;i<els.length;i++)
	{
		if(els[i].type.toLowerCase() == 'button' || els[i].type.toLowerCase() == 'submit' || els[i].type.toLowerCase() == 'reset')
		{
			if(!Element.visible(els[i])) continue;
			
			var btn = new HKCWidget.Button(els[i].parentNode,
								{

								 id: els[i].id
								 ,label: els[i].value
								 ,title : els[i].title
								 ,type: els[i].getAttribute('btntype')
								 ,border: parseInt(els[i].getAttribute('border'))
								 ,icon: els[i].getAttribute('icon')
								 ,onclick : els[i].onclick
								 ,_guid: els[i]	
								});
								
			els[i].id = els[i].id+"_dep";
			els[i].style.position = "absolute";
			els[i].style.top = "-1000px";
			//Element.hide(els[i]);	
		}
	}
}
