var show = 0;
var index = 0;



$('document').ready(function() {

    $('ul#navigation li').mouseenter(function() {
	if( $('#' + $(this).attr('id') + ' ul.navigationDropdown').css('display','none' ) ) {
	    $('#' + $(this).attr('id') + ' ul.navigationDropdown').show();
	}
    });
    
    $('ul#navigation li').mouseleave(function() {
	if( $('#' + $(this).attr('id') + ' ul.navigationDropdown').css('display','block' ) ) {
	    $('#' + $(this).attr('id') + ' ul.navigationDropdown').hide();
	}
    });

    /* Booking system */
    $('a[rel*=facebox]').facebox();
    
	$(document).bind('reveal.facebox', function() {
		$('#facebox .dp').each( function(intIndex){
			$(this).attr('id',jQuery(this).attr('id') + '-2');
		});
	});

	$(document).bind('reveal.facebox', function() {
	    $('#date-2').datepicker({
			minDate: 0,
			maxDate: '+3M',
			numberOfMonths: 3,
			showButtonPanel: true,
			dateFormat: 'dd-mm-yy',
			onSelect: function(dateText, inst) {
				$(".warning").hide();
				
				var todayFull = new Date()
				var month = todayFull.getMonth()+1;
				
				if(month<10) { month = "0" + month; }
				
				var today = todayFull.getDate() + '-' + month + '-' + todayFull.getFullYear();

				if(dateText==today) {
					$(".durationBlock").hide();
					$(".timeBlock").hide();
					$(".warningtoday").show();
				} else {
					$(".durationBlock").show();
					$(".timeBlock").show();
					
				}

				$.get("booking.php", { reqFun: "valDate", date: dateText},
					function(data){
						if(data=="INV_DATE") {
							// show invalid date copytext
							alert('Invalid date');
						}
						else {
							if(data=="VAL_DATE_W") {
								$(".warning90").show();
								$(".duration60").hide();
								$(".duration30").hide();
							}
							else if(data=="VAL_DATE") {
								$(".duration60").show();
								$(".duration30").show();
							}
						}
				});
			}
		});
	});

	$("input[name=duration]").live('click',function() {
		var dpDate = $('#date-2').datepicker('getDate');
		var day = dpDate.getDate();
		var month = dpDate.getMonth() + 1;
		var year = dpDate.getFullYear();
		var selectedDate =  year + '-' + month + '-' + day;
		var duration = $("input[name=duration]:checked").val();

		$(".warning").hide();
		if(duration==30) {
			$(".warning30").show();
		}
		if(duration==60) {
			$(".warning60").show();
		}

		if(duration==90) {
			$(".warning90").show();
		}
		$.getJSON("booking.php", { reqFun: "getAvailableTimes", date: selectedDate, duration: duration},
			function(data) {
				$('#time option').remove();
				var html = '';
				$.each(data,function(k,v) {
					html += '<option value="'+ v +'">' + v + '</option>';
				});
				$('select#time').html(html);
			}
		);
	});


	$('.admin #date').datepicker({
			minDate: 0,
			maxDate: '+3M',
			numberOfMonths: 3,
			showButtonPanel: true,
			dateFormat: 'dd-mm-yy',
			onSelect: function(dateText, inst) {
				$.getJSON("booking.php", { reqFun: "getDayEvents", date: dateText},
					function(data){
						$(".warnings").hide();
						$(".admin .adminResults").html("");
						
						html = "";
						var selectedDate = $('#date').datepicker('getDate');
						var selectedMonth = selectedDate.getMonth() +1;
						html += "<h3>Appointments for " + selectedDate.getDate() + "-" + selectedMonth + "-" + selectedDate.getFullYear() + "</h3>";

						if(data)  {
							$.each(data, function(k,v) {
								html += "<strong>Appointment time:</strong> " + v.time.substring(0, 5) + "<br />";

								switch(v.duration) {
									case 1:
										duration = 30;
										break;
									case 2:
										duration = 60;
										break;
									case 3:
										duration = 90;
										break;
								}
								html += "<strong>Duration:</strong> " + duration + " minutes<br />";
								html += "<strong>Name: </strong>" + v.first_name + " " + v.last_name + "<br />";
								html += "<strong>E-Mail: </strong>" + v.email + "<br />";
								html += "<strong>Telephone number: </strong>" + v.telephone + "<br />";
								html += "<strong>Address: </strong>" + v.address + ", " + v.postal_code + " " + v.city +"<br />";
								
								if(v.message) {
									html += "<strong>Message: </strong>" + v.text + "<br />";
								}
								
								html += "<br />";
								html += "<hr />";
								
							})
							$(".admin .adminResults").html(html);
						}
						else {
							html += "<div>There are no appointments for the selected date.</div><br /><br /><br />";
							$(".admin .adminResults").html(html);
;
						}
					}
				);
			}
		});


	$("#firstName, #lastName, #email, #phone, #address, #postalcode, #city, #text").live('click',function() {
		if(!$(this).hasClass('zero')) {
			$(this).val('');
			$(this).addClass('zero');
		}
	});

   /* $('ul#navigation li').mouseenter(function() {
	if($(this).attr('id')=='home') {
	    return false;
	} else {
	    $('ul.navigationDropdown').eq($('ul#navigation li').index(this)-1).slideDown();
	    index = $('ul#navigation li').index(this)-1;
	    show = 1;
	}
	console.log(show);
	return false;
    });

    $('ul.navigationDropdown').mouseenter(function() {
	show = 2;
	console.log(show);
	return false;
    });

    $('ul.navigationDropdown').mouseleave(function() {
	show = 0;
    });*/

    /*$('ul#navigation li').mouseleave(function() {
	if($(this).attr('id')=='home') {
	    return false;
	} else {
	    
	    
	    $('ul.navigationDropdown').eq($('ul#navigation li').index(this)-1).slideUp();
	   return false; 
	}   
    });*/
});