function changePersons() {
	$('#adults_container ul li').click(function() {
		var nPersons = $(this).attr('id').replace('adults_input_','');

		$.ajax({
			type: 'POST',
			url: sDomain+'requests/changepersons.req.php',
			data: 'nPersons='+nPersons,
			success: function(result){
				aResult = result.split('|');
				if(aResult[0] == 1) {
					$('#ajax-linen').html(aResult[1]);
					$('#totalprice').html(aResult[2]);	
				}
			}
		});
	});
}

function sliderRecently() {
	var bReady = true;
	var nItems = $('#container_recently .item').size();
	var nLeft = 0;
	var nWidth = 324;
	var nActive = 1;
	
	if(nItems > 1) {
		$('#next_recently').show();
	}
	
	$('#next_recently').click(function(){
		if(bReady) {
			bReady = false;
			
			nActive = nActive+1;
			if(nActive == nItems) {
				$('#next_recently').hide();
			}
			
			nLeft = nLeft - nWidth;
			
			$('#container_recently').animate({
				left: nLeft
			}, 750, function(){
				$('#prev_recently').show();
				bReady = true;										  
			});
		}
	});
	
	$('#prev_recently').click(function(){
		if(bReady) {
			bReady = false;
			
			nActive = nActive-1;
			if(nActive == 1) {
				$('#prev_recently').hide();
			}
			
			nLeft = nLeft + nWidth;
			
			$('#container_recently').animate({
				left: nLeft
			}, 750, function(){
				$('#next_recently').show();
				bReady = true;										  
			});
		}
	});
}

function initSelectboxes() {
	$('div.select select').selectbox();
	var zIndex = 9999;
	$('div.form div.entry').each(function(){
		$(this).css('z-index',zIndex);
		zIndex--;
	});
}

function customSelect(){
	$("select").each(function(){
		$(this).wrap("<div class='selector'></div>");
		// Store the value of title in the variable spanText
		var spanText = $(this).attr('title');
		// Use the variable span text as the default for the select field
		var span = "<span class='select'>" + spanText + "</span>";
		$(this).before(span)
		.fadeTo(1, 0)
		.focus(function(){
			$(this).parents(".selector").addClass("focus");
		})
		.change(function(){
			$(this).siblings("span").text($(this).children(":selected").text());
		}).click ( function (){
			$(this).siblings("span").text($(this).children(":selected").text());		
		}) 
		.blur(function(){
			$(this).parents(".selector").removeClass("focus");
		});	
	});
	$('select').trigger("change");
}

function radioButtons() {
	$('.radio').click(function() {
		$('.radio').removeClass('checked');
		$('.radio').find('input').attr('checked','');	
		$(this).addClass('checked');	
		$(this).find('input').attr('checked','checked');	
	});
}

function checkBoxes() {
	$('.checkbox').toggle(
		function() {
			$(this).prev().attr('checked','checked');
			$(this).addClass('checked');
			if($(this).parents('#extraserviceparcel')) {
				var nServiceID = $(this).prev('input').val();
				extraServices(nServiceID,'true');
			}
		},
		function(){
			$(this).prev().attr('checked','');
			$(this).removeClass('checked');
			if($(this).parents('#extraserviceparcel')) {
				var nServiceID = $(this).prev('input').val();
				extraServices(nServiceID,'false');
			}
		}	
	);
}

function selectService(p_nTotalDays,p_nPersons) {
	$('.column:not(.disabled)').click(function() {
		$('.column.active').removeClass('active');
		$(this).addClass('active');
		$('.radio_service').removeClass('checked');				
		$(this).find('.radio_service').addClass('checked');
		$('.radio_service').find('input').attr('checked','');	
		$(this).find('input').attr('checked','checked');
		
		var sService = $(this).attr('class').replace('column ','').replace(' active','');

		$.ajax({
			type: 'POST',
			url: sDomain+'requests/updatestep1.req.php',
			data: 'nTotalDays='+p_nTotalDays+'&nPersons='+p_nPersons+'&sService='+sService,
			success: function(result){
				aPrices = result.split('|');
				$('.total_euro').html('&euro; '+aPrices[0]);
				$('#price_cleaning').html(aPrices[1]);
				$('#price_linen').html(aPrices[2]);
				
//				if(sService == 'basic') {
//					sPrice = 12;
//				} else if(sService == 'plus') {
//					sPrice = 18;
//				}
				
				if(sService == 'comfort') {
					sPrice = 4 * p_nTotalDays;
				}
				$('#title_linen').html(aPrices[3]);
			}
		});
	});
	$('.column:not(.disabled)').hover(function() {
		$(this).children('.top, .bottom').css('background','#e3e1e1');							
	}, function() {
		$(this).children('.top, .bottom').css('background','');							
	});
}

