/* Tendenzkurve Hotelbewertungen (FusionChart)
 * Diese Klasse steuert die Ausgabe der Tendenzkurven in kleiner Ansicht (thumb)
 * und grosser Ansicht (big) 
 * @author Wolfgang Merkens <wm@traveltainment.de>
 * @copyright  2009 Traveltainment AG 
 */

//constructor 
function TTFusionChart(box, data) { 
    if (!document.getElementById) { 
        return; 
    }
    
    this.showElem    = document.getElementById(box);
    this.data        = data;
    this.numData     = this.data.length;
    this.chartSize   = 'thumb';
    this.heightThumb = '40';
    this.heightBig   = '200'; 
    this.chartHeight = this.heightThumb; 
    this.chartWidth  = '100%';
    this.params      = new Object();
    this.fixParams   = new Object();
    this.custParams  = null;
    this.colDataLine = 'FF6600';
    this.labelFont   = 'Arial';
    this.labelSize   = '9';
    this.labelColor  = '646464';
    this.flashSource = '/tools/FusionCharts/charts/Line.swf';             
}

//Datenobjekt neu setzen
TTFusionChart.prototype.setData = function(data) {
    this.data    = data;
    this.numData = this.data.length;
}

//Charttyp und -höhe neu setzen
TTFusionChart.prototype.setChartSize = function(cSize) {
    this.chartSize = cSize;
    if (this.chartSize == 'thumb') {
        this.chartHeight = this.heightThumb;
    } else if (this.chartSize == 'big') {
        this.chartHeight = this.heightBig;
    }     
}

//Defaulthöhe des kleinen Chart neu setzen
TTFusionChart.prototype.setHeightThumbChart = function(cHeight) {
    this.heightThumb = cHeight; 
    if (this.chartSize == 'thumb') {
        this.chartHeight = this.heightThumb;
    }     
}

//Defaulthöhe des grossen Chart neu setzen
TTFusionChart.prototype.setHeightBigChart = function(cHeight) {
    this.heightBig = cHeight;  
    if (this.chartSize == 'big') {
        this.chartHeight = this.heightBig;
    }   
}

//Farbe der Kurve neu setzen
TTFusionChart.prototype.setColDataLine = function(cColor) {
    this.colDataLine = cColor;     
}

//Font-Typ der x-Achsen-Beschriftung neu setzen
TTFusionChart.prototype.setLabelFont = function(lFont) {
    this.labelFont = lFont;     
}

//Font-Grösse der x-Achsen-Beschriftung neu setzen
TTFusionChart.prototype.setLabelSize = function(lSize) {
    this.labelSize = lSize;     
}

//Font-Farbe der x-Achsen-Beschriftung neu setzen
TTFusionChart.prototype.setLabelColor = function(lCol) {
    this.labelColor = lCol;     
}

//Feststehende FusionChart-Parameter
TTFusionChart.prototype.getFixedParams = function() {
    var fixParams = {
        "showBorder"       : '0', /* Gesamt: Border ja/nein */
        "showLegend"       : '0', /* Gesamt: Legende ja/nein */
        "yAxisMinValue"    : '1', /* Y-Achse: min. Wert */
        "yAxisMaxValue"    : '6', /* Y-Achse: max. Wert */
        "adjustDiv"        : '0', /* Y-Achse: autom. Linienziehung ein/aus */
        "numDivLines"      : '4', /* Y-Achse: Anzahl Zwischenlinien */
        "decimals"         : '1', /* Values: Anzahl Nachkommastellen */
        "decimalSeparator" : ',', /* Values: Anzeige Komma */
        "showValues"       : '0', /* Kurve: Werte anzeigen ja/nein */
        "numVDivLines"     : (this.numData - 2) /* X-Achse: Anzahl Zwischenlinien */        
    }
    if (this.chartSize == 'thumb') {
        fixParams["showLabels"]      = '0'; /* X-Achse: Label anzeigen ja/nein */
        fixParams["showYAxisValues"] = '0'; /* Y-Achse: Werte anzeigen ja/nein */
        fixParams["drawAnchors"]     = '0'; /* Kurve: Werte-Anker ein/aus */
        fixParams["canvasPadding"]   = '2'; /* hor. Abstand Kurve-Chart */
    } else {        
        fixParams["canvasPadding"] = '4'; /* hor. Abstand Kurve-Chart */                  
    }
    
    return fixParams;
}

