function printEvents() {
	$('body').toggleClass('print-events');
	window.print();
	$('body').toggleClass('print-events');
}
function printCalendar() {
	$('body').toggleClass('shedule');
	window.print();
	$('body').toggleClass('shedule');
}

/*

$(
		function(){
			$('.magicFX').innerfade({
	            'onStartedProcessing': function(elements, settings, current, last) {showTitle(elements[current]);}
			});

		}
	);

*/

/* --imageMagic--
 * This inline script makes #media_div to be a % of the window height (0<HEIGHT_FACTOR<1).
 * If it is smaller then MIN_DIV_H px or if it is taller then #media_image px,
 * then #media_div's height is bound to that range. Function applied on
 * #media_image load and on window resize.
 */

// Settings:
var MIN_DIV_H = 508;
var PROP_FACTOR = 508/960;
var WIN_W  = $('.media').width(); 
var MAX_DIV_H = 900;
var desired_media_height = WIN_W * PROP_FACTOR;	

function flashMagic() {
	resizeMediaDiv();
	$(function() { resizeMediaDiv(); });
	$(window).resize(function() { resizeMediaDiv();});
	
function resizeMediaDiv() {
	PROP_FACTOR = 508/960;
	WIN_W  = $('.media').width(); 
	MIN_DIV_H = 508;
	MAX_DIV_H = 900;
	desired_media_height = WIN_W * PROP_FACTOR;					

	if (desired_media_height > MAX_DIV_H) {
		desired_media_height = MAX_DIV_H;
	}
	
	if (desired_media_height < MIN_DIV_H) {
		desired_media_height = MIN_DIV_H;
	}
	
	//alert (desired_media_height);
	
	$('div.media').css({height: desired_media_height}); 
	}
}

/* -- end of flashMagic - */
//flashMagic();

