//1. Define custom validation 
jQuery.validator.addMethod("defaultInvalid", function(value, element){
        
    switch (element.value) 
    {               
        case "First Name":
            if (element.name == "first_name") 
                return false;             
        break;
        
        case "Last Name":            
            if (element.name == "last_name") 
                return false;             
        break;
        
        case "Your Email Address":
            if (element.name == "email" ) 
                return false;             
        break;
        
        case "Email Address":
            if (element.name == "email") 
                return false;             
        break;
        
        case "Mobile":
            if (element.name == "mobile") 
                return false;             
        break;
        
        case "Select Booking Date":
            if (element.name == "booking_date") 
                return false;             
        break;
        
        case "Select Booking Time":
            if (element.name == "booking_time") 
                return false;             
        break;
        
        case "Postcode":
            if (element.name == "subscribe_postcode") 
                return false;             
        break;
        
        case "Message":
            if (element.name == "message") 
                return false;             
        break;
        
        default:
            return true;
    }
}, "This field is required.");    
    
$(document).ready(function(){
            
    if( $(".popup").length > 0)                                                       
    {
        $(".popup").colorbox({width:"405px", inline:true, href:"#popup-div"    });
        
        $("a.popup").live('click', function(e) {
            
            e.preventDefault();
            
            $(".popup").colorbox({width:"405px", inline:true, href:"#popup-div"    });
        });
    }
                
    if( $('#slider').length > 0 )
    {
        $('#slider').nivoSlider({
            effect: 'sliceDown', // Specify sets like: 'fold,fade,sliceDown'
            slices: 15, // For slice animations
            boxCols: 8, // For box animations
            boxRows: 4, // For box animations
            animSpeed: 500, // Slide transition speed
            pauseTime: 6000, // How long each slide will show
            startSlide: 0, // Set starting Slide (0 index)
            directionNav: true, // Next & Prev navigation
            directionNavHide: true, // Only show on hover
            controlNav: true, // 1,2,3... navigation
            controlNavThumbs: false, // Use thumbnails for Control Nav
            controlNavThumbsFromRel: false, // Use image rel for thumbs
            controlNavThumbsSearch: '.jpg', // Replace this with...
            controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
            keyboardNav: true, // Use left & right arrows
            pauseOnHover: true, // Stop animation while hovering
            manualAdvance: false, // Force manual transitions
            captionOpacity: 0.8, // Universal caption opacity
            prevText: 'Prev', // Prev directionNav text
            nextText: 'Next', // Next directionNav text
            beforeChange: function(){}, // Triggers before a slide transition
            afterChange: function(){}, // Triggers after a slide transition
            slideshowEnd: function(){}, // Triggers after all slides have been shown
            lastSlide: function(){}, // Triggers when last slide is shown
            afterLoad: function(){} // Triggers when slider has loaded
        });
    }      
    
    $("#subscribes").validate();
    
    $(".subscribe").colorbox({onClosed:function(){
        $('#subscribe-div-form').show();
        $('#subscribe-div-message').hide();
        
        $('#subscribe-div-form input').val('');        
    }});
    
    $("#subscribes a.join_button").click(function(e) {
        
        e.preventDefault();
        
        if ( $("#subscribes").valid() )
        {
            var parameters                  = {};
            parameters['first_name']        = $('#subscribe_first_name').val();
            parameters['email']             = $('#subscribe_email').val();
            parameters['postcode']          = $('#subscribe_postcode').val();
            
            $('#subscribes').block({ 
                message: 'Sending...', 
                css: { border: '0px solid #a00', background: "#cccccc" } 
            });
            
            $.post(base_url + 'postback/subscribe', parameters , function(data)
            {
                $('#subscribes').unblock();
                
                if( data.error != "" )
                    alert(data.error);
                else
                {
                    $('#subscribe-div-form').hide();
                    $('#subscribe-div-message').show();
                }
                
            }, "json");
          }
          else
            alert("Please complete all fields marked with a red asterisk(*).");
    });    
});

/* block functions */

function blockObject(obj)
{
     obj.block
     ({
        message: '<div><img src="'+base_url+'application/modules/global/images/ajax-loader-big.gif"/></div>',  
        css: {border:'0px','background-color':'transparent',position:'absolute'},
        overlayCSS: {opacity:0.04,cursor:'pointer'}
      });
 }

function unblockObject(obj)
{
    obj.unblock();
}


function blockElement(elementSelector)
{
     $(elementSelector).block
     ({
        message: '<div><img src="'+base_url+'application/modules/global/images/ajax-loader-big.gif"/></div>',  
        css: {border:'0px','background-color':'transparent',position:'absolute'},
        overlayCSS: {opacity:0.04,cursor:'pointer',position:'absolute'}
      });
 }

function unblockElement(elementSelector)
{
    $(elementSelector).unblock();
}

function handleJSON_response(data)
{

    if (data.js)
    {
        alert(data.js);
        eval(data.js);
    }

}

setTimeout(function() {
    
    $('div.success').slideUp('slow');
    $('div.error').slideUp('slow');
    
}, 3500);
