/////////////////////////////////////////////////////////////////////////////
//
// Gladiator Components 
//
// http://www.gladiatorweb.com
//
// (c) 2006 by Edward H. Trager .  All Rights Reserved
//
// NAME: gladiatorSVGLoginWidget.js
//
// AUTHOR: Edward H. Trager
//
// LAST UPDATE: 2007.01.03
//
// NOTE: SEE "LICENSE" FILE FOR LICENSING TERMS
//
//////////////////////////////////////////////////////////////////////////////

//
// 
//
var GLADIATOR_SVG_BUTTON_INCLUDED=true;
if(typeof GLADIATOR_CORE_INCLUDED == 'undefined') alert('NOTA BENE: "gladiatorSVGButton.js" REQUIRES "gladiatorCore.js"!');
// DISABLE CHECKING SVG -- THIS IS AN IE WORK-AROUND:
//if(typeof GLADIATOR_SVG_INCLUDED == 'undefined') alert('NOTA BENE: "gladiatorSVGButton.js" REQUIRES "gladiatorSVG.js"!');

//
// gSVGButton
//
function gSVGButton(buttonText,id,left,top,width,height,cornerRadius,eventMethod,parentNode){
	
	var _buttonText="Ok";
	if(buttonText) _buttonText=buttonText;
	
	var _id="svgButton1";
	if(id) _id=id;
	
	var _left=25;
	if(left) _left=left;
	
	var _top=25;
	if(top) _top=top;
	
	var _width=148;
	if(width) _width=width;
	
	var _height=30;
	if(height) _height=height;
	
	var _radius=8;
	if(cornerRadius){
		_radius=cornerRadius;
		var halfHeight= 0.5*_height;
		if(_radius>halfHeight) _radius=halfHeight;
	}
	
	var _eventMethod=false;
	if(eventMethod) _eventMethod=eventMethod;
	
	//
	// Find out svgButtonScreen's stroke width:
	//
	// This does not seem to work yet, don't know why:
	//
	//var _sheet   = getStyleSheet("gladiatorSVGButton");
	//var _myStyle = getCSSClassStyle(_sheet,".svgButtonScreen");
	//var _borderWidth = parseInt( _myStyle.strokeWidth );
	
	var _borderWidth = 1;
	
	//
	// CREATE THE BUTTON CANVAS:
	//
	var _canvas = document.createElementNS(NS.xhtml,"div");
	_canvas.setAttribute("class","gSVGButton");
	_canvas.id=_id;
	_canvas.style.left   = _left+"px";
	_canvas.style.top    = _top+"px";
	_canvas.style.width  = (_width  + _borderWidth*2)+"px";
	_canvas.style.height = (_height + _borderWidth*2)+"px";
	
	//
	// ADD SVG:
	//
	var _svg = new gSVG();
	_svg.drawHeader(_width+_borderWidth*2,_height+_borderWidth*2);
	_svg.addContent('<defs>');
	var gradientScaleFactor = 1.5;
	_svg.addContent(' <linearGradient id="BTX" x1="10" y1="0" x2="10" y2="' + (gradientScaleFactor * _height) + '" gradientUnits="userSpaceOnUse" gradientTransform="translate(0.0,-9.8)"><stop style="stop-color:#fff;stop-opacity:0.0" offset="0.0" id="BTX01" /><stop style="stop-color:#004;stop-opacity:0.5" offset="1.0" id="BTX02" /></linearGradient>');
	_svg.addContent('</defs>');
	_svg.addContent('<g >');
	_svg.addContent(' <rect id="' + _id+'_base' + '" class="svgButtonBase"   width="' + _width + '" height="' + _height + '" rx="' + _radius + '" ry="' + _radius + '" x="' + _borderWidth + '" y="' + _borderWidth + '" />');
	_svg.addContent(' <rect id="' + _id+'_screen' + '" class="svgButtonScreen" width="' + _width + '" height="' + _height + '" rx="' + _radius + '" ry="' + _radius + '" x="' + _borderWidth + '" y="' + _borderWidth + '" style="fill:url(#BTX);"  />');
	_svg.addContent('</g>');
	_svg.drawFooter();
	_svg.render(_canvas);
	
	var _textDiv = document.createElementNS(NS.xhtml,"div");
	_textDiv.setAttribute("class","svgButtonLabel");
	_textDiv.style.width = _width +"px";
	_textDiv.style.height= _height+"px";
	//
	// Setting line-height is necessary to get "vertical-align"
	// to work properly:
	//
	_textDiv.style.lineHeight= (1.0*_height)+"px";
	//
	// Automatic font sizing on buttons: 0.5 is just about right
	//
	_textDiv.style.fontSize  = (0.50*_height)+"px";
	
	var _textNode = document.createTextNode(_buttonText);
	_textDiv.appendChild(_textNode);
	_canvas.appendChild(_textDiv);
	//
	// add to DOM:
	//
	if(parentNode){
		parentNode.appendChild(_canvas);
	}else{
		var bodyRef = document.getElementsByTagName("body").item(0);
		bodyRef.appendChild(_canvas);
	}
	
	//
	// Keep a reference to the buttonBase SVG node:
	// 
	var _buttonBase = document.getElementById(_id+'_base');
	
	////////////////////
	//
	// PUBLIC METHODS
	//
	////////////////////
	
	//
	// disable:
	//
	this.disable = function(){
		
		_buttonBase.setAttribute("class","svgButtonBaseDisabled");
		_textDiv.setAttribute("class","svgButtonLabelDisabled");
		_canvas.removeEventListener("click"    ,_clickMe  ,false);
		_canvas.removeEventListener("mousedown",_mouseDown,false);
		_canvas.removeEventListener("mouseup"  ,_mouseUp  ,false);
		
	}
	
	//
	// enable:
	//
	this.enable = function(){
		
		_buttonBase.setAttribute("class","svgButtonBase");
		_textDiv.setAttribute("class","svgButtonLabel");
		_canvas.addEventListener("click"    ,_clickMe  ,false);
		_canvas.addEventListener("mousedown",_mouseDown,false);
		_canvas.addEventListener("mouseup"  ,_mouseUp  ,false);
		
	}
	
	///////////////////
	//
	// EVENT HANDLERS:
	//
	///////////////////
	
	function _clickMe(){
		
		if(_eventMethod) _eventMethod();
		
	}
	
	function _mouseDown(){
		
		document.getElementById(_id+'_base').setAttribute("class","svgButtonBaseHighlight");
		
	}
	
	function _mouseUp(){
		
		document.getElementById(_id+'_base').setAttribute("class","svgButtonBase");
		
	}
	//
	// addEventMethod:
	//
	this.addEventMethod = function(eventMethod){
		
		_eventMethod = eventMethod;
		
	}
	
	//
	// changeLabel:
	//
	this.changeLabel = function(label){
		
		var newTextNode = document.createTextNode(label);
		_textDiv.replaceChild(newTextNode,_textNode);
		_textNode = newTextNode;
		
	}
	
	//
	// Add Even Handlers:
	//
	_canvas.addEventListener("click"    ,_clickMe  ,false);
	_canvas.addEventListener("mousedown",_mouseDown,false);
	_canvas.addEventListener("mouseup"  ,_mouseUp  ,false);
	
}

