(function($) {
    if ($.browser.mozilla) {
        $.fn.selectable = function(selectable) {
            if (selectable) {
					return this.each(function() {
						$(this).css({
						  'MozUserSelect' : ''
						});
					});	
				}
				else {
					return this.each(function() {
						$(this).css({
						'MozUserSelect' : 'none'
						});
					});
				}
        };
    } else if ($.browser.msie) {
        $.fn.selectable = function(selectable) {
            if (selectable) {
					return this.each(function() {
						$(this).unbind('selectstart.disableTextSelect');
					});	
				}
				else {
					return this.each(function() {
						$(this).bind('selectstart.disableTextSelect', function() {
							return false;
						});
					});
				}
        };
    } else {
		 $.fn.selectable = function(selectable) {
            if (selectable) {
					return this.each(function() {
						$(this).unbind('mousedown.disableTextSelect');
					});
				}
				else {
					return this.each(function() {
						$(this).bind('mousedown.disableTextSelect', function() {
							return false;
						});
					});
				}
        };
    }
})(jQuery);


$.nl2br = function(value) {
	return String(value).split("\n").join("<br />");
}


$.time = function(value) {
	var decimal = value - Math.floor(value);
	
	decimal *= 60;
	decimal = $.pad(decimal, 2, '0');

	return Math.floor(value) + 'h ' + decimal + 'm';	
}

$.extend({
	delegate: function(fn, thisObject) {
      var parameters = Array.prototype.slice.call(arguments).slice(2);

      return function() {
         return fn.apply(thisObject || this, parameters.concat(Array.prototype.slice.call(arguments)));
      };
	}

});


$.userAgent = String(navigator.userAgent).toLowerCase();
$.browser.iphone = ($.userAgent.indexOf('iphone') !== -1);
$.browser.ipad = ($.userAgent.indexOf('ipad') !== -1);
$.browser.mobile = ($.browser.iphone || $.browser.ipad);
$.browser.msie6 = ($.browser.msie && $.browser.version < 7);

$.widget('ui.radio', {

	_create: function() {
		var html = "<div class=\"checkbox\">" + this.element.val() + "</div>";		
		this.element.after(html);
		this.instance = this.element.next();
		this.instance.click($.delegate(this.select, this));
		this.element.change($.delegate(this.update, this));
		this.update();
		this.element.hide();
	},


	select: function() {
		this.element.attr('checked', true);
		this.update();
	},


	update: function() {
		var checked = this.element.is(':checked');
	
		this.instance.toggleClass('active', checked);
	
		if (!checked) return;
		$('input[name=' + this.element.attr('name') +'][type=radio]').not(this.element[0]).trigger('change');
	}
	
});


