$(function(){
    var load_xml_flg = false;
    var cinemas = {};
    function load_xml() {
        var yymmdd = __get_yymmdd();
        var url = 'xml/' + yymmdd + '.xml';
        $.ajax({
            url : url,
            dataType : 'xml',
            success: function(data) {
                load_xml_flg = true;
                __parse_xml(data);
            }
        });
    }

    function __get_yymmdd() {
        var url = window.location.href;
        var yymmdd;
        if (url.match(/\?d=([0-9]+)/)) {
            yymmdd = RegExp.$1;
        } else {
            var dt = new Date();
            var yy = parseInt(dt.getFullYear()) - 2000;
            var mm = parseInt(dt.getMonth() + 1);
            var dd = parseInt(dt.getDate());
            if (mm < 10) {
                mm = '0' + mm;
            }
            if (dd < 10) {
                dd = '0' + dd;
            }
            yymmdd = new String(yy) + new String(mm) + new String(dd);
        }
        return yymmdd;
    }

    function __parse_xml(xml) {
        $('entry', xml).each(function() {
            var cinema = {};
            cinema.title = $('title', this).text();
            cinema.subtitle = $('subtitle', this).text();
            cinema.photo_l = $('photo_l', this).text();
            cinema.story = $('story', this).text();
            cinema.copyright = $('copyright', this).text();
            cinema.officialsite = $('officialsite', this).text();
            cinema.dlp = $('dlp', this).text();
            cinema.staffcast = $('staffcast', this).text();
            cinemas['id' + $('cinema_id', this).text()] = cinema;
        });
    }

    function add_class() {
        $("a img[alt='作品案内']").parent().addClass('popupCinemaInfo');
    }
    
    function attach_handler() {
        $('a.popupCinemaInfo').click(function(e) {
            e.preventDefault();
            var href = $(this).attr('href');
            var cinema_id;
            if (href.match(/cinema_id=([0-9]+)/)) {
                cinema_id = RegExp.$1;
            }
            if (cinema_id) {
                popup_cinema_info(cinema_id);
            }
        });
    }
    
    function popup_cinema_info(cinema_id) {
        if (!load_xml_flg) {
            setTimeout(function(){
                popup_cinema_info(cinema_id);
            }, 1000);
        }
        var cinema = cinemas['id' + cinema_id];
        if (cinema) {
            $('div#cinemaInfo').remove();
            $('<div id="cinemaInfo" style="position: relative; text-align: left;">' 
                + '<h2 style="position: absolute; top: 12px; left: 288px;">' + cinema.title + '</h2>'
                + '<h3 style="position: absolute; top: 30px; left: 288px;">' + cinema.subtitle + '</h3>'
                + '<div style="position: absolute; top: 12px; left: 12px; width: 264px; height: 180px;'
                + ' background-color: #000;'
                + ' background-position: center center; background-repeat: no-repeat;'
                + ' background-image: url(\'' + cinema.photo_l + '\')"></div>'
                + '<img class="simplemodal-close" style="position: absolute; right: 0; cursor: pointer" src="../img/btn_popup_close.png"/>'
                + '<div style="position: absolute; top: 68px; left: 288px; font-size: 11px; width:250px; height: 192px; overflow: auto;">' 
                + cinema.story + '<div>' 
                + cinema.staffcast + '</div>' + '</div>'
                + '<a href="' + cinema.officialsite + '" target="_blank">'
                + ' <img style="position: absolute; top: 200px; left: 200px; cursor: pointer" id="gotoOfficialSite" '
                + '  src="../img/btn_goto_official_site.png" />'
                + '</a>'
                + '<div style="position: absolute; top: 288px; left: 12px; font-size: 10px; font-family: Arial;">' + cinema.copyright + '</div>'
                + '<img style="position: absolute; top: 265px; left: 286px;" id="iconDegitalCinema" src="../img/icon_degital.png" />'
            + '</div>').appendTo($('body')).hide();
            if (cinema.dlp==0) {
                $('img#iconDegitalCinema').hide();
            }
            $('div#cinemaInfo').modal({
                opacity: 60,
                overlayCss: {backgroundColor: "#000"},
                containerCss:{
                    backgroundColor:"#fff",
                    borderColor:"#fff",
                    height:312,
                    padding:0,
                    width:570
                }
            });
        }
    }

    function init() {
        load_xml();
        add_class();
        attach_handler();
    }

    init();
});

