//ESKY APP PUBLIC
/**
 * The first two numbers in the century to be used when decoding a two digit year. Since a two digit year is ambiguous (and date.setYear
 * only works with numbers < 99 and so doesn't allow you to set years after 2000) we need to use this to disambiguate the two digit year codes.
 *
 * @name format
 * @type String
 * @cat Plugins/Methods/Date
 * @author Kelvin Luck
 */
Date.fullYearStart = '20';
Date.abbrMonthNames = ['Sty', 'Lut', 'Mar', 'Kwi', 'Maj', 'Cze', 'Lip', 'Sie', 'Wrz', 'Paź', 'Lis', 'Gru'];
(function() {

	/**
	 * Adds a given method under the given name
	 * to the Date prototype if it doesn't
	 * currently exist.
	 *
	 * @private
	 */
	function add(name, method) {
		if( !Date.prototype[name] ) {
			Date.prototype[name] = method;
		}
	}

	/**
	 * Checks if the year is a leap year.
	 *
	 * @example var dtm = new Date("01/12/2008");
	 * dtm.isLeapYear();
	 * @result true
	 *
	 * @name isLeapYear
	 * @type Boolean
	 * @cat Plugins/Methods/Date
	 */
	add("isLeapYear", function() {
		var y = this.getFullYear();
		return (y%4==0 && y%100!=0) || y%400==0;
	});

	/**
	 * Checks if the day is a weekend day (Sat or Sun).
	 *
	 * @example var dtm = new Date("01/12/2008");
	 * dtm.isWeekend();
	 * @result false
	 *
	 * @name isWeekend
	 * @type Boolean
	 * @cat Plugins/Methods/Date
	 */
	add("isWeekend", function() {
		return this.getDay()==0 || this.getDay()==6;
	});

	/**
	 * Check if the day is a day of the week (Mon-Fri)
	 *
	 * @example var dtm = new Date("01/12/2008");
	 * dtm.isWeekDay();
	 * @result false
	 *
	 * @name isWeekDay
	 * @type Boolean
	 * @cat Plugins/Methods/Date
	 */
	add("isWeekDay", function() {
		return !this.isWeekend();
	});

	/**
	 * Gets the number of days in the month.
	 *
	 * @example var dtm = new Date("01/12/2008");
	 * dtm.getDaysInMonth();
	 * @result 31
	 *
	 * @name getDaysInMonth
	 * @type Number
	 * @cat Plugins/Methods/Date
	 */
	add("getDaysInMonth", function() {
		return [31,(this.isLeapYear() ? 29:28),31,30,31,30,31,31,30,31,30,31][this.getMonth()];
	});

	/**
	 * Gets the name of the day.
	 *
	 * @example var dtm = new Date("01/12/2008");
	 * dtm.getDayName();
	 * @result 'Saturday'
	 *
	 * @example var dtm = new Date("01/12/2008");
	 * dtm.getDayName(true);
	 * @result 'Sat'
	 *
	 * @param abbreviated Boolean When set to true the name will be abbreviated.
	 * @name getDayName
	 * @type String
	 * @cat Plugins/Methods/Date
	 */
	add("getDayName", function(abbreviated) {
		return abbreviated ? Date.abbrDayNames[this.getDay()] : Date.dayNames[this.getDay()];
	});

	/**
	 * Gets the name of the month.
	 *
	 * @example var dtm = new Date("01/12/2008");
	 * dtm.getMonthName();
	 * @result 'Janurary'
	 *
	 * @example var dtm = new Date("01/12/2008");
	 * dtm.getMonthName(true);
	 * @result 'Jan'
	 *
	 * @param abbreviated Boolean When set to true the name will be abbreviated.
	 * @name getDayName
	 * @type String
	 * @cat Plugins/Methods/Date
	 */
	add("getMonthName", function(abbreviated) {
		return abbreviated ? Date.abbrMonthNames[this.getMonth()] : Date.monthNames[this.getMonth()];
	});

	/**
	 * Get the number of the day of the year.
	 *
	 * @example var dtm = new Date("01/12/2008");
	 * dtm.getDayOfYear();
	 * @result 11
	 *
	 * @name getDayOfYear
	 * @type Number
	 * @cat Plugins/Methods/Date
	 */
	add("getDayOfYear", function() {
		var tmpdtm = new Date("1/1/" + this.getFullYear());
		return Math.floor((this.getTime() - tmpdtm.getTime()) / 86400000);
	});

	/**
	 * Get the number of the week of the year.
	 *
	 * @example var dtm = new Date("01/12/2008");
	 * dtm.getWeekOfYear();
	 * @result 2
	 *
	 * @name getWeekOfYear
	 * @type Number
	 * @cat Plugins/Methods/Date
	 */
	add("getWeekOfYear", function() {
		return Math.ceil(this.getDayOfYear() / 7);
	});

	/**
	 * Set the day of the year.
	 *
	 * @example var dtm = new Date("01/12/2008");
	 * dtm.setDayOfYear(1);
	 * dtm.toString();
	 * @result 'Tue Jan 01 2008 00:00:00'
	 *
	 * @name setDayOfYear
	 * @type Date
	 * @cat Plugins/Methods/Date
	 */
	add("setDayOfYear", function(day) {
		this.setMonth(0);
		this.setDate(day);
		return this;
	});

	/**
	 * Add a number of years to the date object.
	 *
	 * @example var dtm = new Date("01/12/2008");
	 * dtm.addYears(1);
	 * dtm.toString();
	 * @result 'Mon Jan 12 2009 00:00:00'
	 *
	 * @name addYears
	 * @type Date
	 * @cat Plugins/Methods/Date
	 */
	add("addYears", function(num) {
		this.setFullYear(this.getFullYear() + num);
		return this;
	});

	/**
	 * Add a number of months to the date object.
	 *
	 * @example var dtm = new Date("01/12/2008");
	 * dtm.addMonths(1);
	 * dtm.toString();
	 * @result 'Tue Feb 12 2008 00:00:00'
	 *
	 * @name addMonths
	 * @type Date
	 * @cat Plugins/Methods/Date
	 */
	add("addMonths", function(num) {
		var tmpdtm = this.getDate();

		this.setMonth(this.getMonth() + num);

		if (tmpdtm > this.getDate())
			this.addDays(-this.getDate());

		return this;
	});

	/**
	 * Add a number of days to the date object.
	 *
	 * @example var dtm = new Date("01/12/2008");
	 * dtm.addDays(1);
	 * dtm.toString();
	 * @result 'Sun Jan 13 2008 00:00:00'
	 *
	 * @name addDays
	 * @type Date
	 * @cat Plugins/Methods/Date
	 */
	add("addDays", function(num) {
		this.setDate(this.getDate() + num);
		return this;
	});

	/**
	 * Add a number of hours to the date object.
	 *
	 * @example var dtm = new Date("01/12/2008");
	 * dtm.addHours(24);
	 * dtm.toString();
	 * @result 'Sun Jan 13 2008 00:00:00'
	 *
	 * @name addHours
	 * @type Date
	 * @cat Plugins/Methods/Date
	 */
	add("addHours", function(num) {
		this.setHours(this.getHours() + num);
		return this;
	});

	/**
	 * Add a number of minutes to the date object.
	 *
	 * @example var dtm = new Date("01/12/2008");
	 * dtm.addMinutes(60);
	 * dtm.toString();
	 * @result 'Sat Jan 12 2008 01:00:00'
	 *
	 * @name addMinutes
	 * @type Date
	 * @cat Plugins/Methods/Date
	 */
	add("addMinutes", function(num) {
		this.setMinutes(this.getMinutes() + num);
		return this;
	});

	/**
	 * Add a number of seconds to the date object.
	 *
	 * @example var dtm = new Date("01/12/2008");
	 * dtm.addSeconds(60);
	 * dtm.toString();
	 * @result 'Sat Jan 12 2008 00:01:00'
	 *
	 * @name addSeconds
	 * @type Date
	 * @cat Plugins/Methods/Date
	 */
	add("addSeconds", function(num) {
		this.setSeconds(this.getSeconds() + num);
		return this;
	});

	/**
	 * Sets the time component of this Date to zero for cleaner, easier comparison of dates where time is not relevant.
	 *
	 * @example var dtm = new Date();
	 * dtm.zeroTime();
	 * dtm.toString();
	 * @result 'Sat Jan 12 2008 00:01:00'
	 *
	 * @name zeroTime
	 * @type Date
	 * @cat Plugins/Methods/Date
	 * @author Kelvin Luck
	 */
	add("zeroTime", function() {
		this.setMilliseconds(0);
		this.setSeconds(0);
		this.setMinutes(0);
		this.setHours(0);
		return this;
	});

	/**
	 * Returns a string representation of the date object according to Date.format.
	 * (Date.toString may be used in other places so I purposefully didn't overwrite it)
	 *
	 * @example var dtm = new Date("01/12/2008");
	 * dtm.asString();
	 * @result '12/01/2008' // (where Date.format == 'dd/mm/rrrr'
	 *
	 * @name asString
	 * @type Date
	 * @cat Plugins/Methods/Date
	 * @author Kelvin Luck
	 */
	add("asString", function() {
		var r = Date.format;
		return r
			.replace(/(yyyy|rrrr)/i, this.getFullYear())
			.replace(/(yy|rr)/i, (this.getFullYear()+'').substring(2))
			.replace(/mmm/i, this.getMonthName(true))
			.replace(/mm/i, _zeroPad(this.getMonth()+1))
			.replace(/dd/i, _zeroPad(this.getDate()));
	});

	/**
	 * Returns a new date object created from the passed String according to Date.format or false if the attempt to do this results in an invalid date object
	 * (We can't simple use Date.parse as it's not aware of locale and I chose not to overwrite it incase it's functionality is being relied on elsewhere)
	 *
	 * @example var dtm = Date.fromString("12/01/2008");
	 * dtm.toString();
	 * @result 'Sat Jan 12 2008 00:00:00' // (where Date.format == 'dd/mm/rrrr'
	 *
	 * @name fromString
	 * @type Date
	 * @cat Plugins/Methods/Date
	 * @author Kelvin Luck
	 */
	Date.fromString = function(s)
	{
		if(!s) return new Date();

		var f = Date.format;
		var d = new Date('01/01/1977');
		var iY = f.indexOf('rrrr');
		if (iY > -1) {
			d.setFullYear(Number(s.substr(iY, 4)));
		} else {
			// TODO - this doesn't work very well - are there any rules for what is meant by a two digit year?
			d.setFullYear(Number(Date.fullYearStart + s.substr(f.indexOf('rr'), 2)));
		}
		var iM = f.indexOf('mmm');
		if (iM > -1) {
			var mStr = s.substr(iM, 3);
			for (var i=0; i<Date.abbrMonthNames.length; i++) {
				if (Date.abbrMonthNames[i] == mStr) break;
			}
			d.setMonth(i);
		} else {
			d.setMonth(Number(s.substr(f.indexOf('mm'), 2)) - 1);
		}
		d.setDate(Number(s.substr(f.indexOf('dd'), 2)));
		if (isNaN(d.getTime())) {
			return false;
		}
		return d;
	};

	// utility method
	var _zeroPad = function(num) {
		var s = '0'+num;
		return s.substring(s.length-2);
		//return ('0'+num).substring(-2); // doesn't work on IE :(
	};

})();

