/*
 * Common functions for the site
 */

var Tscroll_path_to_files = '/js/ts/'

var form_name;
var editor_id;
var editor_name;
var worker_div = "workspace";

function Tscroll_init(id) {
	document.write ('<iframe id="Tscr' + id + '" scrolling=no frameborder=no src="' + Tscroll_path_to_files + 'scroll.html?' + id + '" width="1" height="1"></iframe>');
}

function send_form( fN, divN, url ) {
	dati = $( fN ).serialize();
	e = document.getElementById( divN );
	new Ajax.Updater( divN, url, {
		method:			"POST",
		evalScripts:	true,
		onLoading:		e.innerHTML = '<center><img src="/img/loading.gif" style="padding: 8px 0px 8px 0px;"></center>',
		postBody:		dati
	});
}

function HideMessageDiv() {
	divN = document.getElementById( "message" );
	divN.style.visibility = "hidden";
	divN.style.display = "none";
}

function toggleDivDisplay( divN ) {
	e = document.getElementById( divN );
	if ( e.style.visibility == "hidden" ) {
		e.style.visibility = "visible";
		e.style.display = "block";
	} else {
		e.style.visibility = "hidden";
		e.style.display = "none";
	}
}

function divHide( divN ) {
	e = document.getElementById( divN );
	e.style.visibility = "hidden";
	e.style.display = "none";
	return true;
}

function divShow( divN ) {
	e = document.getElementById( divN );
	e.style.visibility = "visible";
	e.style.display = "block";
	return true;
}

function dropShadow() {
	divShow( "overlay" );
	new Effect.Fade( overlay, { duration: 0.5,  from: 0, to: 0.8 });
}
	
function fixDialogBox() {
	var newtop = Math.round( ( $(document.viewport).getHeight() - $("dialogBox").getHeight() ) / 2 );
	$("dialogBox").setStyle( { top: newtop } );
}

function loadDialog( url ) {
	
	if ( $("user_prf_video" ) != null ) { $("user_prf_video").setStyle( { visibility: "hidden" } ); }
	
	dropShadow();
	divShow( "dialogBox" );
	new Ajax.Updater(
			"dialogBox",
			url,
			{
				method:		"GET",
				evalScripts: true,
				onLoaded:	fixDialogBox() 
			}
		);
}

function unloadDialog() {
	if ( $("user_prf_video" ) != null ) { $("user_prf_video").setStyle( { visibility: "visible" } ); }

	document.getElementById( "dialogBox" ).innerHTML = "";
	divHide( "dialogBox" );
	new Effect.Fade( overlay, { duration: 0.5, from: 0.8, to: 0.0 })
	// divHide( "overlay" );
}

function saveEditorAndStay() {
	document.forms[ form_name ].elements[ editor_name ].value = tinyMCE.get( editor_id ).getContent();
	send_form( form_name, worker_div, editor_action_url );
}

function saveEditor() {
	saveEditorAndStay();
	tinyMCE.execCommand( "mceRemoveControl", false, editor_id )
}

function showHelp( topic ) {
	divShow( "divhelp" );
	load_box( "/help.php?topic=" + topic, "divhelp" );
}

function closeHelp() {
	divHide( "divhelp" );
}

// MESSENGER
var divNumber = 0;
var divShaper = '';
var maxTweets = 5;

function delDiv() {
	divName = "tweet_id_" + ( divNumber - maxTweets + 1 );
	$(divName).remove();
}

function addDiv() {
	if ( divNumber >= maxTweets ) delDiv();

	divNumber = divNumber + 1;
	divName = "tweet_id_" + divNumber;
	divContent = 'Hello world ' + divNumber;
	divRow = '<div style="display: none;" class="yellowHilight" id="' + divName + '">' + divContent + "</div>\n";

	document.getElementById( "div_tweets" ).innerHTML += divRow;
	Effect.Appear( divName, { duration: 0.5 } );
}


// HOMEPAGE USER STATUS TWEETS
var tweetMsgNo = 0;
var maxTweetMsgs = 1;
var tweetMsgContainer = 'tweetstatus';

function removeOldTweet() {
	divName = "tweet_id_" + ( tweetMsgNo - maxTweetMsgs + 1 );
	$(divName).remove();
}

function tweetUserStatus( divContent ) {
	if ( tweetMsgNo >= maxTweetMsgs ) removeOldTweet();

	tweetMsgNo = tweetMsgNo + 1;
	tweetStId = "tweet_id_" + tweetMsgNo;
	divRow = '<div style="display: none;" class="tweet" id="' + tweetStId + '">' + divContent + "</div>\n";

	document.getElementById( tweetMsgContainer ).innerHTML += divRow;
	Effect.Appear( tweetStId, { duration: 0.5 } );
}

function initDialogBox() {
	container = document.body;
	this.container = $(container);
	this.overlay = $('overlay');
	this.dialog = $('dialogBox');
	Event.observe( this.overlay, 'click', function(event) { unloadDialog() } );
	Event.observe( this.dialog, 'click',  function(event) { event.stop() } );
}
