var next_date = "";
var prev_date = "";
var first_click = 1;
var first_click_date = "";
var last_click_date = "";

$(document).ready(function(){
	
	loadCalendar(parent.room_id,"","");

	jQuery("#monthPlus").click(function(){
		loadCalendar(parent.room_id, next_date.substr(0,4), next_date.substr(5,2));
	});
	
	jQuery("#monthMinus").click(function(){
		if(prev_date != "")
			loadCalendar(parent.room_id, prev_date.substr(0,4), prev_date.substr(5,2));
	});	
});

function loadCalendar(room_id, year, month)
{
	jQuery.ajax({
        url:'calendar.php',
        data:"room_id="+room_id+"&year="+year+"&month="+month,
        dataType: 'xml',
        success:
        function(xml) {
        	
        prev_date = jQuery('prev_link', xml).text();
		next_date = jQuery('next_link', xml).text();
					
		jQuery("#first_cal").html(jQuery('calendar1', xml).text());
		jQuery("#second_cal").html(jQuery('calendar2', xml).text());
		
		addEvents();			
		jQuery(".free_day").mouseover(function(){
				mouseoverFunction(jQuery(this).attr("id"));
			});
   
   		updateCal();
        },
        beforeSend: function(){
        	jQuery("#calendar_content").animate({opacity:"0.0"}, 120);
        },
        complete: function(){
        	jQuery("#calendar_content").animate({opacity:"1.0"}, 600);
        }
    });
}

function addPrevLink(date)
{	
	loadCalendar(room_id, date.substr(0,4), date.substr(5,2));
}

function addEvents()
{
	jQuery(".free_day").click(function(){				
		if(first_click == 1)
		{
			clearCel();
			first_click_date = jQuery(this).attr("id");
			first_click = 0;
			jQuery(this).attr("class", "calselstart");
			jQuery(".free_day").mouseover(function(){
				mouseoverFunction(jQuery(this).attr("id"));
			});
						
		}
		else
		{			
			if(last_click_date == "")
			{
				last_click_date = jQuery(this).attr("id");				
				jQuery(".free_day, .calselstart, .calselend, .calover").unbind("mouseover");				
				checkBox(first_click_date, last_click_date);
				calculate(first_click_date, last_click_date, parent.room_id);
			}
			else
			{
				first_click = 1;
				first_click_date = "";
				last_click_date = "";
				jQuery(".free_day, .calselstart, .calselend, .calover").attr("class", "free_day");
				jQuery(this).click();
			}						
		}
		
	});
}

function mouseoverFunction(over_date)
{	
	if(first_click_date != "")
	{
		clearCel();	
	
	if(jQuery("#"+over_date).attr("class") != "reserve_day")
	{		
		if(first_click_date == over_date)
		{
			jQuery("#"+over_date).attr("class", "calselstart");		
		}
		else
		{
			
			over_date = new Date(over_date.substr(0,4), (((over_date.substr(5,2))*1)-1), over_date.substr(8,2));
			var first_date = new Date(first_click_date.substr(0,4), (((first_click_date.substr(5,2))*1)-1), first_click_date.substr(8,2));
			
			//over_date.setDate(over_date.getDate() + 5);
			if(over_date > first_date)
			{			
				jQuery("#"+first_click_date).attr("class", "calselstart");
				while(over_date > first_date)
				{
					first_date.setDate(first_date.getDate() + 1);
					
					over_month = first_date.getMonth(); 
					over_month = over_month+1;
					if(over_month < 10)
						over_month = "0"+over_month;
						
					over_day = first_date.getDate();				
					if(over_day < 10)
						over_day = "0"+over_day;
					
					over_year = first_date.getFullYear();							
					
					if(jQuery("#"+over_year+"-"+over_month+"-"+over_day).attr("class")  == "reserve_day")
					{
						return false;
					}
					else
					{
						jQuery("#"+over_year+"-"+over_month+"-"+over_day).attr("class", "calover");
					}	
				}
					jQuery("#"+over_year+"-"+over_month+"-"+over_day).attr("class", "calselend");
				
			}
			else
			{			
				jQuery("#"+first_click_date).attr("class", "calselend");
				while(over_date < first_date)
				{
					first_date.setDate(first_date.getDate() - 1);
					
					over_month = first_date.getMonth(); 
					over_month = over_month+1;
					if(over_month < 10)
						over_month = "0"+over_month;
						
					over_day = first_date.getDate();				
					if(over_day < 10)
						over_day = "0"+over_day;
					
					over_year = first_date.getFullYear();							
					
					if(jQuery("#"+over_year+"-"+over_month+"-"+over_day).attr("class")  == "reserve_day")
					{
						return false;
					}
					else
					{
						jQuery("#"+over_year+"-"+over_month+"-"+over_day).attr("class", "calover");
					}	
				}
					jQuery("#"+over_year+"-"+over_month+"-"+over_day).attr("class", "calselstart");
			}
		}
	}
	}
}

