String.prototype.trim = function()
{
    return this.replace(/^\s+|\s+$/, '');
};
String.prototype.rtrim = function()
{
    return this.replace(/\s+$/, '');
};
String.prototype.ltrim = function()
{
    return this.replace(/^\s+/, '');
};

if (!TDH)
    var TDH = new Object;

TDH = {
    load: function() {
        TDH.Window.load();
        TDH.Hover.load();
    }
};

TDH.Event = {
    setEvent: function(obj, event, func)
    {
        var event = obj+'.'+event;
        var events = eval(event);
        if (typeof events == 'function') {
            eval(event + ' = function() { events(); func(); }');
        } else {
            eval(event + ' = func;');
        }
        return;
    },
    setEventById: function(id, event, func)
    {
        var ref = '';
        var events = null;
        ref = 'document.getElementById("'+ id +'").' + event;
        events = eval(ref);
        if (typeof events == 'function') {
            ref += ' = function(e) { events(e); func(e); }';
        } else {
            ref += ' = func;';
        }
        ref = eval(ref);
        return;
    },
    getEventTarget: function(event)
    {
        var target = {};
        event = (typeof event == 'undefined') ? window.event : event;
        target = (typeof event.target == 'undefined') ? event.srcElement : event.target;
        if (target.nodeType == 3)
            target = target.parentNode;
        return target;
    }
};

TDH.SwapRestore = {
    elements: {},
    attribs: {},
    init: function()
    {
        return;
    },
    load: function()
    {
        this.init();
        for (var id in this.elements)
        {
            if (typeof this.attribs[id][1] != 'undefined' && this.attribs[id][1] != null) {
                TDH.Event.setEventById(id, 'onmouseover', function (e) {
                    var id = TDH.Event.getEventTarget(e).id;
                    var attrib = TDH.SwapRestore.attribs[id][1];
                    var element = TDH.SwapRestore.elements[id];
                    element.src = attrib.src;
                    element.height = attrib.height;                            
                    element.width = attrib.width;
                });
            }
            if (typeof this.attribs[id][0] != 'undefined' && this.attribs[id][0] != null) {
                TDH.Event.setEventById(id, 'onmouseout', function (e) {
                    var id = TDH.Event.getEventTarget(e).id;
                    var attrib = TDH.SwapRestore.attribs[id][0];
                    var element = TDH.SwapRestore.elements[id];
                    element.src = attrib.src;
                    element.height = attrib.height;                            
                    element.width = attrib.width;
                });
            }
        }
    },
    setImage: function(id, params)
    {
        var element = {};
        if (document.getElementById) {
            TDH.Params.setParams(params);
            element = document.getElementById(id);
            if (element.tagName && element.tagName.match(/img/i)) {
                if ( typeof this.elements[id] == 'undefined') {
                    this.elements[id] = {};
                    this.elements[id] = document.getElementById(TDH.Params.getParam('id'));
                }
                if (typeof this.attribs[id] == 'undefined') {
                    this.attribs[id] = [null,null,null];
                    this.attribs[id][1] = {src: TDH.Params.getParam('src'), height: TDH.Params.getParam('height'), width: TDH.Params.getParam('width')};
                    this.attribs[id][2] = new Image();
                    this.attribs[id][2].src = TDH.Params.getParam('src');
                } else {
                    this.attribs[id][0] = {src: TDH.Params.getParam('src'), height: TDH.Params.getParam('height'), width: TDH.Params.getParam('width')};
                }
            }
        }
    }
};

