/*
 * Config
 */
var colour_on_element_add = '#256799';
var colour_on_element_rem = '#992a25';
var back_colour = '#353535';
var ajax_request_error = "AJAX Request Error";
var popupdiv;
var dimdiv;

function openPGPopup(url)
{
    new Request(
	{
	    url: url.trim(),
	    onSuccess: function(txt) {
		dimSite(txt);
	    },
	    onFailure: function() {
		popupdiv = null;
		alert('The request failed: '+url);
	    }
	}
    ).send();

    return false;
}

function dimSite(content, wide)
{
    if (popupdiv != null) document.body.removeChild(popupdiv);

    popupdiv = new Element('div');
    popupdiv.setAttribute("id", "popupdiv");
    popupdiv.setStyle("opacity", 0);
    if (wide === true)
    {
	popupdiv.setStyle("width", "800px");
	popup_width = 800;
    }
    else popup_width = 450;

    size = window.getSize();
    l = (size.x - popup_width) / 2;
    popupdiv.style.top = '150px';
    popupdiv.style.left = l+'px';
    
    closepopup = new Element("div");
    closepopup.setAttribute("id", "closepopup");
    closepopup.setStyle("opacity", 0.8);
    closepopup.innerHTML = "<span onclick='closePopup();' style='cursor:pointer;'>Zou maachen</span>";

    popupcontent = new Element("div");
    popupcontent.setAttribute("id", "popupcontent");
    popupcontent.innerHTML = content;

    popupdiv.appendChild(closepopup);
    popupdiv.appendChild(popupcontent);
    document.body.appendChild(popupdiv);

    if (dimdiv != null) document.body.removeChild(dimdiv);

    height = size.y;
    dimdiv = new Element('div');
    dimdiv.setAttribute("id", "dimdiv");
    dimdiv.style.height = height+'px';
    dimdiv.setStyle("opacity", 0);
    dimdiv.addEvent('click', function() { closePopup(); });

    var popupfx = new Fx.Tween(popupdiv, { duration: 100 });

    var dimfx = new Fx.Tween(dimdiv, { duration: 100, onComplete: function() { popupfx.start("opacity", 1); } });

    document.body.appendChild(dimdiv);
    dimfx.start("opacity", 0.7);
}

function closePopup()
{
    if (popupdiv != null)
    {
	var popupfx = new Fx.Tween(popupdiv, { duration: 100, onComplete: function() { popupdiv.destroy(); popupdiv = null; } });
	popupfx.start("opacity", 0);
    }
    if (dimdiv != null)
    {
	var dimfx = new Fx.Tween(dimdiv, { duration: 150, onComplete: function() { dimdiv.destroy(); dimdiv = null; } });
	dimfx.start("opacity", 0);
    }
}

function Disappear(div_id)
{
    $(div_id).set('tween',
	{
	    duration: 0.3,
	    onComplete: function() { $(div_id).destroy(); }
	}
    );
    $(div_id).tween('opacity', '0');
}

var slideTimer;
var myEffect;
function showNote(note)
{
	if (myEffect != null) myEffect.cancel();
	$clear(slideTimer);
	$("extra_divs").innerHTML = note;
	$("extra_divs").fade('hide');
	$("extra_divs").setStyle('border-bottom', '1px solid #265d87');
	myEffect = new Fx.Morph($("extra_divs"), {duration: 800, transition: Fx.Transitions.Sine.easeOut});
	myEffect.start({
	    'height': [0, 30],
	    'opacity': [0, 0.8],
	    'padding-top': [0, 15],
	    'border-width': [0, 2]
	});
	var slideUp = function()
	{
		myEffect.start({
		    'height': [30, 0],
		    'opacity': [0.8, 0],
		    'padding-top': [15, 0]
		});
	};
	slideTimer = slideUp.delay(2000);
}

function FadeInOut(id, delay)
{
	if (id != null)
	{
		idRef = $(id);
		idRef.fade('hide');
		idRef.fade('in');
		var fadeout = function(){ idRef.fade('out'); };
		if (delay == null) delay = 3000;
		fadeout.delay(delay);
	}
}

var ajax_req;
var ajax_div;
function AjaxUpdater(args, meth, div)
{
	if (ajax_req != null && ajax_div == div) { ajax_req.cancel(); }
	
	ajax_div = div;
	ajax_req = new Request(
		{
			method: ''+meth+'',
			url: 'ajaxRequest.php',
			onSuccess: function(txt) {
			    ajax_req = null;
			    $(div).innerHTML = txt;
			},
			onFailure: function() {
			    ajax_req = null;
			    alert('The request failed.');
			}
		}
	).send(args);
	return ajax_req;
}

