﻿jQuery(document).ready(function() {

    // Add date pickers to date range search
    // IE does not handle the datepicker's appearance animation very well
    // So for IE users, it'll just snap in (duration 0) - sorry!
    if (jQuery.browser.msie) {
        jQuery('.dateSearch').datepicker({
            changeMonth: true,
            changeYear: true,
            yearRange: '-0:+2',
            minDate: '+0d',
            maxDate: '+2y',
            dateFormat: 'dd-mm-yy',
            duration: 0
        });
    }
    else {
        jQuery('.dateSearch').datepicker({
            changeMonth: true,
            changeYear: true,
            yearRange: '-0:+2',
            minDate: '+0d',
            maxDate: '+2y',
            dateFormat: 'dd-mm-yy'
        });
    }

    // Show/hide dates button for search results
    jQuery('.datesControl a').click(function() {
        var $this = jQuery(this);
        if ($this.hasClass('button_show_dates')) {
            $this.removeClass('button_show_dates').addClass('button_hidedates');
            $this.parent().parent().parent().find('.dateLines').fadeIn('normal');
        }
        else {
            $this.removeClass('button_hidedates').addClass('button_show_dates');
            $this.parent().parent().parent().find('.dateLines').hide();
        }
        return false;
    });

    // The apply button for date searching - think this should be simplified!!!
    jQuery('.applyFilter').click(function() {

        // Get selected PROD links...
        var $prodLinks = jQuery('.byProdLink.selected');
        var prodLink = "";
        for (arrayItem in $prodLinks.get()) {
            if ($prodLinks.get(arrayItem).id != "showAllProds") {
                prodLink = "/p/" + $prodLinks.get(arrayItem).id;
            }
        }

        // Get selected EVENT links...
        var $eventLinks = jQuery('.byEventLink.selected');
        var eventLink = "";
        for (arrayItem in $eventLinks.get()) {
            if ($eventLinks.get(arrayItem).id != "showAllEvents") {
                eventLink = "/e/" + $eventLinks.get(arrayItem).id;
            }
        }

        // Get selected LOCATION links...
        var $locLinks = jQuery('.byLocLink.selected');
        var locLink = "";
        for (arrayItem in $locLinks.get()) {
            if ($locLinks.get(arrayItem).id != "showAllLocs") {
                locLink = "/l/" + $locLinks.get(arrayItem).id;
            }
        }

        // Make query string based on the above
        var linkText = prodLink + eventLink + locLink;

        // Get date range filter if using...
        if (jQuery('.dsFrom').val().length > 0) {
            linkText = linkText + "/sd/" + jQuery('.dsFrom').val();
        }
        if (jQuery('.dsTo').val().length > 0) {
            linkText = linkText + "/ed/" + jQuery('.dsTo').val();
        }

        // If any filters have been applied, change link ref and activate!
        if (linkText.length > 0) {
            this.href = "/buy-tickets" + linkText;
            //jQuery(this).html('SEARCHING, PLEASE WAIT...').attr('href', "defaultNew.aspx?" + linkText);
        }
        else {
            // User hasn't searched for anything - prompt to do so!
            alert('Please choose some productions, locations, or dates');
            return false;
        }

    });

    // Hide expandable dates if there's more than one search result
    if (jQuery('table.dateLines').length > 1) {
        jQuery('table.dateLines').hide();
        jQuery('.datesControl a').removeClass('button_hidedates').addClass('button_show_dates');
    }

    // After a search, we return with filters stored in hidden fields - retrieve and select! (PRODS)
    if (jQuery('#ctl00_MainContent_ProductionFilterField').val().length > 0) {
        var prods = jQuery('#ctl00_MainContent_ProductionFilterField').val().split(",");
        for (prod in prods) {
            jQuery('#' + prods[prod]).addClass('selected');
        }
    }

    // After a search, we return with filters stored in hidden fields - retrieve and select! (EVENTS)
    if (jQuery('#ctl00_MainContent_EventFilterField').val().length > 0) {
        var prods = jQuery('#ctl00_MainContent_EventFilterField').val().split(",");
        for (prod in prods) {
            jQuery('#' + prods[prod]).addClass('selected');
        }
    }

    // After a search, we return with filters stored in hidden fields - retrieve and select! (LOCATIONS)
    if (jQuery('#ctl00_MainContent_LocationFilterField').val().length > 0) {
        var locs = jQuery('#ctl00_MainContent_LocationFilterField').val().split(",");
        for (loc in locs) {
            jQuery('#' + locs[loc]).addClass('selected');
        }
    }

    // After a search, we return with filters stored in hidden fields - retrieve and select! (DATES)
    if (jQuery('#ctl00_MainContent_StartDateFilterField').val().length > 0) {
        var date = jQuery('#ctl00_MainContent_StartDateFilterField').val();
        jQuery('.dsFrom').val(date);
    }
    if (jQuery('#ctl00_MainContent_EndDateFilterField').val().length > 0) {
        var date = jQuery('#ctl00_MainContent_EndDateFilterField').val();
        jQuery('.dsTo').val(date);
    }

    // Ensure that when showing double line filter summary (ie. with date),
    // clicking on any part of the header opens the filter boxes...
    jQuery('p.showingNote').click(function() {
        jQuery(this).parent().find('h2').click();
    });

    // Info dialog boxes for context booking info!
    jQuery('#infoBoxPerfUnavailable').dialog({ modal: true, height: 100, width: 450, autoOpen: false, resizable: false });
    jQuery('a.perfUnavailable').click(function() {
        jQuery('#infoBoxPerfUnavailable').dialog('open');
        return false;
    });
    jQuery('#infoBoxCaptioned').dialog({ modal: true, height: 250, width: 450, autoOpen: false, resizable: false });
    jQuery('a.captionedLink').click(function() {
        jQuery('#infoBoxCaptioned').dialog('open');
        return false;
    });
    jQuery('#infoBoxAudioDescribed').dialog({ modal: true, height: 380, width: 450, autoOpen: false, resizable: false });
    jQuery('a.audioDescribedLink').click(function() {
        jQuery('#infoBoxAudioDescribed').dialog('open');
        return false;
    });

    // Configure datepickers to ensure that values are always valid...
    // ie. FROM must be earlier than TO
    jQuery('#ctl00_MainContent_DateSearchFrom').change(function() {
        var dateFrom = jQuery('#ctl00_MainContent_DateSearchFrom').datepicker('getDate');
        var dateTo = jQuery('#ctl00_MainContent_DateSearchTo').datepicker('getDate');
        if (dateFrom != null && (dateTo == null || dateTo < dateFrom)) {
            jQuery('#ctl00_MainContent_DateSearchTo').datepicker('setDate', dateFrom);
        }
    });
    jQuery('#ctl00_MainContent_DateSearchTo').change(function() {
        var dateFrom = jQuery('#ctl00_MainContent_DateSearchFrom').datepicker('getDate');
        var dateTo = jQuery('#ctl00_MainContent_DateSearchTo').datepicker('getDate');
        if (dateTo != null && (dateFrom == null || dateTo < dateFrom)) {
            jQuery('#ctl00_MainContent_DateSearchFrom').datepicker('setDate', dateTo);
        }
    });

    // Configure divider borders for date search.
    jQuery(".dateLines2").find("tr:first td").css('border', '0px');

});

