﻿function MarkerInfo(heading, latLng, dateTime, content, detailLink, marker) {
    this.latLng_ = latLng;
    this.heading_ = heading;
    this.marker_ = marker;
    this.content_ = content;
    this.dateTime_ = dateTime;
    this.detailLink_ = detailLink;
}

function DestinationMarker() {
    this.rejseplanenUrl = "http://www.rejseplanen.dk/bin/query.exe/mn?externalCall=yes&ZID=A=2@O=##toPoint##@X=12590831@Y=55712525@U=103@L=901012211@B=1@p=1220531599@&ST=255&S=##fromPoint##";
    this.rejseplanenBlankUrl = "http://www.rejseplanen.dk/bin/query.exe/mn";
    this.fromPointTemplate = "##fromPoint##";
    this.toPointTemplate = "##toPoint##";
}

function MarketContentBuilder() {
    this.contentTemplateContainerJQuery = jQuery('div[id*=uiMarkerInfoTemplate]');
    this.contentTemplateJQuery = this.contentTemplateContainerJQuery.find('div.mapMarkerContainerTemplate');
    this.contentTemplate = this.contentTemplateJQuery.get(0);
    this.currentMarkerJQuery = null;
}

jQuery.extend(DestinationMarker.prototype, {
    RequestDirections: function() {
        if (destinationMarker.IsCustomDirectionFull()) {
            var path;
            path = 'http://maps.google.com/maps?f=d&source=s_d&saddr=';
            path = path + this.GetCustomDirection();
            path = path + "&daddr=";
            path = path + this.GetMarkerLocation();
            //path = path + "&geocode=FWTLVQMd2yW_ACklyeyQuzdSRjFJyN3CPcbZOA%3BFZukUAMdpQHAACE9RHjqAWbQ7w&mra=ls&sll=55.784296,12.513428&sspn=0.639398,1.783905&ie=UTF8&z=10";
            //window.open(path);
            newwindow = window.open(path, '', 'height=600,width=1000, toolbar=no');
            if (window.focus) { newwindow.focus() }
            return true;
        }
        return false;

        //this.InitiateDirectionUi(this.GetMarkerLocation(), this.GetCustomDirection(), this.IsForwardDirected(), DrawDirection);
    },

    InitiateDirectionUi: function(from, to, forward, callback) {

        if (forward) {
            callback(from, to);
        }
        else {
            callback(to, from);
        }

    },

    GoToTransportSchedule: function() {
        GetAddress(destinationMarker.GetMarkerGLatLng(), destinationMarker.GoToTransportScheduleWithAddress);
    },

    GoToTransportScheduleWithAddress: function(address) {
        destinationMarker.InitiateDirectionUi(address, destinationMarker.GetCustomDirection(), destinationMarker.IsForwardDirected(), destinationMarker.RedirectToTransportSchedule);
    },

    GoToPredefinedLocation: function(predefinedLocation) {
        if (predefinedLocation) {
            var locationParameters = predefinedLocation.split('|');
            var predefinedLocation = this.ConstructDestinationPoint(locationParameters[0], locationParameters[1]);

            this.InitiateDirectionUi(this.GetMarkerLocation(), predefinedLocation, this.IsForwardDirected(), DrawDirection);
        }
    },

    RedirectToTransportSchedule: function(from, to) {
        if (destinationMarker.IsCustomDirectionFull()) {
            var transportScheduleUrl = destinationMarker.rejseplanenUrl.replace(destinationMarker.fromPointTemplate, from);
            window.open(transportScheduleUrl.replace(destinationMarker.toPointTemplate, to));
        }
        else {
            window.open(destinationMarker.rejseplanenBlankUrl);
        }
    },

    DrawPredefinedDestination: function(predefinedDestination) {

    },

    IsForwardDirected: function() {
        var fromSwitcher = jQuery('li.toSwitcher');

        return (fromSwitcher && fromSwitcher.hasClass('active'));
    },

    IsCustomDirectionFull: function() {
        var address = jQuery('input.customDirectionPointAddress');
        var city = jQuery('input.customDirectionPointCity');

        if (address && city && (address.val() != "Indtast startadresse")) {
            return address.val() && city.val() && (address.val() != "Indtast startadresse");
        }

        return false;
    },

    GetCustomDirection: function() {
        var address = jQuery('input.customDirectionPointAddress');
        var city = jQuery('input.customDirectionPointCity');

        if (address && city) {
            return this.ConstructAddress(address.val(), city.val());
        }

        return null;
    },

    GetMarkerLocation: function() {
        var latSpan = jQuery('div.mapMarkerGeoCoordinates span.mapMarkerLat');
        var lngSpan = jQuery('div.mapMarkerGeoCoordinates span.mapMarkerLng');

        if (latSpan && lngSpan) {
            return this.ConstructDestinationPoint(latSpan.get(0).innerHTML, lngSpan.get(0).innerHTML);
        }

        return null;
    },

    GetMarkerGLatLng: function() {
        var coordinates = this.GetMarkerLocation();
        if (coordinates) {
            var latLng = coordinates.split(',');
            return new GLatLng(latLng[0], latLng[1]);
        }
        return null;
    },

    ConstructDestinationPoint: function(lat, lng) {
        return Trim(lat) + ',' + Trim(lng);

    },

    ConstructAddress: function(address, city) {
        return Trim(address) + ',' + Trim(city);
    },

    SwitchDirection: function(forward) {
        if (forward) {
            $('li.fromSwitcher').addClass('active');
            $('li.toSwitcher').removeClass('active');
        }
        else {
            $('li.fromSwitcher').removeClass('active');
            $('li.toSwitcher').addClass('active');
        }

        return false;
    }

});

