﻿var resizeTimer;
var map = null;
var isInitialMapLoad = true;
var isFirstVisit = false;
var useBestMapView = false;
var totalTheatres = 0;
var latlongs = new Array();
var movieResults;
var myMap;
var mdp;
var tp;
var ftp;
var ft = new Array();
var theaterList = new Array();
var movieList = new Array();
var favoriteTheaterList = new Array();
var ads = new Array();
var bottomOfHeader = 60;
var lockedMovieIndex = -1;
var lockedMovieBody = "";
var hasMappedZipCode = false;
var selectedMovieID = 0;
var favoriteTheatersToolbarOn = false;
var theatersToolbarOn = false;
var movieDetailToolbarOn = true;
var COLD = 1;
var WARM = 3;
var postbackForm = true;
var urlLocation = null;
var urlRadius = 2;
var qsLocation = false;
var domain = "";
var directionsEnabled = false;
var directionDestination = null;
var directionStart = null;
var adsEnabled = false;
var memberID;
var closeWelcome = false;

function WindowOnLoad()
{   
    myMap = $get('myMap');
    YAHOO.util.Event.addListener(window, "resize", OnWindowResize);
    InitializeMap();
    InitializeTheatersPanel();           
    InitializeFavoriteTheatersPanel();
    InitializeMoviesPanel();
    InitializeMovieDetailPanel();
    GetLastZipCode();
    GetLastRadius();
    GetFavoriteTheaters();
    isFirstVisit = (ft.length == 0 && $get('postalCode').value.length == 0);
    if (qsLocation && urlLocation != null && urlLocation.length > 0)
    {
        GetShowtimesUsingUrl(urlLocation, urlRadius);
    }
    else if (ft.length > 0)
    {
        MapFavoriteTheaters();
    }
    else if ($get('postalCode').value.length > 0)
    {
        GetShowtimes();
    }
    else if (urlLocation != null && urlLocation.length > 0)
    {
        //map subdomain
        GetShowtimesUsingUrl(urlLocation, urlRadius);
    }
    else
    {
        myMap.style.visibility = "visible";
        closeWelcome = true; 
    }
    if (isFirstVisit)
    {
        //ShowWelcome();
    }
}

function GetPushPin(i, theaterID)
{
    var pushpinClass = "pushpinBlue"
    if (isFavorite(theaterID))
    {
        pushpinClass = "pushpinFavorite";
    }
    var pushpin = new Sys.StringBuilder();
    pushpin.append("<div id=\"pushpin_");
    pushpin.append(theaterID);
    pushpin.append("\" class=\"pushpinContainer\"><div class=\"pushpinIcon\" title=\"Click to see show times for this theater\"><img id=\"pushpinIcon_");
    pushpin.append(theaterID);
    pushpin.append("\" src=\"images/spacer.png\" class=\"");
    pushpin.append(pushpinClass);
    pushpin.append("\"/><div class=\"pushpinLabel\">");
    pushpin.append((i+1));
    pushpin.append("</div></div><div id=\"pushpinPopup_");
    pushpin.append(theaterID);
    pushpin.append("\" class=\"pushpinPopup\" style=\"display:none;\"><div class=\"pushpinHeading\"><div class=\"pushpinAction\"><img id=\"pushpinDropdown_");
    pushpin.append(theaterID);
    pushpin.append("\" src=\"images/spacer.png\" class=\"pushpinDropdownCue2\" /><img id=\"pushpinClose_");
    pushpin.append(theaterID);
    pushpin.append("\" src=\"images/spacer.png\" class=\"pushpinClose2\" style=\"display:none;\" onclick=\"Retract(this, ");
    pushpin.append(theaterID);
    pushpin.append(");\" /></div><div id=\"pushpinTitle_");
    pushpin.append(theaterID);
    pushpin.append("\" class=\"pushpinTitle\">");
    pushpin.append(theaterList[i].Name);
    pushpin.append("</div></div><div class=\"clear\"></div>");
    pushpin.append("<div class=\"pushpinToolbar\">");
    pushpin.append("<a style=\"padding-left:2px\" href=\"javascript:TogglePushpinDisplay(" + theaterID + ", 'Showtimes');\">Show times</a>");
    pushpin.append(" | <a href=\"javascript:TogglePushpinDisplay(" + theaterID + ", 'Info');\">More info</a>");
    if (directionsEnabled)
    {
        pushpin.append(" | <a href=\"javascript:DisplayDirections(" + i + ");\">Directions</a>");
    }
    pushpin.append("</div>");    
    pushpin.append("<div id=\"pushpinBody_");
    pushpin.append(theaterID);
    pushpin.append("\" class=\"pushpinBody\" style=\"display:none;\">");
    pushpin.append("<div id=\"pushpinShowTimes_" + theaterID + "\">");
    var theater = movieResults[i];
    var hasShowtimes = false;
    if (theater != null && theater.Movies.length > 0)
    {
        for (var j = 0; j < theater.Movies.length; j++)
        {
            var movie = theater.Movies[j];
            pushpin.append("<span onmouseover=\"HighlightMovieByID(" + movie.MovieID + ");\" onclick=\"LockMovieByID(" + movie.MovieID + ");\" onmouseout=\"RevertToLockedMovieDetail(false);\">");
            if (movie.ShowTimes.length > 0)
            {
                hasShowtimes = true;
                pushpin.append("<b>");
                pushpin.append(movie.Title);
                pushpin.append("</b> ");
                for (var k = 0; k < movie.ShowTimes.length; k++)
                {
                    var showtime = movie.ShowTimes[k];
                    if (showtime != null)
                    {
                        if (showtime.TicketingUrl.length > 0)
                        {
                            pushpin.append("<a href=\"" + showtime.TicketingUrl + "\" target=_blank onclick=\"AddShowtimeClick(" + theater.TheaterID + ", " + movie.MovieID + ", new Date('" + showtime.StartTime + "') );\"");
                            if (showtime.ClickCount <= COLD)
                            {
                                pushpin.append(" class=\"cold\"");
                            }
                            else if (showtime.ClickCount <= WARM)
                            {
                                pushpin.append(" class=\"warm\"");
                            }
                            else if (showtime.ClickCount > WARM)
                            {
                                pushpin.append(" class=\"hot\"");
                            }
                            pushpin.append(">");
                            pushpin.append(showtime.Label);
                            pushpin.append("</a>");
                        }
                        else if (theater.Url.length > 0)
                        {
                            pushpin.append("<a href=\"TheaterRedirect.aspx?theaterID=" + theater.TheaterID + "\" target=_blank onclick=\"AddShowtimeClick(" + theater.TheaterID + ", " + movie.MovieID + ", new Date('" + showtime.StartTime + "') );\"");
                            if (showtime.ClickCount <= COLD)
                            {
                                pushpin.append(" class=\"cold\"");
                            }
                            else if (showtime.ClickCount <= WARM)
                            {
                                pushpin.append(" class=\"warm\"");
                            }
                            else if (showtime.ClickCount > WARM)
                            {
                                pushpin.append(" class=\"hot\"");
                            }
                            pushpin.append(">");
                            pushpin.append(showtime.Label);
                            pushpin.append("</a>");
                        }
                        else
                        {
                            pushpin.append(showtime.Label);                            
                        }
                         if (k < movie.ShowTimes.length - 1)
                        {
                            pushpin.append(" | ");
                        }
                    }
                }
                pushpin.append("<br />");
                pushpin.append("</span>");
            }
        }
    }
    if (!hasShowtimes)
    {
        pushpin.append("Sorry no movies/times are available for this theater for ");
        pushpin.append($get('showDate').value + ".");
        if (theaterList[i].Dma != "Los Angeles" && theaterList[i].Dma != "Austin")
        {
            pushpin.append("<br />Currently, we are only providing times for Los Angeles and Austin.  <a href=\"InviteUs.aspx\">Invite us into your area</a>, and we'll get there as soon as we can!");
        }
    }
    pushpin.append("</div>");
    pushpin.append("<div id=\"pushpinTheaterInfo_" + theaterID + "\" style=\"display:none\">");
    pushpin.append(theater.Address + "<br/>");
    pushpin.append(theater.City + ", " + theater.State + " " + theater.PostalCode + "<br/>");
    pushpin.append(theater.Phone);
    if (!theater.OnlineTicketing && theater.Url.length > 0)
    {
        pushpin.append("<br/><a href=\"" + theater.Url + "\" target=\"_blank\">Go to Theater Web Site</a>");
    }
    pushpin.append("<div id=\"pushpinFavorite_" + theaterID + "\">");
    if (isFavorite(theaterID))
    {
        pushpin.append("<span class=\"deleteFavoriteTheaterStar\" title=\"Delete from favorites\" onclick=\"DeleteFromFavoriteTheaters(" + theaterID + ");\"></span><a class=\"theaterFavoriteAction\" href=\"javascript:DeleteFromFavoriteTheaters(" + theaterID + ");\">Delete from favorites</a>");
    }
    else
    {
        pushpin.append("<span class=\"addFavoriteTheaterStar\" title=\"Add to favorites\" onclick=\"AddToFavoriteTheaters(" + theaterID + ", " + i + ");\"></span><a class=\"theaterFavoriteAction\" href=\"javascript:AddToFavoriteTheaters(" + theaterID + ", " + i + ");\">Add to favorites</a>");
    }
    pushpin.append("</div>");
    pushpin.append("</div>");
    pushpin.append("</div></div></div>");
    return pushpin.toString();
}

