$(function() {
	// DATE DROP-DOWN
	$('#offset').change(function() {
		$('#date-select').submit();
	});
	PopulateDatePicker($('#offset optgroup option'));
	
   $(".toggle-version").click(function(){
		$("#main-content").toggleClass("js-version").toggleClass("text-version");
		$(this).text(($("#main-content").hasClass("text-version") ? "Full" : "Text") + " version");
   }).show();

	$('#main-content').addClass("js-version");
});

//*** DatePicker populate from DropDownList
function PopulateDatePicker(obj){
	$('#date-picker-holder').load('/tfl-global/scripts/datepick/datepick-template.html', function() { //this is the template for the calendar
		$('.datepick-trigger,.datepick-close').click(function() {
		  $('#datepick-div').toggle();
		}).find(":first").click();
		var _items = [], _index = 0, _months = [], _formAction = $('#date-select').attr('action'), i = 0;
		$(obj).each(function(index) {
			var _data = $(this).text().split(' ',3);	// date is in format ddd dd yyyy
			var item = {
				"offset":		$(this).val(),
				"weekDay":		_data[0],
				"monthDay":		_data[1],
				"month":			_data[2],
				"weekDayInt":	GetDayIndex(_data[0])
			}
			_items.push(item);
			if (index == 0 || item.monthDay == 1) _months.push(item.month);
		});
		$('#datepick tbody tr td a').each(function(index) {
			if(i < _items.length && _items[i].weekDayInt == _index) {
				$(this).text(_items[i].monthDay);
				$(this).attr("href", _formAction + "?offset=" + _items[i++].offset);
			}
			if (_index++ == 6) _index = 0;
		});
		$('.datepick-header').text(_months.join("/"));
		$('.cal-closures').html($('.sixmonth').html());
	});
}
function GetDayIndex(day){
	for (var i = 0, _daysOfWeek = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ], limit = _daysOfWeek.length; i < limit; ++i)
		if (day == _daysOfWeek[i]) return i;
	return 0;
}
//*** /DatePicker populate from DropDownList
