/**
* @author mrommel (convivo)
**/
doActionSendMessage  = function(){
    var userName = dojo.byId("hddName").value;
    window.open('../Postamt/NeueKonversation.aspx?user=' + userName ,'NachrichtSchreiben','toolbar=no,width=760,height=400,resizable=no,top=40,left=50,scrollbars=yes');
}

doActionBlock  = function(){
    __doPostBack("blockUser", null);
}

doActionAddFavorite  = function(){
    var jsonObject = new dojo.rpc.JsonService('/Services/definition/info.smd');
	jsonObject.getFavouriteCategories(document.getElementById("hddName").value).addCallback(openFavourite);
}

doActionSetCookie = function(){
    var jsonObject = new dojo.rpc.JsonService('/Services/definition/info.smd');
	jsonObject.getCookieCategories().addCallback(openCookies);
}

savePrivate = function(){
	dijit.byId('dlgSubmit').show();
	__doPostBack("savePriv",dojo.byId("form_name").value + "~@|;~" + dojo.byId("form_notiz").value);
}

setCookie = function (cookieId){
	__doPostBack("setCookie",cookieId);
}

openCookies = function(/*string*/ result){

	dijit.byId("dlgCookie").show();
}

openFavourite = function (/*string*/ result) {
	var divFavourite = document.getElementById("openFavourite");
	var lists = result.split('@@@;@');
	var categorylist = lists[0].split('#');	
	var taglist = lists[1].split('#');
    var fav = lists[2].split('*$*');
	console.log(fav[0]+fav[1]+fav[2]+fav[3]+fav[4]);
	//alle Elemente aus dem Div entfernen
	while(divFavourite.hasChildNodes()){
		divFavourite.removeChild(divFavourite.firstChild);
	}
	divFavourite.style.width = "380px";
	divFavourite.style.height = "400px";
	
	var table = document.createElement("table");
	table.style.color = "#00BB00";
	table.style.width = "380px";
	var tBody = document.createElement("tbody");
 
	for (var i = 0; i < (Math.round(categorylist.length/4));i++){
		tImg = document.createElement("tr");
		tLabel = document.createElement("tr");
		tRadioRow = document.createElement("tr");
		for (var j = 0; j < ((Math.round(categorylist.length/3)));j++){//TODO drei durch die Anzahl der Zeilen ersetzen
		//for (var j = 0; j < ((Math.round(categorylist.length/(Math.round(categorylist.length/4)+1))));j++){
			var param = categorylist[i*4+j].split('$');
			//Zelle mit Bild erstellen
			var tdImg = document.createElement("td");			
			tdImg.style.height = "52px";
			tdImg.style.textAlign = "center";
			tdImg.style.verticalAlign = "bottom";
			tdImg.style.width = "85px";			
			var img = document.createElement("img");
			img.src = param[0];
			img.width = 42;
			img.height = 42;
			img.alt = param[1];
			tdImg.appendChild(img);
			//Zelle mit Beschriftung erstellen
			tdLabel = document.createElement("td");			
			//tdLabel.style.height = "30px";
			tdLabel.style.verticalAlign = "top";
			tdLabel.appendChild(document.createTextNode(param[1]));
			
			//Zelle mit Radiobutton erstellen
			var radio = null;
			if (ie7()) {		
			    if ((i == 0 && j == 0 && fav[4]=="") || (fav[4]!="" && fav[4]==param[2]))
			        radio = document.createElement("<input type='radio' name='category' value='"+param[2]+"' checked >");
			    else
			        radio = document.createElement("<input type='radio' name='category' value='"+param[2]+"'>");			
			} else {
			    radio = document.createElement("input");  
    			
			    radio.type = "radio";
			    radio.value = param[2];
			    radio.name = "category";
    			
			    if (i == 0 && j == 0 && fav[4]=="") {radio.checked = "checked";}
                if (fav[4]!="" && fav[4]==param[2]) radio.checked = "checked";
            }
			tdRadio = document.createElement("td");
			tdRadio.appendChild(radio);			
			tdRadio.style.height = "20px";


			tImg.appendChild(tdImg);
			tLabel.appendChild(tdLabel);
			tRadioRow.appendChild(tdRadio);			
		}

		tBody.appendChild(tImg);
		tBody.appendChild(tLabel);
		tBody.appendChild(tRadioRow);
	}
	table.appendChild(tBody);

	divFavourite.appendChild(table);
	
	//Formularfeld "einsehbare Notizen"
	divLabel = initLabelDiv(40);		
	divForm = initFormDiv(40);		
	divLabel.appendChild(document.createTextNode(taglist[0]));	
	formInput = document.createElement("textarea");
	formInput.style.fontFamily = "Verdana,Arial,Sans-Serif";
	formInput.style.width = "270px";
	formInput.style.height = "38px";
	formInput.name = "form_NotePublic";
	formInput.id = "form_NotePublic";
    formInput.value = fav[0];
	
	divForm.appendChild(formInput);	
	
	divFavourite.appendChild(divLabel);
	divFavourite.appendChild(divForm);
	
	//Formularfeld "Name privat"
	divLabel = initLabelDiv(12);		
	divForm = initFormDiv(12);	
	divLabel.appendChild(document.createTextNode(taglist[1]));
	formInput = document.createElement("input");
	formInput.className = "text160";
	formInput.name = "form_NamePrivate";
    formInput.id = "form_NamePrivate";
	formInput.value = fav[1];

	divForm.appendChild(formInput);
	divFavourite.appendChild(divLabel);
	divFavourite.appendChild(divForm);
	
	//Formularfeld "Notiz privat"
	divLabel = initLabelDiv(40);		
	divForm = initFormDiv(40);
	divLabel.appendChild(document.createTextNode(taglist[2]));
	formInput = document.createElement("textarea");
	formInput.style.fontFamily = "Verdana,Arial,Sans-Serif";
	formInput.style.width = "270px";
	formInput.style.height = "38px";
	formInput.name = "form_NotePrivate";
    formInput.id = "form_NotePrivate";
    formInput.value = fav[2];
	
	divForm.appendChild(formInput);		
	divFavourite.appendChild(divLabel);
	divFavourite.appendChild(divForm);
	
	//Radiobuttons zur Sichtbarkeit
	divLabel = initLabelDiv(12);
	divLabel.appendChild(document.createTextNode(taglist[3]));
	divFavourite.appendChild(divLabel);
    if (fav[3]=="") {
	divFavourite.appendChild(initRadioDiv(taglist[4],2,true));
	divFavourite.appendChild(initRadioDiv(taglist[5],0,false));
    } else {
        if (fav[3]=="2") {
            divFavourite.appendChild(initRadioDiv(taglist[4],2,false));
            divFavourite.appendChild(initRadioDiv(taglist[5],0,true));
        } else {
            divFavourite.appendChild(initRadioDiv(taglist[4],2,true));
            divFavourite.appendChild(initRadioDiv(taglist[5],0,false));
        }
    }
	//TODO Ausrichtung der Radiobuttons anpassen
	dijit.byId("dlgFavourite").show();
	dijit.byId("dlgFavourite").style.height="440px";
}
ie7 = function() {
    return (navigator.appVersion.indexOf('MSIE 7.')==-1) ? false : true;
}

