var livebookingSettings = {
connectionid: "UK-RES-DANDDLONDONGROUP:15283",
restaurantid: "103050",
gaAcountNumber: 'UA-19102492-1',
style:{
baseColor: "ff6600",
highlightColor: "ff6600",
borderStyle: "0"
},
callbacks: {
        onConversion: lbConversion
}
};

$(document).ready(function(){
    setupPage();

});

function setupPage(){
    bindHovers();
    bindVenueSwitcher();
    bindBookTableGoButton();
    bindPhotoScrolls();
    bindGalleries();
    bindTandC();
    bindNewsletterSubmitButton();
    if(typeof widgetColor == 'string'){
        jQuery.extend(livebookingSettings, {style: {baseColor: widgetColor, highlightColor: widgetColor, borderStyle: "0"}})
    }
    $('#venue').val(livebookingSettings.restaurantid);
    if(typeof startingVenueId == 'number' || typeof startingVenueId == 'string'){
        if(typeof startingPromotionId == 'number' || typeof startingPromotionId == 'string'){
            updateLiveBooking(startingVenueId, startingPromotionId);
        }
        else{
            updateLiveBooking(startingVenueId);
        }
        
    }
    else{
        embedLivebooking();
    }
    
    $('#searchBox').click(function (event){
        if($(this).val() == 'ENTER YOUR SEARCH'){
        $(this).val('');
        }
    });
    $('#email, #contact_email').click(function (event){
        if($(this).val() == 'ENTER YOUR EMAIL ADDRESS'){
        $(this).val('');
        }
    });
}

function bindHovers(){
    $(".box-hover").fadeTo(250, 0);
    $(".box-hover").hover(function(){
        $(this).fadeTo(250, 1.0);
    },function(){
        $(this).fadeTo(250, 0);
    });
}

function bindVenueSwitcher(){
    $('#venue_switcher').change(function(){
        if($(this).val()!=""){
            document.location = $(this).val();
        }
        
    });
}

function bindPhotoScrolls(){
    $('.scrollLeft').click(function(event){
        event.preventDefault();
        var slider = $(this).parent().siblings('.slider');
        slider.stop().animate({
            scrollLeft: slider.scrollLeft()-315
        }, 1000);
    });
    $('.scrollRight').click(function(event){
        event.preventDefault();
        var slider = $(this).parent().siblings('.slider');
        slider.stop().animate({
            scrollLeft: slider.scrollLeft()+315
        }, 1000);
    });
}

function swap_show(id){
	document.getElementById(id+"-hover").style.display="block";
 //    $("#"+id+"-hover").show();
 }

function swap_hide(id){
   //  $("#"+id+"-hover").hide();
   document.getElementById(id+"-hover").style.display="none";
 }

 function bindBookTableGoButton(){
    $('#book-a-table-go').click(function(event){
        event.preventDefault();
        updateLiveBooking();
    });
}

function updateLiveBooking(venueId, offerId){
    if(typeof venueId != 'number' && typeof startingVenueId != 'string'){
        venueId = $('#venue').val();
    }
        $.ajax({
            url: '/default/bookTable',
            type: 'post',
            data: 'venue='+venueId,
            success: function(data){
                if(venueId){
                    jQuery.extend(livebookingSettings, {restaurantid: venueId});
                }
                if(offerId){
                    jQuery.extend(livebookingSettings, {promotionid: offerId});
                }
                $('#book-a-table-widget').remove();
                $('#book-a-table').append(data);
                if($('#venue-events').length > 0){
                    $.ajax({
                       url: '/venues/venueEvents',
                       data: 'venue='+venueId,
                       success: function(data){
                           $('#venue-events').html(data);
                       }
                    });
                }
                startingPromotionId = null;
                startingVenueId = null;
                embedLivebooking();
            }
        });
}

function embedLivebooking(){
    if(typeof connectionId != 'undefined'){
        jQuery.extend(livebookingSettings, {connectionid: connectionId})
    }
    $('#book-a-table-widget').lbuiDirect(livebookingSettings);
}

function bindGalleries(){
    $('.gallery-link').click(function(event){
        event.preventDefault();
        $.ajax({
            url: $(this).attr('href'),
            success: function(data){
                $('body').remove('#showcase-wrapper');
                $('body').append(data);
                $('#showcase-wrapper').dialog({
                    modal: true,
                    width: 1050,
                    height: 850,
                    dialogClass: 'galleryDialog',
                    show: {effect: 'fade', duration: '1500'},
                    hide: {effect: 'fade', duration: '1500'}
                }).children('#showcase').awShowcase(
                {
                    content_width:  900,
                    content_height: 600,
                    loading: true,
                    arrows: true,
                    buttons: false,
                    keybord_keys: true,
                    mousetrace: false, /* Trace x and y coordinates for the mouse */
                    pauseonover: true,
                    stoponclick: true,
                    transition: 'hslide',
                    transition_delay: 300,
                    transition_speed: 500,
                    thumbnails: true,
                    thumbnails_position: 'outside-last', /* outside-last/outside-first/inside-last/inside-first */
                    thumbnails_direction: 'horizontal', /* vertical/horizontal */
                    thumbnails_slidex: 0 /* 0 = auto / 1 = slide one thumbnail / 2 = slide two thumbnails / etc. */
                });

            }
        });
    });
}

function bindTandC(){
    $('label[for="tandc"]').click(function(){
       $('#modal-content').remove();
        $.ajax({
            url: $(this).attr('rel'),
            success: function(data){
                $('body').append(data);
                $('#modal-content').dialog({
                  width: 800,
                  height: 800,
                  modal: true,
                  resizable: false
                });
                bindAcceptTermsAndConditions()
            }
        });
    });

    $('.modal-link').click(function(e){
        e.preventDefault();
       $('#modal-content').remove();
        $.ajax({
            url: $(this).attr('href'),
            success: function(data){
                $('body').append(data);
                $('#modal-content').dialog({
                  width: 800,
                  height: 800,
                  modal: true,
                  resizable: false
                });
            }
        });
    });
}

function bindAcceptTermsAndConditions(){
    $('#acceptTandC').click(function(){
        if($('#tandc').prop('checked')==false){
            $('#tandc').prop('checked', true)
        }
        $('#modal-content').dialog('close');
    });

}

function bindNewsletterSubmitButton(){
    $('.newsletter-signup-submit input[type="submit"]').click(function(event){
        if($('#tandc').prop('checked')==false){
            event.preventDefault();
            $('label[for="tandc"]').trigger('click');
        }
    });
}

function lbConversion($obj){
        $('<iframe width="1" height="1" frameborder="0">').attr('src', 'http://www.danddlondon.com/default/conversionTracking').hide().appendTo('body');

        var $sTransactionId = $obj.bookingId;
        var $iPartySize = $obj.covers;
        var $iBookingTime = $obj.time;
}
