﻿setClass = function(el, className) {
    el.cssClass = className;
    el.className = className;
}

YAHOO = new Object();
YAHOO.util = new Object();
YAHOO.util.Dom = new Object();

// Copyright Yahoo 2008
YAHOO.util.Dom.setInnerHTML = function (el, html) {
    //el = YAHOO.util.Dom.get(el);
    if (!el || typeof html !== 'string') {
        return null;
    }

    // Break circular references.
    // Could look at using this code elsewhere.
    
    (function (o) {
 
        var a = o.attributes, i, l, n, c;
        if (a) {
            l = a.length;
            for (i = 0; i <l; i += 1) {
                n = a[i].name;
                if (typeof o[n] === 'function') {
                    o[n] = null;
                }
            }
        }
 
        a = o.childNodes;
 
        if (a) {
            l = a.length;
            for (i = 0; i <l; i += 1) {
                c = o.childNodes[i];
 
                // Purge child nodes.
                arguments.callee(c);
 
                // Removes all listeners attached to the element via YUI's addListener.
                if (YAHOO.util.Event) YAHOO.util.Event.purgeElement(c);
            }
        }
 
    })(el);
 
    // Remove scripts from HTML string, and set innerHTML property
    el.innerHTML = html.replace(/<script[^>]*>((.|[\r\n])*?)<\\?\/script>/ig, "");
 
    // Return a reference to the first child
    return el.firstChild;
};
// End Copyright Yahoo 2008



function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  //var rv = -1; // Return value assumes failure.
  var rv = false;
  
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

var ieVersion = getInternetExplorerVersion();

StringBuilder = function(value)
{
    this.strings = new Array("");
    this.append(value);
}
var p = StringBuilder.prototype;
// Appends the given value to the end of this instance.
p.append = function (value)
{
    if (value)
    {
        this.strings.push(value);
    }
}

// Clears the string buffer
p.clear = function ()
{
    this.strings.length = 1;
}


// Converts this instance to a String.
p.toString = function ()
{
    return this.strings.join("");
}
p.getValue = p.toString;

//var everIncrementing

String.prototype.stripSpaces = function( ){ return this.replace( /\s/g, "" ); };

var p = Object.prototype;

p.inheritFrom = function(fnClass) {
    function inheritClasses(fnClass, arrClasses) {
        arrClasses.push(fnClass);
        if (typeof fnClass.__superclasses__ == "object") {
            for (var i = 0; i < fnClass.__superclasses__.length; i++) {
                inheritClasses(fnClass.__superclasses__[i], arrClasses);
            }
        }
    }

    if (this.constructor) {
        if (typeof this.constructor.__superclasses__ == "undefined") {
            this.constructor.__superclasses__ = new Array();
        }
        inheritClasses(fnClass, this.constructor.__superclasses__);
        for (prop in fnClass.prototype) {
            if (typeof fnClass.prototype[prop] == "function") {
                this[prop] = fnClass.prototype[prop];
            }
        }
    }

    
    //if (fnClass.prototype.hello) {
    //    //alert ('creating SuperHello');
    //    this['superHello'] = fnClass.prototype.hello;
    //}
};

p.instanceOf = function(func) {

    if (this.constructor) {
        if (this.constructor == func) {
            return true;
        } else if (typeof this.constructor.__superclasses__ == "object") {
            for (var i = 0; i < this.constructor.__superclasses__.length; i++) {
                if (this.constructor.__superclasses__[i] == func) {
                    return true;
                }
            }
            return false;
        } else {
            return false;
        }
    }

    
};

p.getSuperInit = function(depth) {
    //var scl = this.constructor.__superclasses__.length;
    if (this.constructor) {
        var scd = this.constructor.__superclasses__[depth];
        if (scd) return (scd.prototype.init);
        return null;
    }
    
};

p.superInit = function(depth, name, controlClass) {
    if (this.getSuperInit) {
        this.tSuperInit = this.getSuperInit(depth);
        if (this.tSuperInit) {
            if (name && controlClass) {
                // for Application
                this.tSuperInit(depth + 1, name, controlClass);
            } else {
                this.tSuperInit(depth + 1);
            }
        }
    }
};

p.getSuperclass = function() {
    if (this.constructor) {
        var scl = this.constructor.__superclasses__.length;
        var idx = (scl - 1);
        var res = this.constructor.__superclasses__[0];
        return res;
    }
    
};

var Class = function(options) {
    this.options = options;
    //this.init();
}
var p = Class.prototype;
//p.init = function(depth) {
//    //alert ('p.init');
//    if (!depth && depth != 0) this.init(0);
//    
//    this.superInit(depth);
//    
//    // perhaps need a function to get the current initMe?
//    
//    this.initMe();
//}

chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
function randomString(length) {
	//var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var res = '';
	for (var i=0; i<length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		res += chars.substring(rnum,rnum+1);
	}
	return res;
}

uniqueGeneratedValues = new Object();
// maybe not such a good idea to get these for callbacks - perhaps a more efficient system would save on memory.
// or just have a counter of numbers.

/// Callback
function Callback(callingObject, functionName) {
    this.callingObject = callingObject;
    this.functionName = functionName;

}

var p = Callback.prototype;
p.call = function(param1, param2 , param3) {
    if ((param1 != null) && (param2 != null) && (param3 != null)) {
        if (this.callingObject[this.functionName]) this.callingObject[this.functionName](param1, param2, param3);
    } else {
        if ((param1 != null) && (param2 != null)) {
            if (this.callingObject[this.functionName]) this.callingObject[this.functionName](param1, param2);
        } else {
            if (param1 != null) {
                if (this.callingObject[this.functionName]) this.callingObject[this.functionName](param1);
            } else {
                if (this.callingObject[this.functionName]) this.callingObject[this.functionName]();
            }
        }
    }
}