function ResetGlobalVars()
{
    movieResults = new Array();
    latlongs = new Array();
    movieList = new Array();
    theaterList = new Array();
    lockedMovieBody = "<div class=\"panelBodyMessage\">Put your cursor over a movie title to see details about a movie. Click on a movie title to lock the movie details in place here.</div>";
    RevertToLockedMovieDetail();
}

function GetShowtimes()
{
    postbackForm = false;
    RemovePushpins();
    var location = $get('postalCode').value;
    var showDate = $get('showDate').value;
    if (location.length == 0)
    {
        SetCookie();
        return;
    }  
    hasMappedZipCode = false;
//    map.DetachEvent("onendzoom", GetShowTimesInBox);        		
//    map.DetachPanEvent(GetShowTimesInBox);
	SetCookie();
	useBestMapView = true;
    if (isFirstVisit)
    {
        $get('welcomeLoading').style.visibility = "visible";
        $get('welcomeLoadingText').innerText = "[loading location '" + location + "']"; 
    }
    else
    {
        $get('loading').style.visibility="visible";
        $get('loadingText').innerText = "[loading location '" + location + "']"; 
    }
    $get('moviesPanel_listing').innerHTML = "<div class=\"panelLoading\"><img src=\"images/loading.gif\"><br/>[updating]</div>";
    $get('theatersPanel_listing').innerHTML = "<div class=\"panelLoading\"><img src=\"images/loading.gif\"><br/>[updating]</div>";
    HideTheatersToolbar();
    ResetGlobalVars();
    ShowTimeMaps.GetShowTimes(location, $get('radius').value, showDate, "showtimemapslicense", OnGetShowTimesComplete, OnTimeout, OnError);  
}

function GetShowTimesInBox()
{
    RemovePushpins();
    if (map.GetZoomLevel() > 7)
    {
	    useBestMapView = false;
        $get('moviesPanel_listing').innerHTML = "<div class=\"panelLoading\"><img src=\"images/loading.gif\"><br/>[updating]</div>";
        $get('theatersPanel_listing').innerHTML = "<div class=\"panelLoading\"><img src=\"images/loading.gif\"><br/>[updating]</div>";
        HideTheatersToolbar();
        ResetGlobalVars();
        windowWidth=GetWindowWidth();
        windowHeight=GetWindowHeight();
        var topLeft = map.GetTopLeftLatLong();
        var topLatitude = topLeft.Latitude;
        var leftLongitude = topLeft.Longitude;      
        var bottomRight = map.GetBottomRightLatLong();
        var bottomLatitude = bottomRight.Latitude;
        var rightLongitude = bottomRight.Longitude;
        var showDate = $get('showDate');
        WS_GetShowTimesInBox(showDate.value, topLatitude, bottomLatitude, leftLongitude, rightLongitude);
    }
    else
    {
        //Too big of an area
        var wideZoomMessage = "<div class=\"panelBodyMessage\">You are zoomed out too far to get any meaningful results.  <a href=\"javascript:map.SetZoomLevel(8);\">Zoom in</a> a bit closer.</div>";
        $get('theatersPanel_listing').innerHTML = wideZoomMessage;
        $get('moviesPanel_listing').innerHTML = wideZoomMessage;
    }        
}

function GetShowtimesUsingUrl(location, radius)
{
    postbackForm = false;
    var showDate = $get('showDate').value;
    hasMappedZipCode = false;
	useBestMapView = true;
    if (!isFirstVisit)
    {
        $get('loading').style.visibility="visible";
        $get('loadingText').innerText = "[loading location '" + location + "']"; 
    }
    $get('moviesPanel_listing').innerHTML = "<div class=\"panelLoading\"><img src=\"images/loading.gif\"><br/>[updating]</div>";
    $get('theatersPanel_listing').innerHTML = "<div class=\"panelLoading\"><img src=\"images/loading.gif\"><br/>[updating]</div>";
    HideTheatersToolbar();
    ResetGlobalVars();
     ShowTimeMaps.GetShowTimes({postalCode : location,
        showDate : showDate,
        radius : radius}, OnGetShowTimesComplete, OnTimeout, OnError);  
}

function InitializeMap()
{
    windowWidth=GetWindowWidth();
    windowHeight=GetWindowHeight();
    map = new ShowTimeMap(myMap, 40, -97.13, 4, windowWidth - 365, bottomOfHeader+300);
    map.Resize(windowWidth - 360,windowHeight - 100);
}
function InitializeTheatersPanel()
{
    windowWidth=GetWindowWidth();
    $get('theatersPanel').style.top = bottomOfHeader+158 + "px";
    $get('theatersPanel').style.left = windowWidth - 185 + "px";
   YAHOO.util.Event.addListener($get('theatersPanelBackgroundImage'), "click", CollapsePanel, "theaters", false);
   YAHOO.util.Event.addListener($get('theatersPanel_header'), "click", CollapsePanel, "theaters", false);
}

function InitializeFavoriteTheatersPanel()
{
    windowWidth=GetWindowWidth();
    $get('favoriteTheatersPanel').style.top = bottomOfHeader+10 + "px";
    $get('favoriteTheatersPanel').style.left = windowWidth - 185 + "px";
   YAHOO.util.Event.addListener($get('favoriteTheatersPanelBackgroundImage'), "click", CollapsePanel, "favoriteTheaters", false);
   YAHOO.util.Event.addListener($get('favoriteTheatersPanel_header'), "click", CollapsePanel, "favoriteTheaters", false);
}

function InitializeMoviesPanel()
{
   YAHOO.util.Event.addListener($get('moviesPanelBackgroundImage'), "click", CollapsePanel, "movies", false);
   YAHOO.util.Event.addListener($get('moviesPanel_header'), "click", CollapsePanel, "movies", false);
}

