
var Meals = function() {

    this.html     = new Html()
    this.login    = new Login()
    this.url      = new Url()
    this.thickbox = new ThickBoxGeneric()

    this.init = function() {

        var self = this

        self.thickbox.init()

        self.id    = this.url.clickedUrlArg('Id')
        self.day   = this.url.clickedUrlArg('Day')
        self.month = this.url.clickedUrlArg('Month')
        self.year  = this.url.clickedUrlArg('Year')

        self.loggedIn = self.login.loggedIn()

        self.loadMeal()

        $('#bookMealBringGuests').click( function() { self.showGuestFunctions($(this)) })
        $('#guestAdd').click( function() { self.addGuest() })
        $('#otherGuestAdd').click( function() { self.addOtherGuest() })
        $('#eventSaveButton').click( function() { self.saveMeal() })
    }

    this.loadMeal = function() {

        var self = this

        var hasDate = 0
        if (self.day) {
            hasDate = 1
        }
        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=loadMeal&Id=' + self.id + '&HasDate=' + hasDate,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        if (hasDate == 0) {
                            self.day   = data.Event.day
                            self.month = data.Event.month
                            self.year  = data.Event.year
                        }
                        if (self.day) {
                            $('#bookMealTitle').html(data.Meal.title + ' on ' + self.day + '/' + self.month + '/' + self.year) 
                        } else {
                            $('#bookMealTitle').html(data.Meal.title) 
                        }
                        $('#selfSelectSpan').html(data.Self)
                        $('#membersSelectSpan').html(data.People)
                        if (! self.loggedIn) {
                            $('#bookMealDiv .notLoggedIn').show()
                            $('#selfText').keyup(function() { 
                                                    if ($(this).val() != '') {
                                                        $('#selfSelect').val(0)  
                                                    }
                                                })
                            $('#selfSelect').change(function() { 
                                                        if ($(this).val() != 0) {
                                                            $('#selfText').val('')  
                                                        }
                                                    })
                        }
                    }
                }
        })
    }

    this.showGuestFunctions = function(elem) {

        var self = this

        var checked = self.html.getCheckBoxValue($(elem)) 
        if (checked == 1) {
            $('#selectGuestsTable').show()
        } else {
            $('#selectGuestsTable').hide()
        }
    }

    this.addGuest = function() {

        var self = this

        var id = $('#peopleSelect').val()
        var val = $('#peopleSelect option:selected').html()
        if (! val) {
            alert('Please select a guest name')
            return
        }
        var exists = 0
        $('#selectedGuests div').each(
            function() {
                var thisVal = $(this).html() 
                if (val == thisVal) {
                    alert("You've already added " + val)
                    exists = 1
                    return
                }
            })
        if (exists == 1) {
            return
        }
        self.addGuestDiv(val, id) 
    }    

    this.addOtherGuest = function() {

        var self = this

        var val = $('#otherGuestText').val()
        if (! val) {
            alert('Please enter a guest name')
            return
        }
        var exists = 0
        $('#selectedGuests div').each(
            function() {
                var thisVal = $(this).html() 
                if (val == thisVal) {
                    alert("You've already added " + val)
                    exists = 1
                    return
                }
            })
        if (exists == 1) {
            return
        }
        self.addGuestDiv(val) 
    }

    this.addGuestDiv = function(val, id) {

        var self = this

        if (id) {
            $('#selectedGuests').append("<div class='other person' id='person_" + id + "'>" + val + "</div>")
        } else {
            $('#selectedGuests').append("<div class='other'>" + val + "</div>")
        }
        self.html.unbindClick($('#selectedGuests div'))
        $('#selectedGuests div').click( function() { $(this).remove() })
    }

    this.saveMeal = function () {

        var self = this

        var selfText = ''
        var selfId   = '' 
        if (! self.loggedIn) {
            selfText = $('#selfText').val()
            selfId   = $('#selfSelect').val()
            if (selfText == '' && selfId == 0) {
                alert('Please tell us who you are by selecting from the drop down, or filling in the box')
                return
            }  
        }
        var guests = new Array()
        $('#selectedGuests div').each(
            function() {
                if ($(this).hasClass('person')) {
                    var id = $(this).attr('id').split('_')
                    guests.push(id[1])
                } else {
                    guests.push($(this).html())
                }
            })

        var bringGuests = self.html.getCheckBoxValue($('#bookMealBringGuests'))
        if (bringGuests && guests.length == 0) {
            alert('You have selected \'I would like to bring guests\', but have added no guests')
            return
        }

        $.ajax({
                  type:     'POST',
                  url:      AJAXURL,
                  data:     'Action=bookEvent&Id=' + self.id +
                                            '&Day=' + self.day +
                                            '&Month=' + self.month +
                                            '&Year=' + self.year +
                                            '&SelfId=' + selfId +
                                            '&SelfText=' + selfText +
                                            '&Guests=' + guests.join('|'),
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        alert('Thank you for your booking. See you there!')
                    }
                    tb_remove()
                }
        })
    }
}
