﻿Namespace("vc.Calendar");

/*************************
 * Default Configuration *
 *************************/
var defaultConfig = {
	defaultView: 'monthWidget',
	startOfWeek: 0, // Sunday
	dayVisibilityMask: [ 1, 1, 1, 1, 1, 1, 1 ],
	showDaysOfWeekHeader: true,
	showDaysOfWeekFooter: false,
	showDatesOfOverlappingMonths: false,
	showCalendarTitle: true,
	headerFooterDayOfWeekFormat: "ddd",
	headerDateFormat: "MMMM yyyy",
	
	monthNames: ['January','February','March','April','May','June','July','August','September','October','November','December'],
	monthNamesShort: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
	dayNames: ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
	dayNamesShort: ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],
	
	theme: false,
	buttonIcons: {
		prev: 'circle-triangle-w',
		next: 'circle-triangle-e'
	}
};

vc.DateTime.setConfig(defaultConfig);

// function for adding/overriding defaults
vc.Calendar.setDefaults = function(d) {
	$.extend(true, defaultConfig, d);
};

/* .vccCalendar jQuery function
-----------------------------------------------------------------------------*/

$.fn.vcCalendar = vc.Calendar.Calendar = function (config) {
	// method calling
	if (typeof config == 'string') {
		var args = Array.prototype.slice.call(arguments, 1),
				res;
		this.each(function () {
			var data = $.data(this, 'vc.Calendar');
			if (data) {
				var meth = data[config];
				if (meth) {
					var r = meth.apply(this, args);
					if (res === undefined) {
						res = r;
					}
				}
			}
		});
		if (res !== undefined) {
			return res;
		}
		return this;
	}

	// initialize options
	config = $.extend(true, {},
		defaultConfig,
	//(config.isRTL || config.isRTL === undefined && defaults.isRTL) ? rtlDefaults : {},
		config
	);
	var tm = config.theme ? 'ui' : 'vc'; // for making theme classes

	this.each(function () {
		// References
		var DateTime = vc.DateTime,
			views = vc.Calendar.Views;

		var _elCal = this,
			elCal = $(_elCal),
			calContent = $("<div class='vc-Calendar-Content " + tm + "-widget-content' style='position:relative' />").prependTo(_elCal),
			suggestedViewHeight,
			today = new Date(),
			date = new Date(),
			viewName,
			view,
			viewInstances = {},
			eventSource = null;

		function render() {
			elCal.html("");
			elCal.append(view.render());
			getEventData();
		}

		function nextPeriod() {
			view.nextPeriod();
			render();
		}

		function previousPeriod() {
			view.previousPeriod();
			render();
		}

		function getEventData() {
			if (eventSource) {
				$.ajax({
					type: "GET",
					url: eventSource,
					dataType: "xml",
					success: eventDataReceived
				});
			}
		}

		function eventDataReceived(data) {
			view.appendXmlEvents(data);
		}



		// Initialization
		if (elCal.metadata()) {
			eventSource = elCal.attr('data-vc-calendar-eventSource');
		}

		if (!elCal.hasClass("vc-Calendar"))
			elCal.addClass("vc-Calendar");

		if (!elCal.hasClass("vc-moduleSurface"))
			elCal.addClass("vc-moduleSurface");

		if (config.theme) {
			elCal.addClass('ui-widget');
		}

		DateTime.setYMD(date, config.year, config.month, config.date);

		// set view
		view = new views.View(elCal, config);
		render();

		$(".vc-Calendar-Button-PreviousPeriod", elCal).live("click", function () {
			previousPeriod();
		});

		$(".vc-Calendar-Button-NextPeriod", elCal).live("click", function () {
			nextPeriod();
		});
	});

	return this;
};