function InitializeMovieDetailPanel()
{
   YAHOO.util.Event.addListener($get('movieDetailPanel'), "mouseover", DoNothing);
   YAHOO.util.Event.addListener($get('movieDetailPanelBackgroundImage'), "click", CollapsePanel, "movieDetail", false);
   YAHOO.util.Event.addListener($get('movieDetailPanel_header'), "click", CollapsePanel, "movieDetail", false);
}

function InitializeAds()
{
    windowWidth=GetWindowWidth();
    $get('googleAds').style.left = windowWidth - 472;
    $get('googleAds').style.display = "";
}

function AddPoint(lat, lon)
{
    var pt = new ShowTimeLatLong(lat, lon);
    latlongs.push(pt);
}      

function OnGetShowTimesComplete(result)
{
    if (closeWelcome)
    {
        HideWelcome();
    }
    $get('postalCode').focus();
    if (result.length == 0)
    {
        if (hasMappedZipCode)
        {
            DisplayNoResults();
            map.AttachZoomEvent(GetShowTimesInBox);        		
            map.AttachPanEvent(GetShowTimesInBox);	    
        }
        else
        {
            MapEmptyZipCode();
        }
    }
    else
    {
        movieResults = result;
        DisplayResults(result);
        GetAds();
    }
    hasMappedZipCode = true;
    closeWelcome = true; 
}

function DisplayResults(searchResults)
{
    map.DetachZoomEvent(GetShowTimesInBox);        		
    map.DetachPanEvent(GetShowTimesInBox);
    RemovePushpins();
    
    for (i=0; i < searchResults.length ; i++)
    {
        AddPoint(searchResults[i].Latitude, searchResults[i].Longitude);
    }
//    if (isInitialMapLoad)
//    {
//        map.IncludeInView(latlongs);
//    }
     if (useBestMapView)
     {
         if (!isInitialMapLoad)
         {
            map.DetachZoomEvent(GetShowTimesInBox);        		
            map.DetachPanEvent(GetShowTimesInBox);
            map.AttachZoomEvent(AttachZoom);        		
            map.AttachPanEvent(AttachContinuousPan);
         }
         map.IncludeInView(latlongs);
         if (map.GetZoomLevel() > 16)
         {
            map.SetZoomLevel(16);
         }
     }
    for (i=0; i < searchResults.length ; i++)
     {
        theaterList.push(searchResults[i]);
        var theaterID = searchResults[i].TheaterID;
        var latitude = searchResults[i].Latitude;
        var longitude = searchResults[i].Longitude;
        map.AddPushpin('theater_' + theaterID, latitude, longitude, 23, 17, '', GetPushPin(i, theaterID), 10);
        YAHOO.util.Event.addListener("pushpin_" + theaterID, mouseover, ExpandTitle, theaterID, false);
        YAHOO.util.Event.addListener("pushpin_" + theaterID, mouseout, Retract, theaterID, false);
        YAHOO.util.Event.addListener("theater_" + theaterID, "dblclick", StopPropagation);
        YAHOO.util.Event.addListener("theater_" + theaterID, "mousedown", StopPropagation);
        AddMoviesToMovieList(searchResults[i].Movies);
     }
     myMap.style.visibility = "visible";
     $get('moviesPanel').style.display = "";
     $get('movieDetailPanel').style.display = "";
     isInitialMapLoad = false;
     DisplayMoviesPanel();
     DisplayTheatersPanel();
     DisplayFavoriteTheatersPanel();
     map.AttachZoomEvent(GetShowTimesInBox);        		
     map.AttachPanEvent(GetShowTimesInBox);
}
    
                 
function OnError(result) {
    alert("There was an error calling the bridge: "+ result.get_message());
}

function OnTimeout(result) {
    alert("The bridge call timed out: "+ result.get_message());
}

function UnhighlightPins()
{
    for (var i = 0; i < movieResults.length; i++)
    {
        if (!isFavorite(movieResults[i].TheaterID))
        {
            $get('pushpinIcon_' + movieResults[i].TheaterID).className = "pushpinBlue";
        }
        else
        {
            $get('pushpinIcon_' + movieResults[i].TheaterID).className = "pushpinFavorite";
        }
        Retract(this, movieResults[i].TheaterID);     
    }
}

function SendPinsToBack()
{
    for (var i = 0; i < movieResults.length; i++)
    {
        $get('theater_' + movieResults[i].TheaterID).style.zIndex = "10";    
    }
}

function RemovePushpins()
{
    map.ClearPushpins();
}

function MapEmptyZipCode()
{
    hasMappedZipCode = true;
    if (isInitialMapLoad)
    {
        $get('loading').style.visibility="visible";
        $get('loadingText').innerText = "[loading location '" + $get('postalCode').value + "']"; 
        myMap.style.visibility = "hidden";
    }
   
    DisplayNoResults();
    //Yahoo.GeoCode.GetCoordinates({location : $get('postalCode').value}, OnGetCoordinatesComplete, OnTimeout, OnError);
    map.FindLocation($get('postalCode').value, OnGetCoordinatesComplete);
}

function DisplayNoResults()
{
    RemovePushpins();
    $get('loading').style.visibility = "hidden";
    $get('moviesPanel_listing').innerHTML = "<div class=\"panelBodyMessage\">No movies available in this map view.<br /><br />"
        + "Try <a href=\"javascript:map.SetZoomLevel(map.GetZoomLevel() - 1);\">zooming out</a> to search a larger area.</div>";
    $get('theatersPanel_listing').innerHTML = "<div class=\"panelBodyMessage\">No theaters available in this map view.<br /><br />"
        + "Try <a href=\"javascript:map.SetZoomLevel(map.GetZoomLevel() - 1);\">zooming out</a> to search a larger area.</div>";
    $get('moviesPanel').style.display = "";
    $get('movieDetailPanel').style.display = "";
    $get('theatersPanel').style.display = "";
    $get('favoriteTheatersPanel').style.display = "";
}

function OnGetCoordinatesComplete(result)
{
    if (result != null)
    {
        myMap.style.visibility = "visible";
        isInitialMapLoad = false;
        map.PanToLatLong(result)
        map.SetZoomLevel(14);
    }
    map.AttachZoomEvent(GetShowTimesInBox);        		
    map.AttachPanEvent(GetShowTimesInBox);    
}

function AddMoviesToMovieList(movies)
{
   for (var i = 0; i < movies.length; i++)
   {
        var movie = movies[i];
        var movieListContainsMovie = false;
        for (var j = 0; j < movieList.length; j++)
        {
            if (movie.MovieID == movieList[j].MovieID)
            {
                movieListContainsMovie = true;
                break;
            } 
        }
        if (!movieListContainsMovie && movie.ShowTimes.length > 0)
        {
            movieList.push(movie);
        }
    }        
}

function DisplayMoviesPanel()
{
    var movieBody = new Sys.StringBuilder();
    if (movieList.length == 0)
    {
        if (movieResults.length < 30)
        {
            var showDate = $get('showDate').value;
            movieBody.append("<div class=\"panelBodyMessage\">Sorry, there are no show times available for any of the listed theaters on " + showDate + ".  During our initial launch we'll only have times for theaters in the Los Angeles and Austin markets.  <a href=\"InviteUs.aspx\">Invite us into your area</a>, and we'll get there as soon as we can!");
        }
        else
        {
            movieBody.append("<div class=\"panelBodyMessage\">Your search has returned more than 30 theaters.  Try <a href=\"javascript:map.SetZoomLevel(map.GetZoomLevel() + 1);\">zooming in</a> to narrow your search, so we can give you a better list of show times.");
        }
    }
    else
    {
        movieList.sort(SortMovies);
        for (var i = 0; i < movieList.length; i++)
        {
            var movie = movieList[i];
            movieBody.append("<div class=\"movieListing\" title=\"Click here to lock movie detail below\" onmouseover=\"this.className = 'movieListing highlightMovieListing';HighlightMovie(");
            movieBody.append(i);
            movieBody.append(");\" onmouseout=\"this.className = 'movieListing';UnhighlightPins();RevertToLockedMovieDetail();\" onclick=\"LockMovie(");
            movieBody.append(i);
            movieBody.append(");AddMovieClick(" + movie.MovieID + ");\"><span id=\"lockedMovieIcon_");
            movieBody.append(i);
            movieBody.append("\" class=\"lockedMovieIconOff\" onclick=\"UnlockMovie(");
            movieBody.append(i);
            movieBody.append(");\">&nbsp;</span><span class=\"name\">");
            movieBody.append(movie.Title);
            movieBody.append("</span></div>");
        }
    }
    $get('moviesPanel_listing').innerHTML = movieBody.toString();
    for (var i = 0; i < movieList.length; i++)
    {
        var movie = movieList[i];
        if (movie.MovieID == selectedMovieID)
        {
            LockMovie(i);
        }
    }
}