function extraServices(p_nServiceID,p_bAddService) {
	$.ajax({
		type: 'POST',
		url: sDomain+'requests/updatestep1.req.php',
		data: 'nServiceID='+p_nServiceID+'&bAddService='+p_bAddService,
		success: function(result){
			aPrices = result.split('|');
			$('.total_euro').html('&euro; '+aPrices[0]);
			$('#extra_services').html('&euro; '+aPrices[1]);
		}
	});
}

function toggleIdeal(){
	$('.radio').click(function(){
		if($(this).parents('.row').hasClass('ideal')) {
			$('#ideal').show();
			$('#country').hide();
		} else {
			$('#country').show();
			$('#ideal').hide();
		}
	});
}

function tabs(p_sLatitude,p_sLongitude) {
	$('.tab').click(function(){
		if(!$(this).hasClass('active')) {
			var nIndex = 50;
			$('.tab').each(function(){
				$(this).removeClass('active').css('z-index',nIndex);
				nIndex--;
			});
			$(this).addClass('active').css('z-index','100');
		}
		
		var sTab = $(this).attr('id');
		$('#tab_'+sTab+' a').trigger('click');
		$('.tab_content').hide();
		$('#'+sTab+'_content').show();
		
		$('#tab_content_bottom').show();
		$('#overlay_tab').removeClass('red').removeClass('location');	
		if(sTab == 'tab_location') {
			$('#tab_content_bottom').hide();
			$('#overlay_tab').addClass('location');
			initialize(p_sLatitude,p_sLongitude);
		} else if(sTab == 'tab_book-now' || sTab == 'tab_hibernate') {
			$('#overlay_tab').addClass('red');	
		}

		$('#multimonth .dp-applied').last().addClass('no_mg').find('h2').addClass('ta_left');
	});
	
	var sHash = window.location.hash.replace('#','');
	if(sHash != ''){
		$('#tab_'+sHash).trigger('click');
	}
	
	$('.tab1_link').click(function(){
		var sTarget = $(this).attr('id').replace('tab1_','');
		$('#tab_'+sTarget).trigger('click');	   
	});
}

