(function () {

    var $ = jQuery,
		Abstract = wri.widget.Abstract,
		isJquery = function (it) { return it instanceof $; },
		undefined;

    var FlashMap = wri.widget.FlashMap = function (opts) {
        Abstract.apply(this, arguments);
        if (!isJquery(this.container)) this.container = $(this.container);
    };

    FlashMap.prototype = new Abstract({
        className: "FlashMap",
        container: null,
        flashObject: null,
		isStatic:false,
        ready: false,
        _settings: {
            mode: "display",
            flashVars: {
                isCoordMode: false,
                isEditMode: false,
                display: "display", //Defines the context of the map instance. home,map,state,city, display (testing)
                displayData: null, //Valid values are CityID or StateAbbr from XML data.
                dataPath: "/common/flash/data/", //Defines the path to the XML data folder.
                market: "retail" //Defines the market mode of the map. Valid values are "retail" and "industry".
            },
            flashObject: {
                id: null,
                height: 319,
                width: 425,
                src: "/common/flash/usmap.swf",
                version: "9.0",
                xiSwfUrlStr: null
            },
            flashParams: {
                base: ".",
                allowScriptAccess: "always",
                quality: "high",
                salign:"tl",
                scale:"default",
                menu: "false",
                bgcolor: "#FFFFFF",
                wmode: "transparent"
            },
            flashAttrs: {},
            globalModes: {
                home: {
                    stateClicked: function (st_abbr, proptype) {
                        // delete the map zoom cookies so we don't re-zoom
                        SetCookie('RETAIL_MAPZOOM1', '');
                        SetCookie('RETAIL_MAPZOOM2', '');
                        SetCookie('INDUSTRIAL_MAPZOOM1', '');
                        SetCookie('INDUSTRIAL_MAPZOOM2', '');
                        SetCookie('PADS_MAPZOOM1', '');
                        SetCookie('PADS_MAPZOOM2', '');
                        SetCookie('LAND_MAPZOOM1', '');
                        SetCookie('LAND_MAPZOOM2', '');

                        // go to the state
                        window.location.href = "/" + proptype + "/?st_abbr=" + st_abbr;
                    },
                    recordState: function () { return; }
                },
                map: {
                    cityClicked: function (name) {
                        console.log("cityClicked(" + name + ")");
                        var market = flashMap.getMarket();
                        if (market == "industry") market = "industrial";
                        window.location = "/" + market + "/results.aspx?ma=" + name;
                    },
                    recordState: function (display, displayData) {
                        var market = flashMap.getMarket();

                        if (market == "industry" || market == "industrial") {
                            SetCookie('INDUSTRIAL_MAPZOOM1', display);
                            SetCookie('INDUSTRIAL_MAPZOOM2', displayData);
                        } else if (market == "retail") {
                            SetCookie('RETAIL_MAPZOOM1', display);
                            SetCookie('RETAIL_MAPZOOM2', displayData);
                        } else if (market == "pads") {
                            SetCookie('PADS_MAPZOOM1', display);
                            SetCookie('PADS_MAPZOOM2', displayData);
                        } else if (market == "land") {
                            SetCookie('LAND_MAPZOOM1', display);
                            SetCookie('LAND_MAPZOOM2', displayData);
                        }
                    }
                },
                state: {},
                city: {}
            }
        },
        init: function () {
            var I = this,
				foSettings = this.settings("flashObject"),
				flashVars = this.settings("flashVars"),
                flashParams = this.settings("flashParams"),
                flashAttrs = this.settings("flashAttrs");
			
			//If they don't have flash, prep the static stuff
            if(this.isStatic || !swfobject.hasFlashPlayerVersion("9")){
				this.isStatic = true;
				var aRetail = this.container.find("a.retail"),
					aIndustry = this.container.find("a.industrial"),
					aBrowse = this.container.find("a.btn-browse"),
					rdoRetail = this.container.find("input[value='retail']");
					rdoIndustry = this.container.find("input[value='industrial']");
					
				aIndustry.hide();
				
				rdoIndustry.click(function(evt){
					aRetail.hide();
					aIndustry.show();
				});
				
				rdoRetail.click(function(evt){
					aRetail.show();
					aIndustry.hide();
				});
				
				aBrowse.click(function(evt){
					if(aRetail.is(":visible")){
						//aRetail.click();
						window.location = aRetail.attr("href");
					}else{
						//aIndustry.click();
						window.location = aIndustry.attr("href");
					}
					return false;
				});
				
				this.container.find(".buttons").click(function(evt){
					evt.stopPropagation();
					//return false;
				});
				
			}
			//otherwise, do the Flash
			else{
			
				this.goGlobal();
				
				//set container id, if necessary
				var cId;
				if ((cId = this.container.attr("id")) === null) {
					this.container.attr("id", cId = this.generateUniqueId(this.className));
				}
				//create swf id and append replaceable div in container
				if (foSettings.id === null) {
					foSettings.id = this.generateUniqueId(this.className) + "_swf";
				}
				this.container.empty().append("<div id='" + foSettings.id + "'/>");

				swfobject.embedSWF(foSettings.src, foSettings.id, foSettings.width, foSettings.height, foSettings.version.toString(), foSettings.xiSwfUrlStr, flashVars, flashParams, flashAttrs, function (evt) {
					I.flashObject = document.getElementById(foSettings.id);
					I.ready = true;
					I.trigger("ready");
				});
			
			}

            return this;

        },
        goGlobal: function () {
            var flashMap = window.flashMap = this,
            //mode = this.settings("mode"),
				flashVars = this.settings("flashVars"),
                globalModes = this.settings("globalModes"),
                mode = globalModes[flashVars.display];

            for (var funcName in mode) {
                window[funcName] = mode[funcName];
            }

            return this;
        },

        getMarket: function () 
		{ 
			//return this.flashObject.getMarket.apply(this.flashObject, arguments); 
			if(location.href.toLowerCase().indexOf("/retail/") > -1)
				return "retail";
			else if(location.href.toLowerCase().indexOf("/industrial/") > -1)
				return "industrial";
			else if(location.href.toLowerCase().indexOf("/pads/") > -1)
				return "pads";
			else if(location.href.toLowerCase().indexOf("/land/") > -1)
				return "land";	
		}
    });

})();
