function fadeGall(){
	var wait_time = 1500; // in ms
	var change_speed = 800; // in ms
	var _hold = $('div.gallery');
	if(_hold.length){
		var _t;
		var _f = true;
		var _list = _hold.find('ul.slider > li');
		var _btn = $('<ul class="paging"></ul>');
		_list.each(function(_i){
			_btn.append('<li><a href="#">'+(_i+1)+'</a></li>');
		});
		_btn = $('ul.switcher').find('a');
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		
		_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		_btn.eq(_a).parent('li').addClass('active');
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		_hold.mouseenter(function(){
			_f = false;
			if(_t) clearTimeout(_t);
		}).mouseleave(function(){
			_f = true;
			if(_t) clearTimeout(_t);
			if(_f && wait_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, wait_time);
			}
		});
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:change_speed});
				_list.eq(_ind).addClass('active').animate({opacity: 1}, {queue:false, duration:change_speed});
				_btn.eq(_a).parent('li').removeClass('active');
				_btn.eq(_ind).parent('li').addClass('active');
				_a = _ind;
			}
		}
	}
}
jQuery.fn.gallSlide = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = _options.duration;
		var _timer = _options.autoSlide;
		var _wrap = _hold.find('ul.switcher');
		var _el = _hold.find('ul.switcher > li');
		var _next = _hold.find('a.link-prev');
		var _prev = _hold.find('a.link-next');
		var _count = _el.index(_el.filter(':last'));
		var _w = _el.outerHeight(true);
		var _wrapHolderW = 6;
		var _active = 0;
		function scrollEl(){
			_wrap.eq(0).animate({
				marginTop: -(_w * _active/3) + "px"
			}, {queue:false, duration: _speed});
		}
		_next.click(function(){
			_active = _active + 3;
			if (_active > (_count - 15)) _active = 0;
			scrollEl();
			return false;
		});
		_prev.click(function(){
			_active = _active - 3;
			if (_active < 0) _active = _count - 17;
			scrollEl();
			return false;
		});
	});
}
$(document).ready(function(){
    fadeGall();
    try
    {
	    $('div.preview').gallSlide({
		    duration: 700
    });
    } catch(ex){}

	swfobject.embedSWF("/loader.swf", "theSWF", "550", "504", "9.0.0");
	 $(".slider img").each(
        function() 
        {
            $(this).attr("src",$(this).attr("path"))
        }
    );
});



$(function () {
    try {
        $('img.captify').captify({
            speedOver: 'fast',
            speedOut: 'normal',
            hideDelay: 5000,
            animation: 'always-on',
            prefix: '',
            opacity: '0.7',
            className: 'caption-bottom',
            position: 'bottom',
            spanWidth: '100%'
        });
    } catch (ed) { }
    $(".view_movie").click(function () {
        $(".slider > li").hide();
        $(".gallery_movie").show();
        $(".switcher > li > a").click(function () {

            $(".slider > li").show();
            $(".gallery_movie").hide();
        });
    });

    $(".INTERACTIVEMAP").click(
	        function () {
	            $("#interactive_map").show();
	            $("#interactive_map > .wrapper,.swfClose").click(
	                function () {
	                    $("#interactive_map").hide();
	                }
	            );
	        }
	    );
    // availability Date
    $(".view-brochure").click(function () {
        $("#brochure").show();
        $("#brochure > .wrapper,.brochureClose").click(
	                function () {
	                    $("#brochure").hide();
	                }
	            );
    });

    // tha availability click
    $(".AVAILABILITY").click(
	        function () {
	            $("#availability").fadeIn("slow");
	            $("#availability > .wrapper,.availabilityClose").click(
	                function () {
	                    $("#availability").hide();
	                }
	            );
	            getAvailability($availabliityDate);
	        }
	    );
    // now the availability buttons
    $(".updateButton").click(function () {
        $availabliityDate = new Date($("#availabilityYear").val(), $("#availabilityMonth").val() - 1, $("#availabilityDay").val());
        getAvailability($availabliityDate);
    });
    $(".previousButton > input").click(function () {
        $availabliityDate = new Date($("#availabilityYear").val(), $("#availabilityMonth").val() - 1, parseInt($("#availabilityDay").val()) - 7);
        getAvailability($availabliityDate);
    });
    $(".nextButton > input").click(function () {
        $availabliityDate = new Date($("#availabilityYear").val(), $("#availabilityMonth").val() - 1, parseInt($("#availabilityDay").val()) + 7);
        getAvailability($availabliityDate);
    });
    // now forms
    
        $("").validate();
    
        jQuery.validator.messages.required = "*";
        $("#contactForm").validate(
               {
                   errorPlacement: function (error, element) { },
                   invalidHandler: function (e, validator) {
                       var errors = validator.numberOfInvalids();
                       if (errors) {
                           var message = errors == 1
					            ? 'You missed 1 field. Please also ensure your email address is correct. '
					            : 'You missed ' + errors + ' fields. Please also ensure your email address is correct.';
                           alert(message);

                       } else {

                       }
                   }
               });
        $(".join-form").validate(
               {
                   errorPlacement: function (error, element) { },
                   invalidHandler: function (e, validator) {
                       var errors = validator.numberOfInvalids();
                       if (errors) {
                           var message = errors == 1
					            ? 'You missed 1 field. Please also ensure your email address is correct. '
					            : 'You missed ' + errors + ' fields. Please also ensure your email address is correct.';
                           alert(message);

                       } else {

                       }
                   }
               });

        $("#sendPage").validate(
               {
                   errorPlacement: function (error, element) { },
                   invalidHandler: function (e, validator) {
                       var errors = validator.numberOfInvalids();
                       if (errors) {
                           var message = errors == 1
					            ? 'You missed 1 field. Please also ensure your email address is correct. '
					            : 'You missed ' + errors + ' fields. Please also ensure your email address is correct.';
                           alert(message);

                       } else {

                       }
                   }
               });
    });
    
    var $availabliityDate = new Date();
    function getAvailability(newDate)
	{
	    var $theDate = new Date(newDate);
        $("#availabilityContent .loaded").hide();
        $("#availabilityContent .loading").show();
        
	    $.ajax({
	            url: "/asilia/Availability.aspx?startDate=" + $theDate.toMMDDYYYYString(),
	            cache: false,
	            
	            success: function(data) {
                    $("#availabilityContent .loading").hide();
                    $("#availabilityContent .loaded").show();
	                $("#availabilityContent > .loaded > .content").html(data);
	                // now set the date
	                $("#availabilityDay").val($("#availabilityDate .day").html());
	                $("#availabilityMonth").val($("#availabilityDate .month").html());
	                $("#availabilityYear").val($("#availabilityDate .year").html());
	                $availabliityDate = new Date($("#availabilityDate .year").html(),$("#availabilityDate .month").html()-1,$("#availabilityDate .day").html());    
	            }
	        });
	}
	Date.prototype.toMMDDYYYYString = function () {return isNaN (this) ? 'NaN' : [this.getMonth() > 8 ? this.getMonth() + 1 : '0' + (this.getMonth() + 1), this.getDate() > 9 ? this.getDate() : '0' + this.getDate(), this.getFullYear()].join('/')}
	