function initialize(p_sLatitude,p_sLongitude) {
	var myLatlng = new google.maps.LatLng(p_sLatitude,p_sLongitude);
	var myOptions = {
	  zoom: 15,
	  center: myLatlng,
	  mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	var map = new google.maps.Map(document.getElementById("google_maps"), myOptions);
	
	var image = new google.maps.MarkerImage('images/template/marker.png',
		// This marker is 20 pixels wide by 32 pixels tall.
		new google.maps.Size(43,41),
		// The origin for this image is 0,0.
		new google.maps.Point(0,0),
		// The anchor for this image is the base of the flagpole at 0,32.
		new google.maps.Point(20,20)
	);
	var marker = new google.maps.Marker({
		position: myLatlng, 
		map: map,
		icon: image
	});   
}

function maxPersons() {
	var nAdults = 0;
	var nBabys = 0;
	$('.select_persons ul li').click(function() {
		var bError = false;
		var nMax = parseInt($(this).parents('.selectbox-wrapper').next('select').attr('class').replace('mp',''));
		var nAddPersons = parseInt($(this).html());

		if($(this).parents('.selectbox-wrapper').attr('id') == 'adults_container') {
			if((nBabys + nAddPersons) <= (nMax + 1)) {
				nAdults = nAddPersons;
			} else {
				bError = true;	
			}
		} else if($(this).parents('.selectbox-wrapper').attr('id') == 'kids_container') {
			if((nAdults + nAddPersons) <= (nMax + 1)) {
				nBabys = nAddPersons;
			} else {
				bError = true;	
			}
		}
		
		if(bError) {
			$('#costs .left .exceeded').show();
		} else {
			$('#costs .left .exceeded').hide();
		}
	});
}

function validateForm(p_sForm) {
	var rules = {};
	rules[aFields[2]] = {
		required: true,
		nonumber: true
	};
	rules[aFields[5]] = {
		required: true,
		email: true
	};
	rules[aFields[6]] = {
		required: true
	};
	
	var messages = {};
	messages[aFields[2]] = {
		required: 'Fill in your name please.',
		nonumber: 'Fill in a valid name please'
	};
	messages[aFields[5]] = {
		required: 'Fill in your e-mail please.',
		email: 'Fill in a valid e-mail please.'
	};
	messages[aFields[6]] = {
		required: 'Fill in your message please.'
	};
	$('#'+p_sForm).validate({
		rules: rules,
		messages: messages,
		errorPlacement: function(error, element) {
			var obj = element.parent().next('div.icon');
			error.insertAfter(obj);
			obj.attr('class','icon invalid');
		},
		success: function(label) {
			label.prev('div.icon').attr('class','icon valid');
			label.remove();
		},
		submitHandler: function(form) {
			form.trigger('submit');
		}
	});	
}

function tellaFriend() {
	$('.tell_a_friend').click(function() {
		if($(this).hasClass('open')) {
			$('#tell_a_friend').slideUp(600);	
			$(this).removeClass('open');
		} else {
			$('#tell_a_friend').slideDown(600);	
			$(this).addClass('open');
		}
	});
}

function validateTellaFriend() {
	var rules = {};
	rules[aFields[1]] = {
		required: true
	};
	rules[aFields[2]] = {
		required: true,
		email: true
	};
	rules[aFields[3]] = {
		required: true
	};
	rules[aFields[4]] = {
		required: true,
		email: true
	};
	
	var messages = {};
	messages[aFields[1]] = {
		required: 'Fill in your name please.'
	};
	messages[aFields[2]] = {
		required: 'Fill in your e-mail please.',
		email: 'Fill in a valid e-mail please.'
	};
	messages[aFields[3]] = {
		required: 'Fill in name of receiver please.'
	};
	messages[aFields[4]] = {
		required: 'Fill in e-mail of receiver please.',
		email: 'Fill in a valid e-mail please.'
	};

	$('#tell-a-friendform').validate({
		rules: rules,
		messages: messages,
		errorPlacement: function(error, element) {
			var obj = element.parent().next('div.icon');
			error.insertAfter(obj);
			obj.attr('class','icon invalid');
		},
		success: function(label) {
			label.prev('div.icon').attr('class','icon valid');
			label.remove();
		},
		submitHandler: function(form) {
			form.trigger('submit');
		}
	});	
}

function validateStep2(p_sForm) {
	$('#'+p_sForm).validate({
		rules: {
			sName: {
				required: true,
				minlength: 2
			},
			sAddress: {
				required: true
			},
			sZipcode: {
				required: true,
				minlength: 3,
				maxlength: 8
			},
			sCity: {
				required: true,
				minlength: 2
			},
			sCountry: {
				required: true	
			},
			sPhone: {
				required: true,
				minlength: 6,
				maxlength: 15
			},
			sEmail: {
				required: true,
				email: true
			},
			sDateOfBirth: {
				required: true,
				minlength: 6
			}
		},
		messages: {
			sName: {
				required: 'Fill in your name please.',
				minlength: 'Fill in a valid name please'
			},
			sAddress: 'Fill in your address please.',
			sZipcode: {
				required: 'Fill in your zip code please.',
				maxlenght: 'Fill in a valid zip code please.'
			},
			sCity: {
				required: 'Fill in your city please.',
				minlenght: 'Fill in a valid city please.'
			},
			sCountry: {
				required: 'Fill in your country please.'	
			},
			sPhone: {
				required: 'Fill in your phone number please.',
				equalTo: 'Fill in a valid phone number please.'
			},
			sEmail: {
				required: 'Fill in your e-mailaddress please.',
				email: 'Fill in a valid e-mailaddress please.'
			},
			sDateOfBirth: {
				required: 'Fill in your date of birth please.',
				minlength: 'Fill in a valid date of birth (numeric) please.'
			}
		},
		errorPlacement: function(error, element) {
			var obj = element.parents('div.input').next('div.icon');
			error.insertAfter(obj);
			obj.attr('class','icon invalid');
		},
		success: function(label) {
			label.prev('div.icon').attr('class','icon valid');
			label.remove();
		},
		submitHandler: function(form) {
			form.trigger('submit');
		}
	});	
}

var bFirst = true;
function updateBooknow(p_sStartDate,p_sEndDate,p_bReset) {
	if(p_bReset) {
		bFirst = true;
	}
	if(bFirst) {
		bFirst = false;
		
		var sStartDate = '';
		var sEndDate = '';
		if(!p_bReset) {
			sStartDate = p_sStartDate.replace(/ /gi,'-');
			sEndDate = p_sEndDate.replace(/ /gi,'-');
		} 
		
		$('.input .arrivel').html(sStartDate);
		$('.input_arrival').val(sStartDate);
		$('.input .departure').html(sEndDate);
		$('.input_departure').val(sEndDate);
		
		$.ajax({
			type: 'POST',
			url: sDomain+'requests/updateprice.req.php',
			data: 'sStartDate='+p_sStartDate+'&sEndDate='+p_sEndDate+'&nID='+nID,
			success: function(result){
				aResult = result.split('|');
				$('#rentalprice').html('&euro; '+aResult[0]);
				if(result != '') {
					$('#totalprice').html('&euro; '+parseInt(aResult[1]));
				}
			}
		});
	} else {
		bFirst = true;	
	}
}

function validateHibernateForm() {
	var rules = {};
	rules[aFields[2]] = {
		required: true,
		minlength: 2,
		nonumber: true
	};
	rules[aFields[3]] = {
		required: true,
		minlength: 2,
		nonumber: true
	};
	rules[aFields[4]] = {
		required: true,
		minlength: 2,
		nonumber: true
	};
	rules[aFields[5]] = {
		required: true,
		minlength: 6,
		maxlength: 15
	};
	rules[aFields[6]] = {
		required: true,
		email: true
	};
	
	var messages = {};
	messages[aFields[2]] = {
		required: 'Fill in your name please.',
		minlength: 'Fill in your name please',
		nonumber: 'Fill in a valid name please'
	};
	messages[aFields[3]] = {
		required: 'Fill in your city please.',
		minlength: 'Fill in your city please.',
		nonumber: 'Fill in a valid city please'
	};
	messages[aFields[4]] = {
		required: 'Fill in your country please.',
		minlength: 'Fill in your country please.',
		nonumber: 'Fill in a valid country please'
	};
	messages[aFields[5]] = {
		required: 'Fill in your phone number please.',
		minlength: 'Fill in a valid phone number please.',
		maxlength: 'Fill in a valid phone number please'
	};
	messages[aFields[6]] = {
		required: 'Fill in your e-mail please.',
		email: 'Fill in a valid e-mail please.'
	};

	$('#hibernateform').validate({
		rules: rules,
		messages: messages,
		errorPlacement: function(error, element) {
			var obj = element.parent().next('div.icon');
			error.insertAfter(obj);
			obj.attr('class','icon invalid');
		},
		success: function(label) {
			label.prev('div.icon').attr('class','icon valid');
			label.remove();
		},
		submitHandler: function(form) {
			form.trigger('submit');
		}
	});	
}

function addToFavorites(p_sType) {
	$('.add_to_favorites').click(function() {
		$.ajax({
			type: 'POST',
			data: 'sType='+p_sType,
			url: sDomain+'requests/addtofavorites.req.php',
			success: function(result){
				$('html, body').animate({scrollTop:0}, 500);	
				$('#your_favorites').fadeIn(1000);
				$('.fav_msg').html(result).fadeIn(1000);
				setTimeout("$('.fav_msg').fadeOut(1000);", 3500);
			}
		});
	});
}

function clearInput() {
	$('.fade').focus(function() {
	   $(this).select();
	   $(this).prev().fadeTo(150, 0);
	   $(this).keydown(function() {
		   $(this).css('background-repeat', 'repeat');                  
	   });
	});
	$('.fade').blur(function() {
	   if ( $(this).val() == '' ) {
		   $(this).css('background-repeat', 'no-repeat');
		   $(this).prev().fadeTo(150, 1);
	   }
	});
	$('.fade').each(function(){
	   if ( $(this).val() != '') {
		   $(this).css('background-repeat', 'repeat');
	   };
	});
}
