// path fix
var _lb = window.location.pathname;
if (_lb.match(/\/tfmtop\/(.*)/)) {
    _lb = _lb.replace(/\/tfmtop\/(.*)/, "/tfmtop/");
} else if (_lb.match(/\/dev\/(.*)/)) {
    _lb = _lb.replace(/\/dev\/(.*)/, "/dev/");
} else {
    _lb = '/';
}

// colorbox
$(document).ready(function(){
    $(".request").livequery(function() {
        $(".request").colorbox({width:"840px", height:"500px", iframe:true});
        $("#click").click(function(){
            $('#click').css({"background-color":"#f00", "color":"#fff", "cursor":"inherit"});
            return false;
        });
    });
});

// toggle
$(function(){
    //$("#sliding1").each(function(){
    //    $(this).css("height",$(this).height()+"px");
    //});

    $("#slidingBtn1").click(function() {
        $("#sliding1").slideToggle('slow');
        if ($(this).hasClass('up')) {
            $(this).removeClass('up');
            $(this).addClass('down');
        } else if ($(this).hasClass('down')) {
            $(this).removeClass('down');
            $(this).addClass('up');
        }
    });
});

// fade
$(document).ready(fadefn());
function fadefn() {
    $("#fade").livequery(function() {
        $('#fade').innerfade({
            speed: 3000,
            timeout: 5000,
            type: 'sequence'
        });
    });
}


// update timeTable
$("#todayPgm #timetable .now #program dl dt p").livequery(function() {
    // get server datetime
    var server_time = $.ajax({
        url   : _lb + "shared/php/nowtime.php",
        async : false,
        cashe : false
    }).responseText;

    var server_date = new Date(server_time);
    var local_date  = new Date();
    var diff_time = local_date.getTime() - server_date.getTime();

    // timer
    $.timer(1000, function (timer) {

        var date = new Date();
        var date = new Date(date.getTime() - diff_time);

        var end = getEnd(date);

        if (date.getTime() > end.getTime()) {
            replaceAjax(date);
            timer.reset(end.getTime() - date.getTime());
            timer.stop();
        }
    });

});

// return Date object
function getEnd(date) {
    var onair_time = $("#todayPgm #timetable .now #program dl dt p").html();
    // The date change is five o'clock. 
    var date2 = new Date(date.getTime() - (5 * 60 * 60 * 1000));

    var end = new Date(date2.getFullYear(),
                       date2.getMonth(),
                       date2.getDate(),
                       onair_time.substring(6, 8),
                       onair_time.substring(9, 11),
                       0);
    return end;
}

// get timeTableXML
function replaceAjax(date) {
    // The date change is five o'clock. 
    var date2 = new Date(date.getTime() - (5 * 60 * 60 * 1000));
    var option = { 
        url      : _lb + "shared/xml/timetable.xml?y="+date2.getFullYear()+"&m="+(date2.getMonth()+1)+"&d="+date2.getDate(),
        dataType : "xml",
        cashe    : false,
        success  : function(xml) {
            replaceHTML(xml, date);
        }
    };

    $.ajax(option);
}

// replacement
function replaceHTML(xml, date) {

    var flush = parseXML(xml, date);

    $("#todayPgm").fadeOut("slow", function () {
        $("#todayPgm #timetable .before dl").html(flush['past'] ? flush['past'] : '');
        if (flush['now']) {
            $("#todayPgm #timetable .now").html(flush['now']);
        } else {
            $("#todayPgm #timetable .now").html('<img src="'+_lb+'shared/img/standby.gif" width="178" height="190" alt="STANDBY..." class="standby" />');
        }
        $("#todayPgm #timetable .after").html(flush['future1'] ? flush['future1'] : '');
        $("#todayPgm #sliding1").html(flush['future2'] ? flush['future2'] : '');
        if (!flush['future_flag']) {
            $("#todayPgm p.continue").html('');
        }

        return $("#todayPgm").fadeIn("slow", fadefn());
    });
}