function HighlightMovie(i, highlightTheaters)
{
    if (highlightTheaters == null || highlightTheaters)
    {
        ShowTheatersWithMovie(i);
    }
    var detailBody = new Sys.StringBuilder();
    var movie = movieList[i];
    detailBody.append("<div class=\"movieDetailHeading\"><span class=\"movieDetailName\">");
    detailBody.append(movie.Title);
    detailBody.append("</span> (");
    detailBody.append(movie.Rating);
    detailBody.append(") - Running time ");
    detailBody.append(movie.Runtime);
    detailBody.append("</div>");
    detailBody.append("<div class=\"movieDetailToolbar\">");
    detailBody.append("<a style=\"padding-left:2px\" href=\"javascript:ToggleDetailDisplay('Showtimes');\">Show times</a>");
    detailBody.append(" | <a  href=\"javascript:ToggleDetailDisplay('Synopsis');\">Synopsis</a>");
    detailBody.append("</div>");
    detailBody.append("<div class=\"movieDetailBody\"><div id=\"movieDetailShowTimes\">");
    for (var j = 0; j < theaterList.length; j++)
    {
        var theater = theaterList[j];
        for (var k = 0; k < theater.Movies.length; k++)
        {
            var theaterMovie = theater.Movies[k];
            if (theaterMovie.MovieID == movie.MovieID)
            {
                detailBody.append("<span class=\"movieDetailTheater\" title=\"Center map on this theater\" onclick=\"CenterTheater(");
                detailBody.append(theaterList[j].TheaterID);
                detailBody.append(");\">");
                detailBody.append(theaterList[j].Name);
                detailBody.append("</span>: ");
                for (var m = 0; m < theaterMovie.ShowTimes.length; m++)
                {
                    var showTime = theaterMovie.ShowTimes[m];
                    if (showTime.TicketingUrl.length > 0)
                    {
                        detailBody.append("<a href=\"" + showTime.TicketingUrl + "\" target=_blank onclick=\"AddShowtimeClick(" + theater.TheaterID + ", " + theaterMovie.MovieID + ", new Date('" + showTime.StartTime + "') );\"");
                        if (showTime.ClickCount <= COLD)
                            {
                                detailBody.append(" class=\"cold\"");
                            }
                            else if (showTime.ClickCount <= WARM)
                            {
                                detailBody.append(" class=\"warm\"");
                            }
                            else if (showTime.ClickCount > WARM)
                            {
                                detailBody.append(" class=\"hot\"");
                            }
                        detailBody.append(">");
                        detailBody.append(showTime.Label);
                        detailBody.append("</a>");                    
                    }
                    else if (theater.Url.length > 0)
                    {
                        detailBody.append("<a href=\"TheaterRedirect.aspx?theaterID=" + theater.TheaterID + "\" target=_blank onclick=\"AddShowtimeClick(" + theater.TheaterID + ", " + theaterMovie.MovieID + ", new Date('" + showTime.StartTime + "') );\"");
                        if (showTime.ClickCount <= COLD)
                            {
                                detailBody.append(" class=\"cold\"");
                            }
                            else if (showTime.ClickCount <= WARM)
                            {
                                detailBody.append(" class=\"warm\"");
                            }
                            else if (showTime.ClickCount > WARM)
                            {
                                detailBody.append(" class=\"hot\"");
                            }
                        detailBody.append(">");
                        detailBody.append(showTime.Label);
                        detailBody.append("</a>");
                    }
                    else
                    {
                        detailBody.append(showTime.Label);                         
                    }
                    if (m < theaterMovie.ShowTimes.length - 1)
                    {
                        detailBody.append(" | ");
                    }
                }
                detailBody.append("<br />");
            }
        }
    }
    if (ads[1] != null && ads[1].Html.length > 0)
    {
        detailBody.append("<div class=\"movieDetailAd\">" + ads[1].Html + "</div>");
    }
    detailBody.append("</div><div id=\"movieSynopsis\" style=\"display:none\">" + movie.Synopsis);
    if (movie.Cast.length > 0)
    {
        detailBody.append("<br/>Starring: " + movie.Cast);
    }
    if (movie.Director.length > 0)
    {
        detailBody.append("<br/>Directed by: " + movie.Director);
    }
    if (ads[1] != null && ads[1].Html.length > 0)
    {
        detailBody.append("<div class=\"movieDetailAd\">" + ads[1].Html + "</div>");
    }
    detailBody.append("</div>");
    detailBody.append("</div>");
    var result = detailBody.toString();
    $get('movieDetailPanel_body').innerHTML = result;
    return result;
}

function HighlightMovieByID(movieID)
{
    var movieIndex;
    for (var i = 0; i < movieList.length; i++)
    {
        if (movieID == movieList[i].MovieID)
        {
            movieIndex = i;
            break;
        }
    }
    HighlightMovie(movieIndex, false);
}

function ToggleDetailDisplay(display)
{
    if (display == "Showtimes")
    {
        $get('movieSynopsis').style.display = "none";
        $get('movieDetailShowTimes').style.display = "";
    }
    else
    {
        $get('movieDetailShowTimes').style.display = "none";
        $get('movieSynopsis').style.display = "";
    }
}

function TogglePushpinDisplay(theaterID, display)
{
    if (display == "Showtimes")
    {
        $get('pushpinTheaterInfo_' + theaterID).style.display = "none";
        $get('pushpinShowTimes_' + theaterID).style.display = "";
    }
    else if (display == "Directions")
    {
        $get('pushpinShowTimes_' + theaterID).style.display = "none";
        $get('pushpinTheaterInfo_' + theaterID).style.display = "none";
    }
    else
    {
        $get('pushpinShowTimes_' + theaterID).style.display = "none";
        $get('pushpinTheaterInfo_' + theaterID).style.display = "";
    }
}

function RevertToLockedMovieDetail(highlightTheaters)
{
    if (lockedMovieIndex > 0 && (highlightTheaters == null || highlightTheaters == true))
    {
        ShowTheatersWithMovie(lockedMovieIndex);
    }
    $get('movieDetailPanel_body').innerHTML = lockedMovieBody;
}

function LockMovie(movieListIndex)
{
    var movie = movieList[movieListIndex];
    selectedMovieID = movie.MovieID;
    lockedMovieIndex = movieListIndex;
    lockedMovieBody = HighlightMovie(movieListIndex);
    ExpandPanel(null, "movieDetail");
    for (var i = 0; i < movieList.length; i++)
    {
        PngSwap($get('lockedMovieIcon_' + i), "images/spacer.png", "lockedMovieIconOff");
    }
    PngSwap($get('lockedMovieIcon_' + movieListIndex), "images/lockedMovie.png", "lockedMovieIconOn");
    //hack to get IE and FF to work at the same time
    if (document.all)
    {
        $get('lockedMovieIcon_' + movieListIndex).style.width = "20px";
        $get('lockedMovieIcon_' + movieListIndex).style.height = "20px";
        $get('lockedMovieIcon_' + movieListIndex).style.padding = "0 7px 7px 5px";
    }
}

