/**
 * @version 1.x
 * @package DJ-Events
 * @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
 * @license DJ-Extensions.com Proprietary Use License
 * @author url: http://dj-extensions.com
 * @author email contact@dj-extensions.com
 *
 */
window.onload = function() {
    if (window.jQuery) {
        (function ($) {

            $(document).ready(function () {

                // New location button
                $(document).on('click', '[data-input]', function (e) {

                    var $input = $('#' + $(this).attr('data-input'));
                    var $inputVal = $input.val();

                    switch (parseInt($inputVal)) {
                        case 0 :
                            $input.val(1);
                            $('#jform_location_id option:selected').removeAttr("selected");
                            $('#jform_location_id option:selected').trigger("liszt:updated")
                            $(document).trigger("leaflet:show");
                            $(this).addClass('btn-success');
                            $(this).text(Joomla.JText._('COM_DJEVENTS_USE_LOCATION'));

                            break;
                        case 1 :
                            $input.val(0);
                            $(this).removeClass('btn-success');
                            $(this).text(Joomla.JText._('COM_DJEVENTS_ADD_LOCATION'));
                            break;
                    }

                    $input.trigger('change');

                });


                // Location select change
                $(document).on('change', '#jform_location_id', function (e) {
                    var $val = jQuery(this).find('option:selected').val();
                    var $newLocationInput = $('#jform_new_location');
                    var $newLocationBtn = $('button[data-input=' + $newLocationInput.attr('id') + ']');

                    if(parseInt($val) > 0 && parseInt($newLocationInput.val()) === 1) {
                        $newLocationInput.val(0);
                        $newLocationBtn.removeClass('btn-success');
                        $newLocationBtn.text(Joomla.JText._('COM_DJEVENTS_ADD_LOCATION'));
                        $newLocationInput.trigger('change');
                        jQUery('#jform_online_event0').trigger('change');
                    }
                })

            });
        })(jQuery);

    }
}

