/*
** Javascript snippets for artisokka.org
**
** Require prototype, scriptaculous, accordion.
**
** Try to get all inline-js here..
*/

var development = false;

var pathPrefix = '';
if (development)
{
	pathPrefix = '/test';
	//pathPrefix = '/artisokka2';
}

// frontpage: toggle picture rows
function togglePhase(el) {
	new Effect.Phase(el, {duration: 0.3} );
}

// frontpage: rss - initialize accordion
Event.observe(window, 'load', loadAccordions, false);
function loadAccordions() {
	// only initialize if page contains #newsRss
	if ($('newsRssItems')) {
		var verticalAccordion = new accordion('newsRssItems', {
		    classNames : {
		        toggle : 'rssNewsToggle',
		        toggleActive : 'rssNewsToggleActive',
		        content : 'rssNewsItem'
		    },
			defaultSize : {
	        	//width : 320 
			},
			direction : 'vertical'
		});
	
		// Open first one
		//verticalAccordion.activate($$('#newsRss .newsToggle')[0]);
	}
}

// articles, open stuff
Event.observe(window, 'load', loadArticleAccordions, false);
function loadArticleAccordions() {
	// only initialize if page contains #newsRss
	if ($('articleList')) {
		var verticalAccordion = new accordion('articleContainer', {
		    classNames : {
		        toggle : 'articleToggle',
		        toggleActive : 'articleToggleActive',
		        content : 'articleItem'
		    },
			defaultSize : {
	        	//width : 320 
			},
			direction : 'vertical'
		});
	
		// Open first one
		//verticalAccordion.activate($$('#newsRss .newsToggle')[0]);
	}
}


// login: initialize login through ajax on page load
Event.observe(window,'load',initializeLogin, false);
function initializeLogin() {
	// only initialize if the page contains login-element 
	if ($('loginForm')) {
		// onfocus clear fields
		Event.observe('UserUsername', 'focus', function(){$('UserUsername').value='';}, false);
		Event.observe('UserPassword', 'focus', function(){$('UserPassword').value='';}, false);

		// login submit through ajax
		Event.observe('loginSubmit', 'click', function(event){ 
				new Ajax.Updater(	'headerUserInformation',
									''+pathPrefix+'/users/login', // /users/login
									{	asynchronous:true,
										evalScripts:true,
										parameters:Form.serialize(Event.element(event).form),
										encoding: 'UTF-8',
										requestHeaders:['X-Update', 'headerUserInformation']
									}
								)
			},
			false
		);
	}
}

// oneliners: initialize ajax periodicalupdaters on page load
// if oneliner element is on the page 
Event.observe(window,'load',initializeOneliners, false);
function initializeOneliners() {
	// only initialize if the page contains oneliner-element 
	if ($('newOneliners')) {
		//oneliner ajax periodicalupdater
		// updates the oneliners every 10 minutes = 60*10 = 600 seconds, onComplete add tooltip to a-elements
		// /oneliners/ajaxIndex
		new Ajax.PeriodicalUpdater('onelinerContent', ''+pathPrefix+'/oneliners/ajaxIndex', {asynchronous:true, evalScripts:true, frequency:600, encoding: 'UTF-8', onComplete: initUserView });
	}
}

// comments growing textareas
// not working
/*Event.observe(window,'load',growCommentTextarea, false);

function growCommentTextarea()
{
	// only continue if comment textarea found
	if ($('CommentComment')) {
		Event.observe('CommentComment', 'keyup', _growTextarea, false);
	}	
} // function

function _growTextarea()
{
	var padding=15;
	var _el = $('CommentComment');

	var client_height=_el.scrollHeight;

	// replace return hits with html-linebreaks
	_el.innerHTML=_el.value.replace(/[\n]/g,'<br />&nbsp;');

	var start_height=_el.scrollHeight;
	var to_height;

	if(client_height>start_height)
	{
		to_height=start_height+padding;
	}
	
	if(to_height&&to_height!=client_height)
	{
		_el.style.height=to_height+'px';
	}
	
} */

/* 	*/
// -->