﻿BenefulMap.InfoWin = Class.extend({
    init: function(infoWinDiv, previewImgDiv, navItems, tabs, closeBtn, contentDiv) {
        this.infoWinDiv = infoWinDiv;
        this.previewImgDiv = previewImgDiv;
        this.navItems = navItems;
        this.tabs = tabs;
        this.closeBtn = closeBtn;
        this.contentDiv = contentDiv;

        this.marker;
        this.mapState;
        this.streetView = new BenefulMap.StreetView("#street-view", "#street-view-canvas", infoWinDiv);
        this.openStatus = false;

        this.ajaxListener();
        this.loadListener();
        this.closeListener();
    },
    getStreetView: function() {
        return this.streetView;
    },
    setStreetView: function(streetView) {
        this.streetView = streetView;
    },
    getContentDiv: function() {
        return this.contentDiv;
    },
    getCloseBtn: function() {
        return this.closeBtn;
    },
    getInfoWinDiv: function() {
        return this.infoWinDiv;
    },
    getPreviewImgDiv: function() {
        return this.previewImgDiv;
    },
    getNavItems: function() {
        return this.navItems;
    },
    getTabs: function() {
        return this.tabs;
    },
    getMarker: function() {
        return this.marker;
    },
    setMarker: function(marker) {
        this.marker = marker;
    },
    getMapState: function() {
        return this.mapState;
    },
    setMapState: function(mapState) {
        this.mapState = mapState;
    },
    getOpenStatus: function() {
        return this.openStatus;
    },
    setOpenStatus: function(openStatus) {
        this.openStatus = openStatus;
    },
    setInfoWinListeners: function() {
        var infoWin = this.getInfoWinDiv();
        var self = this;

        $(infoWin).click(function(event) {
            //event.stopPropagation();
        });

        $(infoWin).mouseover(function(event) {
            event.stopPropagation();
        });

        $(infoWin).mousedown(function(event) {
            event.stopPropagation();
        });

        $(infoWin).dblclick(function(event) {
            event.stopPropagation();
        });

        $(this.getCloseBtn()).click(function(event) {
            event.preventDefault();
            self.closeWin();
        });
    },
    load: function(marker, mapState, map) {
        this.setMarker(marker);
        this.setMapState(mapState);

        var bounds = map.getBounds();
        var range = bounds.getNorthEast().lat() - bounds.getSouthWest().lat();
        var latOffset = range * 0.37;

        var markerLng = marker.getLatLng().lng();
        var markerLat = marker.getLatLng().lat();

        var newLatLng = new GLatLng(markerLat + latOffset, markerLng);
        map.panTo(newLatLng);

        $(document).trigger("streetview.update", [marker.getLatLng()]);
        this.navListener();
        this.tabsListener();
        this.closeListener();
        this.setInfoWinListeners();
        this.hidePreviewImg();
        this.openWin(map);
    },
    loadListener: function() {
        var self = this;
        $(document).bind('infowin.load', function(event, marker, mapState, map) {
            self.load(marker, mapState, map);
        });
    },
    showPreviewImg: function() {
        $(this.getPreviewImgDiv() + " img").show();
    },
    hidePreviewImg: function() {
        $(this.getPreviewImgDiv() + " img").hide();
    },
    openWin: function(map) {
        var marker = this.getMarker();
        var infoWin = this.getInfoWinDiv();
        var markerOffset = map.fromLatLngToDivPixel(marker.getLatLng());
        var infoTop = markerOffset.y - ($(infoWin).height() + 30);
        var infoLeft = markerOffset.x - ($(infoWin).width() / 2);
        $(infoWin).show().css({ top: infoTop, left: infoLeft });
        this.setOpenStatus(true);
        this.updatePreviewPhoto();
        this.populate("basics", true);
    },
    updatePreviewPhoto: function() {
        var marker = this.getMarker();
        var self = this;
        WagWorld.Web.AjaxUtility.GetSingleLocation(parseInt(marker.spotId), function(result) {
            var categoryNum = result.value.Category;
            var img = new Image();
            var imgSrc = BenefulMap.Constants.Snippets.INFOWIN_PREVIEW;
            imgSrc = imgSrc.replace("%SPOT_ID%", marker.spotId);
            imgSrc = imgSrc.replace("%CAT_NUM%", categoryNum);
            img.src = imgSrc;
            $(self.getPreviewImgDiv()).html(img);
            self.showPreviewImg();
        });
    },
    navListener: function() {
        var marker = this.getMarker();
        var self = this;
        $(this.getNavItems()).click(function(event) {
            event.preventDefault();
            var pageName = $(this).attr("href").replace("#", "");
            self.populate(pageName, false);
        });
    },
    tabsListener: function() {
        var marker = this.getMarker();
        var self = this;
        $(this.getTabs()).live('click', function(event) {
            event.preventDefault();
            var sendPanelMode = $(this).attr('href');
            sendPanelMode = sendPanelMode.split("#send");
            sendPanelMode = sendPanelMode[1];
            self.populate('send', false, sendPanelMode);
        });
    },
    closeWin: function() {
        $(this.getInfoWinDiv()).hide();
        var markers = [];
        markers = this.getMapState().getMarkers();
        for (i = 0; i < markers.length; i++) {
            markers[i].setImage(BenefulMap.Constants.IMG_MARKER);
        }
        this.setOpenStatus(false);
    },
    closeListener: function() {
        var self = this;
        $(document).bind('infowin.close', function(event) {
            if (self.getOpenStatus()) {
                self.closeWin();
            }
        });
    },
    populate: function(contentType, markerClicked, sendPanelMode, message) {
        var self = this;
        var marker = this.getMarker();

        if (!sendPanelMode) {
            sendPanelMode = 1;
        }
        var params = {};
        var spotId = marker.spotId;
        params.id = spotId;
        params.markerClicked = markerClicked;

        var contentUrl;
        switch (contentType) {
            case 'basics':
                contentUrl = BenefulMap.Constants.Snippets.INFOWIN_BASICS;
                break;
            case 'send':
                contentUrl = BenefulMap.Constants.Snippets.INFOWIN_SEND;
                params.panel = sendPanelMode;
                if (message) {
                    params.message = message;
                }
                break;
            default:
                return false;
        }

        $.ajax({
            type: "GET",
            cache: false,
            url: contentUrl,
            data: params,
            success: function(data, textStatus) {
                $(self.getContentDiv()).html(data);
                $(document).trigger("favorites.add_to", ["#info-win-favorite", spotId]);
                self.getDirectionsListener(spotId);
                self.addReviewListener(spotId);
                if (contentType == 'send') {
                    self.sendListeners(sendPanelMode, contentType, markerClicked);
                }
                $("#info-win a.pop-up").popup({
                    scrollbars: true,
                    resizable: true,
                    status: false,
                    width: 640,
                    height: 600
                });
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                var msg = textStatus + " loading: " + BenefulMap.Constants.Snippets.INFOWIN_BASICS;
            },
            dataType: "html"
        });
    },
    sendListeners: function(panelNum, contentType, markerClicked) {
        var marker = this.getMarker();
        var self = this;
        var businessId = parseInt(marker.spotId);
        if (panelNum == 1 && $("#chk_cc").length) {
            $("#send-email-button").click(function(event) {
                event.preventDefault();
                var ccMe = ($("#chk_cc").get(0).checked);
                var toEmail = $("#txt_to").val();
                var fromName = $("#txt_from").val();
                var optionalMessage = "";

                WagWorld.Web.AjaxUtility.TellFriendAboutSpot(businessId, toEmail, fromName, optionalMessage, function(result) {
                    console.log("result: ", result);
                    if (result.value.Pass == true) {
                        console.log("Pass is true", contentType, markerClicked, 3, message);
                        self.populate(contentType, markerClicked, 3, message);
                    } else {
                        var message = result.value.Reason;
                        self.populate(contentType, markerClicked, panelNum, message);
                        console.log("Pass is false", contentType, markerClicked, 3, message);
                    }
                });
            });
        } else if (panelNum == 2) {
            $("#txt_phone").click(function(event) {
                $(this).val("");
            });
            $("#send-sms-button").click(function(event) {
                event.preventDefault();
                var smsId = parseInt($("#ddlCarrier option:selected").val());
                var mobileNumber = $("#txt_phone").val();
                // ajax pro params: int businessId, int smsId, string mobileNumber
                WagWorld.Web.AjaxUtility.SendToMobile(businessId, smsId, mobileNumber, function(result) {
                    if (result.value.Pass == true) {
                        self.populate(contentType, markerClicked, 4, message);
                    } else {
                        var message = result.value.Reason;
                        self.populate(contentType, markerClicked, panelNum, message);
                    }
                });
            });
        }
    },
    addReviewListener: function(spotId) {
        $(".add-review").click(function(event) {
            event.preventDefault();
            $(document).trigger("addreview.open", [spotId]);
        });
    },
    ajaxListener: function() {
        $("#info-win-content").bind("ajaxSend", function() {
            $(this).addClass("loading");
        }).bind("ajaxComplete", function() {
            $(this).removeClass("loading");
        });
    },
    getDirectionsListener: function(spotId) {
        var self = this;
        $("input.get-directions-from").keypress(function(event) {
            if (event.which == 13) {
                event.preventDefault();
                $(".get-directions-go").click();
            }
        });
        $("#showDirections").click(function(event) {
            event.preventDefault();
            $("#info-bottom-directions").show();
            $(".get-directions-go").click(function(event) {
                event.preventDefault();
                window.location = root + "get-directions/default.aspx?id=" + spotId + "&from=" + $("input.get-directions-from").val();
            });
            $("#cancelDirections").click(function(event) {
                $("#info-bottom-directions").hide();
            });
        });

        $("#cancelDirections").click(function(event) {
            event.preventDefault();
            $("info-bottom-directions").hide();
        });
    }
});

