var ActivityView = jQuery.Class.create({
	init: function(){
		this.divId = null;
		this.dialog = {};
		this.templateLoaded = {
			'event': false,
			'phone': false,
			'task' : false
		};
		this.month = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug",
                    "Sep","Oct","Nov","Dec");
	},

	show: function (id, type) {
		//console.log('id='+id);
		var parent = this;
		$j.ajax({
				url: '/admin/panel/calendar/getEventInfo/'+id+'/'+type,
				type: "get",
				dataType: "json",
				success: function(data, textStatus) {
					//console.log(data);
					var type = 'event';
					if(!parent.templateLoaded[type]) {
						parent.getTemplate(type);
					}
					parent.updateFields(type, data);
					$j(parent.dialog[type]).dialog('open');
					parent.dialog[type].style.display = 'block';
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					//console.log('error');
					alert('Information not responded! Wrong server responce.');
				}
			}
		);
	},

	getTemplate: function(type) {
		var parent = this;
		$j.ajax({
				async: false,
				url: '/admin/panel/calendar/getViewWindow/'+type,
				dataType: "html",
				type: 'get',
				success: function(html, textStatus) {
					var contentDiv = document.createElement('DIV');
					contentDiv.innerHTML = html;
					contentDiv.style.display = 'none';
					parent.divId = type+'ViewWindow_' + parent.getUniqueId();
					contentDiv.id = parent.divId;
					document.body.appendChild(contentDiv);
					parent.dialog[type] = $j('#'+parent.divId)[0];
					var dlgSettings = {
						width: 625,
						height: 450,
						modal: true,
						position: 'center',
						title: '<span style=font-size:11>Event Details...</span>',
						zIndex: 3999,
						autoOpen: false,
						buttons: { "Close": function() { $(this).dialog("close"); } }
					};
					$j(parent.dialog[type]).dialog(dlgSettings);
					parent.definePlaces(type);
				}
			}
		);
		parent.templateLoaded[type] = true;
	},

	definePlaces: function(type) {
		var dlg = $j(this.dialog[type]);
		var places = this.dialog[type][places] = {};
		places.description = dlg.find('.vw_description');
		places.location = dlg.find('.vw_location');
		places.date = dlg.find('.vw_date');
		places.time = dlg.find('.vw_time');
		places.ministry = dlg.find('.vw_ministry');
		places.ministryRow = dlg.find('.vw_row_ministry');
	},

	updateFields: function(type, data) {
		//console.log('data=');
		//console.log(data);
		var dlg = $j(this.dialog[type]);
		var places = this.dialog[type][places];

		places.description.html(data.eventdescription);
		places.location.html(data.locationnotes);


		if (data.eventtime.indexOf(':') >0) {
			parts = data.eventtime.split(':');if (parts[0] > 12) {
				parts[0] = parts[0] - 12;
				ampm = 'pm';
			} else if (parts[0] < 12)
				ampm = 'am';
			else if (parts[0] == '00')
				ampm = 'am';
			else ampm = 'pm';
			data.eventtime = parts[0]+':'+parts[1]+" "+ampm;
		}

		places.time.html(data.eventtime);
		//alert('['+data.ministrytitle+']');
		if (data.ministrytitle != null)
			places.ministryRow.show();
		else
			places.ministryRow.hide();
		places.ministry.html(data.ministrytitle);
		switch(data.type) {
			case 'single':
				var eventDate = strToDate(data.eventdate);
				places.date.html(this.getFormatedDate(eventDate));
			break;
			case 'daily':
			case 'weekly':
				var splitedEvenDates = data.eventdate.split('|');
				var beginDate = this.getFormatedDate(strToDate(splitedEvenDates[0]));
				var endDate = this.getFormatedDate(strToDate(splitedEvenDates[1]));
				places.date.html(beginDate+' ~ '+endDate);
			break;
		}
	},

	getFormatedDate: function(eventDate) {
		return this.month[eventDate.getMonth()]+' '+eventDate.getDate()+', '+eventDate.getFullYear();
	},

	getDayOfWeek: function(dayNumber) {
		switch(dayNumber) {
		case 0:
			return 'Sunday';
		case 1:
			return 'Monday';
		case 2:
			return 'Tuesday';
		case 3:
			return 'Wednesday';
		case 4:
			return 'Thursday';
		case 5:
			return 'Friday';
		case 6:
			return 'Saturday';
		}
	},

	getUniqueId: function() {
		return ((new Date()).getTime() + "" + Math.floor(Math.random() *1000000)).substr(0, 18);
	}
});
