/* RAC ROUTE PLANNER WIDGET   */
/* VERSION : 2.0                   */
/* MODIFIED : 11/01/2011           */
/* FEATURES:                       */
/* 1. Works with BING api          */
/* 2. Legacy support for v1 widget */
/***********************************/

/* LEGACY SUPPORT CODE START */
var legacy = false;

if (typeof(racWidth) !== "undefined") {
	racW = racWidth;
	legacy = true;
}
if (typeof(racAddress) !== "undefined") {
	racAD = racAddress;
	// Format address so any part of the address seperated by '**' or , is now seperated by a new line
	racAD = racAD.replace(/\s*(\*\*|,)\s*/gi, '\n');
	// Convert all new lines into a ', '
	racAD = racAD.replace(/ *(\r|\n)+ */gi, ', ');
	// Check if the address has a trailing ',' if it does remove it
	if (racAD.lastIndexOf(', ') === (racAD.length - 2)) {
		racAD = racAD.substr(0, racAD.lastIndexOf(', '));
	}
	legacy = true;
}
if (typeof(racPostcode) !== "undefined") { 
	racPC = racPostcode;
	legacy = true;
}
/* LEGACY SUPPORT CODE END */
// Test the widget sizes and set to the nearest reasonable widget size
// This also means any legacy code gets the correct widget type
if (racW <= 300) {
	// Set the widget type to skyscraper
	racT = 0;
	// set minimum size for the widget
	if (racW < 165) {
		racW = 165;
	}
} else {
	// Set the widget type to landscape
	racT = 1;
	// set maximum size for the widget
	if (racW > 700) {
		racW = 700;
	}
}

var racRoot = "http://www.rac.co.uk/library/images/route-planner/widget/"; // needs to be a link to live as this is what a client will get when they embed the widget on their site

// The RAC Bing map key
var racKEY = "Atx2NeIW_WlLIH9ak4X0ZvY-ZJc56u1f2dbdMJlNYPFrz8NhdlTxT3IMnCZutcC5";
var racDW = function (s) {
	document.write(s);
};

// Retreive the address and postcode
racAD = unescape(racAD);
racPC = unescape(racPC).toUpperCase().replace(/\s*/gi,'');
var racSearchAD = racPC;

// if the address is not blank then format for the search string and for display on the widget
// else just display the postcode that was supplied
if (racAD !== '') {
	racSearchAD = racAD + ', ' + racPC;
	racAD = racAD.replace(/, /gi, ',<br />');
	racAD = racAD + ',<br />' + racPC;
} else {
	racAD = racPC;
}

// Work out the widths for the map content and input box
// depending if landscape or skyscraper
// Also set the widget background to be used
var racLogo, racMapWidth, racInputWidth;
var racContentAreaWidth = racW - (12 + 22 + 5); // width minus all the margins and padding
if (racT === 0) {
	racLogo = 'logo-sm.png';
	racMapWidth = racContentAreaWidth;
	racInputWidth = racContentAreaWidth - 16; // minus the padding on the input
} else {
	racLogo = 'logo.png';
	racMapWidth = Math.round((racContentAreaWidth - 12) / 2); // minus the gap required then devide by 2
	racInputAreaWidth = racMapWidth;
	racInputWidth = racInputAreaWidth - (36 + 10 + 16); // minus the button, margin and input padding
}
racW = racW - 12; // take off the margin used to add the left hand side of the widget to provide the correct width

/* set the map url depending on if geocoords are available or not */
/* this is for legacy support of v1 */
if (typeof(racCOORDS) !== "undefined") {
	// URL where coordinates are available, this allows for a custom icon to be used on the map
	var racMapUrl = 'http://dev.virtualearth.net/REST/v1/Imagery/Map/Road/' + racCOORDS + '/15?mapSize=' + racMapWidth + ',147&pp=' + racCOORDS + ';24&mapVersion=v1&key=' + racKEY;
} else {
	// URL for V1 support as it only uses the postcode to plot the icon
	var racMapUrl = 'http://dev.virtualearth.net/REST/V1/Imagery/Map/Road/' + racPC + '/15?mapSize=' + racMapWidth + ',147&mapVersion=v1&key=' + racKEY;
}

