/* Using abbreviation YG for all YG content related*/
if (typeof YG == "undefined")
{
	var YG = {};
}

YG.SearchBox = function()
{
	this.Type = null,
	this.BaseURL = 'http://aoc.yg.com/',
	this.SearchPage = 'search',
	this.OpenNewWindow = false,
	this.UseJavaScript = false,
	this.Styles = {
		SearchWrapper: 'yg_custom_search',
		SearchInput: '',
		SearchButton: 'yg_custom_button',
		SearchButtonText: 'Search'
	}
}

YG.SearchBox.prototype =
{
	Get: function()
	{
		var StyleSheet = 'http://aoc.yg.com/tools/css/yg_searchbox.css';
		var StylePrefixes = {1 : 'yg_aoc_search_120', 2 : 'yg_aoc_search_160', 3: 'yg_aoc_search_480', 4: 'yg_aoc_search_189'};
		var Custom = this.Type ? false : true;
		var FormDefaults = Custom ? {Style: 'yg_custom_search_form'} : null;
		var Style = StylePrefixes[this.Type] ? 'yg_aoc_search ' + StylePrefixes[this.Type] : 'yg_aoc_search ' + StylePrefixes[1];
		var Language =  this.Lang ? this.Lang + '/' : '';		
		var Action = '', InputId = 'yg_aoc_search_' + Math.floor(Math.random() * 123456789);
		var WrapperStyle = this.Styles.SearchWrapper ? this.Styles.SearchWrapper : 'yg_custom_search';
		var InputStyle = this.Styles.SearchInput ? this.Styles.SearchInput : '';
		var ButtonStyle = this.Styles.SearchButton ? this.Styles.SearchButton : 'yg_custom_button';
		var ButtonText = this.Styles.SearchButtonText ? this.Styles.SearchButtonText : '';

		document.write('<link rel="stylesheet" type="text/css" media="all" href="' + StyleSheet + '">');
		document.write('<div class="' + (Custom ? WrapperStyle : Style) + '"><a href="' + this.BaseURL + Language + '" title="Age of Conan Database"><div id="yg_aoc_link"></div></a>');

		if(this.OpenNewWindow)
			Action = 'target="_blank"';

		if(this.UseJavaScript)
		{
			var SearchPage = this.SearchPage == 'search' ? 'search?s=' : this.SearchPage + '?fl&s=';
			if(this.OpenNewWindow)
				Action = "window.open('" + this.BaseURL + Language + SearchPage + "'+document.getElementById('" + InputId + "').value);";
			else
				Action = "window.location.href='" + this.BaseURL + Language + SearchPage + "'+document.getElementById('" + InputId + "').value;"

			document.write('<input type="text" name="s" title="Age of Conan Search" id="' + InputId + '" '+(Custom ? 'class="'+InputStyle+'"' : 'class="yg_aoc_search"')+'" maxlength="256" onkeydown="var e = e || event;if((e.keyCode||e.which)==13){ if(this.value == \'\') return false; else ' + Action + '}"/>');
			document.write('<button type="submit" onclick="if(document.getElementById(\'' + InputId + '\').value == \'\') return false; else ' + Action + '" id="yg_aoc_search_submit" class="'+(Custom ? ButtonStyle : 'yg_aoc_search_submit')+'">'+(Custom ? ButtonText : '')+'</button></div>');
		}
		else
		{
			document.write('<form ' + Action +' method="GET" action="' + this.BaseURL + Language + this.SearchPage + '" onsubmit="if(document.getElementById(\'' + InputId + '\').value == \'\') return false;" class="' + (Custom ? FormDefaults.Style : 'yg_aoc_search_form' )+ '">'+(this.SearchPage != 'search' ? '<input name="fl" value="" type="hidden">' : '')+'<input name="s" maxlength="256" title="Age of Conan Search" id="' + InputId + '"'+(Custom ? (InputStyle ? 'class="'+InputStyle+'"' : '') : 'class="yg_aoc_search"')+'"><button type="submit" id="yg_aoc_search_submit" class="'+(Custom ? ButtonStyle : 'yg_aoc_search_submit')+'">'+(Custom ? ButtonText : '')+'</button></form></div>');
		}
	}
}

YG.Search = new YG.SearchBox();
