
/*
 * SVGForm Library (alpha2)
 * Copyright 2008 Michael Farrell
 */
 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
var svgns = "http://www.w3.org/2000/svg";
var svgform_noscript = null;

try {
	svgform_noscript = document.getElementById("svgform_noscript");
} catch (e) {
}

if (svgform_noscript != null) {
	svgform_noscript.style.setProperty("display", "none", "");
}

function SFButton(x, y, w, h, label) {
	// set internal attrs
	this._x = x;
	this._y = y;
	this._w = w;
	this._h = h;
	this._label = label;
	this._visible = true;
	//this._name = name;
	
	// default colours
	this._ibgcolour = '#ffffff';
	this._abgcolour = '#cccccc';
	this._ibocolour = '#000000';
	this._abocolour = '#0000ff';
	this._ilacolour = '#000000';
	this._alacolour = '#0000ff';

	// create group
	this.group = document.createElementNS(svgns, "g");
	//this.group.setAttributeNS(null,"id",name);
	
	// create rectangle
	this.rect = document.createElementNS(svgns, "rect");
	this.rect.setAttributeNS(null,"x",x);
	this.rect.setAttributeNS(null,"y",y);
	this.rect.setAttributeNS(null,"width",w);
	this.rect.setAttributeNS(null,"height",h);
	//this.rect.setAttributeNS(null,"id",name+"_rect");
	this.rect.style.setProperty('fill', this._ibgcolour, '');
	this.rect.style.setProperty('stroke-width', '2px', '');
	this.rect.style.setProperty('stroke', this._ibocolour, '');
	this.rect.style.setProperty('stroke-linejoin', 'round', '');
	
	// create text
	this.text = document.createElementNS(svgns, "text");
	this.text.setAttributeNS(null,"x",x+(w/2));
	this.text.setAttributeNS(null,"y",y+(h/2));
	//this.text.setAttributeNS(null,"id",name+"_text");
	this.text.style.setProperty('text-anchor', 'middle', '');
	this.text.style.setProperty('dominant-baseline', 'central', '');
	this.text.style.setProperty('fill', this._ilacolour, '');
	//this.text.style.setProperty('font-family', 'Calibri', '');
	this.text.textContent = label;
	
	// create standard functions for this
	this.setVisible = function(v) {
		this._visible = v;
		if (v) {
			this.group.style.setProperty('display', 'inline', '');
		} else {
			this.group.style.setProperty('display', 'none', '');
		}
	};
	
	this.getVisible = function() {
		return this._visible;
	};
	
	this.toggleVisible = function() {
		this.setVisible(!this._visible);
	};
	
	this.setFont = function(f, s) {
		this.setFontFamily(f);
		this.setFontSize(s);
	};
	
	this.setFontFamily = function(v) {
		this.text.style.setProperty('font-family', v, '');
	};
	
	this.setFontSize = function(v) {
		this.text.style.setProperty('font-size', v, '');
	};
	
	this.setFontWeight = function(v) {
		this.text.style.setProperty('font-weight', v, '');
	};
	
	this.move = function(x,y) {
		this._x = x;
		this._y = y;
		this.rect.setAttributeNS(null,"x",x);
		this.rect.setAttributeNS(null,"y",y);
		this.text.setAttributeNS(null,"x",x+(this._w/2));
		this.text.setAttributeNS(null,"y",y+(this._h/2));
	};
	
	this.resize = function(w, h) {
		this._w = w;
		this._h = h;
		this.rect.setAttributeNS(null,"width",w);
		this.rect.setAttributeNS(null,"height",h);
		this.text.setAttributeNS(null,"x",this._x+(w/2));
		this.text.setAttributeNS(null,"y",this._y+(h/2));
	};
	
	this.setLabel = function(v) {
		this._label = v;
		this.text.textContent = v;
	};
	
	this.onPress = null;
	this.onMouseOver = null;
	this.onMouseOut = null;
	
	// take over event handling
	this.group.addEventListener("click", this, false);
	this.group.addEventListener("mouseover", this, false);
	this.group.addEventListener("mouseout", this, false);
	
	this.handleEvent = function(e) {
		switch (e.type) {
			case "click":
				if (this.onPress == null) {
					alert("Called default onPress handler for SFButton with label: " + this._label);
				} else {
					this.onPress();
				}
				break;
			
			case "mouseover":
				this.rect.style.setProperty('stroke', this._abocolour, '');
				this.rect.style.setProperty('fill', this._abgcolour, '');
				this.text.style.setProperty('fill', this._alacolour, '');
				if (this.onMouseOver != null) {
					this.onMouseOver();
				}
				break;
				
			case "mouseout":
				this.rect.style.setProperty('stroke', this._ibocolour, '');
				this.rect.style.setProperty('fill', this._ibgcolour, '');
				this.text.style.setProperty('fill', this._ilacolour, '');
				if (this.onMouseOut != null) {
					this.onMouseOut();
				}
				break;
		}
	};
			
				
	
	// add elements to group
	this.group.appendChild(this.rect);
	this.group.appendChild(this.text);
	
	// add group to document
	document.documentElement.appendChild(this.group);
}

function SFLabel(x, y, label) {
   	// set internal attrs
	this._x = x;
	this._y = y;
	this._label = label;
	this._visible = true;
	//this._name = name;
	
	// set defaults
	this._fgcolour = '#000000';
	
	// create text
	this.text = document.createElementNS(svgns, "text");
	this.text.setAttributeNS(null,"x",x);
	this.text.setAttributeNS(null,"y",y);
	//this.text.setAttributeNS(null,"id",name+"_text");
	this.text.style.setProperty('fill', this._fgcolour, '');
	//this.text.style.setProperty('font-family', 'Calibri', '');
	this.text.textContent = label;
	
	// create some methods for this.
	this.setVisible = function(v) {
		this._visible = v;
		if (v) {
			this.text.style.setProperty('display', 'inline', '');
		} else {
			this.text.style.setProperty('display', 'none', '');
		}
	};
	
	this.getVisible = function() {
		return this._visible;
	};
	
	this.toggleVisible = function() {
		this.setVisible(!this._visible);
	};
	
	this.setFgColour = function(c) {
		this._fgcolour = '#000000';
		this.text.style.setProperty('fill', this._fgcolour, '');
	};
	
	this.setLabel = function(l) {
		this._label = l;
		this.text.textContent = l;
	};
	
	this.move = function(x,y) {
		this._x = x;
		this._y = y;
		this.text.setAttributeNS(null,"x",x);
		this.text.setAttributeNS(null,"y",y);
	};
	
	this.setFont = function(f, s) {
		this.setFontFamily(f);
		this.setFontSize(s);
	};
	
	this.setFontFamily = function(v) {
		this.text.style.setProperty('font-family', v, '');
	};
	
	this.setFontSize = function(v) {
		this.text.style.setProperty('font-size', v, '');
	};
	
	this.setFontWeight = function(v) {
		this.text.style.setProperty('font-weight', v, '');
	};
	
	// add group to document
	document.documentElement.appendChild(this.text);
}

