/**
* opens a window screen centered
* @param url url to be opened
* @param windowName window identifier
* @param paras parameters for window (width, height, scrollbars, menubar, etc.)
*/
function openWindowCentered(url, windowName, paras){
	var screenW = screen.availWidth;
	var screenH = screen.availHeight;
	var givenW = 0;
	var givenH = 0;
	var paras = paras.replace(/ /g, '').toLowerCase().split(',');
	newParas = new Array();
	for(para in paras){
		if(typeof(paras[para]) == 'string') {
			entry = paras[para].split('=');
			switch(entry[0]){
				case 'left':
				case 'top':
					break;
				case 'width':
					givenW = entry[1];
					newParas[newParas.length] = entry[0] + '=' + entry[1];
					break;
				case 'height':
					givenH = entry[1];
					newParas[newParas.length] = entry[0] + '=' + entry[1];
					break;
				default:
					newParas[newParas.length] = entry[0] + '=' + entry[1];
					break;
			}
		}
	}
	newParas[newParas.length] = 'left=' + (screenW/2 - givenW/2) + 'px';
	newParas[newParas.length] = 'top=' + (screenH/2 - givenH/2) + 'px';
	newParas = newParas.join(',');
	return window.open(url, windowName, newParas);
}


/**
* checks if a given value is in an array
* @param needle value to be searched for
* @param haystack array to be searched in
*/
function inArray(needle, haystack){
	for(i in haystack){
		if(haystack[i] == needle){
			return true;
		}
	}
	return false;
}


/**
* show or hide the given Object by setting CSS class name to 'show' or 'hide'
* @param objectId
*/
function showhide(objectId) {

  if ( document.getElementById(objectId).className == 'show') {
  	document.getElementById(objectId).className = 'hide';
  	document.getElementById(objectId).style.display = 'none';
  } else {
  	document.getElementById(objectId).className = 'show';
  	document.getElementById(objectId).style.display = 'block';
  }
}


/**
* changes bgcolor of a tablerow by using onmouseover
* @param row name
* @param color (Hex)
*/

function setPointer(theRow, thePointerColor)
{
    // IE 5.0 is to stupid for that
    if(navigator.appName.search(/Microsoft/) > -1 && navigator.appVersion.search(/MSIE 5.0/) > -1){
        return false;
    }
    if (thePointerColor == '' || typeof theRow.style == 'undefined') {
        return false;
    }
    if (typeof document.getElementsByTagName != 'undefined') {
        var theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof theRow.cells != 'undefined') {
        var theCells = theRow.cells;
    }
    else {
        return false;
    }
    var rowCellsCnt  = theCells.length;
    for (var c = 0; c < rowCellsCnt; c++) {
        theCells[c].className = thePointerColor;
    }
    return false;
}
function ImageGallery(user_id)
{
    var wid = "760";
    var hig = "570";
    OpenWin = openWindowCentered('/foto.php?u=' + user_id, "ImageGallery", "toolbar=no,menubar=no,scrollbars=no,width=" + wid + ",height=" + hig);
    OpenWin.focus();
}

var moresmilies = false;
function showMoreSmilies()
{
	if(moresmilies == false) {
		if(typeof dojo == 'object') {
			dojo.lfx.html.wipeIn('moresmilies', 350).play();
		} else {
			document.getElementById('moresmilies').style.display = 'block';
		}
		moresmilies = true;
	} else {
		if(typeof dojo == 'object') {
			dojo.lfx.html.wipeOut('moresmilies', 350).play();
		} else {
			document.getElementById('moresmilies').style.display = 'none';
		}
		moresmilies = false;
	}
}
function chgDisplayByClass(c, t)
{
    var i, j;
    if(typeof c != 'object'){
        c = [c];
    }
	var elements = document.getElementsByTagName(t);
	for(i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(j = 0; j < node.attributes.length; j++) {
			if(inArray(node.attributes.item(j).nodeName, ['class', 'CLASS'])) {
                if(inArray(node.attributes.item(j).nodeValue, c)) {
                    node.style.display = node.style.display == 'none' ? '' : 'none';
				}
			}
		}
	}
}
function chgHeightById(id, oldHeight, newHeight)
{
    el = document.getElementById(id);
    if(el.style.height == oldHeight){
        el.style.height = newHeight;
    }
    else{
        el.style.height = oldHeight;
    }
    return el.style.height;
}
function inArray(v, arr)
{
    for(var i in arr){
        if(arr[i] == v){
            return true;
        }
    }
    return false;
}

