/**
 * booking data picker
 */
var bookingDataPicker = {
	// static config
	option: {
		monthTranslate: {
			0: "Jan",
			1: "Feb",
			2: "Mar",
			3: "Apr",
			4: "May",
			5: "Jun",
			6: "Jul",
			7: "Aug",
			8: "Sep",
			9: "Oct",
			10: "Nov",
			11: "Dec"
		},
		checkIn: {
			day: 	jQuery('input[value="checkIn-day"]'),
			month: 	jQuery('input[value="checkIn-month"]'),
			year: 	jQuery('input[value="checkIn-year"]')
		},
		checkOut: {
			day: 	jQuery('input[value="checkOut-day"]'),
			month: 	jQuery('input[value="checkOut-month"]'),
			year: 	jQuery('input[value="checkOut-year"]')
		},
		
		checkInDate: {
			day: 00,
			month: 00,
			year: 2011
		},
		
		checkOutDate: {
			day: 00,
			month: 00,
			year: 2011
		},
		
		todayDate: {
			day: 00,
			month: 00,
			year: 2011
		},
		
		styles: {
			day: {
				width: 30,
				height: 80
			},
			month: {
				width: 40,
				height: 80
			},
			year: {
				width: 40,
				height: 60
			}
		},
		openAlias: '',
		errorSelectMSG: "Please select check out Date > check in Date!"
	},

	init: function(){
		this.checkObserver();
		
		this.onSubmitRecheck();
	},
	
	checkObserver: function(){
		var self = this;
		
		// set default data
		self.defaultDataCheckIn();
		self.defaultDataCheckOut();
		
		jQuery('.groupe-checkIn, .groupe-checkOut').find(".bookingDate-input").click(function(){
			var $this = jQuery(this)
				input_type = $this.attr('alt');
			
			// check type (in / out)
			var checkValue = $this.attr('name');
			var marker = checkValue.split("-");
			if(marker[0] == 'checkIn'){
				self.writeHtml(input_type, $this, self.option.checkInDate);
			}
			if(marker[0] == 'checkOut'){
				self.writeHtml(input_type, $this, self.option.checkOutDate);
			}
		});
	},
	
	defaultDataCheckIn: function(){
		var self = this;
		
		var currentTime = new Date();
		// set today date. Use for verify not selected date < today
		self.option.todayDate = {
			day: currentTime.getDate(),
			month: currentTime.getMonth(),
			year: currentTime.getFullYear()
		}
		
		self.option.checkInDate = {
			day: currentTime.getDate(),
			month: currentTime.getMonth(),
			year: currentTime.getFullYear()
		}
		
		jQuery('.groupe-checkIn, .groupe-checkOut').find(".bookingDate-input").each(function(key, index){
			var $this = jQuery(this);
			
			// setup default checkin day, month, year
			if($this.attr('alt') == 'day') $this.val(self.option.checkInDate.day);
			if($this.attr('alt') == 'month') $this.val(self.option.monthTranslate[self.option.checkInDate.month]);
			if($this.attr('alt') == 'year') $this.val(self.option.checkInDate.year);
		});
	},
	
	defaultDataCheckOut: function(){
		var self = this;
		
		var currentTime = new Date();
		self.option.checkOutDate = {
			day: currentTime.getDate(),
			month: currentTime.getMonth(),
			year: currentTime.getFullYear()
		}

		jQuery('.groupe-checkOut').find(".bookingDate-input").each(function(key, index){
			var $this = jQuery(this);
			
			// setup default checkin day, month, year
			if($this.attr('alt') == 'day') $this.val(self.option.checkInDate.day);
			if($this.attr('alt') == 'month') $this.val(self.option.monthTranslate[self.option.checkInDate.month]);
			if($this.attr('alt') == 'year') $this.val(self.option.checkInDate.year);
		});
	},
	
	writeHtml: function(type, elm, data_type){
		var self = this;
		
		// remove last dataPicker created elementFromPoint
		jQuery(".dataPickerBox").remove();
		
		// start html concat
		var html = '<div class="dataPickerBox">';
		if(type == 'day'){
			html += '<ul>';
			html += '<li>';
			
			// if month is same month as today then don't allow select day lower thean today
			if(self.option.todayDate.month == data_type.month && self.option.todayDate.year == data_type.year){
				for (var i = data_type.day; i <= self.lastDayOfMonth(data_type.year, data_type.month); i++){
					html += '<a href="#' + i + '">' + i + '</a>';
				}
			}else{
				for (var i = 1; i <= self.lastDayOfMonth(data_type.year, data_type.month); i++){
					html += '<a href="#' + i + '">' + i + '</a>';
				}
			}
			html += '</li>';
			html += '</ul>';
		}
		if(type == 'month'){
			html += '<ul>';
			html += '<li>';
			if(self.option.todayDate.year == data_type.year){
				for (var i = self.option.todayDate.month; i < 12; i++){
					html += '<a href="#' + i + '">' + self.option.monthTranslate[i] + '</a>';
				}
			}else{
				for (var i = 0; i < 12; i++){
					html += '<a href="#' + i + '">' + self.option.monthTranslate[i] + '</a>';
				}
			}
			html += '</li>';
			html += '</ul>';
		}
		
		if(type == 'year'){
			html += '<ul>';
			html += '<li>';
			for (var i = self.option.todayDate.year; i <= self.option.todayDate.year + 5; i++){
				html += '<a href="#' + i + '">' + i + '</a>';
			}
			html += '</li>';
			html += '</ul>';
		}
		
		html += '</div>';
		
		// output html code
		jQuery('.booking-form').append(html);
		
		// get clicked element position 
		var elmPosition = elm.position();
		
		// setter open data picker
		self.option.openAlias = elm.attr('name');
		
		// positionate html generated data picker (method)
		self.positionPicker(type, elmPosition);
		
		// make scrollable data picker (method)
		//self.makeScrollable();
		
		// make clickable data picker (method)
		self.makeClickable();
	},
	
	positionPicker: function(alias, elmPosition) {
		var self = this;

		jQuery("div.dataPickerBox").css({
			'width'		: self.option.styles[alias].width + "px",
			'height'	: self.option.styles[alias].height + "px",
			'left'		: elmPosition.left + "px"
		});
		
		var checkValue = self.option.openAlias;
		var marker = checkValue.split("-");
		if(marker[0] == 'checkIn'){
			jQuery("div.dataPickerBox").css('top', "-" + parseInt(self.option.styles[alias].height + 9) + "px");
		}
		if(marker[0] == 'checkOut'){
			jQuery("div.dataPickerBox").css('top', "-" + parseInt(self.option.styles[alias].height - 17) + "px");
		}

		
	},

	makeScrollable: function(){
		// fix for easy select first and last element
		var extra = 20;
		var $outer = jQuery(".dataPickerBox");
		var $inner = $outer.children(':first');
		
		// get menu height
		var divHeight = $outer.height();
		
		// find last li in container
		var lastElem = $inner.find('li:last');
		$outer.scrollTop(0);
		
		// when user move mouse over menu
		$outer.unbind('mousemove').bind('mousemove',function(e){
			var containerHeight = lastElem[0].offsetTop + lastElem.outerHeight() + 2 * extra;
			var top = (e.pageY - $outer.offset().top) * (containerHeight - divHeight) / divHeight - extra;
			$outer.scrollTop(top );
		});
	},
	
	makeClickable: function(){
		var self = this;
		jQuery("div.dataPickerBox").find('a').click(function(){
			var value = jQuery(this).text();
				
			// set input value
			jQuery('input[name="' + self.option.openAlias + '"]').val(value);
			
			// reset data
			var checkValue = self.option.openAlias;
			var marker = checkValue.split("-");
			if(marker[0] == 'checkIn'){
				self.option.checkInDate[marker[1]] = parseInt(jQuery(this).attr('href').replace("#", ''));
			}
			if(marker[0] == 'checkOut'){
				self.option.checkOutDate[marker[1]] = parseInt(jQuery(this).attr('href').replace("#", ''));
			}
			
			// recheck data
			self.recheckData();
			
			// remove last dataPicker created elementFromPoint
			jQuery(".dataPickerBox").remove();
			
			return false;
		});
	},
	
	recheckData: function(checkSubmit){
		var self = this;
		
		// define "sums" like utils objects
		var sums = {
			today :  new Date(parseInt(self.option.todayDate.year) + "/" + parseInt(self.option.todayDate.month + 1) + "/" + parseInt(self.option.todayDate.day) + ' UTC'),
			checkInt :  new Date(parseInt(self.option.checkInDate.year) + "/" + parseInt(self.option.checkInDate.month + 1) + "/" + parseInt(self.option.checkInDate.day) + ' UTC'),
			checkOut :  new Date(parseInt(self.option.checkOutDate.year) + "/" + parseInt(self.option.checkOutDate.month + 1) + "/" + parseInt(self.option.checkOutDate.day) + ' UTC')
		}
		
		// if chose check in date < today date go and reset data to default value
		if(sums.checkInt.getTime() < sums.today.getTime()) self.defaultDataCheckIn();
		
		// if chose check in date < today date go and reset data to default value
		if(sums.checkOut.getTime() < sums.today.getTime()) self.defaultDataCheckOut();
		
		if(checkSubmit === true){
		
			if(sums.checkOut.getTime() <= sums.checkInt.getTime()) alert(self.option.errorSelectMSG);
			return false;
		}
		
		/* debug value in seconds
			console.log( "sums.today: " + sums.today.getTime()  );
			console.log( "sums.checkInt: " + sums.checkInt.getTime()  );
			console.log( "sums.checkOut: " + sums.checkOut.getTime()  ); 
		*/
		
		/* debug value in HR output
			console.log( "sums.today: " + sums.today  );
			console.log( "sums.checkInt: " + sums.checkInt  );
			console.log( "sums.checkOut: " + sums.checkOut  ); 
		*/
		
		return true;
	},
	
	lastDayOfMonth: function(Year, Month){
		return 32 - new Date(Year, Month, 32).getDate();
    },
	
	onSubmitRecheck: function() {
		var self = this;
	
		jQuery("form.booking-form").submit(function(){
			if(self.recheckData(true)) return true;
			return false;
		});
	}
}

