//------------------------------------------
// Global Search Box - Carrie FF
// � 2007 Shawn Dean - All Rights Reserved
// Author:  Shawn Dean
// Website: dscripting.com
// E-Mail:  deaner225@gmail.com
//------------------------------------------

var GSearch =
{
	'current'   : null,
	'defopt'    : 'web',
	'query'     : null,
	'button'    : null,
	'webnewwin' : true,		// Change this to true if you wish to open google searches in a new window
	'opts'      : new Array(),

	'init' : function(c)
	{
		this.opts   = new Array();
		this.query  = my_getbyid('global-search-query');
		this.button = my_getbyid('global-search-button');

		this.add_event(this.button, 'click'  , GSearch.search);
		this.add_event(this.query , 'keydown', GSearch.keyer);
		this.add_event(this.query , 'focus'  , GSearch.focus);
		this.add_event(this.query , 'blur'   , GSearch.blur);

		var o = my_getbyid('global-search-opts').getElementsByTagName('span');
		if (o.length)
		{
			for (var i=0; i<o.length; i++)
			{
				if (o[i].id.indexOf('global-search:') > -1 && this.css.has(o[i], 'search-option'))
				{
					var x = o[i].id.replace('global-search:', '').toLowerCase();
					this.opts[x] = o[i];
				}
			}
		}

		for (var x in this.opts)
		{
			this.add_event(this.opts[x], 'click', GSearch.select_opt);
		}

		if (c !== null && c != '' && typeof(c) !=' undefined' && this.opts[c])
		{
			this.select_opt(this.opts[c]);
		}
		else if (this.opts[this.defopt])
		{
			this.select_opt(this.opts[this.defopt]);
		}
	},

	'select_opt' : function(e)
	{
		if (!e && window.event)
		{
			e = window.event;
		}

		var z = '';
		var s = (window.event) ? window.event.srcElement : e.target;
		var t = this;

		if (is_ie)
		{
			t = s;
		}

		if (typeof(e) == 'object')
		{
			try
			{
				if (e.tagName.toLowerCase() == 'span')
				{
					if (e.id != '' && e.id.indexOf('global-search:') > -1)
					{
						z = e.id.replace('global-search:', '').toLowerCase();
					}
				}
				else if (t.id.indexOf('global-search:') > -1)
				{
					z = t.id.replace('global-search:', '').toLowerCase();
				}
			}

			catch(me)
			{
				if (t.id.indexOf('global-search:') > -1)
				{
					z = t.id.replace('global-search:', '').toLowerCase();
				}
			}
		}
		else if (typeof(e) == 'string' && e != '')
		{
			z = e.toLowerCase();
		}
		else
		{
			try
			{
				if (t.id.indexOf('global-search:') > -1)
				{
					z = t.id.replace('global-search:', '').toLowerCase();
				}
			}

			catch(me)
			{
				return false;
			}
		}

		if (z == '' || z == GSearch.current || !GSearch.opts[z])
		{
			return false;
		}

		for (var x in GSearch.opts)
		{
			GSearch.css.remove(GSearch.opts[x], 'current');
		}

		GSearch.current = z;
		GSearch.css.add(GSearch.opts[z], 'current');

		var a = my_getbyid('global-search:gallery-opts');
		var b = my_getbyid('global-search:members-opts');
		var c = my_getbyid('global-search:forums-opts');

		a.style.display = (z == 'gallery') ? 'block' : 'none';
		b.style.display = (z == 'members') ? 'block' : 'none';
		c.style.display = (z == 'forums')  ? 'block' : 'none';

		if (z == 'web')
		{
			GSearch.css.add(GSearch.query, 'google');
			GSearch.query.setAttribute('size', 37);

			if (GSearch.query.value == '')
			{
				GSearch.query.value = 'Search by Google';
			}
		}
		else
		{
			GSearch.css.remove(GSearch.query, 'google');
			GSearch.query.setAttribute('size', 40);

			if (GSearch.query.value.toLowerCase() == 'search by google')
			{
				GSearch.query.value = '';
			}
		}

		return false;
	},

	'search' : function(e)
	{
		var q = GSearch.query.value.trim();
		var c = GSearch.current.toLowerCase();

		if (q.toLowerCase().trim() == 'search by google')
		{
			q = '';
		}

		if (c == '' || q == '')
		{
			return false;
		}

		q = escape(q);
		switch (c)
		{
			case 'web':
				var u = 'http://www.carriesearch.com/results.php?domains=www.carriefairfield.com&q='+q+'&sa=Google+Search&sitesearch=&client=pub-9639823196150305&forid=1&ie=ISO-8859-1&oe=ISO-8859-1&cof=GALT%3A%239933FF%3BGL%3A1%3BDIV%3A%23FF99FF%3BVLC%3A663399%3BAH%3Acenter%3BBGC%3AFFFFFF%3BLBGC%3AFFFFFF%3BALC%3A0000FF%3BLC%3A0000FF%3BT%3A000000%3BGFNT%3A0000FF%3BGIMP%3A0000FF%3BLH%3A50%3BLW%3A138%3BL%3Ahttp%3A%2F%2Fwww.carriefairfield.com%2Fimages%2Fcarrie_search.png%3BS%3Ahttp%3A%2F%2Fwww.carriesearch.com%3BFORID%3A11&hl=en';
				if (GSearch.webnewwin === true)
				{
					window.open(u, '_blank');
				}
				else
				{
					ipsclass.location_jump(u, true);
				}

				break;
			case 'forums':
				ipsclass.location_jump('act=Search&CODE=01&forums=all&keywords='+q);
				break;
			case 'gallery':
				var x = '';
				var y = my_getbyid('global-search:gallery-opts').getElementsByTagName('input');

				for (var i=0; i<y.length; i++)
				{
					if (y[i].id.indexOf('global-search:gallery-opts:') > -1 && y[i].checked)
					{
						x = y[i].id.replace('global-search:gallery-opts:', '').toLowerCase();
					}
				}

				if (x == '')
				{
					x = 'caption';
				}

				ipsclass.location_jump('automodule=gallery&req=search&search_where=all&search_in='+x+'&search_for='+q);
				break;
			case 'blogs':
				ipsclass.location_jump('automodule=blog&req=search&blogid=all&keywords='+q);
				break;
			case 'members':
				var x = '';
				var y = my_getbyid('global-search:members-opts').getElementsByTagName('input');

				for (var i=0; i<y.length; i++)
				{
					if (y[i].id.indexOf('global-search:members-opts:') > -1 && y[i].checked)
					{
						x = y[i].id.replace('global-search:members-opts:', '').toLowerCase();
					}
				}

				if (x == '')
				{
					x = 'contains';
				}

				ipsclass.location_jump('act=members&name_box='+x+'&name='+q);
				break;
			defopt:
				return false;
		}

		return false;
	},

	'focus' : function(e)
	{
		if (!e && window.event)
		{
			e = window.event;
		}

		var t = (window.event) ? window.event.srcElement : this;
		if (t.value.toLowerCase() == 'search by google')
		{
			t.value = '';
		}

		t.select();
	},

	'blur' : function(e)
	{
		if (!e && window.event)
		{
			e = window.event;
		}

		var t = (window.event) ? window.event.srcElement : this;
		if (GSearch.current == 'web' && t.value == '')
		{
			t.value = 'Search by Google';
		}
	},

	'keyer' : function(e)
	{
		if (!e && window.event)
		{
	    	e = window.event;
		}

		if (e && e.keyCode)
		{
			if (e.keyCode == 13)
			{
				GSearch.search(e);
				return false;
			}
		}

		return true;
	},

	'add_event' : function(o, t, f, c)
	{
		if (typeof(c) == 'undefined' || c == null && c != true)
		{
			c = false;
		}

		if (o.addEventListener)
		{
			o.addEventListener(t, f, c);
			return true;
		}
		else if (o.attachEvent)
		{
			o.attachEvent('on'+t, f);
			return true;
		}

		return false;
	},

	'css' :
	{
		'add' : function(o, c)
		{
			if (typeof(o) != 'object' || o == null || typeof(o.className) == 'undefined')
			{
				return false;
			}

			if (o.className == null || o.className == '')
			{ 
				o.className = c; 
				return true; 
			}

			if (this.has(o, c))
			{
				return true;
			}

			o.className = o.className+' '+c;
			return true;
		},

		'remove' : function(o, c)
		{
			if (typeof(o) != 'object' || o == null || typeof(o.className) == 'undefined' || o.className == null)
			{
				return false;
			}

			if (!this.has(o, c))
			{
				return false;
			}

			var e       = new RegExp("(^|\\s+)"+c+"(\\s+|$)");
			o.className = o.className.replace(e, ' ');

			return true;
		},

		'has' : function(o, c)
		{
			if (typeof(o) == 'undefined' || o == null || !RegExp)
			{
				return false;
			}

			var e = new RegExp("(^|\\s)"+c+"(\\s|$)");
			if (typeof(o) == 'string')
			{
				return e.test(o);
			}
			else if (typeof(o) == 'object' && o.className)
			{
				return e.test(o.className);
			}

			return false;
		}
	}
}

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g, '');
}