// TemplateControl javascript - must be included on any page that uses TemplateControl web control.
var ua = navigator.userAgent.toLowerCase();
var useIEPngHack = ( ((ua.indexOf("msie 5") != -1) || (ua.indexOf("msie 6") != -1)) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1));

/*
 * Utility functions
 */

function debug(str) {
	//alert(str);
	//t = document.getElementById('debuglog'); if (t != null) t.value = t.value + str + '\n';
}

function fncall(str) {
	//debug(str);
}

// Get Element By Id
function gebi(id) {
	fncall('gebi ' + id);
	return document.getElementById(id);
}

function getSize(element) {
	fncall('getSize ' + element.id);
	return new Dimension(element.width, element.height);
}

function resize(element, w, h) {
	fncall('resize ' + element.id + ' ' + w + ' ' + h);
	element.style.width = w + 'px';
	element.style.height = h + 'px';
}

function move(element, x, y) {
	fncall('move ' + element.id + ' ' + x + ' ' + y);
	element.style.top = '' + y + 'px';
	element.style.left = '' + x + 'px';
}

function setLeft(o,v)	{if(o)o.style.left=v+"px";}
function setTop(o,v)	{if(o)o.style.top=v+"px";}

function getLeft(o)		{if(!o||(!o.style.left))  return 0; return parseInt(o.style.left.replace("px",""));}
function getTop(o)		{if(!o||(!o.style.top))  return 0; return parseInt(o.style.top.replace("px",""));}
function getWidth(o)	{if(!o||(!o.style.width)) return 0; return parseInt(o.style.width.replace("px",""));}
function getHeight(o)	{if(!o||(!o.style.height))return 0; return parseInt(o.style.height.replace("px",""));}
function getPosition(o) {return new Point( getLeft(o), getTop(o));}

function clip(element, rect) {
	fncall('clip ' + element.id + ' ' + rect.x + ' ' + rect.y + ' ' + rect.w + ' ' + rect.h);
	var l = (rect.x);
	var r = (rect.x + rect.w);
	var t = (rect.y);
	var b = (rect.y + rect.h);
	element.style.clip = 'rect(' + t + 'px, ' + r + 'px, ' + b + 'px, ' + l + 'px)';
	element.style.width = parseInt(rect.w) + 'px';
	element.style.height = parseInt(rect.h) + 'px';
}

// Returns a new Dimension scaled as requested
function scale(originalDim, requestedDim, crop) {
	fncall('scale ' + originalDim + ' ' + requestedDim + ' ' + crop);
	if(!requestedDim && originalDim) return originalDim;
	var outAspect = requestedDim.w / requestedDim.h;
	var imgAspect = originalDim.w / originalDim.h;
	if (imgAspect < outAspect == crop) {
		// Scale to max width
		return new Dimension(requestedDim.w, requestedDim.w / imgAspect);
	}
	else {
		// Scale to max height
		return new Dimension(requestedDim.h * imgAspect, requestedDim.h);
	}
}

// Utilities functions
if(typeof String.prototype.endsWith == "undefined"){
    String.prototype.endsWith = function(s){
        var s_i = this.lastIndexOf(s);
        return (s_i < 0)?false:(this.substring(s_i)===s);
    }
}
function AttachEvt(element, eventName, method){
	if(element.attachEvent){ eventName="on"+eventName;element.attachEvent(eventName,method);	}
	else{ element.addEventListener(eventName,method,true); }
}
function cancelBbl(evt){
    if (evt.stopPropagation) {
        evt.stopPropagation();
        evt.preventDefault();
    } else if (evt.cancelBubble) {
        evt.cancelBubble = true;
        evt.returnValue = false;
    }
}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return {x:curleft,y:curtop};
}
function scrollX(){
    return ((document.all)?document.documentElement.scrollLeft:window.pageXOffset);
}
function scrollY(){
    return ((document.all)?document.documentElement.scrollTop:window.pageYOffset);
}
function getDim(element) {
	return new Dimension((element.offsetWidth||element.width), (element.offsetHeight||element.height));
}
/*
 * Classes
 */

// Rectangle can be passed around as Dimension and Point also (duck typing)
function Rectangle(x, y, w, h) {
	this.x = isNaN(x)?0:x;
	this.y = isNaN(y)?0:y;
	this.w = isNaN(w)?0:w;
	this.h = isNaN(h)?0:h;
}
Rectangle.prototype.toString = function(){
	return "Rectangle{ x:"+this.x+", y:"+this.y+", w:"+this.w+", h:"+this.h+" }"
}
function Dimension(w, h) {
	this.w = isNaN(w)?0:w;
	this.h = isNaN(h)?0:h;
}
Dimension.prototype.toString = function(){
	return "Dimension{ w:"+this.w+", h:"+this.h+" }"
}
function Point(x, y) {
	this.x = isNaN(x)?0:x;
	this.y = isNaN(y)?0:y;
}
Point.prototype.toString = function(){
	return "Point{ x:"+this.x+", y:"+this.y+" }"
}
// firefox click() method fix
if(typeof HTMLElement == "function"){
	HTMLElement.prototype.click = function() { 
		var evt = this.ownerDocument.createEvent('MouseEvents');
		evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
		this.dispatchEvent(evt);
	}
}