racDW('<style type="text/css">');
	// Specify the styles that are different between the landscape and skyscraper widgets
	if (racT === 0) {
		// Skyscraper styles
		racDW('#rac-tnt-w-content,  #rac-tnt-w-t, #rac-tnt-w-footer, #rac-tnt-w-footer-t, #rac-tnt-w-b,  #rac-tnt-w-b div {background:transparent url(' + racRoot + 'bg-small.png) no-repeat top right; _background:transparent url(' + racRoot + 'bg-small.gif) no-repeat top right}');
		racDW('#rac-tnt-w-mast {padding:0;}');
			racDW('#rac-tnt-w-map { float:none; }');
			racDW('#rac-tnt-w-strap {color:#72635a; clear:both; padding:17px 0 6px 0; font-size:15px; font-weight:bold;}');
		racDW('#rac-tnt-w-hero {color:#ff5f01; font-size:17px; font-weight:bold; padding:24px 0 24px 2px;}');
		racDW('.rac-tnt-w-form { padding-top: 10px; }');
			racDW('#rac-w-form-btn { margin-top: 10px; }');
		racDW('.rac-tnt-w-text {background:url(' + racRoot + 'bg-stripes-small.gif) repeat-y 0 0; padding:0 0 0 25px;}');
		racDW('#rac-tnt-w-footer { height:50px; }');
		racDW('#rac-tnt-w-footer-t { height:60px; }');
		racDW('#rac-tnt-w-footer p { float:right; margin:0; margin-bottom: 4px; }');
		racDW('#rac-tnt-w-footer a { float:right; clear:right; }');
	} else {	
		// Landscape styles
		racDW('#rac-tnt-w-content,  #rac-tnt-w-t, #rac-tnt-w-footer, #rac-tnt-w-footer-t, #rac-tnt-w-b div {background:transparent url(' + racRoot + 'bg.png) no-repeat top right; _background:transparent url(' + racRoot + 'bg.gif) no-repeat top right}');
		racDW('#rac-tnt-w-mast {padding:0; height:80px;}');
			racDW('#rac-tnt-w-map { float:left; }');
			racDW('#rac-tnt-w-strap {color:#72635a; float:right; padding:53px 0 0 0; text-align:right; font-size:20px;}');
		racDW('#rac-tnt-w-hero {color:#ff5f01; font-size:26px; padding:18px 0 18px 3px;}');
		racDW('.rac-tnt-w-form { float:right; width: ' + racInputAreaWidth + 'px; }');
			racDW('#rac-w-form-pc { float:left; margin-right: 10px; }');
			racDW('#rac-w-form-btn { float:left; margin-top: 1px; }');
		racDW('.rac-tnt-w-text {background:url(' + racRoot + 'bg-stripes.gif) repeat-y 0 0; padding:0 0 0 50px;}');
		racDW('#rac-tnt-w-footer { height:35px; }');
		racDW('#rac-tnt-w-footer-t { height:45px; }');
		racDW('#rac-tnt-w-footer p { float:left; margin:0; }');
		racDW('#rac-tnt-w-footer a { float:right; }');
	}
	
	// Specify standard styles
	racDW('#rac-tnt-widget {font:12px arial,sans-serif; position:relative; margin:0px auto; width:' + racW + 'px; z-index:1; margin-left:12px; margin-bottom:0.5em;}');
	racDW('#rac-tnt-w-content {position:relative; zoom:1; _overflow-y:hidden; padding:0px 22px 0px 0px;}');
	racDW('#rac-tnt-w-t {position:absolute; left:0px; top:0px; width:12px; margin-left:-12px; height:100%; _height:1600px; background-position:top left;}');
	racDW('#rac-tnt-w-mast {padding: 0 0 0 1px;}');
		racDW('#rac-tnt-w-logo, #rac-tnt-w-copy {float:left;}');
	racDW('#rac-tnt-w-mid {clear:both; padding:10px 0 10px 5px; color:#fff;}');
		racDW('#rac-w-form-pc { border: 0; color: #75675E; font-size: 12px; padding: 3px 8px 4px; width:' + racInputWidth + 'px; -o-box-shadow: 0px 1px 5px rgba(0, 0, 0, 0.4); -moz-box-shadow: 0px 1px 5px rgba(0, 0, 0, 0.4); -webkit-box-shadow: 0px 1px 5px rgba(0, 0, 0, 0.4); box-shadow: 0px 1px 5px rgba(0, 0, 0, 0.4); -moz-border-radius: 4px; -webkit-border-radius: 4px; border-radius: 4px; }');
	racDW('#rac-tnt-w-footer {color:#76685f; font-size:11px; position:relative; padding:10px 22px 0px 0px; margin-left:12px; background-position:100% 100%; z-index:-100}');
		racDW('#rac-tnt-w-footer-t { position:absolute; left:0px; top:0px; width:24px; margin-left:-24px; background-position:0% 100%}');
		racDW('#rac-tnt-w-footer p { color:#ABAAA9; line-height:1em; }');
		racDW('#rac-tnt-w-footer a { color:#ABAAA9; line-height:1em; text-decoration:none; }');
		racDW('#rac-tnt-w-footer p a { float:none; }');
		racDW('#rac-tnt-w-footer a:hover, #rac-tnt-w-footer a:focus { text-decoration:underline; }');
	racDW('.rac-clr {clear:both; font-size:1px; line-height:1px; height:1px; }');
