var salesRepFinder = (function($){
	var map,
		mapSelection,
		url,
		repInfo,
		repInfoHeight,
		mapHeight = $("#salesRepArea").height(),
		enabledRegionsIds = [
		    "mountain",
		    "central",
		    "midwest",
		    "great_lakes",
		    "northeast",
		    "puerto_rico",
		    "northcentral",
		    "southeast",
		    "western",
		    "western_can",
		    "central_eastern_can",
		    "quebec",
		    "eastern_ontario",
		    "us",
		    "canada",
		    "middle_east",
		    "scandanavia",
		    "latin_america",
		    "international",
		    "central_europe",
		    "japan",
		    "saudi_arabia"
		],
		
		// Each region is associated with a specific rep ( except if noted ). Rep refers to reps' html page (e.g., vince-hundt.html = vince-hundt)
		// If you are changing a rep for a region, make sure to update the non-js version of the sales rep finder as well (rep-listing.html)
		
		repPages = {
			// US Regions
			"mountain" : "vince-hundt",
			"central" : "central", // Has two reps
			"midwest" : "midwest", // Has two reps
			"great_lakes" : "lars-bergan",
			"northeast" : "scott-harrington",
			"puerto_rico" : "murray-mcintyre",
			"northcentral" : "jody-parker",
			"southeast" : "randy-wilkes",
			"western" : "nick-korn",
			// Canadian Regions
			"western_can" : "terry-charles",
			"central_eastern_can" : "vince-hundt",
			"quebec" : "bob-desbiens",
			"eastern_ontario" : "eastern_ontario", // Has two reps
			// International Regions
			"us" : "",
			"canada" : "",
			"saudi_arabia" : "vince-hundt",
			"central_europe" : "central-europe",
			"scandanavia" : "ulf-qvarford",
			"latin_america" : "lars-bergan",
			"international" : "vince-hundt",
			"japan"	: "ryokusan"
		};
	
	function loadMap(mapType) {
		$("#location").blur();
		if (swfobject.hasFlashPlayerVersion("9.0.0")) {
			swfobject.createCSS("#rep-listing", "display:none;");
			swfobject.createCSS("#selectMapType", "display:block;");
			map = "assets/flash/" + mapType + ".swf";
			swfobject.embedSWF(map, "salesRepMap", "900", "493", "9.0.0", "assets/flash/expressInstall.swf", {
				colorDisabled :"F2F2F2", 
				colorEnabled :"CCCCCC",
				colorHover :"DD0000",
				hoverSpeed :0.4
			}, {
				menu :"false",
				wmode :"transparent"
			});
		}
	}
	
	function slidePanel(endPosition,duration,onComplete){
		$("#salesRepInfo").animate({"left" : endPosition}, duration, onComplete);
	}
	
	function disableMapHover(){
		$('<div id="mapCover"/>').prependTo("#salesRepArea");
	}
	
	function enableMapHover(){
		$("#mapCover").remove();
	}
	
	function loadRepContent(id){
		if ( id == "us"){
			loadMap("us");
			$("#location").val("us");
		}
		else if ( id == "canada"){
			loadMap("canada");
			$("#location").val("canada");
		} else {
			$("#loading").show();
			$.get('contact-us/sales-rep-finder/' + repPages[id] + '.html', function(data){
				$("#loading").hide();
				$("#salesRepContent").empty();
				repInfo = $("#repInfo", $(data));
				repInfo.appendTo("#salesRepContent");
				
				repInfoHeight = $("#salesRepInfo").height();
				
				animateHeight(repInfoHeight, function(){
					slidePanel("443px",1000);
					disableMapHover();
				});
			});
		}
		// Track Map Click in google
		plauditAnalytics.trackEvent("Sales Rep Map", "Click", id);
	}
	
	function animateHeight(newHeight, onComplete){
		$("#salesRepArea").animate({"height" : newHeight}, 500, onComplete);
	}
	
	return {
		init: function(){
			loadMap("us");
			loadMap("canada");
			loadMap("international");
			
			// Set location dropdown to international on page load
			$("#location").val("international");
			
			$("#hideInfo").click(function(){
				slidePanel("951px", 500, function(){
					animateHeight("500");
					enableMapHover();
				});
			});
				
			$("#location").change(function(){
				loadMap($(this).val());
				slidePanel("951px", 500, function(){
					animateHeight("500");
					enableMapHover();
				});
			});
			
			$("#mapCover").live("click", function(){
				slidePanel("951px", 500, function(){
					animateHeight("500");
					enableMapHover();
				});
			});
		},
		
		regionClick: function(id){
			loadRepContent(id);
		},
		
		getEnabledRegionsIds: function(){
			return enabledRegionsIds;
		},
		
		setEnabledRegionsIds: function(value){
			enabledRegionsIds = value;
		}
	};
	
})(jQuery);

salesRepFinder.init();

