(function($) {
	$.WorldWideBanner = {
		version: '0.0.1',

		defaultConf: {
			type: "US",
			pointWidth: 10,
			pointHeight: 10
		},

		footnotes: {
			"US" : {
				text: "For non-US customers only.",
				cities: {
					"Bermuda":1,
					"Tokyo":1,
					"SaintPetersburg":1,
					"Shanghai":1,
					"UnitedKingdom":1
				}
			},
			"UK": {
				text: "For non-UK customers only.",
				cities: {
					"Boston":1,
					"NewYork":1,
					"Tokyo":1,
					"SaintPetersburg":1,
					"Shanghai":1
				}
			}
		},

		cities: {
			"Boston" : {
				title: "CMS Forex Institutional (Boston)",
				country: "CMS Forex Institutional",
				link: "http://www.cmsfxpro.com/",
				offers: [
					"NFA Member",
					"Low Spreads",
					"2 Institutional Platforms",
					"Anonymous trading",
					"FXTradeport"
				],
				coords: [426, 74]
			},
			"NewYork" : {
				title: "New York",
				country: "United States",
				link: "http://www.cmsfx.com",
				offers: [
					"NFA Member",
					"Low Spreads",
					"$200 Minimum Deposit",
					"VT Trader",
					"Futures Trading"
				],
				coords: [416, 80]
			},
			"Bermuda" : {
				title: "Bermuda",
				country: "Bermuda",
				link: "http://www.cmsfxinternational.com",
				offers: [
					"BMA licensed",
					"Ability to Hedge",
					"Up to 400:1 Leverage**",
					"Institutional Services",
					"No tax on profits"
				],
				coords: [424, 98]
			},
			"Tokyo" : {
				title: "Tokyo",
				country: "Japan",
				link: "http://www.cmsfx-japan.com/",
				offers: [
					"FSA registered",
					"Low Spreads",
					"$200 Minimum Deposit",
					"VT Trader",
					"Trust accounts"
				],
				coords: [673, 78]
			},
			"SaintPetersburg" : {
				title: "St. Petersburg",
				country: "Russia",
				link: "http://www.cmsforex.ru",
				offers: [
					"Low Spreads",
					"VT Trader",
					"$200 Minimum Deposit",
					"Russian Educational Resources"
				],
				coords: [550, 47]
			},
			"Shanghai" : {
				title: "Shanghai",
				country: "China",
				link: "http://www.cms-forex.com/zh/",
				offers: [
					"Low Spreads",
					"VT Trader",
					"$200 Minimum Deposit",
					"Chinese Support"
				],
				coords: [651, 90]
			},
			"UnitedKingdom" : {
				title: "United Kingdom",
				country: "United Kingdom",
				link: "http://www.cmsfx.co.uk",
				offers: [
					"FSA Registered",
					"Ability to Hedge",
					"Up to 400:1 Leverage*",
					"VT Trader & MT4",
					"Institutional Services"
				],
				coords: [504, 63]
			}
		},

		defaultTemplate:  '\
			<span id="worldBannerWrapper">\
				<select size="1" name="listOfOffices" id="listOfOffices">\
					<option selected="" disabled>Choose Office</option>\
					<%=options%>\
				</select>\
			</span>\
		',

		optionTemplate: '\
			<option value="<%=cityId%>"><%=cityTitle%></option>\
		',

		tooltipTemplate: '\
			<span class="cityWrapper <%=cityClass%>">\
				<h3><%=title%></h3>\
				<ul>\
					<%=offers%>\
				</ul>\
				<a href="<%=link%>" class="visitButton"></a>\
				<strong><%=footnote%></strong>\
			</span>\
		',

		tooltipOfferTemplate: '\
			<li>\
				<%=offerTitle%>\
			</li>\
		'
	};

	// jQuery plugin implementation
	$.fn.WorldWideBanner = function(conf) {
		var conf = $.extend($.WorldWideBanner.defaultConf, conf);
		var bannerTemplate        = $("<script type='text/html'>"+$.WorldWideBanner.defaultTemplate+"</script>"),
		    optionTemplate        = $("<script type='text/html'>"+$.WorldWideBanner.optionTemplate+"</script>"),
		    tooltipTemplate       = $("<script type='text/html'>"+$.WorldWideBanner.tooltipTemplate+"</script>"),
		    tooltipOfferTemplate  = $("<script type='text/html'>"+$.WorldWideBanner.tooltipOfferTemplate+"</script>");

		var optionsHtml = [];

		for (var cityId in $.WorldWideBanner.cities) {
			var city = $.WorldWideBanner.cities[cityId];
			optionsHtml.push(optionTemplate.tmpl({
				cityId: cityId,
				cityTitle: city.country
			}));
		}

		this.html(bannerTemplate.tmpl({
			options: optionsHtml.join('')
		}));

		$("<div>").attr({id: "citytip"}).appendTo("body");

		$("#listOfOffices").change(function(){
			var city = $(this).val();
			if (!city || !$.WorldWideBanner.cities[city]) {
				return;
			}

			window.location = $.WorldWideBanner.cities[city].link;
		});

		var worldBannerWrapper = $("#worldBannerWrapper");
		var htmlByCityTooltip = function(cityId) {
			var city = $.WorldWideBanner.cities[cityId] || null;
			if (!city) {
				return "no data";
			}

			var offersHtml = [];
			for (var i = 0; i < city.offers.length; i++) {
				offersHtml.push(tooltipOfferTemplate.tmpl({
					offerTitle: city.offers[i]
				}));
			}

			var footnote = "";

			if ($.WorldWideBanner.footnotes[conf.type] && $.WorldWideBanner.footnotes[conf.type].cities[cityId]) {
				footnote = $.WorldWideBanner.footnotes[conf.type].text;
			}

			return tooltipTemplate.tmpl({
				cityClass: cityId,
				title: city.country,
				offers: offersHtml.join(''),
				link: city.link,
				footnote: footnote
			});
		}

		var drawCityPoint = function(city, x, y, width, height) {
			if (worldBannerWrapper.find("." + city).length > 0) {
				return;
			}
			worldBannerWrapper.append('<span class="cityHoverElement '+city+' worldBannerCityHover"> </span>');

			var cityNode = worldBannerWrapper.find("." + city),
				citytip = $("#citytip");

			cityNode
				.css("left", x)
				.css("top", y)
				.width(width)
				.height(height);


			cityNode.click(function() {
				if (!$.WorldWideBanner.cities[city]) {
					return;
				}
				window.location = $.WorldWideBanner.cities[city].link;
			});

			var tooltipHtml = htmlByCityTooltip(city);

			cityNode.tooltip({
				tip: citytip,
				offset: [10, -3],
				effect: 'slide',
				relative: false,
				onBeforeShow: function() {
					citytip.html(tooltipHtml);
				},
				onShow: function() {
					if ($.flashSlideshowAPI) {
						$.flashSlideshowAPI.pause();
					}
				},
				onHide: function() {
					if ($.flashSlideshowAPI) {
						$.flashSlideshowAPI.pause();
					}
				}
			});
		}

		for (var cityId in $.WorldWideBanner.cities) {
			var city = $.WorldWideBanner.cities[cityId];
			drawCityPoint(cityId, city.coords[0], city.coords[1], conf.pointWidth, conf.pointHeight);
		}

		// Don't remove!
		window["WMmouseOver"] = function(city, x, y, width, height, widthFlash, heightFlash) {
			//drawCityPoint(city, x, y, width, height);
		}

		// Don't remove!
		window["WMmouseOut"] = function() {}
	};

}) (jQuery);
