$(document).ready(function () {
    var index = {

        head: $('head'),
        bookingForm: $('.book-now'),

        init: function () {

            if (this.recentlyViewed.control.size() > 0) {
                this.recentlyViewed.init();
            }

            if (this.resultsGrid.control.size() > 0) {
                this.resultsGrid.init();
            }

            if (this.resultsList.control.size() > 0) {
                this.resultsList.init();
            }
            if (this.resultsCar.control.size() > 0) {
                this.resultsCar.init();
            }

            if (this.gallery.control.size() > 0) {
                this.gallery.init();
            }

            if (this.offers.control.size() > 0) {
                this.offers.init();
            }

            if (this.tabs.control.size() > 0) {
                this.tabs.init();
            }
			
			if (this.contactForm.control.size() > 0) {
                this.contactForm.init();
            }

            if (this.enquiryForm.control.size() > 0) {
                this.enquiryForm.init();
            }
			
        },

        tabs: {
            control: $('#tabs'),
            note: $('.note a'),
            tip: $('.tip'),
            init: function () {
                var $me = this;
                $me.control.tabs();
                $me.note.tooltip();
                $me.tip.tooltip({ tipClass: 'sm-tooltip' });
            }
        },

        offers: {
            banner: $('#banner'),
            control: $('#offers ul'),
            items: $('#offers ul li'),
            ribbon: $('#ribbon'),
            init: function () {
                var $me = this;

                var wrap = ($me.items.size() > 1) ? 'circular' : null;

                $me.control.jcarousel({
                    auto: 10,
                    itemVisibleInCallback: function (c, o, i, s) {
                        var type = $(o).attr('data-type');
                        $me.ribbon.attr('class', 'rbn ' + type);
                    },
                    scroll: 1,
                    wrap: wrap
                });
            }
        },

        gallery: {
            control: $('#gallery'),
            tabs: $('#gal-tabs'),
            thumb: $('#gal-images .thumbs ul img'),
            thumbs: $('#gal-images .thumbs ul'),
            image: $('#gal-images .screen img'),
            canvas: $('#map-canvas'),
            screen: $('#gal-map'),
            photos: $('#gallery .photos'),
            panos: $('#gallery .panos'),
            init: function () {
                var $me = this;
                $me.control.tabs({
                    select: function (event, ui) {
                        var id = $me.tabs.find('a:eq(' + ui.index + ')').attr('href');

                        if (id == '#gal-map' && $me.screen.find('#map-canvas').size() < 1) {
                            $me.canvas.css('left', '0').appendTo($me.screen);
                        }
                    }
                });

                if ($me.thumbs.size() > 0) {
                    $me.thumbs.jcarousel();

                    $me.thumb.click(function () {
                        var src = $(this).attr('src').replace('thumbs', 'main');
                        $me.image.attr('src', src);
						//change the link
						var href = $(this).attr('src').replace('thumbs', '');
						$("a.gallery_group").attr('href', href);
                    });
                }

                if ($me.map.canvas.size() > 0) {
                    $me.map.init();
                }
            },
            map: {
                canvas: $('#map-canvas'),
                init: function() {
                    var $me = this;

                    var lat = parseFloat($me.canvas.attr('data-lat'));
                    var lng = parseFloat($me.canvas.attr('data-lng'));

                    var latlng = new google.maps.LatLng(lat, lng);
                    var myOptions = {
                        zoom: 9,
                        center: latlng,
                        mapTypeId: google.maps.MapTypeId.HYBRID,
                        mapTypeControl: false
                    };

                    var map = new google.maps.Map($me.canvas[0], myOptions);

                    $me.createMarker(map, latlng, false);
                    $me.createMarker(map, new google.maps.LatLng(38.7704, -9.1291), true);
                    $me.createMarker(map, new google.maps.LatLng(37.01571219880126, -7.971954345703125), true);
                },
                createMarker: function (map, point, isAirport) {
                    if (isAirport == false) {
                        var marker = new google.maps.Marker({
                            position: point,
                            map: map
                        });

                        return;
                    };

                    var image = new google.maps.MarkerImage(
                        '/images/maps/airport.png',
                        new google.maps.Size(32, 37),
                        new google.maps.Point(0, 0),
                        new google.maps.Point(16, 37)
                    );

                    var shadow = new google.maps.MarkerImage(
                        '/images/maps/airport-shadow.png',
                        new google.maps.Size(54, 37),
                        new google.maps.Point(0, 0),
                        new google.maps.Point(16, 37)
                        );

                    var shape = {
                        coord: [30, 0, 31, 1, 31, 2, 31, 3, 31, 4, 31, 5, 31, 6, 31, 7, 31, 8, 31, 9, 31, 10, 31, 11, 31, 12, 31, 13, 31, 14, 31, 15, 31, 16, 31, 17, 31, 18, 31, 19, 31, 20, 31, 21, 31, 22, 31, 23, 31, 24, 31, 25, 31, 26, 31, 27, 31, 28, 31, 29, 31, 30, 30, 31, 24, 32, 23, 33, 22, 34, 21, 35, 20, 36, 11, 36, 10, 35, 9, 34, 8, 33, 7, 32, 1, 31, 0, 30, 0, 29, 0, 28, 0, 27, 0, 26, 0, 25, 0, 24, 0, 23, 0, 22, 0, 21, 0, 20, 0, 19, 0, 18, 0, 17, 0, 16, 0, 15, 0, 14, 0, 13, 0, 12, 0, 11, 0, 10, 0, 9, 0, 8, 0, 7, 0, 6, 0, 5, 0, 4, 0, 3, 0, 2, 0, 1, 1, 0, 30, 0],
                        type: 'poly'
                    };

                    var marker = new google.maps.Marker({
                        icon: image,
                        shadow: shadow,
                        shape: shape,
                        map: map,
                        position: point
                    });
                }
            }
        },

        recentlyViewed: {
            control: $('#recently-viewed ul'),
            init: function () {
                var $me = this;
                $me.control.jcarousel();
            }
        },

        resultsGrid: {
            control: $('#result-picture li'),
            order: $('#order'),
            init: function () {
                var $me = this;
                $me.control.draggable({
                    revert: true
                });

                $me.order.change(function () {
					var location = $("#order option:selected").val();
              		window.location.href = "http://www.thevillaagency.co.uk/" + location;
                })
            }
        },

        resultsList: {
            control: $('#result-list li'),
            order: $('#order'),
            init: function () {
                var $me = this;

                $me.order.change(function () {
					var location = $("#order option:selected").val();
              		window.location.href = "http://www.thevillaagency.co.uk/" + location;
                })
            }
        },
		
		resultsCar: {
            control: $('#result-list.car-hire li'),
            action: $('.actions a'),
            prices: $('.prices'),
            init: function () {
                var $me = this;

                $me.action.click(function (e) {
                    e.preventDefault();
                    var prices = $(this).parents('div.content').next('.prices')
                    prices.slideToggle();
                    var showHide = $(this).text();
                    showHide = (showHide.indexOf('Hide') > -1) ? showHide.replace('Hide', 'View') : showHide.replace('View', 'Hide');
                    $(this).text(showHide);
                });
            }
        },

		contactForm: {
            control: $('form#contact-form'),
            send: $('input#Send'),
            init: function () {
                var $me = this;
                $me.control.validate();
            }
        },

        enquiryForm: {
            control: $('form#enquiry-form'),
            dates: $('#dates_Required'),
            send: $('input#Send'),
            init: function () {
                var $me = this;

                this.dates.datepicker({
                    buttonImage: 'images/icons/calendar.gif',
                    buttonImageOnly: true,
                    dateFormat: 'dd/mm/yy',
                    minDate: 0,
                    maxDate: '+2Y',
                    showOn: 'both'
                });

                jQuery.validator.addMethod("datePast", function (value, element) {
                    var dates = value.split('/');
                    var date = new Date(parseInt(dates[2]), parseInt(dates[1]) - 1, parseInt(dates[0]));
                    return this.optional(element) || new Date() < date;
                }, "You cannot select a date which in the past");


                $me.control.validate();
            }
        }
    };

    index.init();
});

function addShortlist(v){
    $.ajax({
		type: "POST",
        url: "ajax/shortlist.php",
        data: "villa=" + v,
        success: function(){
            $('#addShortlist' + v).hide();
        }
	});
}
function removeShortlist(v){
	//new Ajax.Request('ajax/shortlist.php?action=remove', {parameters:'villa=' + element});
    $.ajax({
		type: "POST",
        url: "ajax/shortlist.php",
        data: "action=remove&villa=" + v,
        success: function(){
            $('#shortlist' + v).hide();
        }
	});
}
