var org = {
	'o': false,
	'loadingState': false,
	'baseURI': '/organizer/',
	'commentForm': {
		'o': false,
		'update': function(data){
			if(data.html)
			{
				this.hide();
				org.eventForm.hide();
				org.o.before(data.html);
				org.o.prev().bgiframe();
				this.o = org.o.prev();
				$('#org_close_form').click(function(){
					org.commentForm.hide();
					return false
				});
				org.bindFormSubmit(this.o);
			}
		},
		'hide': function(){
			this.o && this.o.css('position', 'relative').hide().remove();
			this.o = false;
		},
		'toggle': function(data){
			if(this.o)
			{
				this.hide();
				return false
			}
			else
			{
				org.sendGet(data);
				return false
			}
		}
	},
	'eventForm': {
		'o': false,
		'update': function(data){
			if(data.html)
			{
				this.hide();
				org.commentForm.hide();
				org.o.before(data.html);
				org.o.prev().bgiframe();
				this.o = org.o.prev();
				$('#org_close_form').click(function(){
					org.eventForm.hide();
					return false
				});
				org.bindFormSubmit(this.o)
			}
			if(data.datepicker)
			{
				data.datepicker.html && $('td.org_datepicker', this.o).html(data.datepicker.html);
				data.datepicker.data && this.bindDatepicker(data.datepicker.data);
				org.bindLinks.call(this.o);
			}
		},
		'hide': function(){
			this.o && this.o.css('position', 'relative').hide().remove();
			this.o = false;
		},
		'toggle': function(data){
			if(this.o)
			{
				this.hide();
				return false
			}
			else
			{
				org.sendGet(data);
				return false
			}
		},
		'bindDatepicker': function(data){
			this.setDate(data.date);
			$('td.org_datepicker a.tooltip', this.o).each(function(i){
				$(this).click(function(e){
					data.navbar[i].day = $('a.org_numbers_selected div', org.eventForm.o).text();
					org.sendGet(data.navbar[i]);
					return false
				});
			});
			org.bindPopups.call($('a.org_numbers_task', this.o).parent());
			$('a.org_numbers', this.o).each(function(i){
				$(this).click(function(e){
					$('a.org_numbers_selected', org.eventForm.o).removeClass('org_numbers_selected');
					$(this).addClass('org_numbers_selected');
					org.eventForm.setDate(data.numbers[i]);
					return false;
				});
			});
			$('div.tasks', this.o).bgiframe();
		},
		'setDate': function(data){
			$('input.day', this.o).val(data.day);
			$('input.month', this.o).val(data.month);
			$('input.year', this.o).val(data.year);
			$('form input.targeturi').val(org.baseURI + '?day=' + data.day + '&month=' + data.month + '&year=' + data.year);
		}
	},
	'auxMenu': function(){
		return $('div.org_menu');
	},
	'notifier': function(){
		return $('#org_notifier');
	},
	'update': function(data){
		if(data.shade)
		{
			data.shade.html && $('#org_shaded_container').html(data.shade.html);
			org.bindShade();
		}
		if(data.top)
		{
			data.top.html && $('form', this.o).eq(0).replaceWith(data.top.html);
			data.top.html && this.bindLinks.call($('form', this.o).eq(0));
		}
		if(data.calendar)
		{
			data.calendar.html && $('#org_container', this.o).html(data.calendar.html)
			data.calendar.html && this.bindLinks.call($('#org_container'));
			this.bindCalendar()
		}
		if(data.list)
		{
			data.list.html && $('#org_container', this.o).html(data.list.html);
			data.list.html && this.bindLinks.call($('#org_container'));
			data.list.html && this.bindPopups.call($('div.org_tasks', this.o).parent());
			data.list.html && this.bindFormSubmit(this.o);
		}
		if(data.navbar)
		{
			data.navbar.html && $('table.org_navbar tr', this.o).html(data.navbar.html);
			data.navbar.html && this.bindLinks.call($('table.org_navbar tr'));
			data.navbar.data && this.bindNavbar(data.navbar.data);
		}
		if(data.mainmenu)
		{
			data.mainmenu.html && $('#org_main_menu').html(data.mainmenu.html);
//			data.mainmenu.html && this.bindLinks('table.mainmenu tr');
			this.bindMainMenu();
		}
		if(data.eventform)
		{
			this.eventForm.update(data.eventform)
		}
		if(data.auxmenu && data.auxmenu.html)
		{
			this.auxMenu().replaceWith(data.auxmenu.html);
			this.bindAuxItem.call(this.auxMenu());
		}
		if(data.notifier)
		{
			this.notifier().replaceWith(data.notifier.html);
			this.bindNotifier();
		}
		if(data.maximizeuri)
		{
			$('#org_maximize').attr('href', data.maximizeuri);
		}
	},
	'sendGet': function(data){
		sjWidgets.sendRequest.apply(this, [data, 'GET', ''])
	},
	'sendPost': function(data, targeturi){
		sjWidgets.sendRequest.apply(this, [data, 'POST', targeturi])
	},
	'bindFormSubmit': function(context){
		var $targeturi = $('<input type="hidden" class="targeturi" value="' + this.baseURI + '" />');
		$('form', context).unbind('submit').submit(function(){
			var food = {};
			$(':input:not(:checkbox),input:checkbox:checked', this).each(function(){
				food[this.name] = this.value == this.title ? '' : $(this).val()
			});
			var $i = $('input.orgsubmit', context);
			if ($i.length)
			{
				var $o = $('<img />')
					.attr('width', $i.attr('width'))
					.attr('height', $i.attr('height'))
					.attr('src', $i.attr('src').replace(/([.]\w+)$/, '_disabled$1'));
				$i.replaceWith($o);
			}
			sjWidgets.sendRequest.apply(org, [food, this.method, $targeturi.val()]);
			return false
		}).prepend($targeturi);
	},
	'bindMainMenu': function(data){
		$('a', '#org_main_menu').each(function(i){
			$(this).click(
				function(){
					if (i < 4)
						org.sendGet(this.search.substr(1))
					else
						org.eventForm.toggle(this.search.substr(1));
					return false
				}
			)
		});
	},
	'bindNavbar': function(data){
		$('table.org_navbar a', this.o).each(function(i){
			$(this).click(
				function(){org.sendGet(data[i]);return false}
			)
		});
	},
	'bindCalendar': function(){
		this.bindPopups.call($('a.org_numbers_task', this.o).parent());
		$('div.org_tasks', this.o).bgiframe();
	},
	'bindPopups': function(){
		try {
			this.hoverIntent({
					sensitivity:3,
					interval:100,
					over:org.popupOnOver,
					out:org.popupOnOut,
					timeout:200
			});
		} catch(e) {
			this.hover(org.popupOnOver, org.popupOnOut);
		}
	},
	'popupOnOver': function(){
		var pos = $(this).position();
		var pfix = {'top':0, 'left':0};
		var sfix = {'top':0, 'left':0};
		if ($.browser.msie && $(this).hasClass('fixme')) {
			$container = $(this).parent().parent();
			pfix = $container.position();
			sfix.top = $container.scrollTop();
			sfix.left = $container.scrollLeft();
		}
		$('.org_tasks', this)
			.addClass('org_visible')
			.css({top:pos.top+sfix.top-pfix.top,left:pos.left+sfix.left-pfix.left})
	},
	'popupOnOut': function(){
		$('.org_visible', this).removeClass('org_visible')
	},
	'bindSearchInput': function(context){
		$('input.organizer_search', context).focus(function(e){
			if (this.value == this.title)
			{
				this.style.color = "#000000";
				this.value = "";
			}
		}).blur(function(e){
			if (this.value == "" || this.value == this.title)
			{
				this.style.color = "#7d7d7d";
				this.value = this.title;
			}
		}).trigger('blur');
	},
	'bindLinks': function(){
		this.find('a').not('.unbound').click(function(e){
			if ($(this).hasClass('confirm') && !confirm($('span.ctext', this).text()))
				return false;
			org.sendGet(this.search.substr(1));
			return false
		});
	},
	'bindAuxItem': function(){
		org.bindPopups.call($('li', this));
		org.bindLinks.call(this);
	},
	'bindNotifier': function(){
		this.bindAuxItem.call(this.notifier());
		$('a.opener', org.notifier()).each(function(i){
			$(this).click(function(){
				toggle($('.org_notifier_list', org.notifier()).get(i));
				$(this).trigger('blur');
				return false
			});
		}).trigger('click');
	},
	'updateItem': function(){
		var food = {};
		if (!this.length)
			return false;
		$('input:hidden', this).each(function(){
			food[this.name] = this.value;
		});
		org.sendGet(food);
	},
	'updateAuxMenu': function(){
		this.updateItem.call(this.auxMenu());
	},
	'updateNotifier': function(){
		this.updateItem.call(this.notifier());
	},
	'setCookie': function(name, value){
		var expDate = new Date();
		var year = expDate.getYear();
		if (year < 1000) year = year + 1900;
		expDate.setYear(year + 1);
		document.cookie = escape(name) + "=" + escape(value) + "; expires=" + expDate.toGMTString() + "; path=/";
	},
	'scrollUp': function(){
		var s = $(window).scrollTop();
		if (s > org.o.position().top)
			$(window).scrollTop(org.o.position().top - 10);
	},
	'shade': function(){
		$('#org_unshaded').hide();
		$('#org_shaded').show();
		org.setCookie('org_shade', '1');
		return false;
	},
	'unshade': function(){
		$('#org_shaded').hide();
		$('#org_unshaded').show();
		org.setCookie('org_shade', '0');
		return false;
	},
	'bindShade': function(){
		$('#org_shaded').length && org.bindLinks.call($('#org_shaded'));
	},
	'setLoadingState': function(s){
		if(s)
			$('#org_loadimg').show();
		else
			$('#org_loadimg').hide();
		this.loadingState = s;
	}
};
$(function(){
	org.o = $('#org');
	org.o.length && org.bindLinks.call(org.o);
	org.o.length && org.bindFormSubmit(org.o);
	org.o.length && org.bindMainMenu();
	org.bindShade();
	if (!org.o.length) {org.o = $("td.content");org.eventForm.o = $("#org_form")}
	org.bindSearchInput(org.o);
	org.bindPopups.call($('div.org_tasks', org.o).parent());
	org.bindCalendar();
	org.bindAuxItem.call(org.auxMenu());
	org.bindNotifier();
	$('a.evtlnk', org.notifier()).click(org.unshade);
	$('a.tooltip', org.o).children('div').bgiframe();
});
