var blogAddCommentDlg = null;
var blogEditor = null;
var blogWaitDialog = null;
	
function initBlog() {
	if(document.getElementById("blogAddComment")) {
		// Initialize the temporary Panel to display while waiting for external content to load
		blogWaitDialog = new YAHOO.widget.Panel("blogWaitDialog",  
			{ width:"240px", 
			  fixedcenter:true, 
			  close:false, 
			  draggable:false, 
			  zindex:4,
			  modal:true,
			  visible:false
			} 
		);
		blogWaitDialog.setBody('<img src="http://us.i1.yimg.com/us.yimg.com/i/us/per/gr/gp/rel_interstitial_loading.gif" />');
		blogWaitDialog.render(document.body);
		
		var handleSubmit = function() {
			if(confirm("Sei sicuro che vuoi aggiungere questo commento?")) {
				if(!jcap()) {
					alert("L'immagine di controllo non e stato riportato corretamente nel aposito spazio");
				} else {
					if(document.getElementById("blogFirstname").value.length > 3 && document.getElementById("blogLastname").value.length > 3) {
						if(!document.all) {
							blogEditor.saveHTML();
						}
						blogWaitDialog.show();
						this.submit();
					} else {
						alert("Devi specificare il tuo nome e cognome per lasciare un commento");
					}
				}
			}
		};
		var handleCancel = function() {
			this.cancel();
			if(!document.all) {
				blogEditor.clearEditorDoc();
				blogEditor.saveHTML();
			}
			document.getElementById("blogFirstname").value = "";
			document.getElementById("blogLastname").value = "";
		};
		
		var submitSuccess = function(ldr) {
			blogWaitDialog.hide();
			_blog_loadCommentList();
			if(!document.all) {
				blogEditor.clearEditorDoc();
				blogEditor.saveHTML();
			}
			document.getElementById("blogFirstname").value = "";
			document.getElementById("blogLastname").value = "";
		}
		
		var submitFailure = function(ldr) {
			blogWaitDialog.hide();
			alert("I could not add your comment because of a failure on the server");
		}
		
		blogAddCommentDlg = new YAHOO.widget.Dialog("blogAddComment", {
			width: "550px",
			height: "500px",
			visible: false,
			fixedcenter: true,
			zIndex: 5000,
			modal: false,			
			buttons : [ { text:"Aggiungi Commento", handler:handleSubmit, isDefault:true },
					  			{ text:"Annulla", handler:handleCancel } ]
		});
		blogAddCommentDlg.render();
		blogAddCommentDlg.callback = {
			success: submitSuccess,
			failure: submitFailure,
			cache: false
		};
		
		
		//now initialize the html editor
		var blogEditor = new YAHOO.widget.Editor('COMMENT', {
		    height: '120px',
		    width: '522px',
		    dompath: true, //Turns on the bar at the bottom
		    animate: true, //Animates the opening, closing and moving of Editor windows
		    toolbar: {
	        titlebar: 'Commento',
	        buttons: [
	            { group: 'textstyle', label: 'Font Style',
	                buttons: [
	                    { type: 'push', label: 'Grassetto', value: 'bold' },
	                    { type: 'push', label: 'Corsivo', value: 'italic' },
	                    { type: 'push', label: 'Sottolineato', value: 'underline' },
	                    { type: 'separator' },
	                    { type: 'select', label: 'Arial', value: 'fontname', disabled: true,
	                        menu: [
	                            { text: 'Arial', checked: true },
	                            { text: 'Arial Black' },
	                            { text: 'Comic Sans MS' },
	                            { text: 'Courier New' },
	                            { text: 'Lucida Console' },
	                            { text: 'Tahoma' },
	                            { text: 'Times New Roman' },
	                            { text: 'Trebuchet MS' },
	                            { text: 'Verdana' }
	                        ]
	                    },
	                    { type: 'spin', label: '13', value: 'fontsize', range: [ 9, 75 ], disabled: true },
	                    { type: 'separator' },
	                    { type: 'color', label: 'Colore del Carattere', value: 'forecolor', disabled: true }
	                ]
	            }
	        ]
   			}
		});
		if(!document.all)
			blogEditor.render();	
		else {
			document.getElementById("COMMENT").style.height="220px";
			document.getElementById("COMMENT").style.width="522px";
			document.getElementById("COMMENT").innerHTML = "Inserisci qui il tuo commento";
		}
		/**
		 * now we should automatically load the list of comments for this blog
		 **/
		_blog_loadCommentList();
	}
}

function _blog_loadCommentList() {
	var blogCommentList = document.getElementById("blogCommentList");
	var blogAddCommentForm = document.getElementById("blogAddComment");
	if(blogCommentList && blogAddCommentForm) {
		blogCommentList.innerHTML = ""; //remove any existing html inside of our comment list.
		
		var blogZone = document.getElementById("blogZone").value;
		var blogMenu = document.getElementById("blogMenu").value;
		var blogLang = document.getElementById("blogLang").value;
		
		var loadSuccess = function(ldr) {
			blogWaitDialog.hide();
			var menuList = ldr.responseXML.getElementsByTagName("menu");
			for(var i = (menuList.length-1); i >= 0; i--) {
				var cnt = blogCommentList.appendChild(document.createElement("div"));
				cnt.className = "blogCommentContainer";
				var body = cnt.appendChild(document.createElement("div"));
				body.className = "blogCommentBody";
				body.innerHTML = menuList[i].getAttribute("description");				
				var author = cnt.appendChild(document.createElement("div"));
				author.className = "blogCommentAuthor";
				author.innerHTML = menuList[i].getAttribute("name");
			}			
		}
		
		var loadFailure = function(ldr) {
			blogWaitDialog.hide();
			alert("I was unable to load the list of comments because of an error on the server");
		}
		
		var callback = {
			success: loadSuccess,
			failure: loadFailure,
			cache: false
		};				

		blogWaitDialog.show();
		var tx = YAHOO.util.Connect.asyncRequest("GET", "xml/menu_children.jsp?Z="+blogZone+"&M="+blogMenu+"&LNG="+blogLang, callback, null); 
	}
}