
/**
 *
 * @desc        TDH Lib
 * @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
 *
 */

if (typeof cloneObj == 'undefined') {
	function cloneObj(obj) {
		for (o in obj) {
			this[o] = obj[o];
		}
	}
}

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;
	}
};

var FX = function() { return true; };
FX.prototype = {
	combo: false,
	timer: null,
	interval: 10,
	tween: 7,
	value: 0,
	sized: false,
	unit: 'px',
	initialize: function(props) {
		this.extend(props || {});
		this.targ = (props.targ) ? $(props.targ) : $(props.trig);
		if (this.init) {
			(this.init.match(/max/i) ? this.setValue(this.max) : this.setValue(this.min));
		} else {
			this.setValue(this.min)
		}
		var obj = this;
		if (props.evt) {
			(new GW.Event()).setEvent(props.trig, props.evt, function(e) {
				obj.toggle();
				return false;
			});
		}
	},
	toggle: function()
	{
		// if (this.sized) return false;
		if (this.timer) return false;
		if (this.onStart) this.onStart();
		if (this.combo) {
			if (this.timers()) return false;
			this.iterate();
		}
		this.sized = (this.sized) ? false : true;
		this.animate();

	},
	animate: function()
	{
		var value = this.getValue();
		if (this.timer) {
			if (value < this.value) {
			   value = Math.ceil(value+((this.value-value)/this.tween));
				this.setValue((value<this.value) ? value : this.value);
			} else if (value > this.value) {
				value = Math.floor(value-((value-this.value)/this.tween));
				this.setValue((value>this.value) ? value : this.value);
			} else {
				this.timer = clearInterval(this.timer);
				return;
			}
		} else {
			var value = this.getValue();
			this.value = (value < this.max) ?
						((this.max-this.min)+value) :
						((0-(this.max-this.min))+value);
			if (this.value < this.min) this.value = this.min;
			this.timer = setInterval(this.animate.bind(this), this.interval);
		}
	}
};

FX.Height = Class.create();
FX.Height.prototype = (new FX()).extend({
	attrib: 'height',
	getValue: function()
	{
		return parseInt(this.targ.style.height);
	},
	setValue: function(value)
	{
		this.targ.style.height = value+this.unit;
	}	
});

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.url = 'http://www.swohiosportszone.com/site/template/';
TDH.self = 'index';

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.Hover = {
    elements: {},
    setHover: function(id, cls)
    {
        cls = cls.toLowerCase();
        this.elements[id] = cls;
        return;
    },
    load: function()
    {
        if (!document.getElementById) return;
        var elements = null;
        for (var e in this.elements) {
            if (typeof e != 'string') continue;
            var element = document.getElementById(e);
            if (!element) continue;
            element.onmouseover = function()
            {
                var tag = this.tagName.toLowerCase();
                this.className += ' '+TDH.Hover.elements[this.id];
            };
            element.onmouseout = function()
            {
                this.className = this.className.replace(TDH.Hover.elements[this.id], '');
            };
        }
    }
};

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);
            this.target = TDH.Params.getParam('src');
            if (!TDH.Params.getParam('image')) {
                this.image = TDH.Params.getParam('src').match(/\.jpg$|\.jpeg$|\.gif$|\.png$/i) ? true : false;
            }
            this.height = (TDH.Params.getParam('height')) ? TDH.Params.getParam('height') : TDH.Window.height;
            this.width = (TDH.Params.getParam('width')) ? TDH.Params.getParam('width') : TDH.Window.width;
            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('image');
        if (!this.image) {
            this.image = TDH.Params.getParam('src').match(/\.jpg$|\.jpeg$|\.gif$|\.png$/i) ? true : false;
        }
        this.modal = parseInt(TDH.Params.getParam('modal')) ? true : false;
        TDH.Window.setCenter();
        TDH.Window.openWindow();
    },
    openWindow: function()
    {
        if (this.image) {
            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;
    }
};

TDH.Event.setEvent('window', 'onload', function()
{
    // Flash Replacement
    if (sIFR) {
	   sIFR.replaceElement(named({sSelector:"h1", sFlashSrc: TDH.url+"swf/rockwell.swf", sColor:"#000000", sWmode:"transparent" }));
	   sIFR.replaceElement(named({sSelector:"#aux-listing h3", sFlashSrc: TDH.url+"swf/rockwell.swf", sColor:"#FFFFFF", sWmode:"transparent" }));
	   sIFR.replaceElement(named({sSelector:"#content h2", sFlashSrc: TDH.url+"swf/andale.swf", sColor:"#8F4297", sWmode:"transparent" }));
	   sIFR.replaceElement(named({sSelector:"#panel h2", sFlashSrc: TDH.url+"swf/andale.swf", sColor:"#FCD107", sWmode:"transparent" }));
    }

	TDH.Window.load();
    
    var flash = '';
    if (flash) {
        TDH.Flash.setObject('flash', 'src='+TDH.url+'swf/sports.swf?mov='+flash+', name=flash, height=325, width=325, quality=high, wmode=transparent');
    }

    if (TDH.self.match(/index/i)) {
        TDH.Flash.setObject('flash', 'src='+TDH.url+'swf/index.swf, name=flash, height=370, width=430, quality=high, wmode=transparent');
    }

    $('search-btn-go').onclick = function()
    {
        $('search-form').submit();
    }

});
