//  CONTROLLER
var C = {
	init: function()
	{
		if($('canvas'))
		{
			E.startPage();
		}	
		else
		{
			E.mainMenu();
			E.mainFoot();
			C.setFeedback();
			C.links.external();
		}			
	},
	
	links: {
		external: function()
		{
			$$('a.ext').each(function(a){
				a.observe('click', function(evt){
					evt.stop();
					nw = window.open(evt.element().readAttribute('href'));
					return false;
				});
			});
		}
	},
	
	setFeedback: function()
	{
		if(!(typeof X).match('undefined'))	// feedback settings
		{
			// comment form observer
			$('comment').observe('click', function(evt){
				evt.stop();
				if(!$('cmnt_box').visible())
				{
					new Ajax.Request('/xhr/comment/form', {parameters:{link:X.link, id:X.id}});
				}
			});
			// comment reply form observer
			$$('a.crep').each(function(reply){
				reply.observe('click', function(evt){
					evt.stop();
					rel = evt.element().readAttribute('rel');
					new Ajax.Request('/xhr/comment/form', {
						parameters:{link:X.link, id:X.id, papa:rel}
					});
				});
			});
			// rating form observer
			$('rating').observe('click', function(evt){
				evt.stop();
				if(!$('rate_box').visible())
				{
					new Ajax.Request('/xhr/rating/form', {parameters:{link:X.link, id:X.id}});
				}
			});
			// tag form observer
			$('tag').observe('click', function(evt){
				evt.stop();
				if(!$('tags_box').visible())
				{
					new Ajax.Request('/xhr/tag/form', {parameters:{link:X.link, id:X.id}});
				}
			});
			// show/hide nested comments
			E.toggleComments();	
		}		
	},
	
	dialog: {
		opts: {
			position: 'absolute',
			top: '0', 
			left: '0',
			width: '250px',	
			height: 'auto',
			zIndex: 10000
		},
		show: function()
		{
			$('dialog_box').show();
			return C.dialog;			
		},
		hide: function()
		{
			$('dialog_box').hide();
			return C.dialog;
		},
		set: function(head, text, opts)
		{
			if((typeof opts).match('object'))
			{
				for(o in opts)
				{
					C.dialog.opts[o] = opts[o];
				}
			}
			var dBox;
			if(!(dBox = $('dialog_box')))
			{
				dBox = new Element('div', {'id':'dialog_box', 'style':'display:none'}).update(
					'<p id="dialog_close">[X]</p><p id="dialog_box_head">&nbsp;</p><p id="dialog_box_body">&nbsp;</p>');
				document.body.appendChild(dBox);
			}
			$('dialog_box_head').update(head);
			$('dialog_box_body').update(text);
			$('dialog_close').observe('click', C.dialog.hide);
			$('dialog_box').setStyle(C.dialog.opts);
			return C.dialog;			
		}
	},
	
	list: {
		formid: null,
		listid: null,		
		create: function(formid, listid)
		{
			C.list.formid = formid || 'form';
			C.list.listid = listid || 'list';
			$(C.list.formid).stopObserving();
			$(C.list.formid).observe('submit', function(evt){
				evt.stop();
				C.list.submit();
				return false;	
			});
			$('list_submit').stopObserving();
			$('list_submit').observe('click', C.list.submit);
			return C.list;			
		},
		set: function(request)
		{
			f = $(C.list.formid);
			f.setAttribute('action', f.readAttribute('action').gsub(/\/[a-z]*$/, '/'+request));
			return C.list;
		},
		add: function(html, id)
		{
			id = id || '';
			var item = new Element('li');
			item.writeAttribute({'class':'list-item', 'id':'id_'+id}).update(html);
			$(C.list.listid).insert ({'bottom':item});
			return C.list;
		},
		del: function()
		{
			$(C.list.listid).getElementsBySelector('li').each(function(item){
				item.stopObserving();
				item.observe('dblclick', function(evt){
					id = evt.element().identify();
					new Ajax.Request('/xhr/xlist/del', {
						method: 'post',
						parameters: {data:id}
					});
				});
			});
			return C.list;
		},
		submit: function()
		{	
			C.list.status('Odesílám..');
			$(C.list.formid).request();
			return false;
		},		
		clear: function(id)
		{
			id = id || false;
			if(id) $(id).highlight().fade();
			else   $(C.list.listid).update('');
			return C.list;
		},
		sort: function()
		{
			var list = C.list.listid;
			Sortable.create(list, {
				onUpdate: function()
				{
					sort = Sortable.serialize(list, {name:'x'});
					new Ajax.Request('/xhr/xlist/sort', {
						method: 'post',
						parameters: {data:sort}
					});
				}
			});
			return C.list;
		},
		focus: function()
		{
			$$('*[name="list_item"]').first().clear().focus();
			return C.list;
		},
		status: function(data)
		{
			stat = $('list_status');
			stat.show().update(data).highlight();
			stat.fade({delay:2});
			return C.list;
		}
	},
	
	redirect: function(url)
	{
		document.location.href = url;
	}
};

