/**
 * @filename activityDetails.js
 * @description attaches event handlers for UI components:
 * - expanding/collapsing of sections, including charts
 * - edit summary button
 * Copyright(c) 2009, Garmin International
 */

ActivitySummaryConstants = {
    css: {
        toggleSelected: 'toggleSelected'
    }
    ,ids: {
        bpmButton:          '#bpmButton',
        bpmSummary:         '#bpmSummary',
        editSummaryButton:  '#editSummaryButton',
        hrZonesButton:      '#hrZonesButton',
        hrZonesSummary:     '#hrZonesSummary',
        paceButton:         '#paceButton',
        paceSummary:        '#paceSummary',
        pomButton:          '#pomButton',
        pomSummary:         '#pomSummary',
        powerZonesButton:   '#powerZonesButton',
        powerZonesSummary:  '#powerZonesSummary',
        speedButton:        '#speedButton',
        speedSummary:       '#speedSummary',
        wattsButton:        '#wattsButton',
        wattsSummary:       '#wattsSummary',
        summarySpeed:		'#summarySectionSpeed',
        summaryPace:		'#summarySectionPace'
    }
    ,buttonSummaryMap: {
        heartRate: {
            '#bpmButton':       '#bpmSummary',
            '#hrZonesButton':   '#hrZonesSummary',
            '#pomButton':       '#pomSummary'
        },
        timing: {
            '#paceButton':      '#paceSummary',
            '#speedButton':     '#speedSummary'
        }
    }
}

jQuery(document).ready(function(){
    var speedButton = jQuery(ActivitySummaryConstants.ids.speedButton);
    var speedSummary = jQuery(ActivitySummaryConstants.ids.speedSummary);
    var paceButton = jQuery(ActivitySummaryConstants.ids.paceButton);
    var paceSummary = jQuery(ActivitySummaryConstants.ids.paceSummary);
    var bpmButton = jQuery(ActivitySummaryConstants.ids.bpmButton);
    var bpmSummary = jQuery(ActivitySummaryConstants.ids.bpmSummary);
    var pomButton = jQuery(ActivitySummaryConstants.ids.pomButton);
    var pomSummary = jQuery(ActivitySummaryConstants.ids.pomSummary);
    var hrZonesButton = jQuery(ActivitySummaryConstants.ids.hrZonesButton);
    var hrZonesSummary = jQuery(ActivitySummaryConstants.ids.hrZonesSummary);
    var powerZonesButton = jQuery(ActivitySummaryConstants.ids.powerZonesButton);
    var powerZonesSummary = jQuery(ActivitySummaryConstants.ids.powerZonesSummary);
    var wattsButton = jQuery(ActivitySummaryConstants.ids.wattsButton);
    var wattsSummary = jQuery(ActivitySummaryConstants.ids.wattsSummary);
    var toggleSelectedClass = ActivitySummaryConstants.css.toggleSelected;
    var summarySpeed = jQuery(ActivitySummaryConstants.ids.summarySpeed);
    var summaryPace = jQuery(ActivitySummaryConstants.ids.summaryPace);
    
    function deselect(button, summary) {
        summary.hide();
        button.removeClass(toggleSelectedClass);
    }
    function select(button, summary) {
        summary.show();
        button.addClass(toggleSelectedClass);
    }
    
    // Speed/pace toggle
    speedButton.click(function() {
        deselect(paceButton, paceSummary);
        select(speedButton, speedSummary);
        summarySpeed.show();
    	summaryPace.hide();
        EventBus.shout(EventBus.Events.CHANGE_TIMING_UNIT, {
            field: 'measurementActivityField_directSpeed',
            unit: User.UNITS.speed // changes based on metric 
        });
    });
    paceButton.click(function() {
        deselect(speedButton, speedSummary);
        select(paceButton, paceSummary);
        summarySpeed.hide();
    	summaryPace.show();
        EventBus.shout(EventBus.Events.CHANGE_TIMING_UNIT, {
            field: 'measurementActivityField_directSpeed',
            unit: User.UNITS.pace // changes based on metric
        });
    });
    
    // HR toggle
    bpmButton.click(function() {
        deselect(pomButton, pomSummary);
        deselect(hrZonesButton, hrZonesSummary);
        select(bpmButton, bpmSummary);
        EventBus.shout(EventBus.Events.CHANGE_HEARTRATE_UNIT, {
            field: 'measurementActivityField_directHeartRate',
            unit: 'unitFormat_bpm'
        });
    });
    pomButton.click(function() {
        deselect(bpmButton, bpmSummary);
        deselect(hrZonesButton, hrZonesSummary);
        select(pomButton, pomSummary);
        EventBus.shout(EventBus.Events.CHANGE_HEARTRATE_UNIT, {
            field: 'measurementActivityField_directHeartRate',
            unit: 'unitFormat_percentMax'
        });
    });
    hrZonesButton.click(function() {
        deselect(pomButton, pomSummary);
        deselect(bpmButton, bpmSummary);
        select(hrZonesButton, hrZonesSummary);
        EventBus.shout(EventBus.Events.CHANGE_HEARTRATE_UNIT, {
            field: 'measurementActivityField_directHeartRate',
            unit: 'unitFormat_zones'
        });
    });
    
    // Power toggle
    wattsButton.click(function() {
        deselect(powerZonesButton, powerZonesSummary);
        select(wattsButton, wattsSummary);
        EventBus.shout(EventBus.Events.CHANGE_POWER_UNIT, {
            field: 'measurementActivityField_directPower',
            unit: 'unitFormat_watt'
        });
    });
    powerZonesButton.click(function() {
        deselect(wattsButton, wattsSummary);
        select(powerZonesButton, powerZonesSummary);
        EventBus.shout(EventBus.Events.CHANGE_POWER_UNIT, {
            field: 'measurementActivityField_directPower',
            unit: 'unitFormat_zones'
        });
    });
    
    // Automatically update speed/pace toggle when user changes activity type
    speedButton.hear(EventBus.Events.UPDATED_ACTIVITY_TYPE, function($self, data) {
        var activityTypeKey = data.key;
        var parentTypeKey = data.parent.key;
        
        // TODO need a mapping, or get this from the backend :l
        if(parentTypeKey == 'running' || activityTypeKey == 'running'
        || parentTypeKey == 'walking' || activityTypeKey == 'walking'
        || activityTypeKey == 'hiking' ) {
            paceButton.click();
        } else {
            speedButton.click();
        }
    });
});