initLabelDiv = function(/*int*/ height){
	var div = document.createElement("div");	
	div.style.height = height + "px";
	div.style.width = "80px";
	div.style.clear = "both";
	div.style.cssFloat = "left";
	div.style.styleFloat = "left";
	div.style.textAlign = "left";
	div.style.marginTop = "10px";
	return div;
}

initFormDiv = function(/*int*/ height){
	var div = document.createElement("div");	
	div.style.height = height + "px";
	div.style.width = "280px";
	div.style.cssFloat = "left";
	div.style.styleFloat = "left";	
	div.style.textAlign = "left";
	div.style.marginTop = "10px";
	return div;
}

initRadioDiv = function(/*string*/ caption, /*int*/ value,/*boolean*/ checked){
	var div = initFormDiv(12);
	div.style.width = "80px";	
	if (ie7())
	    if (checked)
	        formInput = document.createElement("<input type='radio' name='form_Visibility' checked>");
	    else
	        formInput = document.createElement("<input type='radio' name='form_Visibility'>");
	else
	    formInput = document.createElement("input");
	formInput.type = "radio";
	formInput.value = value;
	formInput.name = "form_Visibility";
	if (checked) { formInput.checked = "checked"; }
	
	formDiv = document.createElement("div");
	formDiv.style.width = "20px";
	formDiv.style.cssFloat = "left";
	formDiv.style.styleFloat = "left";
	formDiv.appendChild(formInput);
	
	divText = document.createElement("div");
	divText.style.width = "60px";
	divText.style.cssFloat = "left";
	divText.style.styleFloat = "left";
	divText.appendChild(document.createTextNode(caption));	
	
	div.appendChild(formDiv);	
	div.appendChild(divText);
	return div;
}



deleteCookie = function(){

	$('div[id$=deleteCookie]').toggle('fast',__doPostBack("deleteCookie", null));
}

deleteFavourite = function(typ) {

	if (typ=="fav")
        $('div[id$=divDeleteFavourite]').toggle('fast',__doPostBack("deleteFavorit",null));
    else {
        $('div[id$=divDeleteFavourite]').toggle('fast',__doPostBack("deleteBlock",null));
        __doPostBack("deleteBlock",null);
        
        if( window.location.indexOf('?') > -1 )
		 			window.location = window.location + '&tick=' + new Date().getTime();
    }
}

saveFavourite = function(){
	var favData = "";
	radios = document.getElementsByName("category");
	for (var i = 0; i < radios.length;i++){
		if (radios[i].checked) { favData += radios[i].value + "~@|;~";}
	}
	favData += dojo.byId("form_NotePublic").value + "~@|;~";
	favData += dojo.byId("form_NamePrivate").value + "~@|;~";
	favData += dojo.byId("form_NotePrivate").value + "~@|;~";
	if (document.getElementsByName("form_Visibility")[0].checked == true){
		favData += "0~@|;~";
	} else {
		favData += "2~@|;~";
	}

	dijit.byId('dlgFavourite').hide();
	dijit.byId('dlgSubmit').show();
	__doPostBack("saveFav",favData);	
}
