// JavaScript Document (jQuery)

var brand = 'md';

/***
*
* Functions
*
*/

function addZero(num) {
	return ((num >= 0)&&(num < 10)) ? '0'+num : num+'';
} 

function formatDate(year,month,day,divider) {
	if (divider == null) divider = '-';
	switch(divider) {
		case '/': return addZero(day) + divider + addZero(month) + divider + year;
		default: return year + divider + addZero(month) + divider + addZero(day);
	}
}

function updateList(cityName) {
	if (cityName == null) cityName = '';
	var langCode = $('input#language-code').val();
	var country = $('select#country').val();
	
	var url = langCode + '/' + brand;
	url = (country != 0) ? url + '/' + country + '/' : url + '/';
	
	var dataString = 'fx=get_content';
	
    $.ajax({
		type: 'POST',
		url: url + cityName,
		data: dataString,
		success: function(content) {
		  $("#results").html(content).show();
		}
	});
	return false;
}

/**
 * function setDiscountedRate($offer_radio)
 * 		-sets the innerHTML of <span class="rates-from"></span> when clicking on a radio button within the same ".property"
 *		-$offer_radio is the element <input type="radio" name="offer-PROPID" value="1" checked="checked" class="offer-radio left" />
 *		-a hidden input field with class ".input-rates-from" must be a child of ".hotel-line-one"
 *		-".rates-from" must be a child of ".hotel-line-one" as well
 *		-note that discount is calculate by formula and that we could also grab it from the global promotion array if formula didn't hold
 *		-for use on search results page
*/
function setDiscountedRate($offer_radio) {
	var $hotel_line_one = $offer_radio.parent('.hotel-booking').prev('.hotel-info').children('.hotel-details').children('.hotel-line-one');
	var rate = $hotel_line_one.children('input.input-rates-from:hidden').val();			
	var offer_number = $offer_radio.val();
	var discount = (parseInt(offer_number) + 1) / 10;
	var rate_discounted = Math.ceil(rate - (discount * rate));
	$hotel_line_one.children('.rates-line').children('.rates-from').html(rate_discounted);
}

/**
 * function setMinimumLengthOfStay($offer_radio)
 * 		-sets the innerHTML of <span class="minimum-stay-required"></span> when clicking on a radio button within the same ".property"
 *		-$offer_radio is the element <input type="radio" name="offer-PROPID" value="1" checked="checked" class="offer-radio left" />
 *		-a hidden input field with class ".property-type" must be a sibling of $offer_radio
 *		-a select field with class ".select-number-nights" must be within a div with class ".number-nights" that precedes $offer_radio
 *		-note that promotion is a globally defined array
 *		-for use on search results page
*/
function setMinimumLengthOfStay($offer_radio) {
	var $select_number_nights = $offer_radio.nextAll('.number-nights').children('.select-number-nights');
	var property_type = $offer_radio.siblings('input.property-type:hidden').val();
	var minimum_length_stay = promotion[property_type-1][$offer_radio.val()-1]['mlos'];
	$offer_radio.nextAll('.restrictions').children('.minimum-stay-required').html(minimum_length_stay);
	var selected_value = $select_number_nights.val();
	$select_number_nights.children().remove();
	for (var i=minimum_length_stay;i<31;i++) {
		var selected = (i==selected_value) ? ' selected="selected"' : '';
		$select_number_nights.append('<option value="'+i+'"'+selected+'>'+i+'</option>');
	}
}

/***
*
* Page Events
*
*/

$(function() {
		   
  $("select#country").change(function() {
	var textCity = $('input#text-city').val();
	var url = $('input#language-code').val() + '/';
	if ($(this).value != 0) {
		var dataString = 'fx=get_cities';
		$.ajax({
		  type: 'POST',
		  url: url + brand + '/' + this.value,
		  data: dataString,
		  success: function(content) {
			if ($('select#state option').size() <= 1) {
				$('select#city').removeAttr('disabled').html(content);
			} else {
				$('select#city').attr('disabled','disabled').html('<option value="0">'+textCity+'</option>');
			}
		  }
		});
	} else {
		$('select#city').attr('disabled','disabled').html('<option value="0">'+textCity+'</option>');
	}
    return false;
  });
 
  $('#booking-widget .button').click(function() {
	var city = $('select#city').val();
	if (city != 0) { 									  
		updateList(city);
	} else {
		updateList();
	}
	return false;
  });

  $('select#language-selection').change(function() {
	var url = $('base').attr('href') + this.value + '/' + brand;
	top.location = url;
  });
  
  if ($('#bonus-offers').children().length > 0) {
	$('#bonus-offers').cycle();
  }

});