TDH.Hover = {
    elements: {tag: {}, id: {}},
    setElementByTagName: function(tag, type, cls)
    {
        tag = tag.toLowerCase();
        type = type != null ? type.toLowerCase() : type;
        cls = cls.toLowerCase();
        if (typeof this.elements['tag'][tag] == 'undefined') {
            if (type != null) {
                this.elements['tag'][tag] = {};
                this.elements['tag'][tag][type] = cls;
            } else {
                this.elements['tag'][tag] = cls;
            }
        }
    },
    setElementById: function(id, cls)
    {
        cls = cls.toLowerCase();
        this.elements['id'][id] = cls;
        return;
    },
    load: function()
    {
        var elements = document.getElementsByTagName('*');
        for(var e = 0, c = elements.length; e < c; e++) {
            var element = elements[e].tagName.toLowerCase();
            if (typeof this.elements['tag'][element] != 'undefined') {
                if (typeof this.elements['tag'][element] == 'object') {
                    for( var type in this.elements['tag'][element]) {
                        if (type == elements[e].type.toLowerCase()) {
                            elements[e].onmouseover = function()
                            {
                                var tag = this.tagName.toLowerCase();
                                var type = this.type.toLowerCase();
                                this.className += ' '+TDH.Hover.elements['tag'][tag][type];
                                tag = null;
                                type = null;
                            }
                            elements[e].onmouseout = function()
                            {
                                var tag = this.tagName.toLowerCase();
                                var type = this.type.toLowerCase();
                                this.className = this.className.replace(TDH.Hover.elements['tag'][tag][type], '');
                                tag = null;
                                type = null;
                            }
                        }
                    }
                } else {
                    elements[e].onmouseover = function()
                    {
                        var tag = this.tagName.toLowerCase();
//                        var type = this.type.toLowerCase();
                        this.className += ' '+TDH.Hover.elements['tag'][tag];
                        tag = null;
                        type = null;
                    }
                    elements[e].onmouseout = function()
                    {
                        var tag = this.tagName.toLowerCase();
  //                      var type = this.type.toLowerCase();
                        this.className = this.className.replace(TDH.Hover.elements['tag'][tag], '');
                        tag = null;
                        type = null;
                    }
                }
            }
        }
        elements = null;
        if (document.getElementById) {
            for( var e in this.elements['id'] ) {
                var element = document.getElementById(e);
                element.onmouseover = function()
                {
                    var tag = this.tagName.toLowerCase();
                    var type = this.type.toLowerCase();
                    this.className += ' '+TDH.Hover.elements['id'][this.id];
                    tag = null;
                    type = null;
                }
                element.onmouseout = function()
                {
                    var tag = this.tagName.toLowerCase();
                    var type = this.type.toLowerCase();
                    this.className = this.className.replace(TDH.Hover.elements['id'][this.id], '');
                    tag = null;
                    type = null;
                }
            }
        }
    }
},

TDH.Window = {
    win: null,
    height: 0,
    width: 0,
    target: '',
    modal: false,
    image: false,
    interval: 5,
    timer: 0,
    setWinByElement: function(id, params)
    {
        TDH.Event.setEventById(id, 'onclick', function (e) {
            TDH.Params.setParams(params);
            TDH.Window.target = TDH.Params.getParam('src');
            TDH.Window.image = TDH.Params.getParam('src').match(/\.jpg$|\.jpeg$|\.gif$|\.png$/i) ? true : false;
            TDH.Window.modal = (TDH.Params.getParam('modal')) ? true : false;
            TDH.Window.setCenter();
            TDH.Window.openWindow();
            return false;
        });
    },
    popupWindow: function(params)
    {
        TDH.Params.setParams(params);
        this.target = TDH.Params.getParam('src');
        this.image = TDH.Params.getParam('src').match(/\.jpg$|\.jpeg$|\.gif$|\.png$/i) ? true : false;
        this.modal = (TDH.Params.getParam('modal')) ? true : false;
        TDH.Window.setCenter();
        TDH.Window.openWindow();
    },
    openWindow: function()
    {
        if (this.image === true) {
            if (this.window != null && !this.window.closed) {
                if (this.window.document.images && this.window.document.images.length) {
                    var image = this.window.document.images[0];
                    image.src = this.target;
                    image.height = TDH.Params.getParam('height');
                    image.width = TDH.Params.getParam('width');
                    this.timer = setInterval('TDH.Window.sizeWindow()', 200);
                } else {
                    this.window.close();
                    this.window = window.open('', 'blank', TDH.Params.getParams(true));
                    this.timer = setInterval('TDH.Window.writeWindow()', 200);
                }                 
            } else {
                this.window = window.open('', 'blank', TDH.Params.getParams(true));
                this.timer = setInterval('TDH.Window.writeWindow()', 200);
                this.centerWindow();
            }
        } else {
            if (this.window != null && !this.window.closed) {
                 this.window.location = this.target;
                 this.window.resizeTo(TDH.Params.getParam('width'), TDH.Params.getParam('height'));
            } else {
                this.window = window.open(this.target, 'blank', TDH.Params.getParams(true));
                this.centerWindow();
            }
        }
        this.window.focus();
        return;
    },
    writeWindow: function()
    {
        try
        {
            var html = '';
            this.window.document.open();
            html += '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">';
            html += '<html>';
            html += '<head>';
            html += '<title></title>';
            html += '<style type="text/css">';
            html += 'html, body { margin: 0; padding: 0; }';
            html += '</style>';
            html += '</head>';
            html += '<body>';
            html += '<img src="'+this.target+'" height="'+TDH.Params.getParam('height')+'" width="'+TDH.Params.getParam('width')+'" />';
            html += '</body>';
            html += '</html>';
            this.window.document.write(html);
            this.window.document.close();
            clearInterval(this.timer);
            this.timer = setInterval('TDH.Window.sizeWindow()', 200);
            html = null;
            return;
        }
        catch(e) { return false; }
    },
    sizeWindow: function()
    {
        try
        {
            var div = {};
            var width = TDH.Params.getParam('width');
            var height = TDH.Params.getParam('height');
            div = this.window.document.createElement('div');
            div.appendChild(this.window.document.createTextNode(' '));
            div.style.position = 'absolute';
            div.style.width = '0px';
            div.style.height = '0px';
            div.style.right = '0px';
            div.style.bottom = '0px';
            this.window.document.body.appendChild(div);
            width -= parseInt(div.offsetLeft);
            height -= parseInt(div.offsetTop);
            this.window.resizeBy(width,height);
            this.window.document.body.removeChild(div);
            this.timer = clearInterval(this.timer);
            this.window.focus();
            div = null;
            width = null;
            height = null;
            return;
        }
        catch(e) { return false; }
    },
    setCenter: function()
    {
        TDH.Params.setParam('left', ((screen.width-TDH.Params.getParam('width'))/2));
        TDH.Params.setParam('top', ((screen.height-TDH.Params.getParam('height'))/2));
        return;
    },
    centerWindow: function()
    {
        this.setCenter();
        this.window.moveTo(TDH.Params.getParam('left'), TDH.Params.getParam('top'));
        return;
    },
    focusWindow: function()
    {
        if (this.window.closed || this.window == null) {
            clearInterval(this.timer);
            this.timer = null;
        } else {
            this.window.focus();
        }
        return;
    }
};


