/**
 *
 * @desc        TDH Lib + FX (Opacity)
 * @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 (!Object.clone) {
    Object.prototype.clone = function()
    {
	   var clone = new Object();
	   if (typeof arguments.length != 'undefined') {
	       var args = arguments;
	       for (var t in this) {
	           for (var a = 0, ac = args.length; a < ac; a++) {
	               if (typeof args[a] != 'string') continue;
	               if (t.toLowerCase() != args[a].toLowerCase()) {
	                   clone[t] = this[t];
	               }
	           }
	       }
	   } else {
	       for (var t in this) {
	           clone[t] = this[t];
	       }
	   }
	   return clone;
    };
}

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,
	initialize: function(props) {
		this.extend(props || {});
		this.targ = (props.targ) ? $(props.targ) : $(props.trig);
		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.timer) return false;
	   if (this.combo) {
		    if (this.timers()) return false;
			this.iterate();
	   }
		this.sized = (this.sized) ? false : true;
	    this.targ.style.display = (this.sized) ? 'block' : 'none';
		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.targ.style.display = (this.sized) ? 'block' : 'none';
				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.Opacity = Class.create();
FX.Opacity.prototype = (new FX()).extend({
	attrib: 'opacity',
	initialize: function(props)
	{
		this.extend(props || {});
		this.targ = (props.targ) ? $(props.targ) : $(props.trig);
		this.tween /= .5;
		this.max = ((this.combo) ? 99 : (this.max >= 99 ? 99 : this.max));
		this.min = ((this.combo) ? 0 : (this.min <= 0 ? 0 : this.min));
		this.setValue(this.min);
		var obj = this;
		if (props.evt) {
			(new GW.Event()).setEvent(props.trig, props.evt, function(e) {
	           obj.toggle();
	           return false;
			});
		}
	},
	getValue: function()
	{
		if (window.ActiveXObject) {
			return parseInt(this.targ.style.filter.replace(/\D+/, ''));
		} else {
			return this.targ.style.opacity*100;
		}
	},
	setValue: function(value)
	{
		if (window.ActiveXObject) {
			this.targ.style.filter = "alpha(opacity=" + value + ")";
		} else {
			this.targ.style.opacity = value/100;
		}
	}	
});

FX.Combo = Class.create();
FX.Combo.prototype = {
	initialize: function()
	{
		this.objects = new Array();
        var args = arguments;
		var objects =   (typeof args.length != 'undefined') ?
                        ((typeof args[args.length-1] == 'object') ?
					    args[args.length-1] :
					    null) :
					    null;
		if (!objects) return;
		if (typeof objects.length == 'undefined') objects = [objects];
		for (var o = 0, oc = objects.length; o < oc; o++) {
			objects[o].combo = true;
			var fx = new Array();
			for (var a = 0, ac = args.length-1; a < ac; a++) {
				objects[o].evt = (!a) ? objects[o].evt : null;
				switch (args[a].toLowerCase()) {
					case 'o':
					case 'opacity':
						fx.push(new FX.Opacity(objects[o]));
						break;
					case 'h':
					case 'height':
						fx.push(new FX.Height(objects[o]));
						break;
					case 'w':
					case 'width':
						fx.push(new FX.Width(objects[o]));
						break;
				}
			}
			this.objects.push(fx);
		}

        var clone = this.clone('toggle');
		for (var o = 0, c = this.objects.length; o < c; o++) {
			fx = this.objects[o];
			for (var f = 0, fc = fx.length; f < fc; f++) {
                fx[f].extend(clone);
            }
        }

	},
	iterate: function()
	{
		var fx = this.getThisFX();
		for (var f = 0, fc = fx.length; f < fc; f++) {
			if (fx[f] === this) continue;
			fx[f].sized = (fx[f].sized) ? false : true;
            fx[f].targ.style.display = (fx[f].sized) ? 'block' : 'none';
			fx[f].animate();
		}
        obj = fx;
		for (var o = 0, c = this.objects.length; o < c; o++) {
			if (this.objects[o] === obj) continue;
			fx = this.objects[o];
			for (var f = 0, fc = fx.length; f < fc; f++) {
				if (fx[f].timer) continue;
				if (fx[f].sized) {
					fx[f].sized = false;
					fx[f].animate();
				}
			}
		}
	},
	getThisFX: function()
	{
		for (var o = 0, oc = this.objects.length; o < oc; o++) {
			var fx = this.objects[o];
			for (var f = 0, fc = fx.length; f < fc; f++) {
				if (fx[f] === this) return fx;
			}
		}
		return null;
	},
	timers: function()
	{
		for (var o = 0, c = this.objects.length; o < c; o++) {
			var fx = this.objects[o];
			for (var f = 0, fc = fx.length; f < fc; f++) {
				if (fx[f] === this) continue;
				if (fx[f].timer) return true;
			}
		}
		return false;
	},
    toggle: function()
    {
        if (typeof arguments.length == 'undefined') return;
        var args = arguments;
		for (var o = 0, oc = this.objects.length; o < oc; o++) {
			var fx = this.objects[o];
            for (var f = 0, fc = fx.length; f < fc; f++) {
                fx[f].combo = false;
                for (var a = 0, ac = args.length; a < ac; a++) {
                    if (fx[f].attrib.toLowerCase() == args[a].toLowerCase()) fx[f].toggle();
                }
                fx[f].combo = true;
			}
		}
    }
};

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 = '';

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);
            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;
    }
};

TDH.Fader = {
    fx: null,
    timer: 0,
    interval: 15000,
    index: 0,
    objs: [],
    load: function()
    {
        var faders = document.getElementsByClassName('fader');
        if (typeof faders != 'undefined') {
            var objs = [];
            for (var f = 0, c = faders.length; f < c; f++) {
                this.objs.push({targ: faders[f], max: 100, min: 0, interval: 20});
            }
            this.fx = new FX.Combo('o', this.objs);
            this.fx.objects[this.index][0].toggle();
            this.index++;
            this.timer = setInterval(this.fadeObj.bind(this), this.interval);

        }
    },
    fadeObj: function()
    {
        if (this.index == (this.objs.length)) this.index = 0;
        this.fx.objects[this.index][0].toggle();
        this.index++;
    }
};

TDH.Event.setEvent('window', 'onload', function()
{
    // setup suckerfish
    var nav = document.getElementById('nav');
    if (nav) {
        var nodes = nav.childNodes;
        for (var n = 0, c = nodes.length; n < c; n++) {
           if (nodes[n].tagName &&
               nodes[n].tagName.toUpperCase == 'LI') continue;
               nodes[n].onmouseover = function()
               {
                   this.className = !this.className.match(/hover$/i) ?
                                     this.className+=' hover' :
                                     this.className;
               };
               nodes[n].onmouseout = function()
               {
                   this.className = this.className.replace(/\s*hover/g, '');
               };
        }
        
        // set dd opacity
        var dd = document.getElementsByClassName('nav-dd');
        for (var d = 0, c = dd.length; d < c; d++) {
           dd[d].fx = new FX.Opacity({targ: dd[d], max: 95, min: 95});
           dd[d].fx.toggle();
        }
    }
    
    // Flash Replacement
    if (sIFR) {
	   sIFR.replaceElement(named({sSelector:"div#mid>h1", sFlashSrc: TDH.url+"swf/garamond.swf", sColor:"#003974", sWmode:"transparent" }));
    }

	TDH.Window.load();

    var obj = document.getElementById('flash-img');
    if (!obj || typeof obj == 'undefined') {
        TDH.Flash.setObject('flash-area', 'src='+TDH.url+'swf/header.swf, name=flash_area, height=220, width=499, quality=high, wmode=transparent');
    }

    var obj = document.getElementById('testimonial');
    if (!obj || typeof obj == 'undefined') return;
    TDH.Fader.load();


});