jQuery.extend(MarketContentBuilder.prototype, {

    BuildMarkerInfoBox: function(markerInfo) {
        if (markerInfo) {
            var contentTemplateCopy = this.CloneInfoTemplate();

            var contentTemplateCopyjQuery = jQuery(contentTemplateCopy);
            this.currentMarkerJQuery = contentTemplateCopyjQuery;

            // coordinates
            if (markerInfo.latLng_) {
                contentTemplateCopyjQuery.find('div.mapMarkerGeoCoordinates span.mapMarkerLat').text(markerInfo.latLng_.lat());
                contentTemplateCopyjQuery.find('div.mapMarkerGeoCoordinates span.mapMarkerLng').text(markerInfo.latLng_.lng());
            }

            // title
            if (markerInfo.heading_) {
                contentTemplateCopyjQuery.find('.mapMarkerTitle').text(markerInfo.heading_);
            }

            //text
            if (markerInfo.content_) {
                contentTemplateCopyjQuery.find('.mapMarkerText').html(markerInfo.content_.replace(/\n/, '<br />'));
            }

            //date time
            if (markerInfo.dateTime_ && markerInfo.dateTime_ != '') {
                contentTemplateCopyjQuery.find('.mapMarkerDate').text(markerInfo.dateTime_);
            }

            //read more link
            var linkSpan = contentTemplateCopyjQuery.find('.arrowLink');
            if (this.detailLink_ != '') {
                linkSpan.show();
                linkSpan.find('a.mapMarkerReadMoreLink').attr('href', markerInfo.detailLink_);
            }
            else {
                linkSpan.hide();
            }

            //alert(this.ContentTemplate.get(0).innerHTML);
            return contentTemplateCopy;
        }
    },

    FixDirectionsDivLinks: function() {
        var directionsDiv = jQuery('div.mapMarkerContentDirections:first');
        directionsDiv.find('a').each(function() {
            $(this).replaceWith('<span>' + $(this).text() + '</span>');
        });
    },

    CloneInfoTemplate: function() {
        var copyDiv = document.createElement('DIV');
        copyDiv = this.contentTemplate.cloneNode(true);

        return copyDiv;
    }

});