// parse
function parseXML(xml, date) {

    // The date change is five o'clock. 
    var date2 = new Date(date.getTime() - (5 * 60 * 60 * 1000));

    var year  = date2.getFullYear();
    var month = date2.getMonth();
    var day   = date2.getDate();

    var flush = Array();
    flush['future1']     = '';
    flush['future2']     = '';
    flush['future_flag'] = false;

    $(xml).find("item").each(function() {
        var onair_time = $(this).find("onair_time").text();
        var start = new Date(year,
                             month,
                             day,
                             onair_time.substring(0, 2),
                             onair_time.substring(3, 5),
                             0);
        var end   = new Date(year,
                             month,
                             day,
                             onair_time.substring(6, 8),
                             onair_time.substring(9, 11),
                             0);

        // past
        if (date.getTime() > end.getTime()) {
            flush['past'] = '<dt>'+$(this).find("onair_time").text()+'</dt>';
            if ($(this).find("site_url").text()) {
                flush['past'] += '<dd><a href="'+$(this).find("site_url").text()+'"';
                if ($(this).find("site_url_blank").text()) {
                    flush['past'] += ' target="_blank"';
                }
                flush['past'] += '>'+$(this).find("program_name").text()+'</a></dd>';
            } else {
                flush['past'] += '<dd>'+$(this).find("program_name").text()+'</dd>';
            }
        }

        // now
        if (date.getTime() >= start.getTime() && date.getTime() <= end.getTime()) {
            flush['now'] =
                '<h3>放送中</h3>'+
                '<div id="program">'+
                    '<dl>'+
                        '<dt class="pTitle">'+
                        '<p>'+$(this).find("onair_time").text()+'</p>';

            if ($(this).find("site_url").text()) {
                flush['now'] += '<strong><a href="'+$(this).find("site_url").text()+'"';
                if ($(this).find("site_url_blank").text()) {
                    flush['now'] += ' target="_blank"';
                }
                flush['now'] += '>'+$(this).find("program_name").text()+'</a></strong>';
            } else {
                flush['now'] += '<strong>'+$(this).find("program_name").text()+'</strong>';
            }

            if ($(this).find("message_url").text()) {
                if ($(this).find("messagebox_use").text() == 1) {
                    flush['now'] += '<a class="request" href="'+$(this).find("message_url").text()+'"><img src="'+_lb+'shared/img/btn_message.gif" width="110" height="20" alt="メッセージを送る" class="btnMsg swap" /></a>';
                } else {
                    flush['now'] += '<a href="'+$(this).find("message_url").text()+'" target="_blank"><img src="'+_lb+'shared/img/btn_message.gif" width="110" height="20" alt="メッセージを送る" class="btnMsg swap" /></a>';
                }
            }

            flush['now'] +=
                        '</dt>'+
                        '<dd class="pImg">';

            if ($(this).find("site_url").text()) {
                flush['now'] += '<a id="fade" href="'+$(this).find("site_url").text()+'"';
                if ($(this).find("site_url_blank").text()) {
                    flush['now'] += ' target="_blank"';
                }
                flush['now'] += '>';
            } else {
                flush['now'] += '<span id="fade">';
            }

            
            if ($(this).find("personality").text()) {
                $(this).find("personality").each(function() {
                    if ($(this).find("photo").text()) {
                        flush['now'] += '<img src="'+$(this).find("photo").text()+'" width="60" height="60" alt="'+$(this).find("name").text()+'" />';
                    } else {
                        flush['now'] += '<img src="'+_lb+'personality/img/noimage.jpg" width="60" height="60" alt="" />';
                    }
                });
            } else {
                flush['now'] += '<img src="'+_lb+'personality/img/noimage.jpg" width="60" height="60" alt="" />';
            }

            if ($(this).find("site_url").text()) {
                flush['now'] += '</a>';
            } else {
                flush['now'] += '</span>';
            }

            flush['now'] +=
                        '</dd>'+
                        '<dd class="personality">';

            var personality_name = '';
            $(this).find("personality").each(function() {
                if (personality_name == '') {
                    personality_name += $(this).find("name").text();
                } else {
                    personality_name += '&nbsp;/&nbsp;'+$(this).find("name").text();
                }
            });
            flush['now'] += personality_name;

            flush['now'] +=
                        '</dd>'+
                        '<dd class="todayInfo">'+$(this).find("comment").text()+'</dd>'+
                    '</dl>'+
                    '<table>'+
                        '<caption>番組コーナー</caption>'+
                        '<tr><th>時間</th><th>コーナータイトル</th></tr>';

            $(this).find("corner").each(function() {
                var corner_time     = $(this).find("corner_time").text();
                var cornerEnd = new Date(year,
                                          month,
                                          day,
                                          corner_time.substring(6, 8),
                                          corner_time.substring(9, 11),
                                          0);

                if (date.getTime() < cornerEnd.getTime()) {
                    flush['now'] += '<tr><td class="time">'+$(this).find("corner_start").text()+'</td>';
                    if ($(this).find("corner_url").text()) {
                        flush['now'] += '<td><a href="'+$(this).find("corner_url").text()+'"';
                        if ($(this).find("corner_url_blank").text()) {
                            flush['now'] += ' target="_blank"';
                        }
                        flush['now'] += '>'+$(this).find("corner_name").text()+'</a></td></tr>';
                    } else {
                        flush['now'] += '<td>'+$(this).find("corner_name").text()+'</td></tr>';
                    }
                }
            });

            flush['now'] +=
                    '</table>'+
                '</div>';
        }

        // future
        if ((date.getTime() < start.getTime()) && flush['future1']) {
            flush['future_flag'] = true;
        }

        if ((date.getTime() < start.getTime()) && !flush['future1']) {
            flush['future1'] =
                '<dl class="clearfix">'+
                    '<dt>'+$(this).find("onair_time").text()+'</dt>';
            if ($(this).find("site_url").text()) {
                flush['future1'] += '<dd><a href="'+$(this).find("site_url").text()+'"';
                if ($(this).find("site_url_blank").text()) {
                    flush['future1'] += ' target="_blank"';
                }
                flush['future1'] += '>'+$(this).find("program_name").text()+'</a></dd>';
            } else {
                flush['future1'] += '<dd>'+$(this).find("program_name").text()+'</dd>';
            }

            flush['future1'] +=
                '</dl>';
        }

        if ((date.getTime() < start.getTime()) && flush['future_flag']) {
            flush['future2'] +=
                '<dl class="clearfix">'+
                    '<dt>'+$(this).find("onair_time").text()+'</dt>';
            if ($(this).find("site_url").text()) {
                flush['future2'] += '<dd><a href="'+$(this).find("site_url").text()+'"';
                if ($(this).find("site_url_blank").text()) {
                    flush['future2'] += ' target="_blank"';
                }
                flush['future2'] += '>'+$(this).find("program_name").text()+'</a></dd>';
            } else {
                flush['future2'] += '<dd>'+$(this).find("program_name").text()+'</dd>';
            }

            flush['future2'] +=
                '</dl>';
        }
    });

    return flush;
}