function AjaxUpdaterHighlight(args, meth, div, highlight)
{
	var req = new Request(
		{
			method: ''+meth+'',
			url: 'ajaxRequest.php',
			onSuccess: function(txt) {
				$(div).innerHTML = txt;
				if (highlight == true)
				{
					myEffect = new Fx.Morph($(div), {duration: 800, transition: Fx.Transitions.Sine.easeOut});
					myEffect.start({
					    'opacity': [0, 1.0],
					    'padding-left': [15, 0]
					});
				}
			},
			onFailure: function() {
				alert('The request failed.');
			}
		}
	).send(args);
}

function over(imgname,imgsrc) {
	document[imgname].src=imgsrc;
}

function ShowHide(id1, id2) {
	if (id1 != '') expMenu(id1);
	if (id2 != '') expMenu(id2);
}

function expMenu(id) {
	var itm = null;
	if (document.getElementById) {
		itm = document.getElementById(id);
	} else if (document.all){
		itm = document.all[id];
	} else if (document.layers){
		itm = document.layers[id];
	}

	if (!itm) {
	} else if (itm.style) {
		if (itm.style.display == "none") {
			itm.style.display = "";
		} else {
			itm.style.display = "none";
		}
	} else {
		itm.visibility = "show";
	}
}

function select_all(status,theform) {
	for (i=0;i<theform.length;i++) {
		if(theform.elements[i].name=="pms_id[]") theform.elements[i].checked = status;
		if(theform.elements[i].name=="fav_id[]") theform.elements[i].checked = status;
	}
}

function alert_if_empty(formname, fieldname, message) {
	if (window.document.formname.fieldname.value=='') {
		alert(message);
		return false;
	} else return true;
}

function getSelectedValue(selectid)
{
	return $(selectid).options[$(selectid).selectedIndex].value;
}

function showTipp(fieldid, tipp)
{
	text = "<div class='rc_tipp'>";
	text += "<b class='b1'></b><b class='b2'></b><b class='b3'></b><b class='b4'></b>";
	text += "<div class='rc_tipp_c'>";
	text += "<div class='div_tipp_icon'>&nbsp;</div>";
	text += "<div class='div_tipp_right'>";
	text += "<div class='div_tipp_title'>Informatioun/Tipp:</div>";
	text += "<div class='div_tipp_content'>" + tipp + "</div>";
	text += "</div>";
	text += "<div style='clear:left;'></div>";
	text += "</div>";
	text += "<b class='b4'></b><b class='b3'></b><b class='b2'></b><b class='b1'></b>";
	text += "</div>";
	$(fieldid).innerHTML = text;
	$(fieldid).style.display = '';
}

function ToggleView(id)
{
	var fx = new Fx.Slide($(id), {duration:'short'});
	fx.toggle();
	return false;
}

/* GET ARGS WITGH SIMPLE JS FUNCTION */
function QueryString(key) {
	var value = null;
	for (var i=0;i<QueryString.keys.length;i++) {
		if (QueryString.keys[i]==key) {
			value = QueryString.values[i];
			break;
		}
	}
	return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString_Parse() {
	var query = window.location.search.substring(1);
	var pairs = query.split("&");
	for (var i=0;i<pairs.length;i++) {
		var pos = pairs[i].indexOf('=');
		if (pos >= 0) {
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			QueryString.keys[QueryString.keys.length] = argname;
			QueryString.values[QueryString.values.length] = value;
		}
	}
}
QueryString_Parse();


function addBuddy(user_id)
{
	var req = new Request(
		{
			url: 'ajaxRequest.php',
			onSuccess: function(txt) { handleServerResponse_addbuddy(txt); },
			onFailure: function() { alert('The request failed.'); }
		}
	).send("page=addbuddy&user_id=" + user_id);

	return false;
}

function handleServerResponse_addbuddy(txt)
{
	if (txt == 1) $("link_addbuddy").innerHTML = "Waarden op Best&auml;tegung";
	else if (txt == 11) $("link_addbuddy").innerHTML = "Best&auml;tegt";
	else alert("Feeler: " + txt);
}

function removeBuddy(user_id)
{
	if (confirm("Wells de desen Member wierklech als Frend laeschen?"))
	{
		var req = new Request(
			{
				url: 'ajaxRequest.php',
				onSuccess: function(txt) { handleServerResponse_removebuddy(txt); },
				onFailure: function() { alert('The request failed.'); }
			}
		).send("page=removebuddy&user_id=" + user_id);
	}
	return false;
}

function handleServerResponse_removebuddy(txt)
{
	$("link_addbuddy").innerHTML = "Als Fr&euml;nd er&euml;m gel&auml;scht";
}