var calendar = {

	arrMonth : [ 'január', 'február', 'március', 'április', 'május', 'június', 'július', 'augusztus', 'szeptember', 'október', 'november', 'december' ],

	generate : function( date ) {
		this.init( date );

		this.generateHead();
	//	this.generateWeeks();
		this.generateBody();

		return this.htmlContent;

	},

	init : function( date ) {

		if ( date == null ) {
			this.date = new Date();
		} else {
			this.date = date;
		}

		this.today = new Date();

		this.day = this.date.getDate();
		this.month = this.date.getMonth();
		this.year = this.date.getYear();

		this.act_month = new Date( this.year, this.month, 1 );
		this.next_month = new Date( this.year, this.month + 1, 1 );
		this.next_month_y2k = new Date( this.y2k(this.year), this.month + 1, 1 );
		this.previous = new Date( this.y2k(this.year), this.month - 1, 1 );
		this.next_day = new Date( this.y2k(this.year), this.month, this.day + 1 );
		this.previous_day = new Date( this.y2k(this.year), this.month, this.day - 1 );

		this.first_week_day = this.act_month.getDay() + 1;
		
		/*@cc_on
		//jajajajajajaj

		if (this.first_week_day > 1) {
			this.first_week_day -= 2;	
		} else if ( this.first_week_day == 1 ) {
			this.first_week_day = 6;
		} else {
			this.first_week_day = 5;
		}
		
		@*/

		
		this.days_in_act_month = Math.round((this.next_month.getTime() - this.act_month.getTime()) / (1000 * 60 * 60 * 24));
		
		this.firstDayWeekNum = this.getWeekNum();
	
	},

	getWeekNum : function( date ) {

		if ( date == null ) {
			var year = this.y2k(this.year);
			var when = new Date( year, this.month, 1 );
		} else {
			var year = this.y2k( date.getYear());
			var when = new Date( year, date.getMonth(), 1 );
		}
    	
		var newYear = new Date(year,0,1);
		var modDay = newYear.getDay();

		if (modDay == 0) modDay=6; else modDay--;

		var daynum = ((Date.UTC(this.y2k(year),when.getMonth(),when.getDate(),0,0,0) - Date.UTC(this.y2k(year),0,1,0,0,0)) /1000/60/60/24) + 1;

		if (modDay < 4 ) {
			var weeknum = Math.floor((daynum+modDay-1)/7)+1;
		} else {
			var weeknum = Math.floor((daynum+modDay-1)/7);
			if (weeknum == 0) {
				year--;
				var prevNewYear = new Date(year,0,1);
				var prevmodDay = prevNewYear.getDay();
				
				if (prevmodDay == 0) prevmodDay = 6; else prevmodDay--;
				if (prevmodDay < 4) weeknum = 53; else weeknum = 52;
			}
		}
		
		return + weeknum;
	},

	y2k : function (number) {
		return (number < 1000) ? number + 1900 : number;
	},

	generateHead : function() {

		this.htmlContent = 	"<div class=\"mrgbottom clr\"> \
						        <a href=\"javascript: void(0)\" class=\"scell icoinfo\" title=\"további információ\"></a> \
						        <div class=\"scell hdate\">" + this.arrMonth[this.month] + ", " + this.y2k(this.year)  + "</div> \
						        <a href=\"javascript: fn8_hide_layer_dateselect()\" class=\"scell close\" title=\"ablak bezárása\"></a> \
						    </div> \
						    <div class=\"mrgbottom clr\"> \
						        <a href=\"javascript: calendarHelper.jumpToDate( new Date( " + this.y2k(this.previous.getYear()) + ", " + (this.previous.getMonth()) + ", 1 ) );\" class=\"scell fback\" title=\"egy hónappal vissza\"></a> \
						        <a href=\"javascript: calendarHelper.jumpToDate( new Date(" + this.y2k(this.previous_day.getYear()) + ", " + this.previous_day.getMonth() + ", " + this.previous_day.getDate() + ") );\" class=\"scell back\" title=\"egy nappal vissza\"></a> \
			        			<div class=\"scell dateday\">" + ( (this.day == this.today.getDate()) ? "ma" :  this.day + ".") + "</div> \
						        <a href=\"javascript: calendarHelper.jumpToDate( new Date(" + this.y2k(this.next_day.getYear() ) + ", " + this.next_day.getMonth() + ", " + this.next_day.getDate() + ") );\" class=\"scell forward\" title=\"egy nappal előre\"></a> \
						        <a href=\"javascript: calendarHelper.jumpToDate( new Date( " + this.y2k(this.next_month_y2k.getYear()) + ", " + ( this.next_month_y2k.getMonth() ) + ", 1 ) );\" class=\"scell fforward\" title=\"egy hónappal előre\"></a> \
						    </div> \
						    <div class=\"clr hdays\"> \
						        <div>hét</div> \
						        <div>h</div> \
						        <div>k</div> \
						        <div>sz</div> \
						        <div>cs</div> \
						        <div>p</div> \
						        <div>szo</div> \
						        <div>v</div> \
						    </div>";
	},

	generateWeeks : function () {
		
		this.htmlContent += "<div class=\"fleft\" style=\"margin-right: -1px\">";


		act_month = new Date( this.y2k(this.year), this.month, 1 );

		var modNum = act_month.getDay();
		if ( modNum == 0 ) {
			modNum = 6;
		} else {
			modNum--;
		}

		var weeknum = this.firstDayWeekNum + Math.ceil( ( this.days_in_act_month + modNum ) / 7 );

		for ( var i = this.firstDayWeekNum; i < weeknum; i++ ) {
			this.htmlContent += "<div style=\"_margin-left: 2px\">" + i + "</div>";
		}

		this.htmlContent += "</div>";

	},

	generateBody : function() {

		act_month = new Date( this.y2k(this.year), this.month, 1 );

		var modNum = act_month.getDay();
		if ( modNum == 0 ) {
			modNum = 6;
		} else {
			modNum--;
		}

		var weeknum = this.firstDayWeekNum + Math.ceil( ( this.days_in_act_month + modNum ) / 7 );
		var dayCount = 1;	
		var week_day = 0;
		var untl = 7;

		/*@cc_on
		//var week_day = 1;
		//var untl = 8;
		@*/

		for ( var i = this.firstDayWeekNum; i < weeknum; i++ ) {

			this.htmlContent += "<div class=\"row clr\">";

			//A het szama:
			this.htmlContent += "<span class=\"weeknum\">" + i + "</span>";
			
			for ( var d = 0; d < 7; d++ ) {

				if ( i == this.firstDayWeekNum && this.first_week_day < untl && week_day < this.first_week_day ) {
				
					this.htmlContent += "<span></span>";
					week_day++;
					
				} else if ( i == weeknum-1 && dayCount > this.days_in_act_month ) {
				
					this.htmlContent += "<span></span>";

				} else {

					if (this.day == dayCount ) {	
						this.htmlContent += "<a href=\"javascript: calendarHelper.fillValue('" + this.y2k(this.year) + "-" + (( this.month + 1 < 10 ) ? '0'+(this.month+1) : (this.month+1)) + "-" + dayCount + "'); fn8_hide_layer_dateselect();\" class=\"selected\" title=\"Nap kiválasztása: " + dayCount + ".\" onmouseover=\"calendarHelper.showDay( new Date(" + this.y2k(this.year)  + ", " + this.month + ", "+ dayCount +") )\" onmouseout=\"calendarHelper.clearDay();\">" + dayCount + "</a>";
					} else {
						this.htmlContent += "<a href=\"javascript: calendarHelper.fillValue('" + this.y2k(this.year) + "-" + (( this.month + 1 < 10 ) ? '0'+(this.month+1) : (this.month+1)) + "-" + dayCount + "'); fn8_hide_layer_dateselect();\" title=\"Nap kiválasztása: " + dayCount + ".\" onmouseover=\"calendarHelper.showDay( new Date(" + this.y2k(this.year)  + ", " + this.month + ", "+ dayCount +") )\" onmouseout=\"calendarHelper.clearDay();\">" + dayCount + "</a>";
					}

					dayCount++;
					
				} //endIf

				
				
			} //endFor - weekdays

			this.htmlContent += "</div>";
	
		}
		
		this.htmlContent += "<div id=\"fn8_dateselect_foot\" class=\"foot\"></div>";
		this.htmlContent += "</div>";

	}

}

var calendarHelper = {

	arrWeekDay : [ 'vasárnap', 'hétfő', 'kedd', 'szerda', 'csütörtök', 'péntek', 'szombat' ],

	inputId : "",

	fillValue : function( dateStr ) {
		$( 'fn8_archiv_dateselect_' + this.inputId ).value = dateStr;
	},

	jumpToDate : function( date ) {
		$('layer_dateselect_copy').innerHTML = calendar.generate( date );
	},
	
	showDay : function( date ) {
		$('fn8_dateselect_foot').innerHTML = calendar.arrMonth[date.getMonth()] + " " + date.getDate() + ", " + this.arrWeekDay[ date.getDay() ];
	},

	clearDay : function() {
		$('fn8_dateselect_foot').innerHTML = "";
	}

}

