$(document).ready(function() {
	if(typeof task != "undefined"){
	
	    if(task=="new"){
    	    var updateUrl=SYSTEMPATH+"code/projekte/projekt_einfuegen.php";
    		$("#editform").attr({action: updateUrl});

    		$("#buttonUpload").click(function() {
    			if($("#titel").val() ==""){
    				alert("Ihr Projekt braucht einen Titel!");
    				return false;
				}
    			else
    				$("#editform").submit();
		
    		});
    		$("#cancel").click(function() {	
    			abbrechen();	
    			return false;
    	    });
	    
        }
    }
});
function sort(sortierung){
	document.location.href=SYSTEMPATH+'projekte-'+sortierung; 
}
function sortAndFilter(sortierung,filter){
	document.location.href=SYSTEMPATH+'projekte-'+filter+"-"+sortierung; 
}
/*********************************************************************/
function edit(idx)
{
console.log("edit");	
		var url=SYSTEMPATH+"code/projekte/get_project.php";
	  	var o={};
	  	
	  	o.idx=idx;
	

	  	$.getJSON(url,o,function (project) {

				$("#projekt_inhalt").hide();	
			
				$("#projekt_inhalt_edit").load(
					SYSTEMPATH+"html/templates/project_edit.php",
				
					{}, 
					function() { 
						
					
							
								$("#titel").val(project.titel);
								$("#beschreibung").val(project.abstract);
								$("#mainpic_file").val(project.main_pic);
								$("#pic1_file").val(project.pic_1);
								$("#pic2_file").val(project.pic_2);
								$("#pic3_file").val(project.pic_3);
								
								jQuery("input[@type=radio]", this).each(function()
								{
									if(this.value == project.projekt_art)
										this.checked = true;
								});
								
					
								var updateUrl="../code/projekte/projekt_einfuegen.php?p_id="+idx;
								$("#editform").attr({action: updateUrl});

								$("#buttonUpload").click(function() {
									if($("#titel").val() =="")
										alert("Ihr Projekt braucht einen Titel!");
									else
										$("#editform").submit();
								
								});
								$("#cancel").click(function() {	
									$("#projekt_inhalt_edit").html("");
									$("#projekt_inhalt").show();	
									return false;
							    });
							    //delete pics
							    if(project.main_pic !=""){
							        $("#pic_0_preview").html(project.main_pic);
    							    $("#deletePicLink_0").click(function() {
							        
    							        $("#mainpic_file").val("");
    							        $("#pic_0_preview").html("");
    							        console.log("hier---"+$("#pic_0_preview").html());
    							        return false;
    							    });	
							    }
							    else
							        $("#deletePicLink_0").hide();
							    //
							    if(project.pic_1 !=""){
							        $("#pic_1_preview").html(project.pic_1);
    							    $("#deletePicLink_1").click(function() {
							        
    							        $("#pic1_file").val("");
    							        $("#pic_1_preview").html("");
							        
    							        return false;
    							    });
							    }
							    else
							        $("#deletePicLink_1").hide();
							    //
							    if(project.pic_2 !=""){
							        $("#pic_2_preview").html(project.pic_2);
    							    $("#deletePicLink_2").click(function() {
							        
    							        $("#pic2_file").val("");
    							        $("#pic_2_preview").html("");
							        
    							        return false;
    							    });
							    }
							    else
							        $("#deletePicLink_2").hide();
							    //
							    //
							    if(project.pic_3 !=""){
							        $("#pic_3_preview").html(project.pic_3);
    							    $("#deletePicLink_3").click(function() {
							        
    							        $("#pic3_file").val("");
    							        $("#pic_3_preview").html("");
							        
    							        return false;
    							    });
							    }
							    else
							        $("#deletePicLink_3").hide();
								
							
						}//func
				);
		});
}

