/**
 Source code packer url:
 http://dean.edwards.name/packer/
 */

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function NFW_Event(){
	if (NFW_Event.caller != NFW_Event.getInstance) {
		throw new Error("There is no public constructor for NFW_Event.");
	}
	var home_id=10;
	var _self=this;
	var initialized=false;
	var first_run;
	var _UIholder,UIholderDetails,UIholderUpcoming_UI,_UId,_UIu;
	var month,year;
	var btnPrev,btnNext,btnLoad;
	var autoload=false;
	
	//```````````````````````````````````````````````````````````````````````
	_self.Init=function(args){
		if (typeof(_self.initialized)==undefined) _self.initialized=false;
		if (_self.initialized)return;
		_self.first_run=true;
		
		if (NFWJS.page.params.length==0){
			_self.Initialized(args);
			return;
		}
	};
	//```````````````````````````````````````````````````````````````````````
	_self.Initialized=function(args){
		_self.initialized=true;
		_self.custom_header=false;
		
		if (args.autoload!=undefined)_self.autoload=args.autoload;
		if (args.custom_header!=undefined)_self.custom_header=args.custom_header;

		if (args.month_view!=undefined)_self.InitCalendarMonthView(args.month_view);
		//if (args.month_details!=undefined)_self.InitCalendarMonthDetails(args.month_details);
		
		//$(function(){NFW.FunctionNS.GotoAnchor();});
		_self.first_run=false;
	};
	//```````````````````````````````````````````````````````````````````````
	_self.InitCalendarMonthView=function(ui_holder_id){
		//if (!_self.initialized) alert('Calendar has not been initialized.');
		_self._UIholder=$('#'+ui_holder_id);
		
		_self.RenderSkin();
		_self.LoadData();
	};
	//```````````````````````````````````````````````````````````````````````
	_self.ResetMonth=function(){
		for(row=1;row<=6;row++){
			r=$('tr[r="'+row+'"]',_self._UI); //find the row
			for (c=1;c<=7;c++){
				$('td[c="'+c+'"]',$(r))
					.html('')
					.attr('n','')
					.removeClass('currentmonth')
					.addClass('othermonth')
				;
			}
		}
	};
	//```````````````````````````````````````````````````````````````````````
	_self.LoadMonth=function(){
		
		_self.ResetMonth();
		// dim: days in month
		dim=NFW.FunctionNS.GetDaysInMonth(_self.year,_self.month);
		// fwd: first week day of the month 
		fwd=NFW.FunctionNS.GetFirstDayInWeek(_self.year,_self.month);
		
		col=fwd;
		for(day=1,row=1;(row<=6 && day<=dim);row++){
			var r=$('tr[r="'+row+'"]',_self._UI); //find the row
			for (c=col;c<=7;c++){
				$('td[c="'+c+'"]',$(r))
					.html(day)
					.attr('n',day++)
					.removeClass('othermonth')
					.addClass('currentmonth');
				if(day>dim)break;
			}col=1;
		}
	};
	//```````````````````````````````````````````````````````````````````````
	_self.LoadData=function(year,month){
		
		d=new Date();
		
		if (year==undefined || month==undefined){
			if (_self.first_run && _self.year==undefined){_self.year=year=d.getFullYear();}
			if (_self.first_run && _self.month==undefined){_self.month=month=d.getMonth()+1;}
		}else{
			_self.year=year;
			_self.month=month;
		}
		
		//_self.year=_self.year.split('').shift().join('');
		_self.LoadMonth();
		
    
    
		params='year='+_self.year+'&month='+_self.month;
		$.ajax({
			type: 'POST',
			url: '/ajax/game.calendar.php',
			data: params,
			success: function(data){
				$('#calendar-month').html(NFW.FunctionNS.GetMonthName(_self.month));
				$('#calendar-year').html(_self.year);
	
				data=eval(data);
				for(var i=0;i<data.length;i++){
					cell=$('td[n="'+data[i].day+'"]',_self._UI);
					if (data[i]['home_id']==home_id) img='/images/interface/icons/home-games.gif';
					else img='/images/interface/icons/away-games.gif';
					$(cell).html('<a href="/kelowna-rockets/game-details.php?game_id='+data[i]['game_id']+'"><img src="'+img+'" width="18" height="18" title="'+data[i]['visiting']+' @ '+data[i]['home']+'" /></a>');
				}
				
				if (!_self.autoload){
					if (data.length==0){
						$('#'+_self.btnLoad).hide();
					}else{
						$('#'+_self.btnLoad).show(function(){$(this).slideDown('fast')});
					}
				}
			}
		});
	};
	//```````````````````````````````````````````````````````````````````````
	_self.RenderSkin=function(){
		//alert('1:'+_self.month);
		UIholder=_self._UIholder;
		uiid='event-calendar';
		$(UIholder).html('<table width="90%" border="0" cellpadding="4" cellspacing="1" align="center" id="'+uiid+'"></table>');
		UI=_self._UI=$('#'+uiid);
		
		_self.btnPrev='btnPrevMonth';
		_self.btnNext='btnNextMonth';
		_self.btnLoad='btnLoadMonth';
		
		// add the first row, title of the calendar and prev/next month links.
		if (_self.custom_header == false) {
			$(UI).append(''
				+'<tr class="header">'
			  	+'<td><a href="javascript:;" id="'+_self.btnPrev+'"><img src="'+NFWJS.path.tdir+'/images/arrow-left.gif" border="0" /></a></td>'
	            +'<td width="205" valign="middle" colspan="5"><span id="calendar-month">Month</span> <span id="calendar-year">Year</span></td>'
				+'<td><a href="javascript:;" id="'+_self.btnNext+'"><img src="'+NFWJS.path.tdir+'/images/arrow-right.gif" border="0" /></a></td>'
		    	+'</tr>'
		    	+'');
		    // assign the classes to header cells.
		    $('.header td',UI).addClass('currentmonth');
		}	    
	    // add the second line, week names
	    $(UI).append('<tr class="calander-days"><td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td></tr>');
	    
	    // add 6 more empty lines for the month-days, these will be filled in with relevant day names later on.
	    for(var i=1;i<=6;i++){
	    	$(UI).append(''
	    		+'<tr class="days" r="'+i+'">'
	    		+'<td c="1" n=""></td>'
	    		+'<td c="2" n=""></td>'
	    		+'<td c="3" n=""></td>'
	    		+'<td c="4" n=""></td>'
	    		+'<td c="5" n=""></td>'
	    		+'<td c="6" n=""></td>'
	    		+'<td c="7" n=""></td>'
	    		+'</tr>'
	    		+'');
	    }
	    
	    // assign the classes to empty cells.
	    $('.days td',UI).addClass('othermonth');
		
		// if autoload is not enabled then display Load Calendar button
		if (!_self.autoload) $(UI).append(''
			+'<tr class="footer">'
		  	+'<td></td>'
            +'<td width="205" valign="middle" colspan="5"><a href="javascript:;" id="'+_self.btnLoad+'">Load Calendar</a></td>'
			+'<td></td>'
	    	+'</tr>'
	    	+'');

	    // bind the events to the buttons
	    $('#'+_self.btnPrev).click(function(){
			pm=parseInt(_self.month)-1;
			py=_self.year;
			if (pm==0){pm=12;py--;}
			_self.LoadData(py,pm);
			//if (_self.autoload)_self.LoadDetails();
		});
	    $('#'+_self.btnNext).click(function(){
			nm=parseInt(_self.month)+1;
			ny=_self.year;
			if (nm==13){nm=1;ny++;}
			_self.LoadData(ny,nm);
			//if (_self.autoload)_self.LoadDetails();
		});
	};
	//```````````````````````````````````````````````````````````````````````
};
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
NFW.PageNS.Event=NFW_Event;NFW_Event.__instance__=null;NFW_Event.getInstance=function(){if (this.__instance__==null){this.__instance__=new NFW_Event();}	return this.__instance__;};
