/**
 *
 * @desc        TDH Library
 * @author      Christopher Sanford
 * @version     1.0.0
 * @copyright   (cc) 2006 Christopher Sanford
 * @url         http://www.tdh-marketing.com/
 * @url         http://www.christophersanford.com/
 * 
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General 
 * Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) 
 * any later version.
 *
 * This library 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 Lesser General Public License for more 
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to 
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

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+/, '');
};

var GW = Class.create();
GW.prototype = {
	initialize: function() { return true; }
};

GW.Event = function() { return true; };
GW.Event.prototype = {
	initialize: function() { return true; },
	setEvent: function(obj, event, func)
	{
		var ref = null;
		if (typeof obj == 'string') {
			try {
				ref = eval(obj) ? obj+'.'+event : '$("'+ obj +'").'+event;
			} catch(e) {
				ref = '$("'+ obj +'").'+event;
			}
		} else if (typeof obj == 'object') {
			if (obj.id) {
				return this.setEvent(obj.id, event, func);
			} else {
				var id = this.getRandomId();
				while ($(id)) id = this.getRandomId();
				obj.id = id;
				return this.setEvent(id, event, func);
			}
		}
		try {
			var event = eval(ref);
			if (typeof event == 'function') {
				ref += ' = function(e) { event(e); func(e); }';
			} else {
				ref += ' = func;';
			}
			ref = eval(ref);
		} catch (e) { return null; }

		return (ref);
	},
	getRandomId: function()
	{
		return 'obj-'+Math.floor(Math.random()*1000);
	},
	getTarget: function(event)
	{
		event = (typeof event == 'undefined') ? window.event : event;
		var target = (event && typeof event.target == 'undefined') ? event.srcElement : event.target;
		if (target && target.nodeType == 3) target = target.parentNode;
		return (target) ? target : null;
	}
};

if (!TDH) var TDH = new Object;

TDH = {
    load: function() {
        TDH.Window.load();
    }
};

TDH.url = '';

TDH.Window = {
    window: null,
    target: '',
    image: false,
    interval: 5,
    timer: 0,
    load: function()
    {
        var elements = document.getElementsByTagName('A');
        for(var e = 0, c = elements.length; e < c; e++) {
            if (elements[e].rel && elements[e].rel == 'external') {
                elements[e].target = 'blank';
            }
        }
        elements = null;
        return;
    },
    setElement: 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.height = (TDH.Params.getParam('height')) ? TDH.Params.getParam('height') : TDH.Window.height;
            TDH.Window.width = (TDH.Params.getParam('width')) ? TDH.Params.getParam('width') : TDH.Window.width;
            TDH.Window.setCenter();
            TDH.Window.openWindow();
            return false;
        });
    },
    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) || typeof params[p] != 'string') 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;
    }
};

(new GW.Event()).setEvent('window', 'onload', function()
{
	TDH.load();
    
    // Flash Replacement
    if (sIFR) {
	  // sIFR.replaceElement(named({sSelector:"div#right>h1", sFlashSrc: TDH.url+"swf/times.swf", sColor:"#000000", sWmode:"transparent", sFlashVars: 'offsetTop=0', nPaddingTop: 0 }));
	   sIFR.replaceElement(named({sSelector:"div#right>h2", sFlashSrc: TDH.url+"swf/times.swf", sColor:"#1587AB", sWmode:"transparent", sFlashVars: 'offsetTop=0', nPaddingTop: 0 }));
	   sIFR.replaceElement(named({sSelector:"div#right>h3", sFlashSrc: TDH.url+"swf/times.swf", sColor:"#1587AB", sWmode:"transparent", sFlashVars: 'offsetTop=0', nPaddingTop: 0 }));
    }

if($('flashheader') !== null) {
	TDH.Flash.setObject('flashpic', 'src='+TDH.url+'swf/picfade.swf, name=flash_header, height=185, width=270, quality=high, wmode=transparent');
	TDH.Flash.setObject('flashheader', 'src='+TDH.url+'swf/header.swf, name=flash_header, height=141, width=767, quality=high, wmode=transparent');
}

});