/*********************************************************************/
function edit_autoren(p_id)
{
	$("#autoren").hide();
	$("#edit_author").show();
	var	url=SYSTEMPATH+"code/projekte/projekt_meta_edit.php?p_id="+p_id+"&task=author";
	
	$("#edit_author").load(
		SYSTEMPATH+"html/templates/edit_author.php",
	
		{}, 
		function() { 
		
				$("#form").attr({action: url});
		 		$("#submit_yes").click(function(){ $("#form").submit()});
		
			$("#submit_no").click(function(){ 
				$("#autoren").show();
				$("#edit_author").html("");
			});
			$("#which").html(" Neue Autoren hinzufügen: ");
			
		});
	
	
	
}
/*********************************************************************/

/*********************************************************************/

/*********************************************************************/
function readCookie(name) {

	var cookiename = name + "=";

	var ca = document.cookie.split(';');

	for(var i=0;i < ca.length;i++) {

		var c = ca[i];

		while (c.charAt(0)==' ') c = c.substring(1,c.length);

		if (c.indexOf(cookiename) == 0) return c.substring(cookiename.length,c.length);

	}

	return null;
}






/*********************************************************************/
function pop_up_link(p_id){
	


	$("#addLink").html("<div align='center' ><img src='"+SYSTEMPATH+"html/img/icons/working.gif' /></div>");


	$("#addLink").load(
		SYSTEMPATH+"html/templates/add_link.php",
		{}, 
		function() { 
			var updateUrl=SYSTEMPATH+"code/projekte/projekt_meta_edit.php?p_id="+p_id+"&task=addLink";
	
			$("#linkform").attr({action: updateUrl});
			$("#buttonUpload").click(function() {

				$("#linkform").submit();
	
			});
			//
			$("#cancel").click(function() {
					$("#addLink").html("");
					return false;
			});
		});
	
}
/*********************************************************************/
function delete_link(idx){
	
	var	url=SYSTEMPATH+"code/projekte/projekt_meta_edit.php?task=deleteLink&idx="+idx;


	$("#dl_"+idx).load(
		SYSTEMPATH+"html/templates/really_delete.php",
		{}, 
		function() { 

	 		$("#delete_yes").click(function() {
					document.location.href=url;
					return false;
			});
			$("#delete_no").click(function() {
					$("#dl_"+idx).html("");
					return false;
			});
			$("#which").html(" diesen Link ");
			
	
		}
	);
}


/*********************************************************************/

function pop_up_material(p_id){
	

	
	$("#addDat").html("<div align='center' ><img src="+SYSTEMPATH+"html/img/icons/working.gif' /></div>");
	


	$("#addDat").load(
	   SYSTEMPATH+"html/templates/add_material.php",
		{}, 
		function() { 
			var updateUrl=SYSTEMPATH+"code/projekte/projekt_meta_edit.php?p_id="+p_id+"&task=addDownload";
	
			$("#datform").attr({action: updateUrl});
			
			$("#buttonUpload").click(function() {

				$("#datform").submit();
	
			});
			//
			$("#cancel").click(function() {
					$("#addDat").html("");
					return false;
			});
		});
	
}
/*********************************************************************/

function delete_material(idx,datei,p_id){
	
	var url=SYSTEMPATH+"code/projekte/projekt_meta_edit.php?p_id="+p_id+"&task=deleteFile&idx="+idx+"&dat="+datei;


	$("#dm_"+idx).load(
		SYSTEMPATH+"html/templates/really_delete.php",
		{}, 
		function() { 

	 		$("#delete_yes").click(function() {
					document.location.href=url;
					return false;
			});
			$("#delete_no").click(function() {
					$("#dm_"+idx).html("");
					return false;
			});
			$("#which").html(" diese Datei ");
			
	
		}
	);
}
/*********************************************************************/

