//DBR 1.7 DEFAULT THEME
/**
 * 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';
(function() {
	function add(name, method) {
		if( !Date.prototype[name] ) {
			Date.prototype[name] = method;
		}
	};
	add("isLeapYear", function() {
		var y = this.getFullYear();
		return (y%4==0 && y%100!=0) || y%400==0;
	});
	add("isWeekend", function() {
		return this.getDay()==0 || this.getDay()==6;
	});
	add("isWeekDay", function() {
		return !this.isWeekend();
	});
	add("getDaysInMonth", function() {
		return [31,(this.isLeapYear() ? 29:28),31,30,31,30,31,31,30,31,30,31][this.getMonth()];
	});
	add("getDayName", function(abbreviated) {
		return abbreviated ? Date.abbrDayNames[this.getDay()] : Date.dayNames[this.getDay()];
	});
	add("getMonthName", function(abbreviated) {
		return abbreviated ? Date.abbrMonthNames[this.getMonth()] : Date.monthNames[this.getMonth()];
	});
	add("getDayOfYear", function() {
		var tmpdtm = new Date("1/1/" + this.getFullYear());
		return Math.floor((this.getTime() - tmpdtm.getTime()) / 86400000);
	});
	add("getWeekOfYear", function() {
		return Math.ceil(this.getDayOfYear() / 7);
	});
	add("setDayOfYear", function(day) {
		this.setMonth(0);
		this.setDate(day);
		return this;
	});
	add("subYears", function(num) {
		this.setFullYear(this.getFullYear() - num);
		return this;
	});
	add("addYears", function(num) {
		this.setFullYear(this.getFullYear() + num);
		return this;
	});
	add("addMonths", function(num) {
		var tmpdtm = this.getDate();

		this.setMonth(this.getMonth() + num);

		if (tmpdtm > this.getDate())
			this.addDays(-this.getDate());

		return this;
	});
	add("addDays", function(num) {
		this.setDate(this.getDate() + num);
		return this;
	});
	add("addHours", function(num) {
		this.setHours(this.getHours() + num);
		return this;
	});
	add("addMinutes", function(num) {
		this.setMinutes(this.getMinutes() + num);
		return this;
	});
	add("addSeconds", function(num) {
		this.setSeconds(this.getSeconds() + num);
		return this;
	});
	add("zeroTime", function() {
		this.setMilliseconds(0);
		this.setSeconds(0);
		this.setMinutes(0);
		this.setHours(0);
		return this;
	});
	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()));
	});
	Date.fromString = function(s)
	{
		if(!s) return new Date();

		var f = Date.format;
		var d = new Date('01/01/1977');
		var iY = (f.indexOf('yyyy')!=-1) ? f.indexOf('yyyy') : 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('yy'), 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 :(
	};

})();
// ============================== Calendar object ============================== //
/**
 * Calendar object. Needs Date prototype extensions by Jasrn Zaefferer and Brandon Aaron
 *
 * Example:
 * var c = new Calendar();
 * c.setDate({month:7,year:2008});
 * document.write(c.build(2));
 *
 * @constructor
 * @param {string} selected date string
 */
function Calendar(selectedStr) {
	this.date = new Date();
	this.html = '';
	this.selectedStr = (selectedStr); // || new Date().asString() ;

}
/**
 * Sets internal month and year in the Calendar object. Returns array of month and year;
 * @param {int} month
 * @param {int} year
 * @return {object} month and year array
 */
Calendar.prototype.setDate = function(o) {
	var n = new Date();
	this.month = (!o || (isNaN(o.month) || o.month == null)) ? n.getMonth() : o.month;
	this.year  = (!o || (isNaN(o.year) || o.year == null)) ? n.getFullYear() : o.year;
	return {month:this.month,year:this.year};
}
Calendar.prototype.setLinkedDate = function(o) {
	this.linkedDate = o;
}
/**
 * Converts string date to object for easy use with setDate method.
 * @param {string} string
 * @return {object} date as object
 */
Calendar.prototype.dateToObject = function(string) {
	var s = Date.fromString(string);
	if(!s || s.getFullYear()<0) var s = new Date();
	return {day:s.getDate(),month:s.getMonth(),year:s.getFullYear()};
}
/**
 * Changes current month and sets new one. Function accepts two values: "next" and "prev".
 * It returns array of new month and year.
 * @param {string} prev or next month
 * @return {object} new month and year
 */
Calendar.prototype.go = function(go) {
	var o = {month:this.month,year:this.year};
	if(go=="next"&&o.month++>=11){
		o.month=0;o.year++;
	}
	if(go=="prev"&&o.month--<=0){
		o.month=11;o.year--;
	}
	return o;
}
/**
 * Builds HTML code with numer of moths specified by function parameter
 * @param {int} number of months to show
 * @return {string} Calendar HTML code
 */
Calendar.prototype.build = function(p) {
	this.generateMonth(this.selectedStr,p);
	for(i=1;i<p.loop;i++) {
		this.setDate(this.go("next"));
		this.generateMonth(this.selectedStr,p);
	}
	return this.html;
}
/**
 * Generates HTML code for one month specified by internal date of Calendar.
 * @param {string} selected date string
 * @return {string} Calendar HTML code
 */
