﻿function Invert(source) {
    if (source == null || source.length == 0)
        return source;

    var chars = source.split("");
    var result = "";

    for (var i = chars.length - 1; i >= 0; i--) {
        result += chars[i];
    }

    return result;
}

var googleMap = new GoogleMap();

function GoogleMap() {

    this.Show = function(address, element) {
        initialize(element);
        showAddress(address);
    };

    this.ShowCoordinates = function(longitude, latitude, element) {
        initialize(element);
        showCoordinates(longitude, latitude, element);
    };

    this.GetPoint = function() {
        return map.getCenter();
    };

    var map = null;
    var geocoder = null;

    this.LoadScript = function loadScript(mapKey) {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://maps.google.com/maps?file=api&v=2&oe=utf-8&key=" + mapKey + "&async=2&callback=loadMap";
        document.body.appendChild(script);
    };

    function initialize(element) {
        if (GBrowserIsCompatible()) {
            map = new GMap2(element);
            map.enableScrollWheelZoom();
            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
            geocoder = new GClientGeocoder();
        }
    }

    function showAddress(address, element) {
        if (geocoder) {
            geocoder.getLatLng(address,
            function(point) {
                showPoint(point);
                //marker.openInfoWindowHtml("<div id='mapMarkerText'>" + address + "</div>"); 
            });
        }
    }

    function showCoordinates(longitude, latitude, element) {
        if (geocoder) {
            var point = new GLatLng(latitude.replace(',', '.'), longitude.replace(',', '.'));
            showPoint(point);
        }
    }
    function showPoint(point) {
        map.setCenter(point, 16);
        var marker = new GMarker(point);
        map.addOverlay(marker);
    }
}

function CommentsStuff() {

    function reloadComments(loadListUrl) {
        var url = loadListUrl;
        url += "?cmsPageId=" + $("#CmsPageId").val();
        url += "&cmsPhotoId=" + $("#CmsPhotoId").val();
        url += "&cmsNewsItemId=" + $("#CmsNewsItemId").val();
        url += "&cmsQuestionId=" + $("#CmsQuestionId").val();

        $("#comments-list").load(url);

        var header = $("#comments-container h2:first-child");

        $(document).scrollTop(header.position().top);
    }

    this.Initialize = function(properties) {
        var showAllCommentsUrl = properties["loadListUrl"];
        var form = $("#add-comment-form");

        form.live("submit", function() {
            $.post(properties["addCommentUrl"], $("#add-comment-form").serialize(),
                function(data) {
                    $("#comments-add").html(data);

                    reloadComments(showAllCommentsUrl);
                });

            return false;
        });

        $(".delete-comment").live("click", function(e) {
            var target = $(e.target);
            target.hide();

            $.get(target.attr("href"), function() {
                reloadComments(showAllCommentsUrl);
            });

            return false;
        });
    };
}

String.prototype.startsWith = function(str) {
    return (this.indexOf(str) === 0);
};
