﻿window.addEvent('domready', function() {
    if (typeof sIFR == "function") {
        sIFR.replaceElement(named({ sSelector: ".ShowtimesHeaderTitle", sFlashSrc: "Flash/tradegothic.swf", sColor: "#ffffff", sLinkColor: "#ffffff", sBgColor: "#FFFFFF", sHoverColor: "#ffffff", sCase: "upper", sWmode: "transparent" }));
    }

    $('ShowtimesHeaderSelect').set('tween', { duration: 'short' });
    $('ShowtimesHeaderSelect').addEvent('mouseenter', function() {
        $('ShowtimesHeaderSelect').tween('height', 60);
    });
    $('ShowtimesHeaderSelect').addEvent('mouseleave', function() {
        $('ShowtimesHeaderSelect').tween('height', 0);
    });

    AddExpandEvents();

});

//Show the specified expanded info box
function DisplayExpandedShowtime(expandedShowtimeElement) {
    $('ShowtimeExpanded_' + expandedShowtimeElement).setStyle('display', 'block');
    DisplayTimeLeft(expandedShowtimeElement);

}

//This function calculates the time difference between the show
//time and the current time. It displays the appropriate message
//based on that calculation.
function DisplayTimeLeft(expandedShowtimeElement) {
    //Check for all required values and their validity
    if ( !(IsValidTimeLeftValue(document.getElementById('ShowtimesHiddenDuration_' + expandedShowtimeElement))
        && IsValidTimeLeftValue(document.getElementById('ShowtimesHiddenYear_' + expandedShowtimeElement))
        && IsValidTimeLeftValue(document.getElementById('ShowtimesHiddenMonth_' + expandedShowtimeElement))
        && IsValidTimeLeftValue(document.getElementById('ShowtimesHiddenDay_' + expandedShowtimeElement))
        && IsValidTimeLeftValue(document.getElementById('ShowtimesHiddenHour_' + expandedShowtimeElement))
        && IsValidTimeLeftValue(document.getElementById('ShowtimesHiddenMinute_' + expandedShowtimeElement))) ) {
        return false;
    }
     
    var duration = document.getElementById('ShowtimesHiddenDuration_' + expandedShowtimeElement).value;
    var showtimeDate = new Date(document.getElementById('ShowtimesHiddenYear_' + expandedShowtimeElement).value,
                                document.getElementById('ShowtimesHiddenMonth_' + expandedShowtimeElement).value - 1,
                                document.getElementById('ShowtimesHiddenDay_' + expandedShowtimeElement).value,
                                document.getElementById('ShowtimesHiddenHour_' + expandedShowtimeElement).value,
                                document.getElementById('ShowtimesHiddenMinute_' + expandedShowtimeElement).value);
    var currentDate = new Date();
    var dateDifference = showtimeDate - currentDate;

    //Millisecond values
    var ONE_DAY = 1000 * 60 * 60 * 24;
    var ONE_HOUR = ONE_DAY / 24;
    var ONE_MINUTE = ONE_HOUR / 60;
    var ONE_MONTH = ONE_DAY * 30;
    
    //If the difference is a minute or less
    if (dateDifference <= 60000) {
        //If the time passed since show time is greater than
        //the duration of the show.
        if (Math.abs(dateDifference) > (duration * ONE_MINUTE))
            $('ShowtimesExpandedTimeLeft_' + expandedShowtimeElement).set('text', '(missed)');
        else
            $('ShowtimesExpandedTimeLeft_' + expandedShowtimeElement).set('text', '(on now)');
    }
    else if (dateDifference > ONE_MONTH) {
        $('ShowtimesExpandedTimeLeft_' + expandedShowtimeElement).set('text', '(more than a month from now)');
    }
    else {
        var days = Math.floor(dateDifference / ONE_DAY);
        dateDifference = dateDifference - days * ONE_DAY;
        var hours = Math.floor(dateDifference / ONE_HOUR);
        dateDifference = dateDifference - hours * ONE_HOUR;
        var minutes = Math.floor(dateDifference / ONE_MINUTE);

        var result = 'in ';
        if (days != 0) {
            result += days;
            if (days == 1)
                result += ' day, ';
            else
                result += ' days, ';
        }
        if (hours != 0) {
            result += hours;
            if (hours == 1)
                result += ' hour, ';
            else
                result += ' hours, ';
        }
        result += minutes;
        if (minutes == 1)
            result += ' minute';
        else
            result += ' minutes';

        $('ShowtimesExpandedTimeLeft_' + expandedShowtimeElement).set('text', '(' + result + ')');
    }
}

//A helper function to check the validity of a hidden field
//and it's value.
function IsValidTimeLeftValue(element) {
    if (element == null) return false;
    if (element.value == '') return false;
    if (element.value != parseInt(element.value)) return false;
    return true;
}

function UpdateAjaxSchedule(timezone, body, timezoneSelected, timezoneOptions, sid) {
    var req = new Request({
        method: 'get',
        url: 'Ajax/ShowtimesAjax.aspx',
        data: { 'timezone': timezone, 'body': body, 'timezoneSelected': timezoneSelected, 'timezoneOptions': timezoneOptions, 'sid': sid },
        onRequest: function() { $(body).innerHTML = '<div class=\"ShowtimesLoading\">loading...</div>'; },
        //update: $('ShowtimesBody'),
        onSuccess: function(text) {
            //alert(text);
            $(body).innerHTML = text.split(":::::")[0];
            $(timezoneSelected).innerHTML = text.split(":::::")[1];
            $(timezoneOptions).innerHTML = text.split(":::::")[2];

            AddExpandEvents();
        }
    }).send();
}

function AddExpandEvents() {
    $$('.ShowtimesExpanded').each(function(element) {
        element.addEvent('click', function() {
            element.setStyle('display', 'none');
        });
    });
}