//	PAGE
var P = {
	refresh: function()	{
		document.location.href = document.location.href;
	},
	link: function(url)	{
		document.location.href = url;
	}	
};

//  HELPERS
var H = {
	
};

// admin
var A = {
	manageComments: function() {
		new Ajax.Request('/xhr/comment/manage');
	},
	unmanageComments: function() {
		new Ajax.Request('/xhr/comment/unmanage');
	}
};

//  EFFECTS
var E = {
	vars: {},
	startPage: function()
	{
		new Effect.Morph('canvas', 
		{
			style: { width: '350px', height: '165px' },
			delay: 2,
			duration: 1
		});
		$('message').update('Hello guest, what\'s your name?');
		new Effect.Pulsate('message',
		{
			pulses: 2,
			duration: 0.8,
			delay: 0.3,
			queue: 'end'
		});
		new Effect.Appear('submit', 
		{
			delay: 0.3,
			queue: 'end',
			afterFinish: function() { $('username').focus(); }
		});
	},
	mainMenu: function()
	{
		var logo = $('logo');
		var menu = $('menu');
		if(logo)
		{
			logo.observe('mouseover', function()
			{
				if(E.vars.logoeffectdw) E.vars.logoeffectdw.cancel();
				E.vars.logoeffectup = new Effect.Morph('logo', { style: { height:'130px' }, duration:0.5, afterFinish:function(){ E.vars.logoeffectdw = null; } });

			});
			logo.observe('mouseout', function(){
				if(E.vars.logoeffectup) E.vars.logoeffectup.cancel();
				E.vars.logoeffectdw = new Effect.Morph('logo', { style: { height:'150px' }, duration:0.5, delay:0.8, afterFinish:function(){ E.vars.logoeffectup = null; } });
			});
		}
		if(menu)
		{
			menu.observe('mouseover', function(){
				if(E.vars.logoeffectdw)	E.vars.logoeffectdw.cancel();
				E.vars.logoeffectup = new Effect.Morph('logo', { style: { height:'130px' }, duration:0.5, afterFinish:function(){ E.vars.logoeffectdw = null; } });
			});
			menu.observe('mouseout', function(){
				if(E.vars.logoeffectup)E.vars.logoeffectup.cancel();
				E.vars.logoeffectdw = new Effect.Morph('logo', { style: { height:'150px' }, duration:0.5, delay:0.8, afterFinish:function(){ E.vars.logoeffectup = null; } });
			});
		}
	},
	mainFoot: function()
	{
		var foot = $('foot');
		var copy = $('copy');
		if(foot)
		{
			foot.observe('mouseover', function(){
				E.vars.footeffectdw = new Effect.Morph('foot',
				{
					style: { height: '0px' },
					duration: 0.5,
					delay: 2
				});
			});
			foot.observe('mouseout', function()
			{
				if(E.vars.footeffectdw && E.vars.footeffectdw.currentFrame==0) E.vars.footeffectdw.cancel();
			});
		}
		if(copy)
		{
			copy.observe('mouseout', function(){
				if(E.vars.footeffectdw) E.vars.footeffectdw.cancel();
				E.vars.footeffectup = new Effect.Morph('foot',
				{
					style: { height: '20px' },
					duration: 0.5,
					delay: 0.5
				});
			});
		}		
	},
	toggleComments: function()
	{
		var slides = $$('.slide');
		if(slides)
		{
			slides.each(function(box){
				box.observe('dblclick', function(evt){
					evt.stop();
					$$('.slide').each(function(box){						
						box.next().toggle();
					});
				});
				box.observe('click', function(evt){
					evt.stop();
					evt.element().next().toggle()
				});				
				box.next().hide();
			});
		}
	}
};

document.observe('dom:loaded', C.init);