//Veränderbare FusionChart-Parameter (externe Callback-Funktion nötig)
TTFusionChart.prototype.setChartParams = function() {
                 
    //Default-Params
    var params = {};
               
    if (this.chartSize == 'thumb') { 
        params["chartLeftMargin"]   = '0'; /* linker Abstand Chart-äußeres Div */
        params["chartRightMargin"]  = '1'; /* rechter Abstand Chart-äußeres Div */
        params["chartTopMargin"]    = '0'; /* oberer Abstand Chart-äußeres Div */
        params["chartBottomMargin"] = '1'; /* unterer Abstand Chart-äußeres Div */
        params["divLineColor"]      = 'CCCCCC'; /* Farbe Zwischenlinien */            
    } else {
        params["chartLeftMargin"]    = '6'; /* linker Abstand Chart-äußeres Div */
        params["chartRightMargin"]   = '10'; /* rechter Abstand Chart-äußeres Div */
        params["chartTopMargin"]     = '8'; /* oberer Abstand Chart-äußeres Div */
        params["chartBottomMargin"]  = '1'; /* unterer Abstand Chart-äußeres Div */
        params["divLineColor"]       = 'AAAAAA'; /* Farbe Zwischenlinien */
        params["labelPadding"]       = '10'; /* Abstand Label zur X-Achse */
        params["yAxisValuesPadding"] = '2'; /* Abstand Werte zur Y-Achse */
        params["vDivLineIsDashed"]   = '1'; /* Senk. Zwischenlinien gepunktet ja/nein */
        params["divLineIsDashed"]    = '1'; /* Hor. Zwischenlinien gepunktet ja/nein */
        params["anchorSides"]        = '20'; /* Kurve: Kantenanzahl der Werte-Anker (3-20) */
        params["anchorRadius"]       = '2'; /* Kurve: Grösse der Werte-Anker */
        params["anchorBgColor"]      = 'FFFFFF'; /* Kurve: Bg-Farbe der Werte-Anker */
        params["anchorBorderColor"]  = '000000'; /* Kurve: Rahmen-Farbe der Werte-Anker */
        params["labelDisplay"]       = 'WRAP'; /* X-Achse: Labelformatierung */
    }     
    params["bgColor"]               = 'FFFFFF'; /* Gesamt: BG-Farbe */
    params["outCnvBaseFont"]        = 'Arial'; /* Font Achsenbeschriftung */
    params["outCnvBaseFontColor"]   = '646464'; /* Fontfarbe Achsenbeschriftung */
    params["outCnvbaseFontSize"]    = '9'; /* Font-Grösse Achsenbeschriftung */
    params["canvasBgColor"]         = 'EFEFEF'; /* Datenfläche: BG-Farbe */
    params["canvasBorderThickness"] = '1'; /* Datenfläche: Rahmendicke */
    params["canvasBorderColor"]     = '333333'; /* Datenfläche: Rahmenfarbe */     
    
    //Wenn externe Callback-Funktion vorhanden, dann deren Params holen und entsprechenden Defaultwert überschreiben
    if (typeof(setParamsFusionChart) == 'function') {    
        this.custParams = setParamsFusionChart(this); 
        if (typeof(this.custParams) == 'object') {            
            var tmpKey;	
        	for(tmpKey in this.custParams){        		
                if (typeof(params[tmpKey]) == 'string') {
                    params[tmpKey] = this.custParams[tmpKey];
                }
        	}
        }
    }
    
    this.params = params;                 
           
}

//Beide params-Objekte in einen String schreiben
TTFusionChart.prototype.getParamsString = function() {
    var str = '';     
    for (var i in this.params) {
        str += i + "='" + this.params[i] + "' ";         
    }     
    for (var j in this.fixParams) {
        str += j + "='" + this.fixParams[j] + "' ";
    }
    return str;
}

//Gesamt-Ausgabe generieren
TTFusionChart.prototype.showChart = function() {
    var aHTML    = [''];
    var dataHTML = [''];
    
    if (this.numData > 0) {    
        //Params holen
        this.fixParams = this.getFixedParams();
        this.setChartParams();                 
        var strParams  = this.getParamsString();
        
        //Einzeldaten setzen
        //for (var i in this.data) {
        for (var i = 0; i < this.numData; i ++) {
             if (this.data[i]['rating'] == 0) {
                var setValue = '';
             } else {
                var setValue = ' value=\'' + this.data[i]['rating'] + '\'';
             }
             if (this.chartSize == 'thumb') {
                dataHTML.push('<set' + setValue + ' color=\'' + this.colDataLine + '\' />');
             } else {
                dataHTML.push('<set' + setValue + ' label=\'' + encodeURIComponent(this.data[i]['label']) + '\' color=\'' + this.colDataLine + '\' />');                
             }              
        }
        var strData = dataHTML.join('');
        
        //Styles für Labels der x-Achse
        if (this.chartSize == 'thumb') {
            strData += "<styles><definition><style name='myShadow' type='Shadow' distance='0'/></definition><application><apply toObject='DataPlot' styles='myShadow' /></application></styles>";
        } else if (this.chartSize == 'big') {
            strData += "<styles><definition><style name='myLabelsFont' type='font' font='" + this.labelFont + "' size='" + this.labelSize + "' color='" + this.labelColor + "' isHTML='1'/></definition><application><apply toObject='DATALABELS' styles='myLabelsFont' /></application></styles>";
        }
        
        if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
            // netscape plugin architecture
            aHTML.push('<embed src="' + this.flashSource + '" FlashVars="&chartWidth=' + this.chartWidth + '&chartHeight=' + this.chartHeight + '&debugMode=0&dataXML=<chart ' + strParams + '>');
            aHTML.push(strData);
            aHTML.push('</chart>" ');
            aHTML.push('quality="high" WMode="Transparent" width="' + this.chartWidth + '" height="' + this.chartHeight + '" name="FusionChartLine" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="https://www.macromedia.com/go/getflashplayer" />');
        } else {
            // PC IE
            aHTML.push('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="https://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + this.chartWidth + '" height="' + this.chartHeight + '" id="FusionChartLine'+this.showElem.id+'">');
            aHTML.push('<param name="allowScriptAccess" value="always" />'); 
            aHTML.push('<param name="WMode" value="Transparent" />');
            aHTML.push('<param name="movie" value="' + this.flashSource + '"/>');
            aHTML.push('<param name="FlashVars" value="&chartWidth=' + this.chartWidth + '&chartHeight=' + this.chartHeight + '&debugMode=0&dataXML=<chart ' + strParams + '>');
            aHTML.push(strData);
            aHTML.push('</chart>" />');
            aHTML.push('<param name="quality" value="high" />');
            aHTML.push('</object>');
        }
        
    }
    
    this.showElem.innerHTML = aHTML.join('');  
        
}   
