var defacto_datepicker = function()
{	
	var defaultdate = new Date();
	
	this.today = new Date();
	
	this.firstload = true;	
	
	this.link_clicked = false
	
	this.months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];
	
	this.current_date = { month: this.months[this.today.getMonth()], year: this.today.getFullYear() } // gets changed anyway
	
	this.bookmark = '';
	
	var self = this;
	
	var startat = 0;
	
	this.hash = 
	{
		jan: 'January',
		feb: 'Febuary',
		mar: 'March',
		apr: 'April',
		may: 'May',
		jun: 'June',
		jul: 'July',
		aug: 'August',
		sep: 'September',
		oct: 'October',
		nov: 'November',
		dec: 'December'
	};	
			
	$('a').click(function(){		
		self.bookmark = self.bookmark || ((self.current_date.day ? self.current_date.day + ',' : '') + self.current_date.month +',' + self.current_date.year);
		self.create_bookmark(self.bookmark);	
	});
	
	$.historyInit(function(hash){		
		if(hash && !self.link_clicked)
		{	
			//alert('Loading bookmark with hash');			
			if(hash.split(',').length > 2)
			{				
				defaultdate = self.parse_date(hash);
				self.current_date.day = defaultdate.getDate();				
			}
			else
			{				
				defaultdate = self.parse_date('1,'+hash);
			}
			
			self.firstload = false;				
			
			// setting the date for datepicker
			$('#datepicker').datepicker('setDate', defaultdate);
			
			// manualy highlighting the calendar and keeping the day
			self.highlight_calendar(defaultdate.getFullYear(), defaultdate.getMonth()+1, true);
		}
		
		if(!hash)
		{			
			window.location='default.asp';
		}
	});	
	
	this.months.unshift(' ');
	
	// creating datepicker	
	$("#datepicker").datepicker
	({
		onChangeMonthYear: function(year, month, inst)
		{ 
			setTimeout(function(){ self.highlight_calendar(year, month);}, 250); 			
		},
		defaultDate: defaultdate		
	});	
}

defacto_datepicker.prototype.highlight_calendar = function(year, month, keep_day)
{
	var self = this;	
	
	var cdate = new Date();		
	/*if(cdate.getMonth() !== month - 1)
	{
		$('.ui-state-active').removeClass('ui-state-active');
	}else
	{
		if(cdate.getDay() !== this.current_date.day)
		{
			$('.ui-state-active').removeClass('ui-state-active');
			
		}
	}*/
	
	// restoring highlight back to today's date
	$('.ui-datepicker-current-day').children().removeClass('ui-state-active');
	$('.ui-datepicker-today').children().addClass('ui-state-active');
	
	month = this.months[month];
	
	this.current_date.month = month;
	this.current_date.year = year;		
	
	if ( ! this.firstload)
	{		
		this.update_header(this.current_date.day, this.hash[month]);				
	}
	
	if (typeof(settings[year]) === 'undefined' || typeof(settings[year][month]) === 'undefined')
	{

		// if it is the first time we loading the page then dont hide anything
		// if there are no concerts for current date
		// just display all of them =)		
		if ( ! this.firstload) { 
			$('.featured-concert').hide(); 
			$('.noevents').css("display","block");
		}
		$('.retrospectmessage').hide();
		this.firstload = false;
		return false;
	}
	
	var days = settings[year][month];
	
	var selector = $('table.ui-datepicker-calendar tbody tr td a').unbind('click');
			
	selector.parent().removeClass('active-date');						
		
	$.each(days, function(i, value)
	{
		// Loop through all of the days in this month, extracting the DOM nodes that match our days
		selector.each(function()
		{			
			if ($(this).html() == i)
			{				
				$(this).click(function(){					
					self.current_date.day = $(this).html();
					self.find_concerts(
						$(this).html(), // day
						$('span.ui-datepicker-month').html().toLowerCase().substr(0,3), //month
						$('span.ui-datepicker-year').html() //year
					);
					return false;
				}).addClass('active-date');				
			}
		});
	});
	
	if ( ! this.firstload)
	{
		var day = this.current_date.day ? '.day-' + this.current_date.day : '';

		var shown = $('.featured-concert').hide().filter('.month-'+month+'.year-'+year+day).show();

				
		if ( ! shown.length)
		{
			$('.noevents').css("display","block");
		}
		else
		{
			$('.noevents').css("display","none");
		}
		
		// resetting the day to allow user to choose months
		if(day && !keep_day){ this.current_date.day =''; }
	}
	
	this.firstload = false;
	
	$('table.ui-datepicker-calendar tbody tr td a').click(function(){ return false; });	
}

defacto_datepicker.prototype.find_concerts = function(day, month, year)
{	
	var self = this;
	var concerts = $('.featured-concert').filter('.month-'+month+'.year-'+year+'.day-'+day);
	var bookmark = '';
	
	// got more then one concert
	/*if(concerts.length > 1)
	{*/		
		$('.featured-concert').hide();
		concerts.show();
		
		// setting the bookmark in case user leaves the page
		this.set_bookmark(self.current_date.day + ',' + self.current_date.month +',' + self.current_date.year);
		
		this.update_header(this.current_date.day, this.hash[month]);
		// reset the day
		this.current_date.day ='';		
	/*}
	else
	{	
		this.create_bookmark(self.current_date.month +'-' + self.current_date.year);
		window.location='default.asp?id='+settings[year][month][day];		
	}*/
}

// sets bookmark so when user leaves the page bookmark gets created to remember users position in the calendar
defacto_datepicker.prototype.set_bookmark = function(bookmark)
{		
	this.bookmark = bookmark;
}

// actually creates the bookmark in users history
defacto_datepicker.prototype.create_bookmark = function(bookmark)
{
	// setting the special flag so event wont fire
	this.link_clicked = true;
	$.historyLoad(bookmark);
	this.link_clicked = false;
}

defacto_datepicker.prototype.reset_day = function()
{
	// reset the day
	this.current_date.day ='';
}

defacto_datepicker.prototype.parse_date = function(date)
{
	var utime = Date.parse(date);	
	var d = new Date();
	d.setTime(utime);	
	return d;
}

defacto_datepicker.prototype.update_header = function(day, month)
{	
	var header = $('span.monthheader');
	header.html((day ? day + ' ': '') + month);
	if(settings.regionID && settings.regionID == 1)
	{
		header.prev().html('All regions');
	}
}
