﻿var myRPMap = null;
var mapid = "MapArea";
RPCompare.PinClickShouldOpenNewWindow = false;
//basic functions
$(document).ready(function() {
    $("#FunTextDiv .mapLinks a.map").click(function(e) {
        e.preventDefault();
        SelectRadTabStripTab("rpvMap");
        $.scrollTo("#" + mapid, 500);
    });
    $("#FunTextDiv .mapLinks a.directions").click(function(e) {
        e.preventDefault();
        //Make sure we have a map to begin with by selecting map tab.  (Alas, this won't work otherwise.)
        var container = $("#FunTextDiv .GetDirectionsDiv .xx")
        if (container.hasClass("hidden")) {
            container.show(500, function() { container.removeClass("hidden"); });
            SelectRadTabStripTab("rpvMap");
        } else {
            container.hide(500, function() { container.addClass("hidden"); });
        }

    });
    $("#FunTextDiv .GetDirectionsInvoker").each(function(index, elem) {
        $(elem).click(function(e) {
            e.preventDefault();
            var siteid = $("#chkSaveToCompareList").val();
            var shape = RPCompare.GetRPMap().GetSiteDictionary()[siteid];
            var POI = makeRPPOIFromShape(shape);
            RPCompare.GetRPMap().SetPOI(POI);
            DoGetDirections(elem);
        });
        
       });
       
    $("#MapAreaSection .GetDirectionsInvoker").each(function(index, elem) {
        $(elem).click(function(e) {
            e.preventDefault();
            var siteid = $("#chkSaveToCompareList").val();
            var shape = RPCompare.GetRPMap().GetSiteDictionary()[siteid];
            var POI = makeRPPOIFromShape(shape);
            RPCompare.GetRPMap().SetPOI(POI);
            DoGetDirections(elem);
        });
       });
       
    
    $("#chkSaveToCompareList").click(function(e) {
        ChangeCompareListManualCheckboxClickEvent(this);
    });
    $("#MapAreaChoices ul").hide();

});
$(document).bind("compare_communities_reset", function() {
    $("#chkSaveToCompareList").each(function(index, elem) {
        elem.checked = jQuery.inArray(jQuery(elem).val(), RPCompare.SetOfCompareListItems) > -1;
        //chrome acting goofy with regard to "$" and "jQuery"
    });
});
//Map Setup.
//unfortunately, @!$#@!$@, we have to delay loading the bing map script 
//for the FRELLING TAB STRIP TO WORK IN WEBKIT
(function() {
    $(window).bind("load", function() {
        //Start load of microsoft's Mappoint script.
        //but only if we know there's going to even be a map.
        if ($("#" + mapid).length > 0) {
            setTimeout(function() {
                var loader = new RPMapScriptLoader();
                loader.Load();
            }, 1000);
        }
    });
    function MapSetup() {
        var loader = new RPMapScriptLoader();
        var MyLoad = function(e) {
            RPCompare.GetRPMap().FindProperties(MakeLocationsUrl());
        }
        if (!loader.IsLoaded()) {
            loader.OnLoad(MyLoad);
            loader.Load();
        } else {
            MyLoad();
        }
    }
    function MakeLocationsUrl() {
        return AppRoot() + "Locations.aspx?sitelist=" + GetCurrentSiteID();
    }
    function GetCurrentSiteID() {
        return $("#chkSaveToCompareList").val();
    }
    //GLOBALS:
    window["MyOnClientTabSelected"] = function(sender, args) {
        if (args.get_tab().get_value() == "rpvMap") {
            MapSetup();
        }
        hideInfoBox();
    }
})();
//This is used to create context objects for search pins to be instantiated into
//so that we can lazily load searches, save search results, and display searches
//based upon which checkbox is clicked. 
RPMap.prototype.CreateOrShowContext = function(search, contextName) {
    var thisRPMapObject = this;
    var context = null;
    var DictionaryOfContexts = thisRPMapObject.GetDictionaryOfContexts();
    if (contextName in DictionaryOfContexts)
        context = DictionaryOfContexts[contextName];
    else {
        context = thisRPMapObject.AddAndExtendContext(contextName, {
            "MakeIconFromShapeIndex": function(shape, index) {
                return thisRPMapObject.MakeIcon(contextName, "");
            }
        });
    }

    if (context.IsPopulated) {
        thisRPMapObject.ShowShapesByContext(contextName);
    }
    else {
        var options = thisRPMapObject.CreateDefaultOptions(search, thisRPMapObject.GetSearchLocation(), context.layer, 10, thisRPMapObject.MapCallbackMaker(context));
        thisRPMapObject.FindByOptions(options);
    }
}
RPCompare.GetRPMap = function() {
    if (myRPMap == null) {
        myRPMap = new RPMap({
            "useBingPopover": false,
            "id": mapid,
            "derivesize": true,
            onLoadMap: function() {
                $("#MapAreaChoices ul").show(200);
                
            }
        });
        //extend RPMap we will use later in click event.
    }
    return myRPMap;
}
//Search Map
$(document).ready(function() {
    $("#MapAreaChoices input:checkbox").each(function(i, el) {
        var str = $(el).val();
        var data = jQuery.parseJSON(str);
        $(el).bind('click', data, function(e) {
            //alert(e.data.search);
            var map = RPCompare.GetRPMap();
            if (this.checked) {
                map.CreateOrShowContext(e.data.search, e.data.pin);
            } else {
                map.HideShapesByContext( e.data.pin);
            }
        });
    });
});
/* contact */
$(document).ready(function() {
	$("#FunTextDiv .topSuff .infoLinks a.emailus").colorbox({ iframe:true, width:800, height:600 });
});
/* prop pic view */
//function propview_clicked_handler(img_rot_item, img_sel_item) {
function propview_clicked_handler(sender, args) {
	//img_sel_item.ImageUrl = img_rot_item.ImageUrl;
	//piclog(sender.get_id() + " item was clicked. Item index is " + args.get_item().get_index());
	//var imgSelectedImage = document.getElementById('<%=imgSelectedImage.ClientID%>');
	//var imgSelectedImage = $find('<%=imgSelectedImage.ClientID%>');
	//imgSelectedImage.src = "";

	//imgSelectedImage = imgSelectedImage;   //args.get_item(imgCurrentProp).ImageUrl;
	//document.getElementById("imgSelectedImage").ImageUrl = "";
	//img_item
}
function piclog(text) {
	//var event_log = document.getElementById("event_log");
	//event_log.innerHTML = text + "\x3cbr/\x3e" + event_log.innerHTML;
}
