function preventdefault(event)
{
    if (event.preventDefault)
    {
        event.preventDefault();
    }
    else if (!event.preventDefault)
    {
        event.returnValue = false;
    }
}

$( document ).ready( function () {
    //Google Event Tracking
    if ( $( '#button' ).length ) {
        $( '#button' ).on( 'click', function () {
            //ga('send', 'event', 'button', 'click', 'nav-buttons');
        } );
    }
} );
$( document ).ready( function () {
    //fancybox
    if ( $( ".gal" ).length )
    {
        $( '.gal' ).fancybox( {
            prevEffect: 'none',
            nextEffect: 'none'
        } );
    }
} );
var locations = [
    [ 'Wilmot Cottages', -33.9945, 25.6696, 'hotel.png' ]
];
var markersArray = [ ];
var directionDisplay, marker, infowindow, directionsService, center;

$( document ).ready( function () {
    $( '#directions-panel' ).hide();
    if ( $( "#map" ).length )
    {
        initialize();
    }

    //Map Button Controls
    $( "#clear" ).click( function () {
        $( '#directions-panel' ).empty();
        $( '#searchTextField' ).val( "" );
        $( '#directions-panel' ).hide();
        initialize();
    } );
} );

function initialize()
{
    center = new google.maps.LatLng( locations[0][1], locations[0][2] );
    directionsService = new google.maps.DirectionsService();
    infowindow = new google.maps.InfoWindow();

    directionsDisplay = new google.maps.DirectionsRenderer();
    var myOptions = {
        zoom: 15,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        scrollwheel: false,
        center: center
    };
    var map = new google.maps.Map( document.getElementById( 'map' ), myOptions );
    directionsDisplay.setMap( map );
    directionsDisplay.setPanel( document.getElementById( 'directions-panel' ) );

    for ( var i = 0; i < locations.length; i++ )
    {
        //Add items to drop down
        var infoContent = '<strong>' + locations[i][0] + '</strong>';
        var image = new google.maps.MarkerImage( '/media/main/' + locations[i][3],
                new google.maps.Size( 32, 37 ),
                new google.maps.Point( 0, 0 ),
                new google.maps.Point( 16, 37 ) );

        marker = new google.maps.Marker(
                {
                    map: map,
                    draggable: false,
                    animation: google.maps.Animation.DROP,
                    icon: image,
                    title: locations[i][0],
                    infoWindow: infoContent,
                    position: new google.maps.LatLng( locations[i][1], locations[i][2] )
                } );

        markersArray.push( marker );

        google.maps.event.addListener( marker, 'click', function ()
        {
            infowindow.close()
            infowindow = new google.maps.InfoWindow(
                    {
                        content: this.infoWindow,
                        maxWidth: 400
                    } );
            infowindow.open( map, this );
        } );
    }
    google.maps.event.addListener( map, 'click', function ()
    {
        infowindow.close()
    } );

    var input = document.getElementById( 'searchTextField' );
    var autocomplete = new google.maps.places.Autocomplete( input );
    autocomplete.bindTo( 'bounds', map );
}

function calcRoute()
{
    if ( document.getElementById( 'searchTextField' ).value != "" )
    {
        $( '#directions-panel' ).empty();
        deleteOverlays();

        var request = {
            destination: center,
            origin: document.getElementById( 'searchTextField' ).value,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        };
        directionsService.route( request, function ( response, status ) {
            if ( status == google.maps.DirectionsStatus.OK )
            {
                directionsDisplay.setDirections( response );
                $( '#directions-panel' ).show();
            }
            else
            {
                $( '#directions-panel' ).empty();
                $( '#searchTextField' ).val( "" );
                $( '#directions-panel' ).hide();
                initialize();
            }
        } );
    }
    else
    {
        $( '#directions-panel' ).empty();
        $( '#searchTextField' ).val( "" );
        $( '#directions-panel' ).hide();
        initialize();
    }
}

