/** 
* Class: VisKort.LinkGenerator
*
* Utility base class to create parametized internal or external links
*/
VisKort.Utils.LinkGenerator = OpenLayers.Class(VisKort.Utils, {

    /** 
    * Property: reverseGeocode
    * {<Bool>} true, if a reverse geocoding process is necessary during the parametizing process
    */
    reverseGeocode: false,

    /** 
    * Property: parameters
    * {<Object>} Collection of name-value pairs holding the parameters
    */
    parameters: null,

    /** 
    * Property: hrefTemplates
    * {<Object>} Hashtable that contains link-href templates. One for each link. 
    */
    hrefTemplates: {},

    /** 
    * Property: linkText
    * {<String>} HTML template that contains the links. The anchor tags must have "name" attributes. 
    * hrefTemplates are associated to the anchor tags by the "name".
    */
    linkText: "",

    /** 
    * Constructor: VisKort.LinkGenerator
    *
    * Parameters:
    * linkText - <String>
    * reverseGeocode - <Bool>
    * hrefTemplates - <Array>
    */
    initialize: function(linkText, reverseGeocode, hrefTemplates) {
        this.linkText = linkText;
        this.reverseGeocode = reverseGeocode;
        this.hrefTemplates = hrefTemplates;
    },

    /** 
    * Method: getDivElement
    * Generates a HTML DIV element holding the links.
    *
    * Parameters:
    * parameters - <Object> hashtable with parameters
    * className - <String> the name of the style class
    */
    getDivElement: function(parameters, className) {
        var divElement = document.createElement("div");

        if (this.linkText != "") {
            divElement.className = className;

            this.parameters = parameters;
            
            divElement.innerHTML = this.getTemplatedContent(this.parameters, this.linkText);

            var links = divElement.getElementsByTagName("a");

            var count = links.length;
            for (var i = 0; i < count; i++) {
                var link = links[i];
                var nameParam = link.name;
                var eventhandlerfunc = function(e) { this.sender.onLinkClick(this.param); };
                OpenLayers.Event.observe(link, "click", OpenLayers.Function.bindAsEventListener(eventhandlerfunc, { sender: this, param: nameParam }));
            }
        }

        return divElement;
    },

    /** 
    * Method: onLinkClick
    * Invoked when a user clicks on one of the links
    *
    * Parameters:
    * linkName - <String>
    */
    onLinkClick: function(linkName) {
        var gotoLinkInvokedByCallback = false;
        if (this.parameters) {
            var services = new VisKort.Utils.Services();
            if (this.reverseGeocode) {
                if (this.parameters.lon && this.parameters.lat) {
                    var lonlat = new OpenLayers.LonLat(this.parameters.lon, this.parameters.lat);
                    var callback = function(address) { this.sender.reverseGeocodeCallback(address, this.linkName); };
                    callback = callback.bind({ sender: this, linkName: linkName });

                    this.parameters.address = services.reverseGeocodeLonlat(lonlat, callback);

                    gotoLinkInvokedByCallback = true;
                }
            }
        }

        if (!gotoLinkInvokedByCallback)
            this.gotoLink(linkName);
    },

    /** 
    * Method: reverseGeocodeCallback
    * Reverse geocode callback method
    *
    * Parameters:
    * address - <String>
    * linkName - <String>
    */
    reverseGeocodeCallback: function(address, linkName) {
        this.parameters.address = address;
        this.gotoLink(linkName);
    },

    /** 
    * Method: gotoLink
    * Navigates to the link
    *
    * Parameters:
    * linkName - <String>
    */
    gotoLink: function(linkName) {
        if (this.hrefTemplates && this.hrefTemplates[linkName]) {
            var hrefTemplate = this.hrefTemplates[linkName];
            href = this.getTemplatedContent(this.parameters, hrefTemplate);
            window.open(href);
        }
    },

    CLASS_NAME: "VisKort.Utils.LinkGenerator"
});


/** 
* Class: VisKort.LinkGenerator.Route
*
* LinkGenerator sub class to handle links to the route component ("route from" and "route to")-links
*/
VisKort.Utils.LinkGenerator.Route = OpenLayers.Class(VisKort.Utils.LinkGenerator, {

    HTML: "<div style='padding-left:6px;'><img src='/VisKort/MapImages/car.png' style='float:left;margin-top:0px;'/>&nbsp;Rute:&nbsp;<a href='#' name='from'>Herfra</a>&nbsp;<a href='#' name='to'>Hertil</a></div>",

    initialize: function(geocode) {
        if (viskortRouteTabPanelExists) {
            var args = [this.HTML, geocode];
            VisKort.Utils.LinkGenerator.prototype.initialize.apply(this, args);
        }
    },

    gotoLink: function(linkName) {
        if (linkName == "from") {
            ChangeTab(clientId_RouteTabPanel);

            var address = this.parameters.address;
            viskort.components['route'].Set_Route_From(address);
        }
        else if (linkName == "to") {
            ChangeTab(clientId_RouteTabPanel);

            var address = this.parameters.address;
            viskort.components['route'].Set_Route_To(address);
        }
    },

    CLASS_NAME: "VisKort.Utils.LinkGenerator.Route"
});