function LockMovieByID(movieID)
{
    var movieIndex;
    for (var i = 0; i < movieList.length; i++)
    {
        if (movieID == movieList[i].MovieID)
        {
            movieIndex = i;
            break;
        }
    }
    LockMovie(movieIndex);
}

function UnlockMovie()
{
    lockedMovieIndex = -1;
    lockedMovieBody = "<div class=\"panelBodyMessage\">Put your cursor over a movie title to see details about a movie. Click on a movie title to lock the movie details in place here.</div>";
    RevertToLockedMovieDetail();
}

function SortMovies(a, b)
{
    if (a.Title < b.Title)
    {
        return -1;
    }
    else if (a.Title == b.Title)
    {
        return 0;
    }
    else if (a.Title > b.Title)
    {
        return 1;
    } 
}

function ShowTheatersWithMovie(movieListIndex)
{
    UnhighlightPins();
    var movie = movieList[movieListIndex];
    if (movie != null)
    {
        for (var i = 0; i < movie.TheaterIDs.length; i++)
        {
            //$get('pushPinLabel_' + movie.TheaterIDs[i]).className = "pushPinLabelHighlight";
            if (!isFavorite(movie.TheaterIDs[i]))
            {
                $get('pushpinIcon_' + movie.TheaterIDs[i]).className = "pushpinRed";
            }
            else
            {
                $get('pushpinIcon_' + movie.TheaterIDs[i]).className = "pushpinFavoriteRed";
            }
            ExpandTitle(null, movie.TheaterIDs[i]);
        }
    }
}

function ShowMoreMovieInfo(movieListIndex)
{
    var movie = movieList[movieListIndex];
}

function OnWindowResize()
{
    window.clearTimeout(resizeTimer);
    resizeTimer = window.setTimeout("GetShowTimesInBox()", 500);
    windowWidth=GetWindowWidth();
    windowHeight=GetWindowHeight();
    $get('theatersPanel').style.left = windowWidth - 185 + "px";
    $get('favoriteTheatersPanel').style.left = windowWidth - 185 + "px";
    map.DetachZoomEvent(GetShowTimesInBox);        		
    map.DetachPanEvent(GetShowTimesInBox);	 
    map.Resize(windowWidth - 360, windowHeight - 100);                
}

function DisplayTheatersPanel()
{
    var theaterBody = new Sys.StringBuilder();
    for (var i = 0; i < theaterList.length; i++)
    {
        var theater = theaterList[i];
        theaterBody.append("<div class=\"theaterListing\" title=\"Center map on this theater\" onclick=\"CenterTheater(");
        theaterBody.append(theater.TheaterID);
        theaterBody.append(");\" onmouseover=\"this.className = 'theaterListing HighlightTheaterListing';HighlightTheater(");
        theaterBody.append(theater.TheaterID);
        theaterBody.append(");\" onmouseout=\"this.className = 'theaterListing'\">");
        if (isFavorite(theater.TheaterID))
        {
            theaterBody.append("<span class=\"favoriteTheaterStar\" title=\"Delete from favorites\" onclick=\"DeleteFromFavoriteTheaters(");
            theaterBody.append(theater.TheaterID);
            theaterBody.append(");\" onmouseover=\"PngSwap(this, 'images/deleteFavoriteTheater.png', 'deleteFavoriteTheaterStar');\" onmouseout=\"PngSwap(this, 'images/favoriteTheater.png', 'favoriteTheaterStar');\">&nbsp;</span>");
        }
        else
        {
            theaterBody.append("<span class=\"notFavoriteTheaterStar\" title=\"Add to favorites\" onclick=\"AddToFavoriteTheaters(");
            theaterBody.append(theater.TheaterID);
            theaterBody.append(", ");
            theaterBody.append(i);
            theaterBody.append(");\" onmouseover=\"PngSwap(this, 'images/addFavoriteTheater.png', 'addFavoriteTheaterStar');\" onmouseout=\"PngSwap(this, 'images/pushpinGreenSmall.png', 'notFavoriteTheaterStar');\">&nbsp;</span>");            
        }
        theaterBody.append("<span class=\"theaterListingNumber\">" + (i + 1) + "-</span>");
        theaterBody.append("<span class=\"name\">");
        theaterBody.append(theater.Name);
        theaterBody.append("</span></div>");        
    }
    $get('theatersPanel_listing').innerHTML = theaterBody.toString();
    $get('theatersPanel').style.display = "";
}

function DisplayFavoriteTheatersPanel()
{
    var theaterBodyText = "";
    if (favoriteTheaterList.length > 0 || $get('postalCode').value.length > 0)
    {
        if (favoriteTheaterList.length > 0)
        {
            ShowFavoriteTheatersToolbar();
        }        
        var theaterBody = new Sys.StringBuilder();
        for (var i = 0; i < favoriteTheaterList.length; i++)
        {
            var theater = favoriteTheaterList[i];
            theaterBody.append("<div class=\"theaterListing\" title=\"Center map on this theater\" onclick=\"CenterTheater(");
            theaterBody.append(theater.TheaterID);
            theaterBody.append(");\" onmouseover=\"this.className = 'theaterListing HighlightTheaterListing';HighlightTheater(");
            theaterBody.append(theater.TheaterID);
            theaterBody.append(");\" onmouseout=\"this.className = 'theaterListing'\">");
            theaterBody.append("<span class=\"favoriteTheaterStar\" title=\"Delete from favorites\" onclick=\"DeleteFromFavoriteTheaters(");
            theaterBody.append(theater.TheaterID);
            theaterBody.append(");\" onmouseover=\"PngSwap(this, 'images/deleteFavoriteTheater.png', 'deleteFavoriteTheaterStar');\" onmouseout=\"PngSwap(this, 'images/favoriteTheater.png', 'favoriteTheaterStar');\">&nbsp;</span>");
            theaterBody.append("<span class=\"name\">");
            theaterBody.append(theater.Name);
            theaterBody.append("</span></div>");
        }
        theaterBodyText = theaterBody.toString();
    }
    
    if (theaterBodyText.length > 0)
    {
        $get('favoriteTheatersPanel_listings').innerHTML = theaterBodyText;
    }
    else
    {
        $get('favoriteTheatersPanel_listings').innerHTML = "<div class=\"panelBodyMessage\">You haven't selected any favorites yet.  To add a favorite, click the green box next to your favorite theater in the box below.</div>";
    }               
    $get('favoriteTheatersPanel').style.display = "";
}

function ShowFavoriteTheatersToolbar()
{
    $get('favoriteTheatersPanel_toolbar').innerHTML = "<a href=\"javascript:MapFavoriteTheaters();\" style=\"padding-left:2px\">Map Favorites</a><a href=\"javascript:ClearFavoriteTheaters();HideFavoriteTheatersToolbar();\" style=\"width:20px;margin-left:80px\">Clear</a>";
    $get('favoriteTheatersPanel_toolbar').style.display = "";
    $get('favoriteTheatersPanel_body').style.height = "97px";
    favoriteTheatersToolbarOn = true;
}

function HideFavoriteTheatersToolbar()
{
    $get('favoriteTheatersPanel_toolbar').style.display = "none";
    $get('favoriteTheatersPanel_body').style.height = "113px";
    favoriteTheatersToolbarOn = false;
}


