if (typeof gsi == "undefined") {
    gsi = {};
}

if (typeof gsi.gs == "undefined") {
    gsi.gs = {};
}

gsi.gs.ShowHideToggler = Class.create({
    // company independent member variables
    baseDivId: null,
    baseParentDivId: null,
    baseMapId: null,

    runningEnv: null,
    contextPath: null,
    companyDetailsUrl: null,

    // company dependent member variables
    computedDivId: null,
    computedParentDivId: null,
    computedMapId: null,

    map: null,
    mapDiv: null,

    listingId: null,
    bc: null,
    lon: null,
    lat: null,
    proj: null,
    disableClickLog: null,
    companyName: null,

    initialize: function(divId, parentDivId, mapId, runningEnv, contextPath) {
        this.baseDivId = divId;
        this.baseParentDivId = parentDivId;
        this.baseMapId = mapId;

        this.contextPath = contextPath;
        this.companyDetailsUrl = this.contextPath + '/gs/companyDetails.c';
        this.runningEnv = runningEnv;
    },

    goCalculateRoute: function(input) {
        var args = {
            id: 'c_' + this.listingId,
            lon: this.map.getCenter().lon,
            lat: this.map.getCenter().lat,
            zoom: this.map.getZoom(),
            tab: "yellow",
            routeFrom: input.getValue() };

        // Statistikk
        this._createClickLog('CROUTE');

        window.location = Eniro.GSIMaps.routeURL(jQuery(input).val(), { address: this.companyName, lonlat: this.map.toDegrees(this.map.getCenter()) });
    },

    show: function(listing, companyName, bc, disableClickLog, lon, lat, proj) {
        this._init(listing, companyName, bc, disableClickLog, lon, lat, proj);
        this._initializeHtml();
    },

    hide: function(listing, companyName, bc, disableClickLog, lon, lat, proj) {
        this._init(listing, companyName, bc, disableClickLog, lon, lat, proj);
        $(this.computedDivId).className = "foo";
    },

    _init: function(listing, companyName, bc, disableClickLog, lon, lat, proj) {
        this._resetCompanyDependant();

        this.listingId = listing;
        this.bc = bc;
        this.disableClickLog = disableClickLog;
        this.companyName = companyName;

        this.lon = lon;
        this.lat = lat;
        this.proj = proj;

        this.computedDivId = this._getComputedId(this.baseDivId);
        this.computedParentDivId = this._getComputedId(this.baseParentDivId);
        this.computedMapId = this._getComputedId(this.baseMapId);
    },

    _resetCompanyDependant: function() {
        this.computedDivId = null;
        this.computedParentDivId = null;        
        this.computedMapId = null;

        this.map = null;
        this.mapDiv = null;

        this.listingId = null;
        this.bc = null;
        this.lon = null;
        this.lat = null;
        this.proj = null;
        this.disableClickLog = null;
        this.companyName = null;
    },

    _intializeMap: function () {
        this.mapDiv = $(this.computedMapId);
        if (this._hasMap()) {
            var pos = new OpenLayers.LonLat(this.lon, this.lat);
            pos.transform(new OpenLayers.Projection(this.proj), new OpenLayers.Projection('EPSG:900913'));
            this.map = new Eniro.API.Map(this.mapDiv, this.contextPath + '/jscript/mapapi/1.1', Eniro.GSIMaps.mapOptions({
                defaultPosition: { lonlat: pos, zoom: 15 },
                zoomBar: false,
                keyboardControl: false,
                navigationControl: false
            }));
            var marker = new Eniro.Feature.PopupFeature(pos, {}, {});
            this.map.addFeature(marker);

            var targetUrl = Eniro.GSIMaps.resultURL("yp", this.listingId, "");
            this.map.addControl(new Eniro.GSIMaps.ClickThrough({ destinationUrl: targetUrl }));
            jQuery("#" + this._getComputedId("mapHref")).attr("href", targetUrl);

            /* FIXME: build an appropriate clickthrough URL here
            var args = {
                id: 'c_' + this.listingId,
                lon: this.map.getCenter().lon,
                lat: this.map.getCenter().lat,
                zoom: this.map.getZoom(),
                layers: this.map.getLayersAsString(),
                tab: "yellow" };

            var mapUrl = this.contextPath + "/kart/#" + $H(args).toQueryString();
            this.map.addControl(new EniroMaps.ClickThrough({ destinationUrl: mapUrl }));
            $(this._getComputedId('mapHref')).href = mapUrl;
            */
        }
    },

    _hasMap: function() {
        return ((this.lat != null)
                && (this.lat.length > 0)
                && (this.lon != null)
                && (this.lon.length > 0));
    },

    _createClickLog: function(logId) {
        var that = this;
        new Ajax.Request(that.contextPath + '/tolink.c', {
            method: 'get',
            parameters: {
                bcId: that.bc,
                itemId: logId,
                lineId: that.listingId,
                url: ''
            },
            onSuccess: function () {
                // alert('logged');
            }
        });
    },

    _initializeHtml: function() {
        var that = this;
        new Ajax.Request(that.companyDetailsUrl, {
            method: 'get',
            parameters: {
                detailsType: 'companyexpand',
                lineId : that.listingId},
            onSuccess: function(transport) {
                var responseText = transport.responseText.strip();
                if (responseText.length < 1) return;

                $(that.computedDivId).className = "bar";
                var element = $(that.computedParentDivId);
                element.innerHTML = responseText;
                that._initLightBoxHrefs();

                if (that.computedMapId != null && that.mapDiv == null) {
                    that._intializeMap();
                }

                // Statistikk
                if (!that.disableClickLog) {
                    that._createClickLog('REXPAND');
                }
            }
        });
    },

    _initLightBoxHrefs: function() {
        var elementIds = new Array(
                this._getComputedId('pictureLink'),
                this._getComputedId('videoLink'),
                this._getComputedId('contactLink'));

        for (j = 0; j < elementIds.length; j++) {
            var currentLink = $(elementIds[j]);
            if (currentLink == null) continue;
            new lightbox(currentLink);
        }
    },

    _getComputedId: function(strippedId) {
        return strippedId + '_' + this.listingId;
    }
});

