// (c) 2008 walzguitar.com (HMD Change Technology LLC)
//-----------------------------------------------------------------------------
// - this is freeware, as in free beer, just retain this top portion
//   and note any changes in the Additional Notes section, get
//   the most up-to-date/pure version at walzguitar.com
// - this widget provides a simple API to use this widget, the widget
//    will insert style and form tags for a multi site search box
//
// SIMPLE USE------------------------------------------------------------------
//  - copy the 3 lines below and paste where you want the widget to 
// appear, then remove the comment, //, marks
//<script type="text/javascript">
//WG_ss.writeHTML( );
//</script>
//
// ADVANCED USE----------------------------------------------------------------
// - WG_ss is the default variable reference for the widget object 
// - available function list
//    1 WG_ss.setStyleOverride(boolean)  pass true or false, true will 
//       override the automatic style so you set your own styling, ids 
//       below:
//         - wgform = form, wgq = input for query, wgsel = select control
//         - wgb = input button
//    2 WG_ss.setWidth([0-9][0-9]*)  pass a number, the default is 150px, 
//       this number passed defines the pixel width, this doesn't work in 
//       Chrome
//    3 WG_ss.getRawHTML()  gets the raw, unstyled HTML for the widget
//    4 WG_ss.writeHTML()  uses the document.write function to write the 
//       widget HTML to the document
//    5 WG_ss.setElementHTML(id)  uses the innerHTML property of the element 
//       to set the HTML, use after the element has loaded
//
// ADDITIONAL NOTES------------------------------------------------------------
// - 2009/06/11 Did a long needed fix that affected the Fretplay search.  
//              Added a global replacement of spaces to + symbols in search 
//              queries. Fixed some comment typos and added sites and function.
//
//-----------------------------------------------------------------------------
var WG_ss = new WG_songsearch( );
function WG_songsearch( ) {
	var ovr = false;
	var sites = new Array(
		//url|name exclude http:// from url, put TERMS where query should be
		'search.azchords.com/cgi-bin/azseek.cgi?q=TERMS&Search=Go!|AZChords', 
		'chordie.com/index.php?q=TERMS&np=0&ps=10&wf=2221&s=RPD&wf=2221&wm=wrd&type=&sp=1&sy=1&cat=&ul=&np=0|Chordie',
		'fretplay.com/cgi-bin/tabs.cgi?query=TERMS&type=all|FretPlay',
		'tabcrawler.com/search.php?q=TERMS&search=SCAN!|TabCrawler', 
		'ultimate-guitar.com/search.php?s=TERMS&w=bands|Ultimate-Guitar Bands',
		'ultimate-guitar.com/search.php?s=TERMS&w=songs|Ultimate-Guitar Songs'
	);
	var defs = 'search tabs/chords';
	var dWidth = '150';
	var s = '<style type="text/css">#wgform{background-color:#aaf;margin:4px 4px 4px 4px;padding:4px 4px 4px 4px;width:dWidthpx;}#wgq{color:gray;border:1px solid black;font-size:12px;width:146px;}#wgsel{font-size:10px;border:1px solid black;height:17px;width:122px;}#wgb{border:1px solid black;font-size:10px;height:17px;width:22px;}</style>';
	var h = '<form id="wgform"><input id="wgq" type="text" onclick="WG_ss.clearQ();" value="' + defs + '" /><br /><select id="wgsel">';
	for( var i = 0; i < sites.length; i++ ) {
		h += '<option value="' + sites[ i ].split( '|' )[ 0 ] + 
			'">' + sites[ i ].split( '|' )[ 1 ] + '</option>';
	}
	h += '</select> <input id="wgb" type="button" onclick="WG_ss.doSearch();" value="->" /></form>';
	this.setStyleOverride = function( v ) {
		if( v == true || v == false ) {ovr = v;}
		else{alert( "Set override with true or false." );}
	}
	this.setWidth = function( w ) {
		if( w.match( /^[0-9][0-9]*$/ ) ) {dWidth = w;}
		else {alert( "Use only numbers with setWidth()." );}
	}
	this.getRawHTML = function( ) {
		return h;
	}
	this.writeHTML = function( ) {
		var o;
		if( ovr ) {o = h;}
		else {o = s.replace( /dWidth/, dWidth ) + h;}
		document.write( o );
	}
	this.setElementHTML = function( id ) {
		var o;
		if( ovr ) {o = h;}
		else {
			o = s.replace( /dWidth/, dWidth ) + h;
		}
		document.getElementById( id ).innerHTML = o;
	}
	this.clearQ = function( ) {
		document.getElementById( 'wgq' ).value = '';
	}
	this.doSearch = function( ) {
		var u = document.getElementById( 'wgsel' ).value;
		var s = document.getElementById( 'wgq' ).value.replace( / /g, '+' );
		if( s != defs ) {
			document.location.href = 'http://' + u.replace( /TERMS/, s );
		}
	}
}