function favoriteswitch(id, action)
{
	dojo.io.bind({
		url: '/favorite_profiles.php',
		content: { 'action' : action, 'id' : id, 'ajax' : '1'},
		load: function(type, data, evt) {
			if (action == 'add') {
				dojo.byId('favicon_' + id).src = '/icons/icon_favorite_profile.gif';
				dojo.byId('link_fav_' + id).innerText = '<a id="link_favoriteswitch" href="javascript:void(0)" onClick="favoriteswitch(' + id + ',\'drop\')">Als Favorit entfernen</a>';
				dojo.byId('link_fav_' + id).innerHTML = '<a id="link_favoriteswitch" href="javascript:void(0)" onClick="favoriteswitch(' + id + ',\'drop\')">Als Favorit entfernen</a>';
			} else {
				dojo.byId('favicon_' + id).src = '/icons/dot.gif';
				dojo.byId('link_fav_' + id).innerText = '<a id="link_favoriteswitch" href="javascript:void(0)" onClick="favoriteswitch(' + id + ',\'add\')">Zu den Favoriten hinzufügen</a>';
				dojo.byId('link_fav_' + id).innerHTML = '<a id="link_favoriteswitch" href="javascript:void(0)" onClick="favoriteswitch(' + id + ',\'add\')">Zu den Favoriten hinzufügen</a>';
		    }
		},
		mimetype: "text/plain"
	});
	return false;
}


var extsetting = false;
function extsettings() {
	if(extsetting == false) {
		dojo.lfx.html.wipeIn('extsettings', 550).play();
		extsetting = true;
	} else {
		dojo.lfx.html.wipeOut('extsettings', 550).play();
		extsetting = false;
	}
}


var mailboxsearch = false;
function showMailboxsearch()
{
	if(mailboxsearch == false) {
		if(typeof dojo == 'object') {
			dojo.lfx.html.wipeIn('mailboxsearch', 350).play();
		} else {
			document.getElementById('mailboxsearch').style.display = 'block';
		}
		mailboxsearch = true;
	} else {
		if(typeof dojo == 'object') {
			dojo.lfx.html.wipeOut('mailboxsearch', 350).play();
		} else {
			document.getElementById('mailboxsearch').style.display = 'none';
		}
		mailboxsearch = false;
	}
}


/* Größe des Eingabefeldes am Inhalt anpassen */
function dynamicTextareaSize(elem, minHeight, maxHeight) {
    elem = document.getElementById(elem);
    var content = elem.value;
    var minHeight = parseInt(minHeight);
    var maxHeight = parseInt(maxHeight);

    var lines = content.split('\n');
    var newHeight = 0;
    var oldPos = -1;
    for(var count = 0; count < lines.length; count++) {
        var position = lines[count].search(/ |-/);
        while(position != oldPos) {
            if(parseInt(position / elem.cols) != parseInt(oldPos / elem.cols))
                newHeight += Math.abs(oldPos%elem.cols - (elem.cols-1));
            oldPos = position;
            position++;
            position = lines[count].slice(position).search(/ |-/)+position;
        }
        newHeight += Math.abs((lines[count].length-1) % elem.cols - elem.cols) + lines[count].length-1;
    }

    newHeight = newHeight / elem.cols * 14;

    if(newHeight < minHeight) {
        newHeight = minHeight;
    }
    if(newHeight > maxHeight) {
        newHeight = maxHeight;
    }
    elem.style.height = newHeight+'px';

}

function hideForumDescLong(forumId)
{
    if(typeof dojo == 'object') {
		dojo.io.bind({
    		url: '/forum/viewforum.php?mode=forumLongDesc&forumId=' + forumId,
    		load: function(type, data, evt){

    			dojo.lfx.html.wipeOut('forumLongDesc', 950).play();
    		},
    		mimetype: "text/json"
		});
    } else {
        return true;
    }
    return false;
}


