$(document).ready(setupExpands);
function setupExpands() {
    $('a.trailers').click(clickTrailers);
    $('a.theater-expando').click(clickTheater);
}
function clickTrailers(event) {
    event.preventDefault();
    var urlparts = $(this).attr('href').split('/');
    var movieid = urlparts[urlparts.length - 2];

    var hideSpeed = 'slow';
    var clicked = $(this);
    var clickedWasOpen = clicked.hasClass('open');
    var loadPage = '/services/GetMovieDetailView.aspx?movieid=' + movieid;

    $('a.trailers').removeClass('open');

    if (clickedWasOpen) { hideContent(); }
    else if ($('div.expando').length > 0) { hideContent(); loadContent(); }
    else { loadContent(); }

    function loadContent() {
        //if ($.browser.msie && $.browser.version.substr(0, 3) == '6.0') { clicked.parent().css({ position: "absolute", bottom: "334px", right: "0px" }); }
        clicked.addClass('open');
        clicked.parents('li:first').after('<div class="expando-wrap"></div>');
        $('.expando-wrap').append('<div class="expando"></div>');
        addLoader();
        $('div.expando').load(loadPage, showNewContent);
    }
    function addLoader() {
        $('.expando-wrap').append('<div class="loader"><img src="../assets/global/reelzLoading.gif" /></div>').fadeIn('normal');
    }
    function removeLoader() { $('div.loader').fadeOut('normal').remove(); }
    function showNewContent() { removeLoader(); }
    function removeExpando() { $('div.expando-wrap').remove(); }
    function hideContent() { $('div.expando-wrap *').fadeOut(hideSpeed, removeExpando()); }
}

function clickTheater(event) {
    event.preventDefault();
    var urlparts = $(this).attr('href').split('/');
    var theaterid = urlparts[urlparts.length - 2];
    var hideSpeed = 'slow';
    var clicked = $(this);
    var clickedWasOpen = clicked.hasClass('open');

    var select = document.getElementById("selDay");
    var loadPage = '/services/Theater/TheaterDetail.aspx?format=html&date=' + select.options[select.selectedIndex].value + '&theaterid=' + theaterid;

    $('a.theater-expando').removeClass('open');

    if (clickedWasOpen) { hideContent(); }
    else if ($('div.expando-theater').length > 0) { hideContent(); loadContent(); }
    else { loadContent(); }

    function loadContent() {
        //if ($.browser.msie && $.browser.version.substr(0, 3) == '6.0') { clicked.parent().css({ position: "absolute", bottom: "334px", right: "0px" }); }
        clicked.addClass('open');
        clicked.parents('div:first').append('<div class="theater-expando-wrap"></div>');
        $('.theater-expando-wrap').append('<div class="expando-theater"></div>');
        addLoader();
        $('div.expando-theater').load(loadPage, showNewContent);
    }
    function addLoader() {
        $('.theater-expando-wrap').append('<div class="loader"><img src="../assets/global/reelzLoading.gif" /></div>').fadeIn('normal');
    }
    function removeLoader() { $('div.loader').fadeOut('normal').remove(); }
    function showNewContent() { removeLoader(); $('a.trailers').click(clickTrailers); }
    function removeExpando() { $('div.theater-expando-wrap').remove(); }
    function hideContent() { $('div.theater-expando-wrap *').fadeOut(hideSpeed, removeExpando()); }
}

function changeDay(select, theaterid) {
    var hideSpeed = 'slow';
    var loadPage = '/services/Theater/TheaterDetail.aspx?format=html&theaterid=' + theaterid + '&date=' + select.options[select.selectedIndex].value;
    var height;
    if ($('#movieshowtimes').hasClass('open')) { height = $('div.movieshowtimes-wrap *').css("height"); $('#movieshowtimes').removeClass('open'); hideContent(); }
    loadContent();

    function loadContent() {
        // if ($.browser.msie && $.browser.version.substr(0, 3) == '6.0') { clicked.parent().css({ position: "absolute", bottom: "334px", right: "0px" }); }
        $('#movieshowtimes').addClass('open');
        $('#movieshowtimes').append('<div class="movieshowtimes-wrap"></div>');
        $('.movieshowtimes-wrap').append('<div class="movieshowtimes-expando"></div>');
        addLoader();
        $('div.movieshowtimes-expando').load(loadPage, showNewContent);
    }
    function addLoader() {
        if (height) $('.movieshowtimes-wrap').css("height", height);
        $('.movieshowtimes-wrap').append('<div class="loader"><img src="../assets/global/reelzLoading.gif" /></div>').fadeIn('normal');
    }
    function removeLoader() { $('.movieshowtimes-wrap').css("height", ""); $('div.loader').fadeOut('normal').remove(); }
    function showNewContent() { removeLoader(); $('a.trailers').click(clickTrailers); }
    function removeExpando() { $('div.movieshowtimes-wrap').remove(); }
    function hideContent() { $('div.movieshowtimes-wrap *').fadeOut(hideSpeed, removeExpando()); }
}
function selectAll(checked) {
    var movieNameCB = $('.cb-moviename');
    for (var i = 0; i < movieNameCB.length; i++) {
        movieNameCB[i].checked = checked;
    }
}
var dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

function getDate(i) {
    var date = new Date();
    date.setDate(date.getDate() + i);
    var month = date.getMonth() + 1;
    var today = month + "/" + date.getDate() + "/" + date.getFullYear();
    return today
}
function getFormattedDate(i) {
    var date = new Date();
    date.setDate(date.getDate() + i);
    return dayNames[date.getDay()] + ", " + date.getDate() + " " + monthNames[date.getMonth()] + " " + date.getFullYear();
}