/** 
* Class: VisKort.LinkGenerator.Rejseplanen
*
* LinkGenerator sub class to handle links to the rejseplanen (external site)
*/
VisKort.Utils.LinkGenerator.Rejseplanen = OpenLayers.Class(VisKort.Utils.LinkGenerator, {

    ONELINK_MODE_HTML: "<a href='#' name='fromto'><img src='http://info.rejseplanen.dk/file.php?name=/files/logoer/RP/RP_logo_RGB_transparant.gif' border='0' style='height: 24px;'/></a>",
    TWOLINKS_MODE_HTML: "<div style='padding-left:4px;'><img src='http://info.rejseplanen.dk/files/logoer/RP/RP_logo_lille.gif' style='float:left;margin-top:0px;'/>&nbsp;Rejseplan:&nbsp;<a href='#' name='from'>Herfra</a>&nbsp;<a href='#' name='to'>Hertil</a></div>",
    ONELINK_MODE_HREF: { fromto: "http://www.rejseplanen.dk/bin/query.exe/mn?S=<!--#fromAddress#-->&SADR=1&Z=<!--#toAddress#-->&ZADR=1" },
    TWOLINKS_MODE_HREF: { from: "http://www.rejseplanen.dk/bin/query.exe/mn?S=<!--#address#-->&SADR=1", to: "http://www.rejseplanen.dk/bin/query.exe/mn?Z=<!--#address#-->&ZADR=1" },

    initialize: function(linkMode, geocode) {
        if (linkMode == "OneLink") {
            var args = [this.ONELINK_MODE_HTML, geocode, this.ONELINK_MODE_HREF];
            VisKort.Utils.LinkGenerator.prototype.initialize.apply(this, args);
        }
        else if (linkMode == "TwoLinks") {
            var args = [this.TWOLINKS_MODE_HTML, geocode, this.TWOLINKS_MODE_HREF];
            VisKort.Utils.LinkGenerator.prototype.initialize.apply(this, args);
        }
    },

    CLASS_NAME: "VisKort.Utils.LinkGenerator.Rejseplanen"
});



/** 
* Class: VisKort.LinkGenerator.GoogleRoutePlan
*
* LinkGenerator sub class to handle links to the google maps rout planner (external site)
*/
VisKort.Utils.LinkGenerator.GoogleRoutePlan = OpenLayers.Class(VisKort.Utils.LinkGenerator, {

    ONELINK_MODE_HTML: "<a href='#' name='fromto'><img src='http://google.com/favicon.ico ' border='0' style='height: 24px;'/></a>",
    TWOLINKS_MODE_HTML: "<div style='padding-left:4px;'><img src='http://google.com/favicon.ico ' style='float:left;margin-top:0px;'/>&nbsp;Ruteplan:&nbsp;<a href='#' name='from'>Herfra</a>&nbsp;<a href='#' name='to'>Hertil</a></div>",
    ONELINK_MODE_HREF: { fromto: "http://maps.google.dk?saddr=<!--#fromAddress#-->&daddr==<!--#toAddress#-->" },
    TWOLINKS_MODE_HREF: { from: "http://maps.google.dk?saddr=<!--#address#-->&SADR=1", to: "http://maps.google.dk?daddr=<!--#address#-->" },

    initialize: function(linkMode, geocode) {
        if (linkMode == "OneLink") {
            var args = [this.ONELINK_MODE_HTML, geocode, this.ONELINK_MODE_HREF];
            VisKort.Utils.LinkGenerator.prototype.initialize.apply(this, args);
        }
        else if (linkMode == "TwoLinks") {
            var args = [this.TWOLINKS_MODE_HTML, geocode, this.TWOLINKS_MODE_HREF];
            VisKort.Utils.LinkGenerator.prototype.initialize.apply(this, args);
        }
    },

    CLASS_NAME: "VisKort.Utils.LinkGenerator.GoogleRoutePlan"
});


/** 
* Class: VisKort.LinkGenerator.Municipality 
*
* LinkGenerator sub class to handle links to the municipality component
*/
VisKort.Utils.LinkGenerator.Municipality = OpenLayers.Class(VisKort.Utils.LinkGenerator, {

    HTML: "Kommune: <a href='#' name='municipality'><!--#municipality#--></a>",

    initialize: function() {
        if (viskortMunicipalityTabPanelExists) {
            var args = [this.HTML, false];
            VisKort.Utils.LinkGenerator.prototype.initialize.apply(this, args);
        }
    },

    gotoLink: function(linkName) {
        ChangeTab(clientId_MunicipalityTabPanel);
        var municipality = this.parameters.municipality;
        viskort.components['municipality'].MunicipalityLink(municipality);
    },

    CLASS_NAME: "VisKort.Utils.LinkGenerator.Municipality"
});