TDH.Params = {
    params: {},
    setParams: function(params)
    {
        this.params = {};
        var temp = [];
        temp = params.split(/\s*,\s*/);
        for (var t = 0, c = temp.length; t < c; t++) {
            temp[t] = temp[t].split(/\s*=\s*/);
            if (temp[t].length == 2) {
                temp[t][0] = temp[t][0].trim();
                temp[t][1] = temp[t][1].trim();
                temp[t][0] = temp[t][0].toString().toLowerCase();
                this.params[temp[t][0]] = temp[t][1];
            } else if (temp[t].length > 2) {
                var key = temp[t].shift();
                key = key.trim();
                temp[t] = temp[t].join('=');
                this.params[key] = temp[t];
            }
        }
        temp = null;
    },
    setParam: function(key, value)
    {
        this.params[key] = value;
        return;
    },
    getParams: function(format)
    {
        if (typeof format != 'undefined' && format === true) {
            var params = [];
            for (var p in this.params) {
                if (params.push) {
                    params.push(p+"="+this.params[p]);
                } else {
                    params[params.length] = p+"="+this.params[p];
                }
            }
            params = params.join(', ');
            return params;
        } else {
            return this.params;
        }
    },
    getParam: function(key)
    {
        return (typeof this.params[key] != 'undefined') ? this.params[key] : null;
    }
};

TDH.Flash = {

    setObject: function(element, params)
    {
        if (document.getElementById) {
            var flash = '';
            element = document.getElementById(element);
            TDH.Params.setParams(params);
            params = TDH.Params.getParams();
            params['movie'] = params['src'];
            flash += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+ params['width'] +'" height="'+ params['height'] +'">';
            for ( var p in params) { if (p.match(/name|swliveconnect/i)) continue; flash += '<param name="'+ p +'" value="'+ params[p] +'" />'; }
            flash += '<embed type="application/x-shockwave-flash" src="'+ params['src'] +'" width="'+ params['width'] +'" height="'+ params['height'] +'" name="'+ params['name'] +'" wmode="'+ params['wmode'] +'"';
            flash += '></embed>';
            flash += '</object>';
            element.innerHTML = flash;
            flash = null;
        }
        return;
    }
};

var externalLinks = function() {
	
	if (!document.getElementsByTagName) return;

	var anchors = document.getElementsByTagName("a");
	
	for (var i=0; i<anchors.length; i++) {
		
		var anchor = anchors[i];
		
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			
		anchor.target = "_blank";
	}

	var maparea = document.getElementsByTagName("area");
	
	for (var e=0; e<maparea.length; e++) {
		
		var maparea = maparea[e];
		
		if (maparea.getAttribute("href") && maparea.getAttribute("rel") == "external")
			
		maparea.target = "_blank";
	}

}

var sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp("\s*sfhover"), "");
		}
	}
}

TDH.Event.setEvent('window', 'onload', function()
{
    externalLinks();
    sfHover();
});
