/*
 
 Community specific functions
 @author
 	Pelle Andersson, Promedia, Nerikes Allehanda
 
 */




Event.observe(window,'load',forum_init);

/*
	Init the functions
*/

function forum_init(){
	prepareQuote();
	prepareToogle();
	prepareForumGroupChange();
	prepareScrollPostingIntoView();
}

/*
	Init quoting functionality
*/

function prepareQuote(){
	
	$$("a.quote").each(function(item){
		item.onclick = function(){
			Quote.copyAndPasteQuote(this);return false;
		}
	});
}

/*
	Init toogle functionality for showing terms and condition
*/

function prepareToogle(){
	$$("a#terms").each(function(item){
		item.onclick = function(){
			$('longTerm').toggle(); return false;
		}
	});
}
/*
	Init functionality to change forum group
*/

function prepareForumGroupChange(){
	$$("select#option").each(function(item){
		item.onchange = function(){
			goToForumGroup(this);
		}
	});
}

/*
	This method scroll a specific posting in to view
	based on what posting link the user clicked on previous
	page.

*/

function prepareScrollPostingIntoView(){
	var entry = getUrlParameter();
	if(entry != -1){
		var liList = $$("li");
		if(liList != null && liList.length > 0){
			for(var i=0; i < liList.length; i++){
				if(liList[i].id == entry){
					var li = liList[i];
					$(li.id).scrollTo();
					break;
				}
			}
		}
	}
}

/*
	Redirects to the chosen forum group.
*/

function goToForumGroup(select){
	window.location.href = select.options[select.selectedIndex].value;
}

/*
	Get a specific parameter for posting entry
*/

function getUrlParameter(){
	var index = document.URL.indexOf("?");
	var urlParameter = -1; 
	
	if(index > -1 ){
		var queryString = document.URL.substring(index);
		var arr = new Array();
		arr = queryString.split("=");
		if(arr.length > 0){
			
			urlParameter = arr[1].substring(2);
			if (urlParameter.length == 0)
			{
				urlParameter = -1;
			}
		}
	}
	
	return urlParameter;
}

/* *** Quote *** */

var Quote = {
	
	isQuote: 1,
	divId: "errorQuote",
	fieldsetId: "errorFieldSet",
	
	/**
		Method that captures the text which has been
		selected by the user and then returns it.
	*/
	
	display: function(){
	 	if (document.getSelection) {
    		var str = document.getSelection();
    		if (window.RegExp) {
      			var regstr = unescape("%20%20%20%20%20");
      			var regexp = new RegExp(regstr, "g");
      			str = str.replace(regexp, "");
    		}
  		} 
  		else if (document.selection && document.selection.createRange) {
    			var range = document.selection.createRange();
    			var str = range.text;
  		} 
  		else {
  			var str = "-1";
  		}
  		return str;
	
	},
	
	/**
		Method for getting a text selection, copy it and paste it in the body text area.
		
		@param linkObject
			The link(a tag) being clicked.
	*/
	
	copyAndPasteQuote: function(linkObject){
		var strinL = "";
		var loggedIn = this.isLoggedIn();
		if(loggedIn == false){
			this.removePreviousError();
			this.appendError(linkObject, "<p>Du m\u00E5ste vara inloggad f\u00F6r att kunna citera/skapa inl\u00E4gg.</p>");
		}
		else if(this.display() == ""){
			this.removePreviousError();
			this.appendError(linkObject, "<p>Du har inte markerat n\u00E5gon text som du vill citera! Markera texten du vill citera genom att dra musen \u00F6ver den och klicka p\u00E5 Citera knappen igen. Den markerade texten kommer d\u00E5 att kopieras till din postning.</p>");
		}
		else if(this.display() == "-1"){
			this.removePreviousError();
			this.appendError(linkObject, "<p>Tyv\u00E4rr \u00E4r denna operation ej m\u00F6jlig med din webbl\u00E4sare</p>");
		}	
		else{
			if (this.isQuote == 1){ 
				strinL = '[quote][quoteduser]' + this.getUser(linkObject) + ' skrev:' + '[/quoteduser]' + '\n' + this.display() + '[/quote]' + '\n'; 
			} 
			if (this.isQuote == 2){ 
				strinL = '[b]' + this.display() + '[/b]' + '\n'; 
			} 
			this.getTextArea().value = this.getTextArea().value + strinL;
			this.getTextArea().focus();
		}
	
	},
	
	/**
		Get the current user.
			@param linkObject
				The link being clicked.
	*/
	
	getUser: function(linkObject){
		return linkObject.id;
	},
	
	/**
		Append an error message if something goes wrong.
			@param linkObject
				The link being clicked.
			@param errorMessage
				The error message to show.
	*/
	
	appendError: function(linkObject, errorMessage){
		var parentDiv = linkObject.parentNode.parentNode.parentNode;
		
		var fieldSet = document.createElement("FIELDSET");
		fieldSet.style.border = "none";
		fieldSet.style.margin = "0px 0px 0px 0px";
		fieldSet.onclick = function(){ Quote.removeError(this); }
		fieldSet.id = this.fieldsetId;
		fieldSet.title = "Klicka var som helst p\u00E5 meddelandet f\u00F6r att ta bort det";
		fieldSet.style.cursor = "pointer";
		
		var div = document.createElement("DIV");
		div.className = "error";
		div.id = this.divId;
		div.innerHTML = errorMessage;
		fieldSet.appendChild(div);
		parentDiv.appendChild(fieldSet);
	},
	
	/**
		Removes the current error message showed in page.
		Is called by the container which holds the error message.
			@param element
				The element to remove.
	*/
	
	removeError: function (element){
		var parent = element.parentNode;
		try{
			parent.removeChild(element);
		}
		catch(x){
		}
	
	},
	
	/**
		Removes all current error messages on the page.
		This method is called just before an error occurs and
		another errormessage will be shown.
	*/
	
	removePreviousError: function(){
			var fsets = $$("fieldset#" + this.fieldsetId);
			if(fsets != null && fsets.length > 0){
				for(var i = 0; i < fsets.length; i++){
					var p = fsets[i].parentNode;
					p.removeChild(fsets[i]);
				}
			}
	},
	
	/**
		Checks if user is logged in or not.
	*/
	
	isLoggedIn: function(){
		var btns = $$("input.standardButton");
		var loggedIn = true;
		for(var i = 0; i < btns.length; i++){
			if(btns[i].disabled){
				loggedIn = false;
				break;
			}
		}
		return loggedIn;
	},
	
	/**
		Get the textarea in wich the text selection will be pasted in.
	*/
	
	getTextArea: function(){
		var all = $$("textarea");
		var textArea = null;
	
		for(var i=0; i< all.length; i++){
			if( (all[i].id == "body") && (all[i].tagName.toUpperCase() == "TEXTAREA") ){
				textArea = all[i];
				break;
			}
		}
		
		return textArea;
	}
}

