/*
 * Author: Chris Phillips
 * Date: 3/25/10
 * 
 * Description: 
 * 		This file uses JS arguments passed in via the SRC attribute.
 * 		The idea for this is from this article: http://feather.elektrum.org/book/src.html
 */

var writeButton = function(){
	/* Grab all <script> tags currently in the DOM */
	var scripts = document.getElementsByTagName('script');
	/* Grab a handle on the last one. That will be the one that called me */
	var myScript = scripts[ scripts.length - 1 ];
	
	/* Pull out the domain name */
	var serverName = myScript.src.replace(/http:\/\/([^\?]+?)\/.+/i,'$1');
	/* Pull out the URL query string */
	var queryString = myScript.src.replace(/^[^\?]+\??/,'');
	
	/* Parse the query string into a "params" object with properties */
	var params = parseQuery( queryString );
	
	/* Are they looking for the big or small button? */
	var biglink = (params['biglink'].toLowerCase()=='true')?true:false;
	
	/* Write out the img tag that will be the button */
	document.write('<img class="WideStormFootPrintButton" src="http://' + serverName + '/Images/spacer.gif" onclick="window.open(\'http://' + serverName + '/?fuseaction=Negotiate.Start&VIN=' + params['vin'] + '\',\'NegotiateOnline\',\'scrollbars=yes, width=800, height=600, top=20, left=20\').focus();" style="cursor:pointer" title="Negotiate this vehicle online.">')
	/* Write out the style block that will style the button */
	document.write('<style type="text/css">.WideStormFootPrintButton{width:' + (biglink?'292':'175') + 'px;height:' + (biglink?'79':'47') + 'px;background-image:url(http://' + serverName + '/Images/Negotiate/Negotiate' + (biglink?'_Big':'_Small') + '.gif);}</style>');
	
	/* Funtion to turn query strings into a parameter map */
	function parseQuery ( query ) {
	   var Params = new Object ();
	   if ( ! query ) return Params; // return empty object
	   var Pairs = query.split(/[;&]/);
	   for ( var i = 0; i < Pairs.length; i++ ) {
	      var KeyVal = Pairs[i].split('=');
	      if ( ! KeyVal || KeyVal.length != 2 ) continue;
	      var key = unescape( KeyVal[0] );
	      var val = unescape( KeyVal[1] );
	      val = val.replace(/\+/g, ' ');
	      Params[key.toLowerCase()] = val;
	   }
	   return Params;
	}
}();