function delete_projekt(p_id){
	
	var url=SYSTEMPATH+"code/projekte/projekt_meta_edit.php?p_id="+p_id+"&task=deleteProjekt";

	$("#loopBox_"+p_id).hide();
	$("#loopBox_edit_"+p_id).load(
		SYSTEMPATH+"html/templates/really_delete.php",
		{}, 
		function() { 

	 	//	$("#delete_yes").attr({ href: url});
	 		$("#delete_yes").click(function() {
					document.location.href=url;
					return false;
			});
			$("#delete_no").click(function() {
					$("#loopBox_edit_"+p_id).html("");
					$("#loopBox_"+p_id).show();
					return false;
			});
			$("#which").html(" diese Ressource ");
			
	
		}
	);
}




/*********************************************************************/
var help_flag=false;
function showHelp(){
	
	
	starttxt="<a href='javascript:showHelp();'><img src='../../html/img/icons/help.gif' border='0' style='vertical-align:middle;margin-top:-2px; margin-right:5px;'></a>Information zu den Formatierungsoptionen der Wiki-Syntax ";
	starttxt+="<a href='javascript:showHelp();'>[weiter]</a>";

	txt="<img src='../../html/img/icons/help.gif' border='0' style='vertical-align:middle;margin-top:-2px; margin-right:5px;'>Information zu den Formatierungsoptionen der Wiki-Syntax ";
	txt+="<div class='dimmed_txt'><table><tr><td>";
	txt+="Textuelle Einbindung von Hyperlinks:</td><td> [[Name|http://www.beispiel.com]]</td></tr>";
	txt+="<tr><td>Link mit Icon  <img src='../../html/img/icons/link.gif'> einbinden :</td><td>[[http://www.beispiel.com]]</td></tr>";

	txt+="<tr><td>Textformatierung :</td><td>[i]<i>Kursiv</i>[/i]</td></tr>";
	txt+="<tr><td></td><td>[strong]<b>fett</b>[/strong]</td></tr></table>";
	
	txt+="<a href='javascript:showHelp();'><img src='../../html/img/icons/close.gif' border='2' style='margin-top:-15px; ; float:right;'></a>";
	txt+="</div>";
	if(!help_flag){
		document.getElementById('wiki_help').innerHTML=txt;
		help_flag=true;
	}
	else{
		document.getElementById('wiki_help').innerHTML=starttxt;
		help_flag=false;
	}
}
/*********************************************************************/
function enable_tag_input() {
	
	$("#keywords").show();
}
/*********************************************************************/
function cancel_keyword() {
	
	$("#keywords").hide();
}
/*********************************************************************/
function abbrechen() {
	
	document.location.href=SYSTEMPATH+'projekte'; 
}
function checkTitel() {
console.log("hier");	
//  if($("#titel").val() ==""){
//  
//  	$("#warning").html("Ihre Ressource braucht einen Titel!");
//  	return false;
//  }
//  else
//  	$("#editform").submit();
//  	
}

/********************************************************************************************/

function write_message(w_id,uid){
	
	$("#message_eingabe_"+uid).show();
	user_id=readCookie('dgtf_userid');

	
	$("#message_eingabe_"+uid).html("<div align='center'><img src='"+SYSTEMPATH+"/html/img/icons/working.gif' /></div>");

	$("#message_eingabe_"+uid).load(
		SYSTEMPATH+"html/templates/write_message.php",
		{}, 
		function() { 

	 
			var updateUrl=SYSTEMPATH+"code/nachrichten/nachricht_senden.php?empfaenger="+uid;
			$("#message_form").attr({action: updateUrl});

			$("#buttonUpload").click(function() {$("#message_form").submit();});
			$("#cancel").click(function() {	
				$("#message_eingabe_"+uid).html("");
				$("#message_eingabe_"+uid).hide();
				return false;
		    });
	
		}
	);
	
	
}
/*********************************************************************/
/*********************************************************************/
function showFullView(path){

	
	
	$("<div id='picViewContainer'><div id='picView'><img src='"+path+"' /></div></div>").appendTo('body').fadeIn(100).center();
	
	$("#picViewContainer").click(function() {$("#picViewContainer").remove();});

}

/*********************************************************************/
function preloader(file) 

{

	img = new Image(); 

	img.src = file;

}