function deleteOverlays()
{
    if ( markersArray )
    {
        for ( i in markersArray )
        {
            markersArray[i].setMap( null );
        }
        markersArray.length = 0;
    }
}
$( document ).ready( function () {
    //Owl-Carousel
    if ( $( ".owl-slide" ).length ) {
        var owl = $( ".owl-slide" );
        owl.owlCarousel( {
            navigation: false,
            pagination: false,
            autoplayHoverPause: false,
            loop: true,
            singleItem: true,
            items: 1,
            lazyLoad: true
        } );
        owl.trigger( 'owl.play', 5000 );
        $( ".owl-slide" ).hover( function () {
            $( ".slide-controls" ).show();
        }, function () {
            $( ".slide-controls" ).hide();
        } );
        $( ".slide-controls" ).hover( function () {
            $( ".slide-controls" ).show();
        }, function () {
            $( ".slide-controls" ).hide();
        } );
        $( '.slide-prev' ).click( function () {
            owl.trigger( 'owl.prev' );
        } );
        $( '.slide-play' ).click( function () {
            owl.trigger( 'owl.play', 5000 );
            $( ".slide-play" ).hide();
            $( ".slide-pause" ).show();
        } );
        $( '.slide-pause' ).click( function () {
            owl.trigger( 'owl.stop' );
            $( ".slide-play" ).show();
            $( ".slide-pause" ).hide();
        } );
        $( '.slide-next' ).click( function () {
            owl.trigger( 'owl.next' );
        } );
        $( ".slide-controls" ).hide();
        $( ".slide-play" ).hide();
    }
} );
$( document ).ready( function () {
    //UI To Top Button
    $().UItoTop( {
        easingType: 'easeOutQuart'
    } );
} );
$( document ).ready(
        function ()
        {
            if ( $( "#form" ).length )
            {
                //Load Php Form
                $.ajax(
                        {
                            url: '/includes/form/form.php',
                            type: 'POST',
                            data: ""
                        }
                ).done(
                        function ( response )
                        {
                            $( "#form" ).html( response );
                            $( "#submit" ).removeAttr( "disabled" );
							$.formUtils.addValidator({
								name : 'greaterThanToday',
								validatorFunction : function(value, $el, config, language, $form) {
									var now = new Date();
									var myDate = new Date(value);
									return myDate > now;
								},
								errorMessage : 'you can not arriva in the past',
								errorMessageKey: 'badFutureDate'
							});
							
							$.formUtils.addValidator({
								name : 'lessThanArrive',
								validatorFunction : function(value, $el, config, language, $form) {
									var now = new Date($("#arrival").val());
									var myDate = new Date(value);
									return myDate > now;
								},
								errorMessage : 'you can not leave before you arrive',
								errorMessageKey: 'badLeaveDate'
							});
                            //Handle Form Submit
                            $.validate(
                                    {
                                        form: '#query',
                                        scrollToTopOnError: false,
                                        onSuccess: function ( event )
                                        {
                                            $( ".busy" ).show();
                                            $( "#submit" ).attr( "disabled", "disabled" );
                                            var datastring = $( "#query" ).serialize();
                                            $.ajax(
                                                    {
                                                        url: '/includes/form/processor.php',
                                                        type: 'POST',
                                                        data: datastring,
                                                    }
                                            ).done(
                                                    function ( response )
                                                    {
                                                        $( "#query" ).html( response );
                                                    }
                                            ).fail(
                                                    function ( response )
                                                    {
                                                        $( "#query" ).html( "Failed To Load Form" );
                                                    }
                                            );
                                            return false;
                                        },
                                        onError: function ()
                                        {
                                        }
                                    }
                            );
                            $( '#message' ).restrictLength( $( '#maxlength' ) );
                        }
                ).fail(
                        function ( response )
                        {
                            $( "#foot_form" ).html( "Failed To Load Form" );
                        }
                );
            }
        }
);
$( document ).ready( function () {
    if ( $( '.masonry' ).length )
    {
        $container = $( ".masonry" );
        $( ".masonry" ).imagesLoaded( function () {
            var msnry = new Masonry( ".masonry", {
                // options...
                itemSelector: '.item'
            } );
        } );
    }
} );
$.fn.imagesLoaded = function ( callback ) {
    var elems = this.find( 'img' ),
            len = elems.length,
            _this = this;

    elems.bind( 'load', function () {
        if ( --len <= 0 ) {
            callback.call( _this );
        }
    } ).each( function () {
        // cached images don't fire load sometimes, so we reset src.
        if ( this.complete || this.complete === undefined ) {
            var src = this.src;
            // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
            // data uri bypasses webkit log warning (thx doug jones)
            this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
            this.src = src;
        }
    } );

    return this;
};
