general.namespace('SW.Controls');

(function() {
    /**
    * ctor
    */
    var ForecasterReport = SW.Controls.ForecasterReport = function(container) {
        this.container = container;
    };
    
    ForecasterReport.prototype.constructor = ForecasterReport;
    
    ForecasterReport.prototype.displayData = function(forecastDataCollection, currentRegionId) {
    
        var me = this;
        
        // that's been encoded using URL encoding technique
        function URLDecode(psEncodeString) 
        {
          // Create a regular expression to search all +s in the string
          var lsRegExp = /\+/g;
          // Return the decoded string
          return unescape(String(psEncodeString).replace(lsRegExp, " ")); 
        }

        function captureGsiAd (response) {
            me.setContent(response.argument.dateHTML, response.argument.bodyHTML, URLDecode(response.responseText));
        }
    
        function skipGsiAd (response) {
            me.setContent( response.argument.dateHTML, response.argument.bodyHTML );
        }

        if (forecastDataCollection && forecastDataCollection.ForecasterReportData && forecastDataCollection.ForecasterReportData.FullReport) {
            var dateHTML = '<span style="font-size: 11px; font-weight:normal;">Reported on</span> ' + general.webServiceDateFormat(forecastDataCollection.ForecasterReportData.DateRelevant.Local, general.dateFormat.masks.fullDate);
            if (forecastDataCollection.ForecasterReportData.ReportName) {
                dateHTML += " <span style=\"font-size: 11px; font-weight:normal;\">for</span> '" + forecastDataCollection.ForecasterReportData.ReportName + "'";
            }
            
            var bodyHTML = forecastDataCollection.ForecasterReportData.FullReport;
            
            if (bodyHTML.indexOf("<!-- GSIADMOD -->") != -1) {
                var gsiAdCallBackArgs = {
                    success: captureGsiAd,
                    failure: skipGsiAd,
                    argument: {
                        dateHTML : dateHTML,
                        bodyHTML : bodyHTML
                    }
                };
            
                var sUrl = "/WebServices/Links.asmx/GenGsiAd?regionId=" + currentRegionId;
                var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, gsiAdCallBackArgs);
            }
            else this.setContent(dateHTML, bodyHTML);
        }
        else this.displayError("There is no text report for this region"); // TODO better message
    };
    
    ForecasterReport.prototype.setContent = function(dateHTML, bodyHTML, gsiAd) {
        var html = '';
        if (dateHTML) html += '<div class="text-report-date">' + dateHTML + '</div>';
        if (bodyHTML) {
            var adcode = '<div id="content-300x250">';
            adcode += '<h6>advertisement</h6>';
            adcode += '<a href="http://ad.doubleclick.net/jump/sportnet.ws.surfrep/home;tile=4;sz=300x250;ord=123456789?" target="_blank"><img src="http://ad.doubleclick.net/ad/sportnet.ws.surfrep/home;tile=4;sz=300x250;ord=123456789?" width="300" height="250" border="0" alt=""></a>';
            adcode += '</div>';
            
            if (gsiAd) {
                var breakGsiAt = bodyHTML.indexOf("<!-- GSIADMOD -->");
                beginning = bodyHTML.substring(0, breakGsiAt);
                end = bodyHTML.substring(breakGsiAt, bodyHTML.length);
                bodyHTML = beginning + gsiAd + end;
            }
            
            //Add add code to text. If you can find the middle stick it there if not put it at the end.
            if (bodyHTML.indexOf("SWELL FORECAST") != -1) {
                var breakPostAt = bodyHTML.indexOf("SWELL FORECAST");
                beginning = bodyHTML.substring(0, breakPostAt);
                end = bodyHTML.substring(breakPostAt, bodyHTML.length);
                bodyHTML = beginning + adcode + end;
            }
            else {
                bodyHTML += adcode;
            }
            
            html += '<div class="text-report-body">' + bodyHTML + '</div>';
        }
        this.container.innerHTML = html;
    };

    /**/
    ForecasterReport.prototype.displayMessage = function(message) {
        var html = '<div class="text-report-message-notice">' + message + '</div>';
        this.setContent(null, html);
    };

    /**/
    ForecasterReport.prototype.displayError = function(message) {
        var html = '<div class="text-report-message-error">' + message + '</div>';
        this.setContent(null, html);
    };
})();
/* end: chart functions */