EventRaiser = function() {
    this.init();
}
var p = EventRaiser.prototype;
p.init = function() {
    this.listeners = new Object();
}

// each callback has a unique callback ID?
//  may be quite effective - automatically gets generated.

p.addListener = function(listenerCallback, eventName) {
    if (!this.listeners[eventName]) {
        //this.listeners[eventName] = new Array();
    }
}
p.removeListener = function(listenerCallback, eventName) {
    
}
p.raiseEvent = function(eventName) {
    
}

Control = function() {
    this.init(0);
}
var p = Control.prototype;
p.inheritFrom(Class);

p.init = function(depth) {
    this.superInit(depth);
    if (!this.options) this.options = new Object();
    
    
}
p.setFocus = function() {
    this.getEl().setFocus();
}

p.setMarginLeft = function(value) {
    this.getEl().style.marginLeft = value;
}

p.setWidth = function(value) {
    this.getEl().style.width = toPx(value);
}
p.setHeight = function(value) {
    this.getEl().style.height = toPx(value);
}

p.setInnerHTML = function(value) {
    var res = YAHOO.util.Dom.setInnerHTML(this.getEl(), value);
    if (this.bindElEvents) this.bindElEvents();
    return res;
}

p.cRender = function() {
    // conditional render
    if (this.autoRender == true) this.render();
}

p.dispose = function() {
    this.el = this.getEl();

    this.el.onclick = null;
    this.el.control = null;
    
    this.el = null;
    this.div = null;
    this.span = null;
}

p.getEl = function() {
    if (this.el) return this.el;
    if (this.div) return this.div;
    if (this.span) return this.span;
}
p.setWidth = function(value) {
    this.setStyle('width', value + 'px');
}
p.setHeight = function(value) {
    this.setStyle('height', value + 'px');
}
p.setStyle = function(name, value) {
    if (!this.el) this.el = this.getEl();
    
    if (name == 'float') {
        this.setFloat(value);
    } else {
        this.el.style[name] = value;
    }
    
    //this.el.style.setAttribute(name, value);
}
p.setFloat = function(value) {
    this.el.style.cssFloat = value;
    this.el.style.styleFloat = value;
}
p.setClass = function(className) {
    setClass(this.getEl(), className);
}

p.setVisibility = function(visible) {
    if (!this.el) this.el = this.getEl();
    if (visible) {
        
        if (this.cDisplay || this.cDisplay == '') {
            this.el.style.display = this.cDisplay;
        } else {
            this.el.style.display = 'block';
        }
        
    } else {
        if (this.el.style.display != 'none') {
            this.cDisplay = this.el.style.display;
        }
        
        this.el.style.display = 'none';
    }
}
p.show = function() {
    this.setVisibility(true);
}
p.hide = function() {
    this.setVisibility(false);
}

p.createMyEl = function(tagName, className) {
    var e = nE(tagName);
    if (!className && this.className) {
        className = this.className;
    }
    if (className) {
        setClass(e, className);
        this.className = className;
    }
    this.el = e;
    return e;
}

p.createMySpan = function(className) {
    return this.createMyEl('span', className);
}

p.createMyDiv = function(className) {
    return this.createMyEl('div', className);
}
p.getContainerEl = function() {
    // this container may be an HTML element
    
    if (this.container) {
        if (this.container.getEl) return this.container.getEl();
        if (this.container.container) {
            if (this.container.container.getEl) return this.container.container.getEl();
        }
        if(this.container.tagName) {
            return this.container;
        }
    }
    
}
p.addMySpan = function() {
    // or container element.
    
    if (this.container.div) {
        this.container.div.appendChild(this.span);
    }   else {
        if (this.container.container.div) {
            this.container.container.div.appendChild(this.span);
        }
    }
    
}

p.addMyEl = function() {
    this.el = this.getEl();
    if(this.el) {
        this.el.control = this;
        if (this.elBeenAdded != true) {
            if (this.container.addControl) {
                this.container.addControl(this);
            } else {
                if (this.container.appendChild) {
                    this.container.appendChild(this.el);
                } else {
                    if (this.container.getEl) {
                        this.container.getEl().appendChild(this.el);
                    }
                }
            }
            this.elBeenAdded = true;
        }
    }
}

p.addMyDiv = function() {
    // perhaps will only do this at the appropriate time.
    this.addMyEl();
    
}


function isIE() {
    if(document.all) return true;
    return false;
} 

toPx = function(value) {
    if (value.indexOf) {
        if (value.indexOf('px') > -1) {
            return value;
        }
        if (value.indexOf('%') > -1) {
            return value;
        }    
    } else {
        return value + 'px';
    }
}

pxToNum = function(value) {
    var t = value.replace('px', '');
    var res;
    
    res = parseInt(t); // Hopefully will throw errors here.
    return res; 
}

getSiteRootUrl = function() {
    var href = window.location.href;
    //alert('href ' + href);
    var pos1 = href.indexOf('//') + 2;
    //alert('pos1 ' + pos1);
    var pos2 = href.indexOf('/', pos1) + 1;
    //alert('pos2 ' + pos2);
    var res;
    if (pos1 > -1) {
        res = href.substring(pos1, pos2);
    }    
    return res;
}