var j = jQuery.noConflict();

j(document).ready(function() {
	// przełączanie między lotem w dwie i jedną stronę
	j('input[type=radio]','.triptype').live('change', function() {
		var box = j(this).parents("form").attr("class");

		var o = j("."+box).find('input[type=radio]:checked','.triptype');
		var inputs = j("."+box).find('.out-date','#tab_qsfFlights,#promo_qsfFlights');
		switch(j(this).val()){
		    case 'roundtrip':
	            inputs.removeAttr('disabled');
	            inputs.show();
		    break;
		    case 'oneway':
	            inputs.attr('disabled','disabled').val('');;
	            inputs.hide().find('#fly_to_date_0').val('');
		    break;
		}
	});

	// daty wylotu i przylotu
	function update_departure(postfix) {
		// zapobieganie np. dnia 31 w kwietniu
		Date.format = 'dd mmm rrrr';
		var months_first = Date.fromString("01 "+j("#departure1_month_year"+postfix).val());
		if (months_first.getDaysInMonth()<parseInt(j("#departure1_day"+postfix).val())) {
			j("#departure1_day"+postfix).val(months_first.getDaysInMonth());
		}
		months_first = Date.fromString("01 "+j("#departure2_month_year"+postfix).val());
		if (months_first.getDaysInMonth()<parseInt(j("#departure2_day"+postfix).val())) {
			j("#departure2_day"+postfix).val(months_first.getDaysInMonth());
		}
		
		// ustawianie daty na min 'jutro'
		var extra_zero = '';
		Date.format = 'dd mmm rrrr';
		if (j("#departure1_day"+postfix).val().length == 1) {
			extra_zero = "0";
		}
		var departure_temp = Date.fromString(extra_zero+j("#departure1_day"+postfix).val()+" "+j("#departure1_month_year"+postfix).val());
		var now = new Date()
		if (departure_temp < now) {
			j("#departure1_day"+postfix).val(now.getDate()+1);
		}

		extra_zero = '';
		Date.format = 'dd mmm rrrr';
		if (j("#departure1_day"+postfix).val().length == 1) {
			extra_zero = "0";
		}
		var departure1 = Date.fromString(extra_zero+j("#departure1_day"+postfix).val()+" "+j("#departure1_month_year"+postfix).val());
		extra_zero = '';
		if (j("#departure2_day"+postfix).val().length == 1) {
			extra_zero = "0";
		}
		var departure2 = Date.fromString(extra_zero+j("#departure2_day"+postfix).val()+" "+j("#departure2_month_year"+postfix).val());
		
		Date.format = 'rrrr-mm-dd';
		if (departure1>departure2) {
			j("#fly_from_date_0"+postfix).val(departure1.asString());
			j("#fly_to_date_0"+postfix).val(departure1.asString());
			j("#departure2_day"+postfix).val(j("#departure1_day"+postfix).val());
			j("#departure2_month_year"+postfix).val(j("#departure1_month_year"+postfix).val());
		} else {
			j("#fly_from_date_0"+postfix).val(departure1.asString());
			j("#fly_to_date_0"+postfix).val(departure2.asString());
		}
	}


	//140x350
	if (j(".qsfBox_140").length) {
		var today_140 = new Date();
		if (today_140.getDaysInMonth()==today_140.getDate()) {
			today_140.addDays(1);
		}
		Date.format = 'mmm yyyy';
		j("#departure1_month_year_140").html('');
		j("#departure2_month_year_140").html('');
		for (var i=0;i<=12;i++) {
			j("#departure1_month_year_140").append('<option value="'+today_140.asString()+'">'+today_140.asString()+'</option>');
			j("#departure2_month_year_140").append('<option value="'+today_140.asString()+'">'+today_140.asString()+'</option>');
			today_140.addMonths(1);
		}
		j("#departure1_day_140").val(today_140.getDate());
		j("#departure2_day_140").val(today_140.getDate());
		update_departure('_140');

		j("#departure1_day_140").change(function(){

			update_departure('_140');
		});
		j("#departure2_day_140").change(function(){update_departure('_140');});
		j("#departure1_month_year_140").change(function(){update_departure('_140');});
		j("#departure2_month_year_140").change(function(){update_departure('_140');});
	}

	//250x250
	if (j(".qsfBox_250").length) {
		var today_250 = new Date();
		if (today_250.getDaysInMonth()==today_250.getDate()) {
			today_250.addDays(1);
		}
		Date.format = 'mmm yyyy';
		j("#departure1_month_year_250").html('');
		j("#departure2_month_year_250").html('');
		for (var i=0;i<=12;i++) {
			j("#departure1_month_year_250").append('<option value="'+today_250.asString()+'">'+today_250.asString()+'</option>');
			j("#departure2_month_year_250").append('<option value="'+today_250.asString()+'">'+today_250.asString()+'</option>');
			today_250.addMonths(1);
		}
		j("#departure1_day_250").val(today_250.getDate());
		j("#departure2_day_250").val(today_250.getDate());
		update_departure('_250');

		j("#departure1_day_250").change(function(){

			update_departure('_250');
		});
		j("#departure2_day_250").change(function(){update_departure('_250');});
		j("#departure1_month_year_250").change(function(){update_departure('_250');});
		j("#departure2_month_year_250").change(function(){update_departure('_250');});
	}

	//120x600
	if (j(".qsfBox_120").length) {
		var today_120 = new Date();
		if (today_120.getDaysInMonth()==today_120.getDate()) {
			today_120.addDays(1);
		}
		Date.format = 'mmm yyyy';
		j("#departure1_month_year_120").html('');
		j("#departure2_month_year_120").html('');
		for (var i=0;i<=12;i++) {
			j("#departure1_month_year_120").append('<option value="'+today_120.asString()+'">'+today_120.asString()+'</option>');
			j("#departure2_month_year_120").append('<option value="'+today_120.asString()+'">'+today_120.asString()+'</option>');
			today_120.addMonths(1);
		}
		j("#departure1_day_120").val(today_120.getDate());
		j("#departure2_day_120").val(today_120.getDate());
		update_departure('_120');

		j("#departure1_day_120").change(function(){

			update_departure('_120');
		});
		j("#departure2_day_120").change(function(){update_departure('_120');});
		j("#departure1_month_year_120").change(function(){update_departure('_120');});
		j("#departure2_month_year_120").change(function(){update_departure('_120');});
	}

});
