﻿


function rotateTabs() {
    $('#rotate > ul').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 8000);
}

$(document).ready(function () {
rotateTabs();
$('.buttons a').hide();

// MAKE EVERYTHING THE SAME HEIGHT

    // FUNCTION TO COMPARE TWO ELEMENTS AND SET THE GREATEST HEIGHT ACRPSS BOTH
    function compareWidgetHeights(selector1, selector2) {
        var a = $(selector1).innerHeight();
        var b = $(selector2).innerHeight();
        
        if( a <= b ) {
            var setHeights = 'min-height: ' + b + 'px;height: auto !important; height: ' + b + 'px;';
        } else {
            var setHeights = 'min-height: ' + a + 'px;height: auto !important; height: ' + a + 'px;';
        }

        $(selector1).attr('style',setHeights);
        $(selector2).attr('style',setHeights);
    }
    
     
    // FUNCTION TO FIND LARGEST ITEM AND APPLY TO ALL MATCHED ELEMENTS
    function evenHeights(selector) {
    
        // set max height to 0
        var max = 0;
        var item = 0;
        
        // iterate through each matched element and get its height
        $(selector).each(
            function() {
                var item = $(this).height();
                
                // compare the current largest height to the current items height
                // if the item is larger, reset the max height to the items height
                if ( max < item )  {
                    max = item;
                }
        });
        
        // create the style declaration
        var cssHeights = 'min-height: ' + max + 'px;height: auto !important; height: ' + max + 'px;';
        // apply the style declaration to each matched element
        $(selector).attr('style',cssHeights);
    }

    compareWidgetHeights('.home #newsWidget', '.home #donatePanel');
    compareWidgetHeights('.home #registrationWidget', '.home #competitionWidget');
    compareWidgetHeights('.playYourPart #centralTopWidgets .large', '.playYourPart #centralTopWidgets .small');

    evenHeights("#homeRightPanel .widgetBox");
    evenHeights("#centralBottomWidgets .widgetBox");
    evenHeights("#bottomWidgets .widgetBox");
    evenHeights("#footerWidgets .widgetBox");

// FONT SIZE CONTROLS
    initFontSize();

    $('input:text').hint();

    $('.increase').bind("click", function(e){
      fontIncrease();
      return false;
    });

    $('.decrease').bind("click", function(e){
      fontReduce();
      return false;
    });

    $(".lightbox").lightbox({fitToScreen: true});



// MARLOWE ON LOCATION EVENTS

    $("#marloweEventsBtn").bind("click", function(e) {
        $("#marloweEvents").show();
        $("#kentEvents").hide();
        $("#marloweEventsBtn").addClass("current");
        $("#kentEventsBtn").removeClass("current");
        e.preventDefault();
    });

    $("#kentEventsBtn").bind("click", function(e) {
        $("#kentEvents").show();
        $("#marloweEvents").hide();
        $("#kentEventsBtn").addClass("current");
        $("#marloweEventsBtn").removeClass("current");
        e.preventDefault();
    });
    
    
    // load date picker and set the min date to be today 
    $(".marloweInMotion .calendar").datepicker({
    onSelect: function(dt) {
        getShows(dt);
        return false;
    },
    minDate: -20
    });    
});

    function getShows(dt){
        alert("Calling Sagittarius.ENTA.WebServices.EntaService.getShowsForDate with " + dt);
        Sagittarius.ENTA.WebServices.EntaService.getShowsForDate(dt, OnSucceededEnta, OnFailedEnta);
        //alert("Back");
    }
    
    function writeOutShowResults(divTgt, shows){
        var dTgt = document.getElementById(divTgt);
        if(dTgt != null){
            dTgt.innerHTML = "<p class=\"date\">" + shows[0].ShowDate + "</p>";
            if(shows != null && shows.length >= 1){
                for(var i = 0; i < shows.length; i++){
                    dTgt.innerHTML += "<p class=\"eventHeader\"><a href=\"#viewMore\" title=\"View more details\">" + shows[i].Name + "</p>";
                    dTgt.innerHTML += "<p>Short description, which we may not have. TBC</p>";
                }
            }else{
                dTgt.innerHTML += "<p>Sorry, there are no shows scheduled for this day.</p>";
            }
        }
    }

    function OnSucceededEnta(result, userContext, methodName){
        alert(methodName);
        var frm = document.forms[0];
        if(methodName == "getShowsForDate"){
            //alert("" + result.length + " shows returned!");
            writeOutShowResults("marlowe-event-listing", result);
        }
    }
    
    function OnFailedEnta(error){
        //hideWorking();
        var stackTrace = error.get_stackTrace();
        var message = error.get_message();
        var statusCode = error.get_statusCode();
        var exceptionType = error.get_exceptionType();
        var timedout = error.get_timedOut();

        // Display the error.

        var divError = document.getElementById("error-msg");
        divError.style.display = "block";
        divError.innerHTML = "Stack Trace: " +  stackTrace + "<br/>" +
        "Service Error: " + message + "<br/>" +
        "Status Code: " + statusCode + "<br/>" +
        "Exception Type: " + exceptionType + "<br/>" +
        "Timedout: " + timedout;
    }   