var now = new Date();
now = new Date( now.getFullYear(), now.getMonth(), now.getDate() + 1, 0, 0, 0, 0 );


var showNumber = function(id, hostId) {
	$(id).click( function(e) {
		e.preventDefault();

		$x = $(this);
		$.getJSON( 'hostnumber.php', { 'host': hostId }, function(d) {
			$x.replaceWith( '<b>' + d.telephone + '</b>' );
		}, "json" );
	} );
}

		
var jump = function(p, v) {
	var url = window.location.href;
	var l = '&language=' + v;

	if ( url.indexOf('language=') >= 0 ) {
		url = url.replace( /&language=[0-9]+/, l );
	} else {
		if ( url.indexOf('?') < 0 ) {;
			url += '?';
		}
		url += '&language=' + v
	}
	window.location = url;
}


var prependZero = function(value) {
	var out = value;

	if ( value < 10 ) {
		out = '0' + value;
	}

	return out;
}


var getClass = function( arr, value, def, tooltag, responseTime ) {
	var out = new Array( true, def, tooltag );

	var n = '' + now.getFullYear() + prependZero(now.getMonth() + 1) + prependZero(now.getDate());

	var limit = 0;

	if ( responseTime >= 24 ) {
		limit = Math.ceil( responseTime / 24 );
	}

	var xnow = new Date( now.getTime() );
	xnow.setDate( now.getDate() + limit );

	if ( now <= value ) {
		var d = '' + value.getFullYear() + prependZero(value.getMonth() + 1) + prependZero(value.getDate());

		if ( arr[d] ) {
			if ( arr[d] == 'B' ) {
				out[0] = false;
				out[1] += ' z';
				out[2] = 'No rooms available';
			} else if ( arr[d] == 'P' ) {
				out[1] += ' a';
				out[2] = 'Some rooms available';
				if ( xnow > value ) {
					out[2] += ', but host may not respond in time';
				}
			}
		} else {
			if ( xnow > value ) {
				out[0] = true;
				out[1] += ' b';
				out[2] = 'Rooms available, but host may not respond in time';
			}
		}
	} else {
		out[0] = false;
		out[1] += ' b';
		out[2] = 'No rooms available';
	}

	//out[2] += ' #' + limit + '#' + responseTime + '#';


	return out;
}


var gc = new Array();

var makeCalendar = function(div, host, options) {
	var d = $(div);

	d.datepicker( {
		beforeShowDay: function(d) {
			//$('#x').append( '<b>#' + gc + '#</b><br>' );
			return getClass(gc , d, 'bom', 'All rooms available', options.responseTime );
		},

		firstDay: 1,

		onSelect: function(dt, inst) {
			$('#x').html( dt );
		},

		navigationAsDateFormat: true, 
		//prevText: '<', 
		prevText: 'MM', 
		//currentText: 'MM y', 
		nextText: 'MM',
		//nextText: '>',
		//altField: '#x', 
		//altFormat: 'DD, d MM, yy'
		onChangeMonthYear: function(year, month, inst) {
			$.ajax( {
				url: 'ajaxcalendar.php?host=' + host + '&year=' + year + '&month=' + month,
				success: function(json) {
					gc = json;
				},
				dataType: 'json',
				async: false
			} );

		},

		onSelect: function(dateText, inst) {
			if ( options.bookingUrl ) {
				var url = options.bookingUrl.replace( '##TERM##', dateText );
				window.open( url );
			} else {
				bookUrl( dateText );
			}


		}

	} );

	if ( options.monthNames ) {
		d.datepicker( 'option', 'monthNames', options.monthNames );
		d.datepicker( 'option', 'monthNamesShort', options.monthNames );
	}

	//$.datepicker.setDefaults($.datepicker.regional['fr']);

	d.datepicker( 'option', 'changeMonth', true );
	d.datepicker( 'option', 'changeYear', true );
	d.datepicker( 'option', 'showOtherMonths', true );
	d.datepicker( 'option', 'selectOtherMonths', true );
	d.datepicker( 'option', 'minDate', '+1d' );
	d.datepicker( 'option', 'maxDate', '+18m' );
	//$('#calendar').datepicker( 'option', 'showButtonPanel', true );
	//$('#calendar').datepicker( 'option', 'altFormat', 'yy-mm-dd' );
	d.datepicker( 'option', 'dateFormat', 'yymmdd' );
	//$('#calendar').datepicker( 'option', 'altField', '#x' );
	//$('#calendar').datepicker( 'option', 'numberOfMonths', 2 );
	d.datepicker( 'option', 'dayNamesMin', options.weekDays );

}


function setCookie( name, value ) {
	var d = new Date();
	d.setDate( d.getDate() + 7 ); // one week into the future.

	document.cookie = name + "=" + value + "; expires=" + d.toGMTString() + "; path=/";
}


function getCookie(name) {
	var na = name + '=';

	var out = null;

	var c = document.cookie;

	if ( c ) {
		var ca = c.split( ';' );
	}

	for ( var i in ca ) {
		var v = ca[i];

		var n = v.charAt(0) == ' ' ? v.substring(1, v.length) : v;

		if ( n.indexOf(na) == 0 ) {
			out = n.substring( na.length, n.length );

			break ;
		}
	}

	return out;
}


function historyAppend( name, item ) {
	var h = getHistory( name );

	var i = 0;

	// Remove any duplicates.
	// (Deleting items from an array is kind of odd in Javascript)
	while ( i < h.length ) {
		if ( h[i] == item ) {
			h.splice( i, 1 );
		} else {
			i++;
		}
	}

	var size = h.length;

	if ( size >= 10 ) {
		// Remove the oldest entries when array contain > 10 entires.
		for ( var i = size; i >= 10; i-- ) {
			h.shift();
		}
	}
	h.push( item );

	setCookie( name, h.toString() );
}


function getHistory( name ) {
	var h = new Array();
	var c = getCookie( name );

	if ( c ) {
		h = c.split( ',' );
	} 


	return h;
}


$().ready( function() {
	var t = $('#tagwall');

	if ( t.length && cities ) {
		t.html( '' );

		for ( var c in cities ) {
			t.append(
				//'#' + cc.toLowerCase() + '#' +  
				//'<img src="images/icon_' + cities[c][4].toLowerCase() + '.jpg">';
				'<span class="' + cities[c][0] + '"><a href="' + 
				'?country=' + cities[c][1] + 
				'&state=' + cities[c][2] + 
				'&city=' + cities[c][3] + 
				'&language=' + language + '" ' +
				'title="Bed &amp; Breakfast ' + c + ' (' + cities[c][4] + ')"' + 
				'>' + c + '</a><span> ' +
				''
				//'<img src="images/icon_' + cities[c][4].toLowerCase() + '.jpg">'
				// + '</span> ' 
			);
		}
	}
} );
