﻿/*
* Offline_Data 
* Used to pull Market/Theatre information so users can easily find AMC Theatres to lookup
* movie information at MovieTickets.com or Fandango.com. This will be deployed when site 
* experiences hard errors (database down, CMS application fails, etc).
*/

$(document).ready(function() {
    var offline_data;
    var ticketURL;

    var selectMarketText = '--Select Market';
    var selectTheatreText = '--Select Theatre';
    var offline_xml = '/uploadedfiles/Movies_and_Events/Theatre_Offline.xml';
    var mt_URL = 'http://www.movietickets.com/house_detail.asp?house_id=';
    var fandango_URL1 = 'http://www.fandango.com/';
    var fandango_URL2 = '/theaterpage?fromTsp=1';


    $.get(offline_xml, function(data) {
        offline_data = data;
        var marketList = $('#markets');
        $('DMAMarket', offline_data).each(function() {
            $('<option>').text($(this).attr('Name')).appendTo(marketList);
        });
    }, 'xml');

    $('#markets').change(function() {
        var val = $(this).val();
        var theatreList = $('#theatres').empty();
        var theatreID;
        var theatreName;
        var fandangoUnitCode;
        var selectedTheatreLocation;

        $('#theatres').attr('disabled', true);
        if (val != '0') {
            $('#theatres').attr('disabled', false);
        }
        $('<option>').val('0').text(selectTheatreText).appendTo(theatreList);

        $('DMAMarket', offline_data).filter(function() {
            return val == $(this).attr('Name');
        }).find('Theatre').each(function() {
            $(this).find("TheatreID").each(function() {
                theatreID = $(this).text();
            });
            $(this).find("FandangoUnitCode").each(function() {
                fandangoUnitCode = $(this).text();
            });
            $(this).find("Name").each(function() {
                theatreName = $(this).text();
            });
            $(this).find("OnlineTicketingIndicator").each(function() {
                var onlineTicketIndicator = $(this).text();
                if (onlineTicketIndicator == 'Y') {
                    ticketURL = mt_URL + theatreID;
                }
                else {
                    ticketURL = fandango_URL1 + theatreName + '_' + fandangoUnitCode + fandango_URL2;
                }
            });
            $(this).find("Name").each(function() {
                $('<option>').val(ticketURL).text(theatreName).appendTo(theatreList);
            });
        });
    });

    $('#btnSubmitTheatre').click(function() {
        selectedTheatreLocation = $('#theatres').val();
        if (selectedTheatreLocation == '' || selectedTheatreLocation == '0') {
            $('#errors').text('Please select a theatre to continue.');
        } else {
            window.location = selectedTheatreLocation;
        }
    });

});
