var FC = {
	hideCalendar : function (e, cal) {
		var __returnFalse = false;
		
		if (e.target.className == 'cal-input'){ // if the target is the input field
			__returnFalse = true;
		}
		
		if ($(e.target).parents(".cal-popup").length) { // if the target is a child of the pop-up
			__returnFalse = true;
		}
		
		if ($(e.target)[0].tagName == 'A' && $(e.target).text().indexOf('Month') != -1) { // // if the target is one of the 'Month' buttons on the popup
			__returnFalse = true;
		}
		
		if ($(e.target)[0].className) { // if the target is the 'close-cal' div
			if ($(e.target)[0].className.indexOf("close-cal") != -1) {
				__returnFalse = false;
			}
		}
		
		if ($(e.target).parents(".close-cal").length) { // if the target is a child of the 'close-cal' div
			__returnFalse = false;
		}

		if (__returnFalse) {
			return false;
		}
		
		$('body').unbind("click", function () {FC.hideCalendar()});		
		/*@cc_on @*/ 
			/*@if (@_jscript_version <= 5.7) 
				$('.cal-popup', cal).bgiframe().hide();  
			@else @*/ 
				$('.cal-popup', cal).hide(); 
		/*@end @*/
		return false;
	},
	calendar: function(){
		$.getScript("../../module/js/jquery-ui-personalized-1.5.3.packed.js", function(){
			/*@cc_on 
				@if (@_jscript_version <= 5.7) 
					$.getScript('../../module/js/jquery.bgiframe.min.js'); 
				@end 
			@*/
			
			$('.content.calendar').each(function(){
				var theCal = $(this)[0];
				
				function setDates (input) {}
				
				$(".cal-content", theCal).datepicker({ 
					changeMonth: false,
					changeYear: false,
					closeAtTop: false, 
					dateFormat: "dd/mm/yy",
					duration: "", 
					firstDay: 1,
					minDate: '0d',
					maxDate: '+2y',
					mandatory: true,
   					nextText: "Next Month",
					numberOfMonths: 2, 
					dayNamesMin: getDays(),
					monthNames: getMonths(),
					//beforeShow: readLinked(),
					onSelect: function(date) { 
						$('.cal-input', theCal).val(date);
						$('.cal-popup',theCal).toggle();
						updateLinked(date) ;
    				},
					prevText: "Previous Month", 
					rangeSelect: false
				}).children("div").css({ width : "390px",position:"absolute"});
				
				$('.choose-date').show();
				$('.choose-date',this).css({display:'inline-block',verticalAlign:"middle"});
				$(".choose-date", this).bind("click", function (e) {
					$('body').bind("click", function (e) {FC.hideCalendar(e, theCal)});

					/*@cc_on @*/ 
						/*@if (@_jscript_version <= 5.7) 
							$('.cal-popup', theCal).bgiframe().show();  
						@else @*/ 
							$('.cal-popup', theCal).show(); 
					/*@end @*/	
					
					
					// look for the empty row and set a boolean if the row is completely empt
					// difficult to find a way to discern an empty row
					/*
					$('.ui-datepicker-days-row', theCal).each(function () {
						var __isEmpty = true;
						$('td', this).each(function () {
							if (this.firstChild.nodeValue.search(/[0-9]/) != -1) {
								__isEmpty = false;
							}
						});
						//alert(__isEmpty);
					});
					*/
					
					
					return false; // to stop bubbling
				});
				$(".cal-input", this).bind("focus", function(){
					$('body').bind("click", function (e) {FC.hideCalendar(e, theCal)});
					/*@cc_on @*/ 
						/*@if (@_jscript_version <= 5.7) 
							$('.cal-popup', theCal).bgiframe().toggle();  
						@else @*/ 
						$('.cal-popup', theCal).show(); 
					/*@end @*/	
					return false;
				});
			});
		});
		$('head > link').filter(':first').after('<link rel="stylesheet" type="text/css" href="../../module/styles/calendar/ui.datepicker.css" media="screen" />');
	}
}
$(function () { 
	if($(".calendar").length){FC.calendar();}
});

var CONST_DAYS_en = new Array('Su','Mo','Tu','We','Th','Fr','Sa');

var CONST_MONTHS_en = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

var CONST_DAYS_default = new Array('Su','Mo','Tu','We','Th','Fr','Sa');

var CONST_MONTHS_default = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

function getDays() {
	try {
		var days	=	eval("CONST_DAYS_"+currentLocale);
		return days;
	} catch(e1) {
		try {
			var defDays	=	eval("CONST_DAYS_"+defaultLocale);
			return defDays;
		} catch(e2) {
			return eval("CONST_DAYS_default");
		}		
	}
}

function getMonths() {
	try {
		var days	=	eval("CONST_MONTHS_"+currentLocale);
		return days;
	} catch(e1) {
		try {
			var defDays	=	eval("CONST_MONTHS_"+defaultLocale);
			return defDays;
		} catch(e2) {
			return eval("CONST_MONTHS_default");
		}		
	}
}