$(function() {
	
	  if(jQuery.browser.msie) {
		$('.main-menu li').hover( 
			function () { $(this).addClass('hover'); }, 
			function () { $(this).removeClass('hover'); }
		);
		//$("#book-now select").mouseleave(function(event) { event.stopPropagation(); });
	  }  


	Date.format = 'mm/dd/yyyy';
	Date.firstDayOfWeek = 0;	
	
	$('.date-pick').datePicker();
	
	                   // $('#checkin,#checkout').attr('readonly', 'readonly');
	
						$('#checkin').click(function() {
							$('.booknow-form').hide();
							$('.calendar-in').show();
							$('.book-title').html('Check-in Date');
							
						});
						
						$('#checkout').click(function() {
							$('.booknow-form').hide();
							$('.calendar-out').show();
							$('.book-title').html('Check-out Date');
						});
						
						$('.calendar-in, .calendar-out').datePicker(
						  {
						    createButton: false,
                            inline: true
						  }
						).bind('click', function() {
						    updateSelects(this, $(this).dpGetSelected()[0]);
							return false;
						}).bind('dateSelected', function(e, selectedDate, $td, state) {
						    updateSelects(this, selectedDate);
							$(this).hide();
							$('.booknow-form').show();
							$('.book-title').html('Online Reservations');
						});
						
						var today = new Date();
						$('#checkin').val(today.asString());
						$('#checkout').val(today.addDays(1).asString());

                        $('.calendar-in').dpDisplay().hide();
						$('.calendar-out').dpSetStartDate(today.addDays(-1).asString()).dpSetSelected(today.addDays(1).asString()).dpDisplay().hide();
						
						var updateSelects = function (e, selectedDate)
						{
                            if(!selectedDate) return true;
							var t = new Date(selectedDate);

							if ($(e).hasClass('calendar-in')) {
								$('#checkin').val(t.asString());
								var dt = new Date(t);
                                var edate = new Date($('#checkout').val());
                                
							    $('.calendar-out').dpClose().dpSetStartDate(dt.addDays(1).asString());
                                
								// set end date +2 days by default, if needed
								if(dt-edate>=0) {
                                    $('#checkout').val(t.addDays(1).asString());
                                    $('.calendar-out').dpSetSelected(t.asString()); 
                                }
                                $('.calendar-out').dpDisplay().hide();
                                
							} else if ($(e).hasClass('calendar-out')) {
								$('#checkout').val(t.asString());
							} 
						}

						

				$("#ajaxloading")
        		.ajaxStart(function(){
        			$(this).show();
        		})
        		.ajaxComplete(function(){
        			$(this).hide();
        		});
	            var showEventsPerMonth = function(mo,yr) {
			        if ( typeof(ajaxevents)=='undefined' ) return;
			        if($("#jqCalendarResults1").length == 0) return;
			        var txt = '';
				    var sevents = new Array();
				    var showitems = new Array();
					
				    $.ajax({
				      type: 'POST',
                      url: "calendar.php"+"ajax.php",
                      data: {m:mo, y:yr},
                      cache: true,
                      async: true,
                      success: function(events){
                        $("#jqCalendarResults1").html(events);
                      }
                    });
				    return;
				    
				    jQuery.each(ajaxevents, function(index,  item){
					d = index;
					// for (var d in ajaxevents) {
				        if(d.substring(0,2) == mo && d.substring(6,10) == yr) {
				            var xk = item;
				 			// var xk = ajaxevents[d];
				
				            // for(i=0;i<item.length ;i++) {
  							for(i=0;i< ajaxevents[d].length;i++) {
				                if(!sevents[xk[i]]) {
				                    sevents[xk[i]] = 1;
				                    showitems.push(ajaxitems[xk[i]]);
				                }
				            }
				        }
				    // }
					});
				
					var lnd = 0;
					jQuery.each(showitems, function(index,  item){	d = index;					
				    // for (var d in showitems) {
				        var itm = item; //
						// var itm =showitems[d];
						// if (itm != "undefined" && itm.title != "undefined") {
						if (lnd <= 3) {	
							txt+='<li><a title="'+itm.title+'" href="?id='+itm.id+'&width=544&height=320&TB_iframe=true" onclick="tb_show(this.title,this.href,null);return false;" class="thickbox"><span>'+itm.start_date+'</span>: '+itm.title+'</a></li>';
						}
						//}
						//if(lnd>3) break;
						lnd++; 
				    // }
					});
				
				    if($('#jqCalendarResults').length) {
				        if(txt.length == 0) {
				           $('#jqCalendarResults').html('');
						}
				        else {
				           $('#jqCalendarResults').html('<ul>'+txt+'</ul>');
				        }
				    }
				
				
			    }

			    $('#jqCalendar').datePicker(
					{
						inline:true,
						showYearNavigation:false,
						renderCallback:function($td, thisDate, month, year)
						{
						    if(typeof(ajaxevents)!='undefined') {
								var now = new Date();
								if(ajaxevents[thisDate.asString()]) {
								    if($td.hasClass('current-month')) {
			    				     $td.addClass('hasevents');
			    				     $td.attr('title', 'Show events on '+thisDate.asString());
								    }
			    				}
						    }
						}
					}
				)
				.bind(
				   'dpMonthChanged', 
				   function(e, month, year)
					{
					    var mo = (month+1);
					    if((mo+'').length == 1) mo='0'+mo;
					    var yr = year+'';

					    showEventsPerMonth(mo,yr);
					}
				)
				.bind(
					'dateSelected',
					function(e, selectedDate, $td)
					{
					    var selDate = selectedDate.asString();
						var selMonth = selectedDate.getMonth()+1;
						var selYear = selectedDate.getFullYear();
					    document.location.href="calendar.php"+'?m='+selMonth+'&y='+selYear+'#cal';
					}
			    ).dpClose().dpDisplay();
					
				var date = new Date();
				var month = date.getMonth()+1;
				month = (month > 9 ? month : "0"+month);
				year = date.getFullYear();
				// showEventsPerMonth(month, year);
			});