/* copyright 2009-2010 by jproxx.com. All rights reserved. */
var bIsFirebugReady = (!!window.console && !!window.console.log);

function getDateString(dateValue){
	var checkstr = "0123456789";
	var separator = ".";
	var separator1_pos;
	var separator2_pos;
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;
	var j = 0;
	if (dateValue.length> 0) {
		for (i = 0; i < dateValue.length; i++) {
			if ((isNaN(dateValue.substr(i,1)) == true) && (dateValue.substr(i,1) != separator)) {err = 19;}
		}
		if (err == 0) {
			for (i = 0; i < dateValue.length; i++) {
				if ((dateValue.substr(i,1)) == separator) {
					if (j == 0) { separator1_pos = i;}
					if (j == 1) {separator2_pos = i;}
					j++;
				}
				if (j > 2) {err = 20;}
			}
			day = dateValue.substr(0,separator1_pos);
			month = dateValue.substr(separator1_pos+1,separator2_pos-separator1_pos-1);
			year = dateValue.substr(separator2_pos+1,dateValue.length-1);
			if (year.length == 2) {
				if (year < 50) {year = 20 + year;}
				else {year = 19 + year;}
			}
			else if(year.length != 4) {err = 26;}
			if (month.length == 1) {month = 0 + month;}
			if ((month < 1) || (month > 12)) {err = 21;}
			if (day.length == 1) {day = 0 + day;}
			if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {leap = 1;}
			if ((month == 2) && (leap == 1) && (day > 29)) {err = 22;}
			if ((month == 2) && (leap != 1) && (day > 28)) {err = 23;}
			if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {err = 24;}
			if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {err = 25;}
			if ((day == 0) && (month == 0) && (year == 00)) {err = 0; day = ""; month = ""; year = ""; separator = "";}
		}
		if (err != 0) {return false;}
		else {return day + separator + month + separator + year;} 
	}else{return false;}
}

// builds a Date Object from a given Datestring
function getDateObject(dateValue){
	var date_str = getDateString(dateValue);
	var date_arr = date_str.split(".");
	if (date_arr.length == 3){
		var dateString = date_arr[2]+"/"+date_arr[1]+"/"+date_arr[0];  // yyyy/MM/dd
		return new Date(dateString);
	}
	else{
		return undefined;
	}
}

function checkDate(dateValue){
	if (getDateString(dateValue)==false){
		return false;
	}else{
		return true;
	}
}

function isNumeric(input)
{
   return (input - 0) == input && input.length > 0;
}

function showMessageBox(dialog, content, title, message){
	$(dialog).dialog('option', 'title', title);
	$(content).html(message);
	$(dialog).dialog("open");	
}

function validate(dialog, content){
	var error = "<ul>";
	var from = $("#search_from").getValue();
	var to = $("#search_to").getValue();
	var from_state = false;
	var to_state = false;
	if (!(from_state = checkDate(from))){error = error+"<li>Früheste Anreise</li>";}	
	if (!(to_state = checkDate(to))){
		error = error+"<li>Späteste Abreise</li>";
	}
	
	if (from_state & to_state){
		var from_date = getDateObject(from);
		var to_date = getDateObject(to);
		if (to_date<from_date){
			error = error+"<li>Das Anreisedatum liegt hinter dem Abreisedatum!</from_date>";
		}
	}
	
	var persons = $("#search_p").getValue();
	if ( !isNumeric(persons) || persons<=0 ){
		error=error+"<li>Die Anzahl der Personen muss eingegeben werden!</li>";
	}
	error=error+"</ul>";
	
	if (error.length > 9){
		showMessageBox(dialog, content, "Fehlerhafte Eingabe(n)", "Bitte überprüfen Sie Ihre Eingaben. <br/><br/><div class='dialog_error'>Fehler: </div>"+error);
		return false;
	}else{
		return true;
	}
}

jQuery(function($){
	$.datepicker.regional['de'] = {
		closeText: 'schließen',
		prevText: '&#x3c;zurück',
		nextText: 'Vor&#x3e;',
		currentText: 'heute',
		monthNames: ['Januar','Februar','März','April','Mai','Juni',
		'Juli','August','September','Oktober','November','Dezember'],
		monthNamesShort: ['Jan','Feb','Mär','Apr','Mai','Jun',
		'Jul','Aug','Sep','Okt','Nov','Dez'],
		dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
		dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
		dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
		dateFormat: 'dd.mm.yy', firstDay: 1,
		isRTL: false};
	$.datepicker.setDefaults($.datepicker.regional['de']);
});

$(document).ready(function() {
	$("input:visible:enabled:first").focus();
	
	// Dialog
	$("#dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		modal: true,
		width: 400,
		minWidth: 400,
		maxWidth: 500,
		buttons: {
		Ok: function() {
				$(this).dialog('close');
			}
		}
	});	
	
	// setting up the date picker from date
	$('#search_from').datepicker({
		numberOfMonths: 3,
		showButtonPanel: true,
		showOn: 'both', 
		buttonImage: '/images/calendar.png', 
		buttonImageOnly: true,
		showAnim: 'fold',
		minDate: 0,
		maxDate: '+2Y'
	});	

	// setting up the date picker to date
	$('#search_to').datepicker({
		numberOfMonths: 3,
		showButtonPanel: true,
		showOn: 'both', 
		buttonImage: '/images/calendar.png', 
		buttonImageOnly: true,
		showAnim: 'fold',
		minDate: 0,
		maxDate: '+2Y'
	});
	
	// setting up the gallery
	window.api = $("#autoscroll").scrollable({
		size: 1
	}).circular().autoscroll({
		autoplay: true,
		api: true,
		steps: 1,
		interval: 6000 
	});

        $('#laundry_info_button').tooltip();
});