function HighlightTheater(theaterID)
{
    UnhighlightPins();
    var isMapped = false;
    for (var i = 0; i < theaterList.length; i++)
    {
        if (theaterList[i].TheaterID == theaterID)
        {
            isMapped = true;
            break;
        }
    }
    if (isMapped)
    {
        if (!isFavorite(theaterID))
        {
            $get('pushpinIcon_' + theaterID).className = "pushpinRed";
        }
        else
        {
            $get('pushpinIcon_' + theaterID).className = "pushpinFavoriteRed";
        }
        ExpandTitle(null, theaterID);
    }
}

function AddToFavoriteTheaters(theaterID, theaterIndex)
{
    if (theaterIndex == null)
    {
        for (var i = 0; i < theaterList.length; i++)
        {
            if (theaterList[i].TheaterID == theaterID)
            {
                theaterIndex = i;
                break;
            }
        }
    }
    if (!isFavorite(theaterID))
    {
        ft.unshift(theaterID);
        var date = new Date();
        SetCookie();
        var theaterIDs = new Array();
        theaterIDs.push(theaterID);
        favoriteTheaterList.unshift(theaterList[theaterIndex]);
        $get('pushpinIcon_' + theaterID).className = "pushpinFavorite";
        $get('pushpinFavorite_' + theaterID).innerHTML = "<span class=\"deleteFavoriteTheaterStar\" title=\"Delete from favorites\" onclick=\"DeleteFromFavoriteTheaters(" + theaterID + ");\"></span><a class=\"theaterFavoriteAction\" href=\"javascript:DeleteFromFavoriteTheaters(" + theaterID + ");\">Delete from favorites</a>";
        DisplayFavoriteTheatersPanel();
        DisplayTheatersPanel();
    }
    UpdateFavoriteTheaters();
}

function DeleteFromFavoriteTheaters(theaterID)
{
    var indexToRemove = -1;
    for (var i = 0; i < favoriteTheaterList.length; i++)
    {
        if (favoriteTheaterList[i].TheaterID == theaterID)
        {
            indexToRemove = i;
            break;
        }
    }
    if (indexToRemove > -1)
    {
        favoriteTheaterList.splice(indexToRemove, 1);
    }
    indexToRemove = -1;
    for (var i = 0; i < ft.length; i++)
    {
        if (ft[i] == theaterID)
        {
            indexToRemove = i;
            break;
        }
    }
    if (indexToRemove > -1)
    {
        ft.splice(indexToRemove, 1);
    }
    SetCookie();
    if (ft.length == 0)
    {
        HideFavoriteTheatersToolbar();
    }
    var theaterListIndex = -1;
    for (var i = 0; i < theaterList.length; i++)
    {
        if (theaterList[i].TheaterID == theaterID)
        {
            theaterListIndex = i;
            break;
        }
    }
    $get('pushpinIcon_' + theaterID).className = "pushpinBlue";
    $get('pushpinFavorite_' + theaterID).innerHTML = "<span class=\"addFavoriteTheaterStar\" title=\"Add to favorites\" onclick=\"AddToFavoriteTheaters(" + theaterID + ", " + i + ");\"></span><a class=\"theaterFavoriteAction\" href=\"javascript:AddToFavoriteTheaters(" + theaterID + ", " + theaterListIndex + ");\">Add to favorites</a>";
    DisplayFavoriteTheatersPanel();
    DisplayTheatersPanel();
    UpdateFavoriteTheaters();
}

function GetFavoriteTheaters()
{
    var theaterIDString = GetCookieValue("showtimemaps", "favoriteTheaters");
    var theaterIDs = new Array();
    if (theaterIDString.indexOf(",") > -1)
    {
        theaterIDs = theaterIDString.split(",");
    } 
    else if (theaterIDString.length > 0)
    {
        theaterIDs.push(theaterIDString);
    }
    if (theaterIDs.length > 0)
    {
        ft = theaterIDs;
        ResetGlobalVars();
        var showDate = $get('showDate');
        ShowTimeMaps.GetFavoriteTheaters({theaterIDs : theaterIDs,
            showDate : showDate.value}, OnGetFavoriteTheatersComplete, OnTimeout, OnError); 
    }
    else
    {
        DisplayFavoriteTheatersPanel();
    }
}

function OnGetFavoriteTheatersComplete(result)
{
    favoriteTheaterList = result;
    DisplayFavoriteTheatersPanel();
}

function ClearFavoriteTheaters()
{
    favoriteTheaterList = new Array();
    ft = new Array();
    SetCookie();
    DisplayFavoriteTheatersPanel();
    DisplayTheatersPanel();
    UpdateFavoriteTheaters();
}

function CenterTheater(theaterID)
{
    for (var i = 0; i < theaterList.length; i++)
    {
        var theater = theaterList[i];
        if (theater.TheaterID == theaterID)
        {   
            map.DetachPanEvent(GetShowTimesInBox);
            map.AttachPanEvent(AttachContinuousPan);
            var destLatLong = new ShowTimeLatLong(theater.Latitude, theater.Longitude);
            map.PanToLatLong(destLatLong);
            break;
        }
    }
}

function GetTheaterByID(theaterID)
{
    for (var i = 0; i < theaterList.length; i++)
    {
        var theater = theaterList[i];
        if (theater.TheaterID == theaterID)
        {   
            return theater;
        }
    }
}

function MapFavoriteTheaters()
{
    map.DetachZoomEvent(GetShowTimesInBox);        		
    map.DetachPanEvent(GetShowTimesInBox);
	useBestMapView = true;
    if (isInitialMapLoad)
    {
        $get('loading').style.visibility="visible";
        $get('loadingText').innerText = "[loading favorite theaters]"; 
        myMap.style.visibility = "hidden";
    }
    $get('moviesPanel_listing').innerHTML = "<div class=\"panelLoading\"><img src=\"images/loading.gif\"><br/>[updating]</div>";
    $get('theatersPanel_listing').innerHTML = "<div class=\"panelLoading\"><img src=\"images/loading.gif\"><br/>[updating]</div>";
    ShowTheatersToolbar();
    latlongs = new Array();
    movieList = new Array();
    theaterList = new Array();
    var showDate = $get('showDate');
    ShowTimeMaps.GetFavoriteTheaters({theaterIDs : ft,
       showDate : showDate.value}, OnMapFavoriteTheatersComplete, OnTimeout, OnError); 
}

function ShowTheatersToolbar()
{
    $get('theatersPanel_toolbar').innerHTML = "<a href=\"javascript:GetShowTimesInBox();\" style=\"padding-left:2px\">Map other theaters in this area</a>";
    $get('theatersPanel_toolbar').style.display = "";
    $get('theatersPanel_body').style.height = "127px";
    theatersToolbarOn = true;
}

function HideTheatersToolbar()
{
    $get('theatersPanel_toolbar').style.display = "none";
    $get('theatersPanel_body').style.height = "143px";
    theatersToolbarOn = false;
}

function OnMapFavoriteTheatersComplete(results)
{
    if (results.length == 0)
    {
        MapEmptyZipCode();
    }
    else
    {
        movieResults = results;
        DisplayResults(results);
        GetAds();
    }
}

function SetCookie()
{
    var expires = new Date();
    expires.setDate(365);
    var sbcookie = new Sys.StringBuilder();
    sbcookie.append("showtimemaps=");
    if (ft.length > 0)
    {
        sbcookie.append(escape("favoriteTheaters=" + ft.toString()));
    }
    sbcookie.append(escape("|postalCode=" + $get('postalCode').value + "|radius=" + $get('radius').value + "|") + "; expires="+expires.toGMTString() + ";path=/");
    if (domain.length > 0)
    {
        sbcookie.append(";domain=" + domain);
    }
    document.cookie = sbcookie.toString();
}

function GetLastZipCode()
{
    var postalCode = GetCookieValue("showtimemaps", "postalCode");
    $get('postalCode').value = postalCode;
}

