$(document).ready(function(){
	$("textarea[id^='notes_textarea_']").live("focus", function(){
		if($(this).attr("empty")==1){
			$(this).val("");
			$(this).attr("empty", "0");
		}
	});
	$("textarea[id^='notes_textarea_']").live("blur", function(){
		if($(this).attr("empty")==0){
			var uwid=$(this).attr("id");
			uwid=uwid.split("_");
			uwid=uwid[2];
			saveNote(uwid);
		}
		if($(this).val()==""){
			$(this).val("Enter your text here...");	
			$(this).attr("empty", "1");
		}
	});
});




function GetXmlHttpObject(){
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
	  return new XMLHttpRequest();
	}
	if (window.ActiveXObject){// code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function notesTimeout(uuwid){
	window['xmlhttpNotes_'+uwid].abort();
	document.getElementById("notes_"+uwid).innerHTML="<div class='error'>Connection has timed out <input type='button' class='ImagesRefresh' onclick='getNotes("+uwid+")' /> <input type='button' class='ImagesSettings' onclick='showSettingsNotes("+uwid+")' /></div>";
}

function showSettingsNotes(uwid){
	document.getElementById("dimBackground").style.display="block";
	document.getElementById("settings").style.display="block";
	
	window['xmlhttpNotes_'+uwid]=GetXmlHttpObject();
	
	if (window['xmlhttpNotes_'+uwid]==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="widget_notes/ajax/showSettings.php";
	url=url+"?uwid="+uwid+"&sid="+Math.random();
	window['xmlhttpNotes_'+uwid].onreadystatechange=function(){
		if (window['xmlhttpNotes_'+uwid].readyState!=4){
			document.getElementById("settings").innerHTML="<div class='load'></div>";
		}
		if (window['xmlhttpNotes_'+uwid].readyState==4){
			document.getElementById("settings").innerHTML=window['xmlhttpNotes_'+uwid].responseText;
		}
	};
	window['xmlhttpNotes_'+uwid].open("GET",url,true);
	window['xmlhttpNotes_'+uwid].send(null);
}

function hideSettingsNotes(){
	document.getElementById("settings").style.display="none";
	document.getElementById("dimBackground").style.display="none";
}

function updateFieldNotes(uwid,field_name,value){
	window['xmlhttpNotes_'+uwid]=GetXmlHttpObject();
	
	if (window['xmlhttpNotes_'+uwid]==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="widget_notes/ajax/updateField.php";
	url=url+"?uwid="+uwid+"&field_name="+field_name+"&value="+value;
	url=url+"&sid="+Math.random();
	window['xmlhttpNotes_'+uwid].onreadystatechange=function(){
		if (window['xmlhttpNotes_'+uwid].readyState!=4){
			document.getElementById("update_field_status").innerHTML="<div class='load'></div>";
		}
		/*if (window['xmlhttpNotes_'+uwid].readyState==4){
			document.getElementById("update_field_status").innerHTML=window['xmlhttpNotes_'+uwid].responseText;
			showSettingsNotes(uwid);
		}*/
		if (window['xmlhttpNotes_'+uwid].readyState==4){
			document.getElementById("update_field_status").innerHTML="";//remove loading icon
			if(window['xmlhttpNotes_'+uwid].responseText==1){//if data was valid and settings have been saved...
				document.getElementById("validate_"+field_name).setAttribute("class", "success");
				document.getElementById("validate_"+field_name).style.display="inline";
				document.getElementById("validate_"+field_name).innerHTML='<span class="pointer">&nbsp;</span>'+'OK';
				$("#validate_"+field_name).delay(1000).fadeOut(1000);
			}
			else if(window['xmlhttpNotes_'+uwid].responseText==0){//...else, if data was valid but settings could not be saved...
				document.getElementById("validate_"+field_name).setAttribute("class", "error");
				document.getElementById("validate_"+field_name).style.display="inline";
				document.getElementById("validate_"+field_name).innerHTML='<span class="pointer">&nbsp;</span>'+'Error';
				$("#validate_"+field_name).delay(3000).fadeOut(1000);
			}
			else{//...else, data was not valid...
				document.getElementById("validate_"+field_name).setAttribute("class", "error");
				document.getElementById("validate_"+field_name).style.display="inline";
				document.getElementById("validate_"+field_name).innerHTML='<span class="pointer">&nbsp;</span>'+window['xmlhttpNotes_'+uwid].responseText;
				$("#validate_"+field_name).delay(3000).fadeOut(1000);
			}
		}
	};
	window['xmlhttpNotes_'+uwid].open("GET",url,true);
	window['xmlhttpNotes_'+uwid].send(null);
}

function getNotes(uwid){
	window['xmlhttpNotes_'+uwid]=GetXmlHttpObject();
	
	if (window['xmlhttpNotes_'+uwid]==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="widget_notes/ajax/getNotes.php";
	url=url+"?uwid="+uwid+"&sid="+Math.random();
	window['xmlhttpNotes_'+uwid].onreadystatechange=function(){
		if (window['xmlhttpNotes_'+uwid].readyState!=4){
			document.getElementById("notes_"+uwid).innerHTML="<div class='load'></div>";
		}
		if (window['xmlhttpNotes_'+uwid].readyState==4){
			clearTimeout(window['notes_'+uwid+'_timeout']);
			document.getElementById("notes_"+uwid).innerHTML=window['xmlhttpNotes_'+uwid].responseText;
		}
	}
	window['xmlhttpNotes_'+uwid].open("GET",url,true);
	window['xmlhttpNotes_'+uwid].send(null);
	
	window['notes_'+uwid+'_timeout']=setTimeout("notesTimeout("+uwid+")", 5000);
}

function enableDisableNotes(id,value){
	if(value==1)
		document.getElementById(id).removeAttribute("disabled");
	else
		if(value==0)
			document.getElementById(id).setAttribute("disabled", "disabled");
}

function saveNote(uwid){	
	
	$.ajax({
		type: "POST",
		url: "widget_notes/ajax/saveNote.php",
		data: "uwid="+uwid+"&value="+$("#notes_textarea_"+uwid).val(),
		error: function(xhr,error){
			$("#update_notes_status_"+uwid).html("<div class='error'>Error: "+error+"</div>");
		}
	});

}

function elementInfoNotes(uwid,element){
	
	document.getElementById("validate_"+element).removeAttribute("class"); //remove previous errors (if any) and show the normal tooltip
	
	window['xmlhttpNotes_'+uwid+'_elementInfo']=GetXmlHttpObject();
	
	if (window['xmlhttpNotes_'+uwid+'_elementInfo']==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="widget_notes/ajax/elementInfoNotes.php";
	url=url+"?"+element+"&sid="+Math.random();
	
	window['xmlhttpNotes_'+uwid+'_elementInfo'].open("GET",url,false);
	window['xmlhttpNotes_'+uwid+'_elementInfo'].send(null);
	
	document.getElementById("validate_"+element).style.display="inline";
	document.getElementById("validate_"+element).innerHTML='<span class="pointer">&nbsp;</span>'+window['xmlhttpNotes_'+uwid+'_elementInfo'].responseText;
	$("#validate_"+element).delay(3000).fadeOut(1000);
}