racDW('</style>');

// Widget Begins
racDW('<div id="rac-tnt-widget">');
	racDW('<div id="rac-tnt-w-content">');
		racDW('<div id="rac-tnt-w-t"></div>');
		racDW('<div id="rac-tnt-w-mast">');
			racDW('<div id="rac-tnt-w-logo">');
				racDW('<a href="http://www.rac.co.uk" target="_blank"><img src="' + racRoot + racLogo + '" alt="RAC - the driving people" border="0" /></a>');
			racDW('</div>');
			racDW('<div id="rac-tnt-w-strap">Route Planner</div>');
		racDW('</div>');
		racDW('<div id="rac-tnt-w-hero">Plan your route</div>');
		racDW('<div id="rac-tnt-w-mid">');
			racDW('<img src="' + racMapUrl + '" id="rac-tnt-w-map" width="' + racMapWidth + '" height="147" alt="Map for ' + racSearchAD + '" />');
			racDW('<div class="rac-tnt-w-form">');		
				racDW('<form action="http://www.rac.co.uk/route-planner/?cmp=routeplannerwidget" target="_new" method="post">');
					racDW('<input type="text" name="point0" id="rac-w-form-pc">');
					racDW('<input type="image" id="rac-w-form-btn" src="' + racRoot + 'go.gif" alt="Go" />');
					racDW('<input type="hidden" value="' + racSearchAD + '" name="point1" />');
					racDW('<input type="hidden" value="oneway" name="there_and_back" />');
					racDW('<input type="hidden" value="time" name="optmz" />');
					racDW('<input type="hidden" value="" name="highways" />');
					racDW('<input type="hidden" value="" name="tolls" />');
					racDW('<input type="hidden" value="mi" name="units" />');
					racDW('<input type="hidden" value="driving" name="mode" />');
					racDW('<div class="rac-clr"></div>');
					racDW('<p>Enter your location or postcode above to get a personalised route to:</p>');
					racDW('<div class="rac-tnt-w-text"><strong>' + racAD + '</strong></div>');
				racDW('</form>');
			racDW('</div>');
			racDW('<div class="rac-clr"></div>');	
		racDW('</div>');	
	racDW('</div>');	
	racDW('<div id="rac-tnt-w-footer">');
		racDW('<p><a href="http://www.rac.co.uk/?cmp=routeplannerwidget" target="_blank">Powered by RAC</a></p>');
		racDW('<a href="http://www.rac.co.uk/widgets/route-planner/?cmp=routeplannerwidget" target="_blank">Get this widget</a>');
		racDW('<div id="rac-tnt-w-footer-t"></div>');
	racDW('</div>');
racDW('</div>');
// Widget ends

/* LEGACY SUPPORT CODE START */
if (legacy === true) {
	// Div to hide legacy hard coded copy within code snippit used
	racDW('<div style="display:none">');
}
/* LEGACY SUPPORT CODE END */
