$(document).ready(function(){
    $('.chart').hide();
    
    // Load carousel on home page only
    if ($('#feature').length)
    {
        $('#feature').jcarousel({
            auto: 3,
            scroll: 1,
            wrap: 'both'
        });
    }
    
    $('#show-charts').click(function(){
        $('.chart').each(function(){
            var chartID = $(this).attr('id');
            var salonID = chartID.substr(5);
            var chart = $(this);
            
            $(this).show();
            
            $.ajax({
                url:      '/ajax/getPurchaseHistoryLastYearJSON.php',
                data:     {salonID: salonID}, 
                dataType: 'json',
                type:     'post',
                success:  function(result){
                    $.plot(chart,result,{xaxis: {mode:"time"}});
                    $('.purchase-history').hide();
                }
            });
        });
    });
    
    $('#get-assistance').click(function(){
        $('#Welcome').toggle();
    });
    
    $('#select-all-managers').live('click',function(){
        $('#managers-list-form input').each(function(index) {
            $(this).attr('checked',true);
            UpdateSalonAccessKPSS($(this).get(0));
        });
    });
    
    $('#deselect-all-managers').live('click',function(){
        $('#managers-list-form input').each(function(index) {
            $(this).attr('checked',false);
            UpdateSalonAccessKPSS($(this).get(0));
        });
    });

    $('#salon-toggle').live('change',function(){
        var page = $('#current-page').val();
        var url = '/index.php?Page=' + page + '&SalonID=' + $(this).val();
        window.location = url;
    });

    $('.remove-assigned-salon').live('click',function(){
        $(this).parent().addClass('remove-me');
        $('#dialog-remove-assigned-confirm').show();
        $('#dialog-remove-assigned-confirm').dialog({
            resizable: false,
            modal: true,
            buttons: {
                "Remove this salon": function(){
                    $(this).dialog("close");
                    var salonid = $('.remove-me > img').attr('id');
                    var uid = $('#selected-uid').val();
                    
                    $.post('/ajax/AssignedSalonsRemove.php',{salonid: salonid, uid: uid},function(data){
                        if (data == 'success')
                        {
                            $('#' + salonid).parent().hide();
                        }
                        else
                        {
                            alert('There was a problem removing this salon. Please try again.');
                        }
                    });
                },
                Cancel: function() {
                    $('.remove-me').removeClass('remove-me');
                    $(this).dialog("close");
                }
            }
        });
    });
});
