$(document).ready(function(){
    var dialog_url = "edit_cinema_sched";
    var $sale_diag = $('<div id="sale_diag"></div>').dialog({
        autoOpen: false,
        modal: true,
        width: '1000px',
        buttons: {
            'Save': function(event, ui) {
                $.ajax({
                    url: js_urls.edit_schedule_url + dialog_url + '.php',
                    data: $('#im_cinemas_frm').serialize(),
                    type: 'POST',
                    cache: false,
                    success: function(r) {
                        $sale_diag.html(r);
                    }
                });
            }
        }
    });


    $('.add_movie').on('click', function(ev){
        ev.preventDefault();

        dialog_url = 'add_movie';
        var location = $(this).attr('rel');

        $.ajax({
            url: js_urls.add_movie_url,
            data: 'location=' + location,
            cache: false,
            type: 'POST',
            success: function(r) {
                $sale_diag.html(r);
                $sale_diag.dialog("open");
            }
        });

        return false;
    });

    $('.edit_cinema_sched').on('click', function(ev){
        ev.preventDefault();

        dialog_url = 'edit_cinema_sched';
        var id = $(this).attr('rel');

        $.ajax({
            url: js_urls.edit_cinema_sched_url,
            data: 'sched_id=' + id,
            cache: false,
            type: 'POST',
            success: function(r) {
                $sale_diag.html(r);
                $sale_diag.dialog("open");
            }
        });

        return false;
    });

    $('.add_cinema_sched').on('click', function(ev){
        ev.preventDefault();
        dialog_url = 'add_cinema_sched';
        var re = $(this).attr('rel');
        var rel = re.split('|')
        var location = rel[0];
        var movie_code = rel[1];
        $.ajax({
            url: js_urls.add_cinema_sched_url,
            data: 'location=' + location + '&movie_code=' + movie_code,
            cache: false,
            type: 'POST',
            success: function(r) {
                $sale_diag.html(r);
                $sale_diag.dialog("open");
            }
        });

        return false;
    });


});