Calendar.prototype.generateMonth = function(selectedStr,p){
	var selected = new Date.fromString(selectedStr);
	var today = new Date().zeroTime();
	var maxOutDate = p.outBlock;
	var maxRetDate = p.inBlock;
	var blocked = new Date().zeroTime().addDays(p.blockedDays);
	var linked = (this.linkedDate) ? new Date(this.linkedDate.year,this.linkedDate.month,this.linkedDate.day).zeroTime() : false ;

	if(!this.month&&!this.year) this.setDate();

	var fd = new Date(this.year, this.month).getDay() - Date.firstDayOfWeek; // First Day
	var monthLength = new Date(this.year, this.month).getDaysInMonth();
	var monthName = Date.monthNames[this.month];

	var html = '<table class="calendar-table" cellpadding="0" cellspacing="0">';
	html += '<tr><th colspan="7">'+monthName + "&nbsp;" + this.year+'</th></tr>';
	html += '<tr class="calendar-header">';
	for(var i = Date.firstDayOfWeek, j=0; i <= 12; i++ ){
		html += '<td class="calendar-header-day">'+Date.abbrDayNames[(i>6) ? i-7 : i]+'</td>';
		if(j++>=6) break;
	}
	html += '</tr><tr>';

	var day = 1;
	for (var i = 0; i < 6; i++) {
	for (var j = 0; j <= 6; j++) {
		html += '<td class="calendar-day">';
		if (day <= monthLength && (i > 0 || j >= ((fd>=0) ? fd : 7+fd))) {
			var calDay = new Date(this.year,this.month,day).zeroTime();
			css = 'month-day';
			css += (calDay-today==0) ? ' is-today' : '';
			css += (calDay.isWeekend()) ? ' is-weekend' : '';
			css += (linked&&calDay-linked==0) ? ' is-linked' : '';
			css += (calDay<blocked && p.blockedDays!=false) ? ' is-blocked' : '';
			css += (calDay<p.blockTo && p.blockTo!=false) ? ' is-disabled' : '';
			css += (calDay>p.blockFrom && p.blockFrom!=false) ? ' is-disabled' : '';
			css += (calDay<new Date().zeroTime().addDays(3)&&calDay>new Date().zeroTime()&&p.service=='flights') ? ' only-cc-payment' : '';
			css += (calDay-selected==0) ? ' is-selected' : '';
			//css += (linked&&calDay<linked) ? ' is-linked-disabled' : '';
			css += (linked&&calDay<linked) ? ' is-disabled' : '';
			//css += (!linked&&calDay>maxOutDate || linked&&calDay>maxRetDate) ? ' is-out-of-range' : '';
			css += (!linked&&calDay>maxOutDate || linked&&calDay>maxRetDate) ? ' is-disabled' : '';
			html += '<a class="'+css+'" rel="'+calDay.asString()+'">'+day+'</a>';
			day++;
		} else {
			html += '&nbsp;';
		}
		html += '</td>';
	}
	if (day > monthLength) { break; } else { html += '</tr><tr>'; }
	//html += '</tr><tr>';
	}
	html += '</tr></table>';
	this.html += html;
	return this.html;
}
// ============================== Calendar object ============================== //

