/*
Copyright 2007 TikiRobot. All rights reserved.
This file is part of TikiChart.

TikiChart is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

TikiChart is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with TikiChart.  If not, see <http://www.gnu.org/licenses/>.
*/

 $(document).ready(function(){
    $('#warning').remove();
 });
 
function TikiChart() {
}

TikiChart.prototype.updateChart = function() {
    //alert('update!');
    values = $('.value')
    var data = ''
    var type = $('#chartType').val();

    var max = Number.MIN_VALUE;
    var min = Number.MAX_VALUE;    
    $('.value', this).each(function() {
        if ((this.value != "") && (null != this.value)) {
            var val = parseFloat(this.value);
            if (val<min) min = val;
            if (val>max) max = val;
        }
    })

    if ('lc' == type) {
        var scale = 100.0/(max-min);
    } else {
        if(min<0) {
            alert('Negative values are currently supported only for line charts. Sorry!');
            return;
        }
        var scale = 100.0/max;
        min = 0;
    }
    
    $('.value', this).each(function() {
        if ((this.value != "") && (null != this.value)) {
            data += ((this.value-min)*scale).toFixed(1) + ',';
        }
    })
    data = data.replace(/,$/,'');

    var url = 'http://chart.apis.google.com/chart?cht='+type+'&chs=250x200&chd=t:'+data;    

    if(("p" == type) || ("p3" == type)) {
        var labels = '&chl=';
        var gotLabel = false;
        $('.label', this).each(function() {
            labels += this.value + '|';
            if (this.value != '') gotLabel = true;
        })        
        if (gotLabel) url += labels;
    } else {
        var labelString = this.createLabels(min, max, type);
        url += labelString;
    }
    
    var title = $('#chartTitle').val(); 
    if ('' != title) {
        url += '&chtt='+title;
    }
    
    $('#preview').attr('src', url);
    $('#embedField').val('<img src="'+url+'" width="250" height="200"/>');
    $('#imgUrlField').val(url);

    
}

TikiChart.prototype.addRow = function() {
    $("#data").append("<div class=row><div class=left><input type=text class=label size=20></div><input type=text class=value size=10></div>");
}

TikiChart.prototype.popUp = function(url, w, h) {
    window.open(url, 'TikiChartPopUp', 'width='+w+',height='+h+'menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes');
}


TikiChart.prototype.createLabels = function(min, max, type) {
    var showX = $('#showXLabel').attr('checked');
    var showY = $('#showYLabel').attr('checked');

    if (showX && showY) {
        var str = '&chxt=x,y';
    } else if (showX) {
        var str = '&chxt=x';
    } else if (showY) {
        var str = '&chxt=y';
    } else {
        return '';
    }
    
    if('bhs' == type) {
        if (showX) {
            str += '&chxr=0,';
            str += min+','+max            
        }
        
        if (showY) {
            if (showX) {
                str += '&chxl=1:';
            } else {
                str += '&chxl=0:';        
            }
            $('.label', this).each(function() {
                str += '|' + this.value;
                if (this.value != '') gotLabel = true;
            })        
        }
    } else {
        if (showX) {
            str += '&chxl=0:';
            $('.label', this).each(function() {
                str += '|' + this.value;
                if (this.value != '') gotLabel = true;
            })        
        }
        
        if (showY) {
            if (showX) {
                str += '&chxr=1,';
            } else {
                str += '&chxr=0,';        
            }
            str += min+','+max
        }    
    }
    
    return str;

}
