general.namespace('SW.Controls');

// class RegionDataTabCollection
(function() {

    var RegionDataTabCollection = SW.Controls.RegionDataTabCollection = function(containerId) {
        /// <summary>ctor - renders tabs onto page into container</summary>
        /// <param name=""></param>
        /// <returns></returns>
        this.container = document.getElementById(containerId);
        this.currentRegion;
        this.lastRegion = null;
        this.lastStartDate = null;
        this.lastTab = null;
        this.tabs = {};
        this.currentForecastDataRequestId = "not possible";
        this.initialize();

    };
    RegionDataTabCollection.prototype.constructor = RegionDataTabCollection;

    // constants
    RegionDataTabCollection.MAX_WINDOW_HEIGHT = "";

    RegionDataTabCollection.prototype.initialize = function() {
        /// <summary></summary>
        /// <param name=""></param>
        /// <returns></returns>

        var fragment = document.createDocumentFragment();

        var tabListContainer = $C("div", null, { classes: "tab-menu" },
            this.tabList = $C("ul")
        );
        this.tabContentContainer = $C("div", null, { classes: "tab-content" },
            this.forecastChartContainer = $C("div", { height: 0 }),
            this.toolsChartContainer = $C("div")
        );

        fragment.appendChild(tabListContainer);
        fragment.appendChild(this.tabContentContainer);

        /*
        <div class="tab-menu">
        <ul>
        <li><a href="#" class="tab active">Charts</a></li>
        <li><a href="#" class="tab">Text Reports</a></li>
        </ul>
        </div>
        <div class="tab-content">
        <div id="forecastChartContainer">swf</div>
        <div id="charts"></div>
        <div id="reporterForecast"></div>
        </div>
        */
        this.tabs.textReports = new SW.Controls.RegionDataTab("Surf Report", "textReports", true, this);
        this.tabs.textReports.onShow.subscribe(this.onShowTextReports, this);

        this.tabs.surfWind = new SW.Controls.RegionDataTab("Charts", "surfWind", false, this);
        this.tabs.surfWind.onShow.subscribe(this.onShowCharts, this);
        this.tabs.surfWind.onHide.subscribe(this.onHideCharts, this);


        this.tabs.tools = new SW.Controls.RegionDataTab("Links", "tools", false, this);
        this.tabs.tools.onShow.subscribe(this.onShowTools, this);
        this.tabs.tools.onHide.subscribe(this.onHideTools, this);

        //this.tabs.tideWeather = new SW.Controls.RegionDataTab("Tide/Weather", "tideWeather", false, this);
        //this.tabs.tideWeather.onShow.subscribe(this.onShowTideWeather, this);
        //this.tabs.tideWeather.onHide.subscribe(this.onHideTideWeather, this);



        this.container.appendChild(fragment);

        // setup what goes in the tab content
        var textContainer = $C("div", { borderBottom: "1px dashed #D6D3BD" });
        this.tabs.textReports.content.appendChild(textContainer);
        this.regionForecasterReport = new SW.Controls.ForecasterReport(textContainer);
		
    };

    RegionDataTabCollection.prototype.select = function(name) {
        for (var i in this.tabs) {
            if (i != name) this.tabs[i].hide();
            else {
                this.currentTab = this.tabs[i];
                this.currentTab.show();
            }
        }
        this.displayRegionData();
    };

    /* when changing tabs*/
    RegionDataTabCollection.prototype.displayRegionData = function(region, startDate) {

        /// <summary></summary>
        /// <param name=""></param>
        /// <returns></returns>
        region = region || SWMap.map.getCenter().getTightestContainingRegion();
        startDate = startDate || SWMap.dateBar.currentStartDate;
        if (region !== this.lastRegion || startDate !== this.lastStartDate || this.currentTab !== this.lastTab) {
            this.lastRegion = region;
            this.lastStartDate = startDate;
            this.lastTab = this.currentTab;
            this.currentRegion = region;
            // if the region does not have graphs
            if (!region.hasGraphs) {
                //this.displayMessage("There are no charts or text reports for <br />'" + region.name + "'<br />Please navigate to a smaller region.");
                document.getElementById("region-forecast-data-container").style.display = "none";
                document.getElementById("map-default-links").style.display = "block";
            }
            else {
                var forecastDataManager = new SW.ForecastDataManager();
                SWMap.loadingIndicator.addLoad();
                forecastDataManager.onGetDataSuccess.subscribe(this.forecastDataManager_getDataSuccess, this, true);
                forecastDataManager.onGetDataFailure.subscribe(this.forecastDataManager_getDataFailed, this);
                forecastDataManager.onGetDataFailure.subscribe(function() { SWMap.loadingIndicator.removeLoad(); }, this);
                forecastDataManager.getRegionData(region, startDate);

                //show data
                document.getElementById("region-forecast-data-container").style.display = "block";
                document.getElementById("map-default-links").style.display = "none";
            }
        }
    };

    RegionDataTabCollection.prototype.displayMessage = function(message) {
        if (this.tabs.textReports.isSelected) { this.regionForecasterReport.displayMessage(message); }
        else { this.regionForecastChart.displayMessage(message); }
    };

    RegionDataTabCollection.prototype.displayError = function(message) {
        if (this.tabs.textReports.isSelected) { this.regionForecasterReport.displayError(message); }
        else { this.regionForecastChart.displayError(message); }
    };

    RegionDataTabCollection.prototype.displayClickForRegion = function(region) {
        this.currentRegion = region;
        var me = this;
        var message;
        if (region.hasGraphs) {
            if (this.tabs.textReports.isSelected) {
                var linkId = YAHOO.util.Dom.generateId();
                message = ("<a href=\"#\" id=\"" + linkId + "\">View text report for " + region.name + "</a>");
                YAHOO.util.Event.addListener(linkId, "click",
                    function(evt) {
                        YAHOO.util.Event.preventDefault(evt);
                        me.select("textReports");
                    }
                );
            }
            else if (this.tabs.tools.isSelected) {
                var linkId = YAHOO.util.Dom.generateId();
                message = ("<a href=\"#\" id=\"" + linkId + "\">View links for " + region.name + "</a>");
                me.toolsChartContainer.style.display = "none";
                YAHOO.util.Event.addListener(linkId, "click",
                    function(evt) {
                        YAHOO.util.Event.preventDefault(evt);
                        me.select("tools");
                    }
                );
            }
            else { // charts
                if (region.hasGraphs) {
                    var linkId = YAHOO.util.Dom.generateId();
                    message = ("<a href=\"#\" id=\"" + linkId + "\">View charts for " + region.name + "</a>");
                    YAHOO.util.Event.addListener(linkId, "click",
                        function(evt) {
                            YAHOO.util.Event.preventDefault(evt);
                            me.select("surfWind");
                        }
                    );
                }
            }
        }
        else {
            message = "There are no charts or text reports for <br />'" + region.name + "'<br />Please navigate to a smaller region."
        }
        this.displayMessage(message);
    };

    RegionDataTabCollection.prototype.forecastDataManager_getDataSuccess = function(forecastDataCollection) {
        var me = this;
		SWMap.loadingIndicator.removeLoad(); // TODO remove hardcoded global instance

        //JS added call to record pageview when region changes
        //trigger ad refresh and analytics page view
        if (null !== this.currentRegion) {
            var sidead = document.getElementById('ctl00_ctl00_ctl160x600');
            var sidead2 = document.getElementById('ctl00_ctl00_ctl728x90');
            sidead.src = "/assets/ads/ad_frame_160x600.aspx?pvalue=" + this.currentRegion.adCode;
            sidead2.src = "/assets/ads/ad_frame_728x90.aspx?pvalue=" + this.currentRegion.adCode;
            var analyticsLink = pageTracker._trackPageview("/swellwatch/" + this.currentRegion.name);
        }


        /// <summary></summary>
        /// <param name="forecastDataCollection">the data structure that contains graph and text report data</param>
        /// <param name="me">a reference to the RegionDataTabCollection since "this" scope is the forecastDataManager</param>
        /// <returns></returns>
        // if no data, then display an error message
        if (!forecastDataCollection) {
            this.displayError("Sorry, there is no data for this region for the selected time");
        }
        else if (this.tabs.textReports.isSelected) {
            if (forecastDataCollection.ForecasterReportData && forecastDataCollection.ForecasterReportData.FullReport) {
                // TODO - hide all flash chart things
                this.regionForecasterReport.displayData(forecastDataCollection, this.currentRegion.regionId);
            }
            // if there is no forecaster report data
            else {
                var linkId = YAHOO.util.Dom.generateId();
                // TODO - hide all flash chart things
                this.displayMessage("There is no text report for '" + forecastDataCollection.title + "'. <br /> <a href=\"#\" id=\"" + linkId + "\">View Charts</a>");
                YAHOO.util.Event.addListener(linkId, "click",
                    function(evt) {
                        YAHOO.util.Event.preventDefault(evt);
                        me.select("surfWind");
                    }
                );
            }
            this.regionForecastChart.updateChartParts(false, false, false, false);
            SWMap.loadingIndicator.removeLoad(); // TODO remove hardcoded global instance
        }
        else if (this.tabs.tools.isSelected) {
            // TODO - hide all flash chart things
            this.toolsChartContainer.style.display = "block";
            this.loadToolData(this.currentRegion.regionId, this);
            SWMap.loadingIndicator.removeLoad(); // TODO remove hardcoded global instance
        }
        else if (this.tabs.surfWind.isSelected) {
            if (null == this.regionForecastChart) {
                this.regionForecastChart = new SW.Controls.ForecastChart(this.forecastChartContainer, false, false, false, false);
                //this.regionForecastChart.onDataDisplayComplete.subscribe(function() {SWMap.loadingIndicator.removeLoad(); }, this);
            }

            if (this.currentForecastDataRequestId != forecastDataCollection.requestId) {
                this.regionForecastChart.displayData(forecastDataCollection); // TODO - just the types for the selected tab
                this.currentForecastDataRequestId = forecastDataCollection.requestId;
            }
            this.regionForecastChart.updateChartParts(true, true, true, true);
        }

        SWMap.loadingIndicator.removeLoad(); // TODO remove hardcoded global instance

    };

    RegionDataTabCollection.prototype.forecastDataManager_getDataFailed = function(data, me) {
        /// <summary></summary>
        /// <param name=""></param>
        /// <returns></returns>
        var message = "Error getting data for the region <br />'" + data.title + "'<br />We have been notified of this error.";
        me.displayError(message);
    };


    RegionDataTabCollection.prototype.onShowCharts = function(args, me) {
        //me.regionForecastChart.displayMessage("Loading...");
        //me.displayRegionData(); is not called here, because the other tabs haven't necessarily been hidden yet
        me.forecastChartContainer.style.height = RegionDataTabCollection.MAX_WINDOW_HEIGHT;
    };

    RegionDataTabCollection.prototype.onHideCharts = function(args, me) {
        //me.regionForecastChart.hideMessage();
    };

    RegionDataTabCollection.prototype.onShowTideWeather = function(args, me) {
        me.forecastChartContainer.style.height = RegionDataTabCollection.MAX_WINDOW_HEIGHT;
    };

    RegionDataTabCollection.prototype.onHideTideWeather = function(args, me) {
    };

    RegionDataTabCollection.prototype.onShowTextReports = function(args, me) {
        //me.displayRegionData(); is not called here, because the other tabs haven't necessarily been hidden yet
        me.forecastChartContainer.style.height = "0px";
    };

    RegionDataTabCollection.prototype.onShowTools = function(args, me) {
        me.regionForecastChart.updateChartParts(false, false, false, false);
    };
    RegionDataTabCollection.prototype.onHideTools = function(args, me) {
        me.toolsChartContainer.style.display = "none";
    };

    RegionDataTabCollection.prototype.loadToolData = function(currentRegionId, me) {
        //build links objects
        var toolsDataCallBackArgs = {
            success: displaytoolsData,
            failure: displaytoolsData,
            argument: { regionId: currentRegionId }
        };
        var sUrl = "/WebServices/SurfLinks.aspx?regionid=" + currentRegionId;
        var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, toolsDataCallBackArgs);

        function displaytoolsData(linksDataCollection) {
            me.toolsChartContainer.innerHTML = linksDataCollection.responseText;
        };

    };


})();