$().ready(function() {
	var selected_date = "";
	$.datepicker.setDefaults($.datepicker.regional[""]);
	$(".calendar").datepicker({ minDate: 0, maxDate: "+3M +10D", defaultDate: 0});
	$(".calendar").datepicker("option", $.datepicker.regional["en-gb"]);
	$(".calendar").datepicker("option", 'dateFormat', 'yy-mm-dd');
	$('.radio').radio();
	
	if($('.menu-list').length > 0) {
		$('.menu-list').find('td').each(function(index) {
			$(this).addClass('column'+(index*1+1));
		});
	}
	
	if($('#book-a-table .booking').length>0) {
		$("#book-a-table div#makeMeScrollable").smoothDivScroll({ });
	//	menu-list
		
		$('#book-a-table .booking .form-1 .buttons .button').click(function (event) {
			event.preventDefault();
			selected_date = $.datepicker.formatDate( "yy/mm/dd", $(".calendar").datepicker("getDate"));
			$("#number_of_people").text($('.party-size').val());
			$("#party_type").text($('.party-type option:selected').text());
			$("#date_choosen").text($.datepicker.formatDate( "DD, d MM yy", $(".calendar").datepicker("getDate")));
	
			
			$(".radio-buttons").load("get.php", { 
				'type' : $('.party-type option:selected').val(), 
				'pax' : $('.party-size').val(), 
				'date' :  $.datepicker.formatDate( "yy-m-d", $(".calendar").datepicker("getDate"))   
			}, function(){
				$('.radio').radio();
			});
			
			$(this).closest('.item').animate({opacity: 0}, 300);
			$(this).closest('.item').next('.item').animate({opacity: 1}, 300);
			$(".items").animate({opacity: 1, right: $('.booking .item').width(), top: 0 }, 500);
		});	
		
		$('#book-a-table .booking .form-2 .buttons .button').click(function (event) {
			event.preventDefault();
			var time = $("#book-a-table .booking .form-2 input[type='radio']:checked").val();
			if(time=="" || time==null) {
				$('#book-a-table .booking .form-2 .information').show();
				return false;
			} else {
				$('#book-a-table .booking .form-2 .information').hide();
			}		
			
			$(this).closest('.item').animate({opacity: 0}, 300);
			$(this).closest('.item').next('.item').animate({opacity: 1}, 300);
			$(".items").animate({opacity: 1, right: 2*$('.booking .item').width(), top: 0 }, 500);
		});	
		
		$('#book-a-table .booking .form-3 .buttons .button').click(function (event) {
			event.preventDefault();
			selected_date = $.datepicker.formatDate( "yy-mm-dd", $(".calendar").datepicker("getDate"));
			var number_of_people = $('.party-size').val();
			var party_type = $('.party-type').val();
			var time = $("#book-a-table .booking .form-2 input[type='radio']:checked").val();
			var name = $("#book-a-table .booking .form-3 input[name='name']");
			var surname = $("#book-a-table .booking .form-3 input[name='surname']");
			var telephone = $("#book-a-table .booking .form-3 input[name='telephone']");
			var email = $("#book-a-table .booking .form-3 input[name='email']");
			var request = $("#book-a-table .booking .form-3 textarea[name='request']");
																		
			if(name.val()=="" || surname.val()=="" || telephone.val()=="") {
	
				if(name.val()=="") {
					name.addClass('error');
				} else {
					name.removeClass('error');
				}
				if(surname.val()=="") {
					surname.addClass('error');
				} else {
					surname.removeClass('error');
				}
				if(telephone.val()=="") {
					telephone.addClass('error');
				} else {
					telephone.removeClass('error');
				}
				$('#book-a-table .booking .form-3 .information').show();
				return false;
			} else {
				$('#book-a-table .booking .form-3 .information').hide();
				
				$.ajax({
					type: "POST",
					url: "save.php",
					data: "name="+name.val()+"&surname="+surname.val()+"&telephone="+telephone.val()+"&email="+email.val()+"&request="+request.val()+"&selected_date="+selected_date+"&number_of_people="+number_of_people+"&party_type="+party_type+"&time="+time,
					success: function(msg){
						$('.feedback').html(msg);
					}
				});	
				
			}
			
			$(this).closest('.item').animate({opacity: 0}, 300);
			$(this).closest('.item').next('.item').animate({opacity: 1}, 300);
			$(".items").animate({opacity: 1, right: 3*$('.booking .item').width(), top: 0 }, 500);
		});	
		
		$('#book-a-table .booking .link-button').click(function (event)  {
			event.preventDefault();
			
			var index = $(".link-button").index(this);			
			$(this).closest('.item').animate({opacity: 0}, 300);
			$(this).closest('.item').prev('.item').animate({opacity: 1}, 300);
			$(".items").animate({opacity: 1, right: index*$('.booking .item').width(), top: 0 }, 500);
		});
	}


	$("#history div#makeMeScrollable").smoothDivScroll({ 
		autoScroll: "onstart", 
		autoScrollDirection: "backandforth",
		scrollStep: 15,
		scrollInterval: 10,
		autoScrollStep: 1,
		autoScrollInterval: 30,
		startAtElementId: "startAtMe",
		visibleHotSpots: "always",
		hotSpotsVisibleTime: 5,
		autoScrollLeftLimitReached: function() {
			//alert("Autoscroll left limit reached.");
		},
		movedToElementNumber: function(eventObj, data) {
			alert("Moved to element number " + data["elementNumber"] + ".");
		}
		
	});
	
	$("#history .timeline .item").hover(function (){
		$("#makeMeScrollable").smoothDivScroll("stopAutoScroll");
		$(this).find('.full-version').show();
		$(this).find('.short-version').hide();
		
	});
	
	$("#history .timeline .item").mouseleave(function (){
		$("#makeMeScrollable").smoothDivScroll("startAutoScroll");
		$(this).find('.full-version').hide();
		$(this).find('.short-version').show();
		
	});
	
	
	
	$(".menu-list table img").hover(function() {
		$element = $('.overlay');
		$element.find('img').attr({'src':$(this).attr("src")});
		$current = $element.insertAfter($(this));
		$current.show();	
	} , function() {
		$(this).parent().parent().find('.overlay').remove();
		$element.find('img').attr({'src':$(this).attr("src")});
		$current = $element.insertAfter($(this));
		$current.hide();
	});	
	
	
	var count = $(".banner img").length;
	var animation_duration = 8000;
	var animation_fast = 500;
	var long = 11000;
	var current_panel = 1;
	var half =  -1*$(".banner img").width()/3.5;
	$(".banner img").animate({left: half}, {duration: 0 });
	var index = 0;			

	$.timer(0, function (timer) { 
		switch(current_panel){
			case 1:
				$(".banner img").eq(index).stop().animate({left: 0}, {duration: animation_duration});	
				current_panel = 2;
				timer.reset(long);
			break;
			
			case 2:
				$(".banner img").eq(index).stop().animate({left: half}, {duration: animation_duration});	
				current_panel = 3;
				timer.reset(0);
			break;

			case 3:
				$(".banner img").eq(index).stop().animate({left: -620}, {duration: animation_duration});	
				current_panel = 4;
				timer.reset(long);
			break;

			case 4:
				$(".banner img").eq(index).stop().animate({left: half}, {duration: animation_duration});	
				current_panel = 1;
				timer.reset(long);
				if(count>1) {
					current_panel = 5;	
					index = index+1;
				}
				if(count==index)
				{
					index = 0;
				}
				
			break;

			case 5:
				$(".banner img").eq(index-1).stop().animate({height: 'hide', opacity: 1}, {duration: animation_fast});	
				$(".banner img").eq(index).stop().animate({height: 'show', opacity: 1}, {duration: animation_fast});	
				
				current_panel = 1;
				timer.reset(animation_fast*2);
			break;

		}
		//current_panel = Math.round(Math.random()*2+1);
		 
	});
	
	
	var max_number = Math.max($('#middle .main-menu').height(), $('#middle .page').height());
	if(max_number>$('#middle .page').height())
		$('#middle .page').height(max_number)
			
			
	
});