function checkBox(first_date, last_date)
{	
	first_date = new Date(first_date.substr(0,4), (((first_date.substr(5,2))*1)-1), first_date.substr(8,2));
	last_date = new Date(last_date.substr(0,4), (((last_date.substr(5,2))*1)-1), last_date.substr(8,2));
	
	if(last_date < first_date)
	{
		tmp = first_date;
		first_date = last_date;
		last_date = tmp;
	}	
	
	jQuery("#jsfrom_day option:selected").removeAttr('selected');
	jQuery("#jsfrom_day option").each(function(){
		if(jQuery(this).val() == first_date.getDate())
		{
			jQuery(this).attr("selected", "selected");
		}		
	});	
	
	jQuery("#jsfrom_month option:selected").removeAttr('selected');
	jQuery("#jsfrom_month option").each(function(){
		if(jQuery(this).val() == (first_date.getMonth()+1))
		{
			jQuery(this).attr("selected", "selected");
		}		
	});
	
	jQuery("#jsfrom_year option:selected").removeAttr('selected');
	jQuery("#jsfrom_year option").each(function(){
		if(jQuery(this).val() == first_date.getFullYear())
		{
			jQuery(this).attr("selected", "selected");
		}		
	});
	
	jQuery("#jsto_day option:selected").removeAttr('selected');
	jQuery("#jsto_day option").each(function(){
		if(jQuery(this).val() == last_date.getDate())
		{
			jQuery(this).attr("selected", "selected");
		}		
	});	
	
	jQuery("#jsto_month option:selected").removeAttr('selected');
	jQuery("#jsto_month option").each(function(){
		if(jQuery(this).val() == (last_date.getMonth()+1))
		{
			jQuery(this).attr("selected", "selected");
		}		
	});
	
	jQuery("#jsto_year option:selected").removeAttr('selected');
	jQuery("#jsto_year option").each(function(){
		if(jQuery(this).val() == last_date.getFullYear())
		{
			jQuery(this).attr("selected", "selected");
		}		
	});
	//alert(jQuery("#jsfrom_day option").val());
}

function updateCal()
{
	first_click = 1;
	first_click_date = "";
	last_click_date = "";
	
	from_day = jQuery("#jsfrom_day option:selected").val();	
	from_month = jQuery("#jsfrom_month option:selected").val();
	from_year = jQuery("#jsfrom_year option:selected").val();
	
	to_day = jQuery("#jsto_day option:selected").val();	
	to_month = jQuery("#jsto_month option:selected").val();
	to_year = jQuery("#jsto_year option:selected").val();
	
	clearCel();
		
	var first_date = new Date(from_year, (from_month-1), from_day);
	var last_date = new Date(to_year, (to_month-1), to_day);
	
		
	if(from_day < 10)
	{
		from_day = "0"+from_day;
	}
	
	if(from_month < 10)
	{
		from_month = "0"+from_month;
	}
	
	if(last_date > first_date && jQuery("#"+from_year+"-"+from_month+"-"+from_day).attr("class") != "reserve_day")
	{			
		jQuery("#"+from_year+"-"+from_month+"-"+from_day).attr("class", "calselstart");
			while(last_date > first_date)
			{
				first_date.setDate(first_date.getDate() + 1);
				
				over_month = first_date.getMonth(); 
				over_month = over_month+1;
				if(over_month < 10)
					over_month = "0"+over_month;
					
				over_day = first_date.getDate();				
				if(over_day < 10)
					over_day = "0"+over_day;
				
				over_year = first_date.getFullYear();							
				
				if(jQuery("#"+over_year+"-"+over_month+"-"+over_day).attr("class")  == "reserve_day")
				{
					return false;
				}
				else
				{
					jQuery("#"+over_year+"-"+over_month+"-"+over_day).attr("class", "calover");
				}	
			}
				jQuery("#"+over_year+"-"+over_month+"-"+over_day).attr("class", "calselend");
	}
	
	calculate(from_year+"-"+from_month+"-"+from_day, to_year+"-"+to_month+"-"+to_day, parent.room_id);
}

function clearCel()
{
	jQuery(".calover").attr("class", "free_day");
	jQuery(".calselend").attr("class", "free_day");
	jQuery(".calselstart").attr("class", "free_day");	
}

function calculate(date_from, date_to, room_id)
{
	jQuery.ajax({
        url:'calculate.php',
        data:"date_from="+date_from+"&date_to="+date_to+"&room_id="+room_id,
        dataType: 'text',
        success:
        function(txt) {        	
        	jQuery("#price").html(txt);
        },
        beforeSend: function(){
        	jQuery("#loading").show();
        },
        complete: function(){
        	jQuery("#loading").hide();
        }
    });
}