function GetLastRadius()
{
    var radius = GetCookieValue("showtimemaps", "radius");
    for (var i = 0; i < $get('radius').options.length; i++)
    {
        if ($get('radius').options[i].value == radius)
        {
            $get('radius').selectedIndex = i;
        }
    }
}

function GetCookieValue(name, prefix)
{
    var c = GetCookie(name);
    if (c == null || c.length == 0)
    {
        return "";
    }
    var begin=c.indexOf(prefix + "=")
    if (begin > -1)
    {
        begin += prefix.length + 1;
    }
    var end = c.indexOf("|", begin);
    if(end==-1)
    {
        end=c.length;
    }
    var result = window.unescape(c.substring(begin,end));
    if (result == null)
    {
        result = "";
    }
    return result;
}

function ExpandTitle(e, theaterID)
{
    //Atlas attach/detach events won't work, because they report the srcElement as the sub-div that 
    //was clicked on, instead of the div that the event is actually attached to. 
    //Same for ExpandDetail and Retract
    YAHOO.util.Event.removeListener("pushpin_" + theaterID, mouseover, ExpandTitle);
    YAHOO.util.Event.removeListener("pushpin_" + theaterID, "click", ExpandDetail);
    YAHOO.util.Event.addListener("pushpin_" + theaterID, "click", ExpandDetail, theaterID, false);
    $get('theater_' + theaterID).style.zIndex = "11";
    $get('pushpin_' + theaterID).style.width = "235px";
    $get('pushpinPopup_' + theaterID).style.height = "";
    $get('pushpinPopup_' + theaterID).style.display = "";            
    $get('pushpinDropdown_' + theaterID).style.display = "";
    $get('pushpinClose_' + theaterID).style.display = "none";
}

function ExpandDetail(e, theaterID)
{
    YAHOO.util.Event.removeListener("pushpin_" + theaterID, mouseover, ExpandTitle);
    YAHOO.util.Event.removeListener("pushpin_" + theaterID, mouseout, Retract);
    YAHOO.util.Event.removeListener("pushpin_" + theaterID, "click", ExpandDetail);
    YAHOO.util.Event.removeListener("pushpin_" + theaterID, mousewheel, StopPropagation);
    YAHOO.util.Event.addListener("pushpin_" + theaterID, mousewheel, StopPropagation);
    YAHOO.util.Event.removeListener("pushpin_" + theaterID, "mousedown", StopPropagation);
    YAHOO.util.Event.addListener("pushpin_" + theaterID, "mousedown", StopPropagation);
    SendPinsToBack();
    $get('theater_' + theaterID).style.zIndex = "11";
    $get('pushpinBody_' + theaterID).style.display = "";
    $get('pushpinPopup_' + theaterID).style.height = "200px";
    $get('pushpinBody_' + theaterID).style.height = 181-$get('pushpinTitle_' + theaterID).offsetHeight+"px";
    $get('pushpinDropdown_' + theaterID).style.display = "none";
    $get('pushpinClose_' + theaterID).style.display = "";
    PanToFitScreen($get('pushpin_' + theaterID));
    AddTheaterClick(theaterID);
}

function Retract(e, theaterID)
{
    YAHOO.util.Event.removeListener("pushpin_" + theaterID, mouseover, ExpandTitle);
    YAHOO.util.Event.removeListener("pushpin_" + theaterID, mouseout, Retract);
    YAHOO.util.Event.removeListener("pushpin_" + theaterID, "click", ExpandDetail);
    YAHOO.util.Event.addListener("pushpin_" + theaterID, mouseover, ExpandTitle, theaterID, false);
    YAHOO.util.Event.addListener("pushpin_" + theaterID, mouseout, Retract, theaterID, false);
    $get('theater_' + theaterID).style.zIndex = "10";
    $get('pushpinPopup_' + theaterID).style.display = "none";
    $get('pushpinBody_' + theaterID).style.display = "none";
    $get('pushpin_' + theaterID).style.width = "32px";
    $get('pushpin_' + theaterID).style.height = "32px";
}

function PanToFitScreen(pushpin)
{
    var pos = YAHOO.util.Dom.getXY(pushpin);
    windowHeight=GetWindowHeight();
    var needsToMoveY = windowHeight - 265;
    var amountToMoveY = 0;
    if (pos[1] > needsToMoveY)
    {
        amountToMoveY = pos[1] - needsToMoveY;
    }
    windowWidth=GetWindowWidth();
    var needsToMoveX = windowWidth - 420;
    var amountToMoveX = 0;
    if (pos[0] > needsToMoveX)
    {
        amountToMoveX = pos[0] - needsToMoveX;
    }
    if (amountToMoveX > 0 || amountToMoveY > 0)
    {
        var centerCoord = map.GetCenterLatLong();
        var centerPixel = map.ConvertLatLongToPixel(centerCoord);
        var newCenterPixel = new ShowTimePixel();
        newCenterPixel.Set(centerPixel.X + amountToMoveX, centerPixel.Y + amountToMoveY);
        var newCenterCoord = map.ConvertPixelToLatLong(newCenterPixel);
        map.DetachPanEvent(GetShowTimesInBox);
        map.AttachPanEvent(AttachContinuousPan);
        map.PanToLatLong(newCenterCoord);
    }
}

function CollapsePanel(e, panel)
{
    $get(panel + 'Panel_header').style.height = 29;
    $get(panel + 'Panel_body').style.display = "none";
    if ($get(panel + 'Panel_toolbar'))
    {
        $get(panel + 'Panel_toolbar').style.display = "none";
    }
    var p = $get(panel + 'Panel');
    p.style.height = 30;
    if (Sys.Browser.name == Sys.Browser.InternetExplorer && Sys.Browser.version < 7)
    {
        $get(panel + 'PanelBackgroundImage').filters(0).src = "images/" + panel + "_compact.png";
    }
    else
    {
        p.style.background = "url(images/" + panel + "_compact.png) no-repeat top left";
    }
    switch (panel)
    {
        case "favoriteTheaters":
            var theatersPanel = $get('theatersPanel');
            MovePanel(theatersPanel, theatersPanel.style.left, bottomOfHeader + 40); 
            break;
        case "movies":
            var movieDetailPanel = $get('movieDetailPanel');
            MovePanel(movieDetailPanel, movieDetailPanel.style.left, bottomOfHeader + 40); 
            break;
    }
    YAHOO.util.Event.removeListener($get(panel + 'PanelBackgroundImage'), "click", CollapsePanel);
    YAHOO.util.Event.addListener($get(panel + 'PanelBackgroundImage'), "click", ExpandPanel, panel, false);
    YAHOO.util.Event.removeListener($get(panel + 'Panel_header'), "click", CollapsePanel);
    YAHOO.util.Event.addListener($get(panel + 'Panel_header'), "click", ExpandPanel, panel, false);
}

function ExpandPanel(e, panel)
{
    $get(panel + 'Panel_header').style.height = 16;
    $get(panel + 'Panel_body').style.display = "";
    if ($get(panel + 'Panel_toolbar') && eval(panel + 'ToolbarOn'))
    {
        $get(panel + 'Panel_toolbar').style.display = "";
    }
    var p = $get(panel + 'Panel');
    p.style.height = 260;
    if (Sys.Browser.name == Sys.Browser.InternetExplorer && Sys.Browser.version < 7)
    {
        $get(panel + 'PanelBackgroundImage').filters(0).src = "images/" + panel + "_expanded.png";
    }
    else
    {
        p.style.background = "url(images/" + panel + "_expanded.png) no-repeat top left";
    }
    switch (panel)
    {
        case "favoriteTheaters":
            var theatersPanel = $get('theatersPanel');
            MovePanel(theatersPanel, theatersPanel.style.left, bottomOfHeader + 160); 
            break;
        case "movies":
            var movieDetailPanel = $get('movieDetailPanel');
            MovePanel(movieDetailPanel, movieDetailPanel.style.left, bottomOfHeader + 270); 
            break;
    }
    YAHOO.util.Event.removeListener($get(panel + 'PanelBackgroundImage'), "click", ExpandPanel);
    YAHOO.util.Event.addListener($get(panel + 'PanelBackgroundImage'), "click", CollapsePanel, panel, false);
    YAHOO.util.Event.removeListener($get(panel + 'Panel_header'), "click", ExpandPanel);
    YAHOO.util.Event.addListener($get(panel + 'Panel_header'), "click", CollapsePanel, panel, false);
}

