/*
@author LB

Implements all the FlipKey namespace functions that can be expected globally using jQuery
*/

if (typeof FlipKey != "object") FlipKey = new Object();
FlipKey.LIGHTBOX = {};

//Takes a date and returns an array of [0] equal to true/false indicating whether or not this date is selectable, 
//[1] equal to a CSS class name(s) or '' for the default presentation and [2] an optional popup tooltip for this date.
FlipKey.disableDates = function(date)
{
    var year = date.getFullYear();
    var month = date.getMonth();
    var day = date.getDate();
    
    //Allow today to be booked, only disable if given dates numerical day is less than todays numercal day
	if (date < new Date() && !(date.getDate() == new Date().getDate() && date.getMonth() == new Date().getMonth() && date.getFullYear() == new Date().getFullYear()))
	{
    	return [false, '', 'Dates in past cannot be booked'];
	}
	
	//Do not allow user to select a ddeparture date that is before arrival date
	if (mode == "departure" && FlipKey.arrival_date)
	{
    	if (date <= FlipKey.arrival_date) return [false, '']; 		   
	}
	
	if (!FlipKey.booked_dates) return [true, ''];

	if (FlipKey.booked_dates[year] == "none" || !FlipKey.booked_dates[year]) return [true, ''];

	if (FlipKey.booked_dates[year][month])
	{
	    if (mode == "departure") {
			if (FlipKey.booked_dates[year][month][day] == 2) {
				return [false, '', 'Dates is not available'];
			}
			else return [true, ''];
		}
		else if (mode == "arrival") {
			if (FlipKey.booked_dates[year][month][day] == 1 || FlipKey.booked_dates[year][month][day] == 2) {
				return [false, '', 'Dates is not available'];
			}
			else return [true, ''];
		}
		else return [true, ''];
	}

	else return [true, ''];
}

FlipKey.beforeShow = function(input)
{
    if (input.id == "arrival_date") 
    {
        mode = "arrival";
        FlipKey.active_datepicker = "arrival";
    }
    else if (input.id == "departure_date") 
    {
        mode = "departure";
        FlipKey.active_datepicker = "departure";
    }
}

FlipKey.arrival_config =
{
    dateFormat: 'mm-dd-yy', 
    duration: 0, 
    beforeShow: FlipKey.beforeShow,
    beforeShowDay: FlipKey.disableDates,
    onSelect: selectArrival,
	constrainInput: true
};

FlipKey.departure_config =
{
    dateFormat: 'mm-dd-yy', 
    duration: 0, 
    beforeShow: FlipKey.beforeShow,
    beforeShowDay: FlipKey.disableDates,
    onSelect: selectDeparture
};

Date._MD=new Array(31,28,31,30,31,30,31,31,30,31,30,31)
Date.prototype.getMonthDays=function(month){var year=this.getFullYear();if(typeof month=="undefined"){month=this.getMonth();}if(((0==(year%4))&&((0!=(year%100))||(0==(year%400))))&&month==1){return 29;}else{return Date._MD[month];}};

//Loads the hash of dates for a property that will be used in the disableDate hangler below.
FlipKey.loadAvailability = function(property_id)
{
    jQuery.get('/properties/calendar_ajax/' + property_id +'/' + new Date().getFullYear(), 
        function(data)
        {
            FlipKey.booked_dates = eval(data).dates;     
        });
}

   
FlipKey.showLightboxAjax = function(url, close_button, loading, callback)
{
    FlipKey.LIGHTBOX.close_button = close_button;
    $.post(url, function(data)
    {
        $.modal(data, 
        {
            close: FlipKey.LIGHTBOX.close_button,
            closeHTML: '<a id="simplemodal-close" class="small_button small_red"><span>X</span></a>'
        });
        
        if (typeof callback == "function") callback();
    });
    

}

FlipKey.showLightboxText = function(text, close_button, loading)
{
    $.modal(text, 
    {
        close: close_button,
        closeHTML: '<a id="simplemodal-close" class="small_button small_red"><span>X</span></a>'
    });  
}


FlipKey.showLightboxFromDiv = function(el, close_button, loading)
{
    if (!close_button) close_button = false;
    

        if (typeof el == "string") el = jQuery("#" + el).modal({
            close: close_button,
            persist: true,
            closeHTML: '<a id="simplemodal-close" class="small_button small_red"><span>X</span></a>'
        });  
 
}

FlipKey.closeLightbox = function()
{
    $.modal.close();
}

function selectArrival(date)
{
    FlipKey.arrival_date = $.datepicker.parseDate('mm-dd-yy', date);
    $("#arrival_date").blur();    

    var month = ((FlipKey.arrival_date.getMonth()+1) < 10) ? '0' + (FlipKey.arrival_date.getMonth()+1) : FlipKey.arrival_date.getMonth()+1;
	
	var day = FlipKey.arrival_date.getDate()+1;
	var year = FlipKey.arrival_date.getFullYear();
	if (day > FlipKey.arrival_date.getMonthDays(FlipKey.arrival_date.getMonth()))
	{
		day = 1;
		month ++;
		if(month > 12)
		{
			month = 1;
			year ++;
		}
			
	}

	FlipKey.active_datepicker = null;	
	$('#departure_date')[0].value = month + '-' + day + '-' + year;    
    
    if ($('#flexible_dates')) $('#flexible_dates').value = "on";

    return true;
}

function selectDeparture(date)
{
    FlipKey.departure_date = $.datepicker.parseDate('mm-dd-yy', date);
    FlipKey.active_datepicker = null;
    
    return true;
}

FlipKey.createDatePicker = function()
{
    $().ready(function()
    {
        //Keps inputs from looking editable
        $("#arrival_date")[0].onfocus = function() { $("#arrival_date")[0].blur() };
        $("#departure_date")[0].onfocus = function() { $("#departure_date")[0].blur() };
        
        FlipKey.arrival_datepicker = $("#arrival_date").datepicker(FlipKey.arrival_config);
        FlipKey.departure_datepicker = $("#departure_date").datepicker(FlipKey.departure_config);
    });

}

FlipKey.createTooltips = function()
{
  $().ready(function()
  {
	$('.tooltiptext').tooltip();
  });
}

FlipKey.loadGoogleMap = function()
{
    $().ready(function()
    {
        if (document.getElementById('smallGoogleMap')) document.getElementById('smallGoogleMap').src = FlipKey.google_map_url;
    });
}

FlipKey.createPropertyPhotoPopups = function()
{
    $().ready(function()
    {
        $('.property_photo').each(function(i, el)
        {
            var src = jQuery(el).attr('large_photo_src');
            if (src)
            {
                jQuery(el).tooltip({ 
                    delay: 0, 
                    showURL: false, 
                    bodyHandler: function() { 
                        return $("<img/>").attr("src", src); 
                    }
                }); 
                
            }
                
        });
        
    });
}

