/*
	Adds class "error" to one or more input / textarea / select
	fields, specified by its name given as parameter 0 ... n
	
	e.g.:
	
	articleFlagError('article_send_toName', 'article_send_to');
*/
function articleController() {
	var eventElement = null;
	var shownComments = new Array();
	
	// ARTICLE SEND - Makes a input field look faulty
	// param0, param1, ... - name of input fields
	this.flagError = function() {
		var parentDiv = $("div#artikel_send");
		for(var i=0; i < arguments.length; i++) {
			parentDiv.find("*[name='"+arguments[i]+"']").addClass('error');
		}
	}
	
	
	// ARTICLE SEND - Makes a input field look normal
	// param0, param1, ... - name of input fields
	this.flagNoError = function() {
		var parentDiv = $("div#artikel_send");
		for(var i=0; i < arguments.length; i++) {
			parentDiv.find("*[name='"+arguments[i]+"']").removeClass('error');
		}	
	}
	
	
	// Start the XAJAX send process
	this.doSendStart = function(jqueryEventElement) {
		eventElement = jqueryEventElement;
		$("img#article_send_load").show();
		$("input#article_send_button").hide();
		xajax_articleSendForm(xajax.getFormValues(jqueryEventElement.parent('form').get(0).id));		
		eventElement.parent('form').find(':input').each(function(){ this.disabled = true; });
	}
	
	
	// Is called, when send process is finished - call routine can be found in XAJAX handler(s)!
	// errorOccured - Integer, whether error occured while sending or not
	this.doSendEnd = function(errorOccured) {
		if(errorOccured != 1) {
			$("div#artikel_send_content").slideDown(1000);
		}
		window.setTimeout(function() { handleSendEnd(errorOccured) }, (errorOccured!=1?3500:0));
	}
	
	
	// PRIVATE - Is called by doSendEnd() after timeout
	// errorOccured - Integer, whether error occured while sending or not
	var handleSendEnd = function(errorOccured) {
		eventElement.parent('form').find(':input').each(function(){ this.disabled = false; });
		$("img#article_send_load").hide();
		$("input#article_send_button").show();
		if (errorOccured != 1) {
			$("div#artikel_send_content").slideUp(500);
			$("div#artikel_send").find(":input[type!='button'][type!='hidden']").val('');
		}
	}
	
	
	// Open article print popup with "intelligent" width and height
	this.doPrint = function(articlePrintUrl) {
		var popupWidth = parseInt($("div#left").width()) + 40;
		if (popupWidth < 200) {
			popupWidth = 200;
		}
		
		var popupHeight = parseInt((screen.width / screen.height) * popupWidth);
		if (popupHeight > (0.9 * screen.availHeight)) {
			popupHeight = 0.9 * screen.availHeight;
		}
		
		window.open(
			articlePrintUrl,
			'articlePrintView',
			'height='+popupHeight+',width='+popupWidth+',scrollbars=yes'
		);
	}
	
	
	// Starts the XAJAX process for sending comments by using XAJAX functions
	this.doSendCommentStart = function() {
		xajax_articleCommentSend(xajax.tools.getFormValues('articleCommentForm'));
	}
	
	
	// Starts the XAJAX process for reloading comments by using XAJAX functions
	this.doReloadCommentStart = function() {
		xajax_articleCommentReload(xajax.tools.getFormValues('articleCommentForm'));
	}
	
	
	// Is called, when send process is finished by XAJAX process
	// Call routines may be found in XAJAX part
	this.handleSendCommentEnd = function(errorOccured) {
		var divMessage = $('div#articleCommentMessage');
		
		// errorOccured will be one of the following values:
		// - 0: no error occured, dataset was saved
		// - fieldError: fields are empty / faulty filled
		// - saveError: save operation (e.g. DB) failed
		// - loginError: not logged in
		
		if (!isNaN(errorOccured)) {
			$("form#articleCommentForm").find(":input[type!='button'][type!='hidden']").val('');
			divMessage
				.removeClass('error')
				.html('Ihr Kommentar wurde erfolgreich in der Datenbank gespeichert.')
				.slideDown(500)
				.queue(function() {
					window.setTimeout('$(\'div#articleCommentMessage\').slideUp(500)', 2500);
					$(this).dequeue();
				});
			this.doReloadCommentStart();		
		} else {
			var errorMessage = 'Es ist ein Fehler aufgetreten.';
			if (errorOccured == 'fieldError') {
				errorMessage = 'Bitte korrigieren Sie die <b>rot markierten</b> Felder.';
			} else if (errorOccured == 'systemError') {
				errorMessage = 'Kommentare k&ouml;nnen <b>nur auf dem Livesystem</b> abgeschickt werden.';
			} else if (errorOccured == 'saveError') {
				errorMessage = 'Der Kommentar konnte leider nicht in der Datenbank gespeichert werden. Bitte sp&auml;ter erneut probieren.';
			} else if (errorOccured = 'loginError') {
				errorMessage = 'Bitte <b>melden Sie sich an</b>, bevor Sie einen Kommentar absenden.';
			}
			divMessage
				.addClass('error')
				.html(errorMessage)
				.slideDown(500);
		}
	}
	
	
	// Is called, when reloading process is finished by XAJAX process
	// Call routines may be found in XAJAX part
	// commentIds - PHP-XAJAX-array (numeric indiced) with found commentIds
	this.handleReloadCommentEnd = function(commentIds) {
		var tempShownComments = shownComments;
		var commentCount = 0;
		shownComments = new Array();
		
		if ((typeof commentIds == 'array' || typeof commentIds == 'object') && typeof commentIds.length == 'number') {
			commentCount = commentIds.length;
			for(i=0; i < commentCount; i++) {
				if (tempShownComments[commentIds[i]] == true) {
					this.showCommentText(commentIds[i], true);
				}
			}
		}
		
		if (commentCount > 0) {
			$('div#articleCommentsContainer').show();
		} else {
			$('div#articleCommentsContainer').hide();
		}
	}
	
	
	// As flagError() - only for comments
	this.commentFlagError = function() {
		var parentElement = $("form#articleCommentForm");
		for(var i=0; i < arguments.length; i++) {
			parentElement.find("*[name='"+arguments[i]+"']").addClass('error');
		}
	}
	
	
	// As flagNoError() - only for comments
	this.commentFlagNoError = function() {
		var parentElement = $("form#articleCommentForm");
		for(var i=0; i < arguments.length; i++) {
			parentElement.find("*[name='"+arguments[i]+"']").removeClass('error');
		}	
	}

	
	// Hides the text information of a comment with given commentId
	this.hideCommentText = function(commentId) {
		var commentNodeElement = $('div.container_kommentare').find('span#comment_'+commentId);
		if (commentNodeElement.size() > 0) {
			shownComments[commentId] = false;
			var temp = null;
			if (document.all) {
				temp = commentNodeElement.find('span.commentText').hide();	
			} else {
				temp = commentNodeElement.find('span.commentText').fadeOut(750);
			}
		
			temp.queue(function() {
				commentNodeElement
					.find('span.commentTeaser')
					.find('a').show();		
				$(this).dequeue();
			});
		}
	}
	
	
	// Shows the text information for a comment with given commentId
	this.showCommentText = function(commentId) {
		var commentNodeElement = $('div.container_kommentare').find('span#comment_'+commentId);
		// if reload by XAJAX process, second argument will be given
		var fadeInTimeout = (arguments.length == 1 ? true : false);
		
		if (commentNodeElement.size() > 0) {
			shownComments[commentId] = true;
			commentNodeElement.find('span.commentTeaser').find('a')
 				.hide().queue(function() {
					if (fadeInTimeout > 0 && !document.all) {
						commentNodeElement.find('span.commentText').fadeIn(750);
					} else {
						commentNodeElement.find('span.commentText').show();						
					}
					$(this).dequeue();
				});
		}
	}
}