// ============================== jQuery extension for Calendar ============================== //
var	esky_calender_translate = {
	'OutGreaterThenReturn' : 'Out date is greater then in date.',
	'OutDateToClose' : 'Out date is to close to current date.',
	'DateOutOfRange' : 'Selected date is out of range.',
	'OnlyCCPayment' : 'OnlyCCPayment'
}
jQuery.fn.extend({
	esky_calendar_render: function(parameters) {
		$this = $(this);
		var container = $this.attr('id');
		var defaults = {
				thisInput: false,
				linkedInput: false,
				month: null,
				year: null,
				loop: 1,
				type:'popup',
				go: false,
				fade: false,
				outBlock: new Date().zeroTime().addDays(364),
				inBlock: new Date().zeroTime().addDays(363),
				blockTo: new Date().zeroTime(),
				blockFrom: false,
				blockedDays: 0,
				allowSameDay: false,
				i18n: esky_calender_translate,
				clickCallback: false,
				service : 'flights'
			};
		var p = $.extend({},defaults, parameters);

		var tI = document.getElementById(p.thisInput).value;

		var c = new Calendar(tI);

		if (p.linkedInput!=false) {
			var lI = document.getElementById(p.linkedInput).value;
			// Use linked date from other input field (other calendar) only if
			// this input and linked input have diffrernt names and
			// date is not set or calendar was displayed by navigating with buttons
			var lIobject = c.dateToObject(lI);
			if(!/^[0-9\-\.\\]+$/.test(tI)) tI = lI;

			if (p.thisInput != p.linkedInput && (!p.month && !p.year)) {
				//$.extend(p, lIobject); //Powoduje ustawienie kalendarza linkowanego na dacie kalendarza poprzedniego
				c.setLinkedDate(lIobject);
			}
			if (p.thisInput != p.linkedInput && ((!p.month && !p.year) || p.go != false)) {
				c.setLinkedDate(lIobject);
			}
		}

		var d = (!p.month && !p.year) ? c.dateToObject(tI) : {month:p.month,year:p.year} ;
		var calDay = new Date(d.year,d.month,1).zeroTime();
		if(calDay<p.blockTo) d = {month:p.blockTo.getMonth(),year:p.blockTo.getFullYear()};
		if(calDay>p.outBlock) d = {month:p.outBlock.getMonth(),year:p.outBlock.getFullYear()};
		
		c.setDate(d);

		var pv = $.extend({},p,c.go("prev"),{go:"prev"});
		var px = $.extend({},p,c.go("next"),{go:"next"});

		var cd = new Date();
		var pc = $.extend({},p,{month:cd.getMonth(),year:cd.getFullYear(),go:true});

		$this
			.click(function(e){
				e.stopPropagation();
				return false;				
			})		
			.html(c.build(p))
			.prepend(
				$('<div class="calendar-navigation"></div>')
				.prepend($('<a href="javascript:void(0);" class="calendar-button calendar-next" title="'+cal_text.NEXT+'">&raquo;</a>').click(function(e){
					$('#'+container).esky_calendar_render(px);
					e.stopPropagation();
				}))
				.prepend($('<a href="javascript:void(0);" class="calendar-button calendar-current">'+cal_text.THISMONTH+'</a>').click(function(){$this.esky_calendar_render(pc)}))
				.prepend($('<a href="javascript:void(0);" class="calendar-button calendar-previous" title="'+cal_text.PREV+'">&laquo;</a>').click(function(e){
					$('#'+container).esky_calendar_render(pv);
					e.stopPropagation();
				}))
			);
		if (p.type == 'popup') {
			$this.append($('<a href="javascript:void(0);" class="calendar-button calendar-close">' + cal_text.CLOSE + '</a>').click(function(){
				$this.remove()
			}));
		}

		$('a.month-day','#'+container).not('.is-disabled,.is-blocked')
		.click(function(){
			if(typeof p.clickCallback == 'function'){
				p.clickCallback($(this).attr('rel'));
			}
			$('#'+p.thisInput).not(':disabled').val( $(this).attr('rel') ).removeClass('virgin').blur();
			$('.is-selected','#'+container).removeClass('is-selected');
			$('#custom-field-help').remove();
			$(this).addClass('is-selected');
			if(typeof(calculateCheckoutDate)=='function') calculateCheckoutDate(p.thisInput); // Only for HOTELS ASF
			if(p.type == 'popup') $this.remove();
		})
		.mouseover(function(){
			$(this).addClass('is-hover');
		})
		.mouseout(function(){
			$(this).removeClass('is-hover');
		});

		if(p.allowSameDay==false){
			$('a.is-linked').not('.is-disabled').unbind().click(function(){
				alert(p.i18n.OutGreaterThenReturn);
				return false;
			});
		}
		$('a.is-blocked').not('.is-disabled,.is-linked,.is-linked-disabled').unbind().click(function(){
			alert(p.i18n.OutDateToClose);
			return false;
		});
		$('a.is-out-of-range').unbind().click(function(){
			alert(p.i18n.DateOutOfRange);
			return false;
		});
		$('a.is-disabled,a.is-linked-disabled').unbind().click(function(){
			return false;
		});
		$('a.only-cc-payment').not('.is-disabled').tipBox(p.i18n.OnlyCCPayment);

		$this.bgIframe();
		return $this;
	},
	esky_calendar: function(parameters){
		var rel =  '#' + parameters.thisInput;

		if ($('#esky_calendar').size()>0) {
			var inputId = $('#esky_calendar').data('inputId');
			$('#esky_calendar').remove();
			if (inputId==rel) return false;
		}

		$('body').append(
			$('<div></div>').attr('id','esky_calendar').hide()
		);

		$input = $(rel);
		$input.attr('autocomplete','off');
		$calendar = $('#esky_calendar');
		$error = $('p#message'); 

		var xy = $input.offset();
		var test = {
			x: parseInt(xy.left) + parseInt($calendar.width()),
			y: parseInt(xy.top) + parseInt($input.height()) + parseInt($calendar.height())
		};
		var view = getViewportSize(true);

		var cLeft = (view.width<test.x) ? xy.left - $calendar.width() +  $input.width() - 4 : xy.left;
		var cTop = (view.height<test.y) ? xy.top - $calendar.height() -  $input.height() : xy.top+$input.height()+8;

		$calendar
			.esky_calendar_render(parameters)
			.css({position:'absolute',top:cTop,left:cLeft})
			.data('inputId',rel);

		if(parameters.fade>0){
			$calendar.fadeIn(parameters.fade);
		}else{
			$calendar.show();
		}

		return $(this);
	}
});
// ============================== jQuery extension for Calendar ============================== //