function AttachContinuousPan()
{
    map.DetachPanEvent(AttachContinuousPan);                    
    map.AttachPanEvent(GetShowTimesInBox);
}

function AttachZoom()
{
    map.DetachZoomEvent(AttachZoom);                    
    map.AttachZoomEvent(GetShowTimesInBox);
}

function StopPropagation(e)
{
    YAHOO.util.Event.stopPropagation(e);
}

function PngSwap(span, src, className)
{
    if (Sys.Browser.name == Sys.Browser.InternetExplorer && Sys.Browser.version < 7)
    {                 
        span.filters(0).src = src;
    }
    else
    {
        span.className = className;
    }
}

function isFavorite(theaterID)
{
    for (var i = 0; i < ft.length; i++)
    {
        if (ft[i] == theaterID)
        {
            return true;
        }
    }
    return false;
}

function MovePanel(panel, x, y)
{
    panel.style.top = y;
    panel.style.left = x;
}

function GetCookie(name){var dc=document.cookie;var prefix=name+"=";var begin=dc.indexOf("; "+prefix);if(begin==-1){begin=dc.indexOf(prefix);if(begin!=0)return null;}else{begin+=2;}var end=document.cookie.indexOf(";",begin);if(end==-1){end=dc.length;}return window.unescape(dc.substring(begin+prefix.length,end));}
function GetWindowWidth(){var width=0;if(typeof(window.innerWidth)=='number'){width=window.innerWidth;}else if(document.documentElement&&document.documentElement.clientWidth){width=document.documentElement.clientWidth;}else if(document.body&&document.body.clientWidth){width=document.body.clientWidth;}if(!width||width<100){width=100;}return width;}
function GetWindowHeight(){var height=0;if(typeof(window.innerHeight)=='number'){height=window.innerHeight;}else if(document.documentElement&&document.documentElement.clientHeight){height=document.documentElement.clientHeight;}else if(document.body&&document.body.clientHeight){height=document.body.clientHeight;}if(!height||height<100){height=100;}return height;}

function AddShowtimeClick(theaterID, movieID, startTime)
{
    ShowTimeMaps.AddShowtimeClick(theaterID, movieID, startTime, null, null, null);  
}

function AddTheaterClick(theaterID)
{
    ShowTimeMaps.AddShowtimeClick(theaterID, -1, new Date(1970, 0, 1), null, null, null);  
}

function AddMovieClick(movieID)
{
    ShowTimeMaps.AddShowtimeClick(-1, movieID, new Date(1970, 0, 1), null, null, null);  
}

function AddTheaterMovieClick(theaterID, movieID)
{
    ShowTimeMaps.AddShowtimeClick(theaterID, movieID, new Date(1970, 0, 1), null, null, null);  
}

function UpdateFavoriteTheaters()
{
    if (memberID > 0)
    {
        ShowTimeMaps.UpdateFavoriteTheaters({memberID : memberID,
            favoriteTheaters : ft.toString()}, null, null, null);
    }
}

function DoNothing() {}

function WS_GetShowTimesInBox(showDate, topLatitude, bottomLatitude, leftLongitude, rightLongitude)
{
    ShowTimeMaps.GetShowTimesInBox(topLatitude, bottomLatitude, leftLongitude, rightLongitude, 
        showDate, "showtimemapslicense", GetShowTimesInBox_Completed);
//    var service = $object('GetShowTimesInBox');
//    service.get_parameters().licenseKey = "showtimemapslicense";
//    service.get_parameters().showDate = showDate;
//    service.get_parameters().topLatitude = topLatitude;
//    service.get_parameters().bottomLatitude = bottomLatitude;
//    service.get_parameters().leftLongitude = leftLongitude;
//    service.get_parameters().rightLongitude = rightLongitude;
//    if ( ! service.invoke() )
//    {
//        service.abort();
//        service.invoke();
//    }
}

 function GetShowTimesInBox_Completed(sender, eventArgs)
{
    OnGetShowTimesComplete(sender);
}

function GetShowTimesInBox_Aborted(sender, eventArgs)
{
    //debug.trace('request aborted');
}

function GetShowTimesInBox_Error(sender, eventArgs)
{
    alert("error");
}

function GetAds()
{
    if (adsEnabled)
    {
        ShowTimeMaps.GetAds(null, OnGetAdsComplete, OnTimeout, OnError);
    }
}

function OnGetAdsComplete(result)
{
    ads = result;
    if (result[0] != null && result[0].Html.length > 0)
    {
        var html = result[0].Html;
        $get('moviesPanel_ad').innerHTML = html;
        for (var i = 0; i < theaterList.length; i++)
        {
            var theaterID = theaterList[i].TheaterID;
            $get('pushpinShowTimes_' + theaterID).innerHTML += "<div class=\"pushpinShowTimesAd\">" + html + "</div>";
        }
        $get('theatersPanel_ad').innerHTML = html;
    }
    if (result[1] != null && result[1].Html.length > 0)
    {
        var html = result[1].Html;
        $get('favoriteTheatersPanel_ad').innerHTML = html;
    }
    if (result[2] != null && result[1].Html.length > 0)
    {
        var html = result[2].Html;
        for (var i = 0; i < theaterList.length; i++)
        {
            var theaterID = theaterList[i].TheaterID;
            $get('pushpinTheaterInfo_' + theaterID).innerHTML += "<div class=\"pushpinTheaterInfoAd\">" + html + "</div>";
        }
    }
}

function toggleInputTip(text, control, on)
{
    if (on)
    {
        if (control.value.length == 0 && control.value != text)
        {
            control.className = "inputTip";
            control.value = text;
        }
    }
    else
    {
        if (control.value == text)
        {
            control.value = "";
        }
        control.className = "";
    }
}

function DisplayDirections(theaterIndex)
{
    $get('directions').style.display = "";
    var theater = theaterList[theaterIndex];
    var theaterInfo = new Sys.StringBuilder();
    theaterInfo.append(theater.Name + ", ");
    theaterInfo.append(theater.Address);
    theaterInfo.append(", ");
    theaterInfo.append(theater.City);
    theaterInfo.append(", ");
    theaterInfo.append(theater.State);
    theaterInfo.append(", ");
    theaterInfo.append(theater.PostalCode);
    $get('directionsTheaterInfo').innerHTML = theaterInfo.toString();
    directionDestination = theater.Address + ", " + theater.City + ", " + theater.State + ", " + theater.PostalCode;
}

function GetDirections()
{
    RemovePushpins();
    map.DetachZoomEvent(GetShowTimesInBox);        		
    map.DetachPanEvent(GetShowTimesInBox);
    map.GetRoute(directionStart, directionDestination, null);
    $get('directions').style.display = "none";
}

function HideDirections()
{
    $get('directions').style.display = "none";
}

function ShowWelcome()
{       
    $get('welcome').style.visibility = "visible";
    $get('welcome').style.zIndex = 31;
}

function HideWelcome()
{
    $get('welcome').style.visibility = "hidden";
    $get('welcome').style.zIndex = 0;
}
