/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var featurecounter = 0;
var stationidnumberconnector;
/**
 * updates the graphvalues based on a slider
 */
function initGraphBySlider() {
    stationsliderarray.sort(stationdatesort);
    stationidnumberconnector = new Array();
    var i = 0;
    for(station in stationsliderarray){
        stationidnumberconnector[i] = stationsliderarray[station];
        objects[stationsliderarray[station][4]].push(i);
        i++;
    }
    jQuery("#graphslider").slider("destroy");
    jQuery("#graphslider").slider({
        handle: '#gshandle',
        min: 0,
        max: stationidnumberconnector.length-1,
        change: function(e,ui){
            updateGraphs(ui.value);
        },
        slide: function(e,ui){
            var station = stationidnumberconnector[ui.value][0];
            mypos = jQuery('#graphslider a').position().left + 2;
            jQuery("#stationhover").html(station);
            jQuery("#stationhover").css({
                left: mypos
            });
        },
        start: function(e,ui){
            jQuery('#stationhover').fadeIn('fast');
        },
        stop: function(e,ui){
            jQuery('#stationhover').fadeOut('fast');
        }
    });

    jQuery("#next").unbind('click');
    jQuery("#previous").unbind('click');
    jQuery("#next").click(function(){
        var thisValue = jQuery("#graphslider").slider('value');
        jQuery("#graphslider").slider('value',thisValue+1);
    });
    jQuery("#previous").click(function(){
        var thisValue = jQuery("#graphslider").slider('value');
        jQuery("#graphslider").slider('value',thisValue-1);
    });
    jQuery("#graphslidercontainer").show();
}

/**
 * Comment
 */
function updateGraphs(id){
    // Check if the old selected feature is still shown, if it is we change it to
    // default color
    var features = pointlayer.features;
    if(selectedFeatureLine != null){
        for(var i = 0; i < features.length; i++){
            if(selectedFeatureLine == features[i]){
                pointlayer.drawFeature(selectedFeatureLine, "default");
            }
        }
    }

    // Update the graphs
    var stationid = stationidnumberconnector[id][3];
    jQuery("#temperaturImg").attr({
        src : "image.map?id="+stationid+"&graphType=0&size=0"
    });
    jQuery("#condImg").attr({
        src: "image.map?id="+stationid+"&graphType=1&size=0"
    });
    jQuery("#salImg").attr({
        src: "image.map?id="+stationid+"&graphType=2&size=0"
    });
    jQuery("#sigmatImg").attr({
        src: "image.map?id="+stationid+"&graphType=3&size=0"
    });
    jQuery("#tsimg").attr({
        src: "image.map?id="+stationid+"&graphType=10&size=0"
    });

    // Paint the selected feature in the right color
    var featureid = stationidnumberconnector[id][4];
    for(i = 0; i < features.length; i++){
        if(features[i].id == featureid){
            pointlayer.drawFeature(features[i], "sliderselect");
            selectedFeatureLine = features[i];
        }
    }
}

var selectedFeatureLine;

function stationdatesort(a,b){
    var da = new Date(a[3]);
    var db = new Date(b[3]);
    if(da.getMilliseconds() > db.getMilliseconds()){
        return 1;
    }else if(da.getMilliseconds() < db.getMilliseconds()){
        return -1;
    }else{
        if(a[0] > b[0]){
            return 1;
        }else if(a[0] < b[0]){
            return -1;
        }else{
            return 0;
        }
    }
}
