/**
 * Anwiki is a multilingual content management system <http://www.anwiki.com>
 * Copyright (C) 2007-2008 Antoine Walter <http://www.anw.fr>
 * 
 * Anwiki is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 * 
 * Anwiki is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Anwiki.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Anwiki's JS toolbox.
 * @package Anwiki
 * @version $Id$
 * @copyright 2007-2008 Antoine Walter
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License 3
 */

AnwUtils = {

cfg: function(sParam)
{
	return g_anwcfg[sParam];
},

getPageName: function(){
	return g_pagename;
},

getActionUrl: function(){
	return g_actionurl;
},

getActionPageUrl: function(){
	return g_actionpageurl;
},

chkall: function(sClassName, oParent){
	var chks=document.getElementsByClassName(sClassName, oParent);
	for (var i=0, count=chks.length; i<count; i++) if (!chks[i].disabled) chks[i].checked='checked';
},

chknone: function(sClassName, oParent){
	var chks=document.getElementsByClassName(sClassName, oParent);
	for (var i=0, count=chks.length; i<count; i++) if (!chks[i].disabled) chks[i].checked='';
},

getCookie: function(sVarName){
	cook = document.cookie;
	sVarName += "=";
	place = cook.indexOf(sVarName,0);
	if(place <= -1) return("0");
	else {
		end = cook.indexOf(";",place)
		if(end <= -1) return(unescape(cook.substring(place+sVarName.length,cook.length)));
		else return(unescape(cook.substring(place+sVarName.length,end)));
	}
},

setCookie: function(sVarName, sValue, sPermanent){
	if(sPermanent){
		dateExp = new Date(2020,11,11);
		dateExp = dateExp.toGMTString();
		ifpermanent = '; expires=' + dateExp + ';';
	} else ifpermanent = '';
	domain = '; path=/; ';
	document.cookie = sVarName + '=' + escape(sValue) + domain + ifpermanent;
}
} //end class AnwUtils



// textarea focus line - thanks to http://www.the-asw.com !

function setTextareaFocusLine(nLine, textarea)
{
	var asLines = textarea.value.split("\n");
	var nTotal = 0;
	nLine--;
	for (var i=0; i<asLines.length && i < nLine; i++)
	{
		nTotal += (asLines[i].length + 1);
	}
	setCaretPos( nTotal, nTotal+asLines[nLine].length, textarea);
	textarea.scrollTop = nLine*15;
}

function setCaretPos(start, end, textarea)
{
	end = end || start;
	textarea.focus();
	if (textarea.setSelectionRange)
		textarea.setSelectionRange(start, end);
	else if (document.selection) {
		var range = textarea.createTextRange();
		range.moveStart('character', start);
		range.moveEnd('character', - textarea.value.length + end);
		range.select();
	}textarea.focus();
}

function getSelection(textarea)
{
	var start = getSelectionStart(textarea);
	var stop = getSelectionEnd(textarea);
	return textarea.value.substring(start, stop);
}

function replaceSelection(str, keep, textarea)
{
	textarea.focus();
	
	var start = getSelectionStart(textarea);
	var stop = getSelectionEnd(textarea);
	var end = start + str.length;
	var scrollPos = textarea.scrollTop;
		
	textarea.value = textarea.value.substring(0, start) + str + textarea.value.substring(stop);
	if ( keep ) setCaretPos(start, end, textarea);
	else setCaretPos(end, 0, textarea);
	textarea.scrollTop = scrollPos;
}

function getSelectionStart(textarea)
{
	if ( typeof textarea.selectionStart != 'undefined' )
		return textarea.selectionStart;
	
	// IE Support
	textarea.focus();
	var range = textarea.createTextRange();
	range.moveToBookmark(document.selection.createRange().getBookmark());
	range.moveEnd('character', textarea.value.length);
	return textarea.value.length - range.text.length;
}

function getSelectionEnd(textarea)
{
	if ( typeof textarea.selectionEnd != 'undefined' )
		return textarea.selectionEnd;

	// IE Support
	textarea.focus();
	var range = textarea.createTextRange();
	range.moveToBookmark(document.selection.createRange().getBookmark());
	range.moveStart('character', - textarea.value.length);
	return range.text.length;
}

function textareaInit(element)
{
	textareaAutoExpandInit(element);
	if (AnwLock.getLock()){
		element.observe('keypress', AnwLock.getLock().renew.bind(AnwLock.getLock()));
	}
}
function textareaKeyPress(evt)
{
	textareaAutoExpandOnKey(evt);
	
	//only if selectionStart is supported (TODO:)
	if ( typeof evt.target != 'undefined' && typeof evt.target.selectionStart != 'undefined' ){
		textareaSmartTabsOnKey(evt);
	}
}

// textarea resizable

function textareaCountLines(element) 
{
	strtocount=element.value;
	cols=element.cols;
	var hard_lines = 1;
	var last = 0;
	while ( true ) 
	{
		last = strtocount.indexOf("\n", last+1);
		hard_lines ++;
		if ( last == -1 ) break;
	}
	var soft_lines = Math.round(strtocount.length / (cols-1));
	var hard = eval("hard_lines  " + unescape("%3e") + "soft_lines;");
	if ( hard ) soft_lines = hard_lines;
	return soft_lines;
}

//est appellé a chaque frappe sur le clavier
function textareaAutoExpandOnKey(e) 
{
	//var element = e.target;
	var element = (window.event)?window.target:e.target;
	if (!element) element=window.event.srcElement;
	
	//var key=(window.Event)?e.which:e.keyCode;
	var key=(window.event)?window.event.keyCode:e.which;
	if(key==13 //enter
		|| key==8 //back (firefox only)
		|| key==0 //delete (firefox only)
		|| (key >= 115 && key <= 125))
	{
		textareaAutoExpandInit(element);
	}
	if (key == 118) //quick hack for paste
	{
		setTimeout(function(){textareaAutoExpandInit(element);},1);
	}
} 

function textareaAutoExpandInit(element) 
{
	element.rows=textareaCountLines(element)+1;
}

