dojo.require("dijit.Editor");
dojo.require("dijit._editor.plugins.AlwaysShowToolbar");
dojo.require("dijit._editor.plugins.EnterKeyHandling");
dojo.require("dijit._editor.plugins.FontChoice"); 

function uploadFormInit(){

	var queryList = dojo.query(".popup");
	dojo.forEach(queryList, function(popupElement){
		var closeButtons = dojo.query(".btn-close, .btn-cancel", popupElement);
		dojo.forEach(closeButtons, function(closeButton){
			dojo.connect(closeButton, "onclick", function(domEvent){
				dojo.stopEvent(domEvent);
				if(!dojo.hasClass(closeButton, "btn-cancel-disabled"))
				{
					//popupElement.parentNode.removeChild(popupElement);
					hidePopup();
					var bodies = dojo.query(".ad-body-popupOpen");
					dojo.forEach(bodies, function(body){
						dojo.removeClass(body, "ad-body-popupOpen");
					});
				}
			
			});
		});
		var imgs = dojo.query(".imgHolder img");
		dojo.forEach(imgs, function(img){
			function repositionImage(){
				var totalHeight = dojo.style(img.parentNode, "height");
				var imgHeight = dojo.style(img, "height");
				dojo.style(img , "marginTop", ((totalHeight - imgHeight) / 2)+"px");
			}
			if(dojo.style(img, "height")){
				repositionImage();
			} else {
				dojo.connect(img, "onload", repositionImage);
			}
		});
	});
	
	
	queryList = dojo.query(".inputPhotos .imageHolder img");
	dojo.forEach(queryList, function(tinyImage){
		function repositionImage(){
			var totalHeight = dojo.style(tinyImage.parentNode, "height");
			var imgHeight = dojo.style(tinyImage, "height");
			dojo.style(tinyImage , "marginTop", ((totalHeight - imgHeight) / 2)+"px");
		}
		
		if(dojo.style(tinyImage, "height")){
			repositionImage();
		} else {
			dojo.connect(tinyImage, "onload", repositionImage);
		}
	});
}

function addEditor(){

	var programmaticElement = dojo.byId('programmatic');
	
	// Bail out?
	if(!programmaticElement) {
		return;
	}
	
	var targetHeight = dojo.style("descriptionData", "height");
	dojo.byId('programmatic').innerHTML = dojo.byId("descriptionData").value;
	
	var dijitEditor = new dijit.Editor({plugins:[{name:'dijit._editor.plugins.FontChoice', command:'fontSize', custom:[1,2,3]},'|','bold','italic','|','insertOrderedList','insertUnorderedList'], height: targetHeight+"px"}, dojo.byId('programmatic')); 
	dojo.style('descriptionData', "display", "none");
	
	dojo.connect(dijitEditor, "onDisplayChanged", function(eventInfo){
		dojo.byId("descriptionData").value = dijitEditor.getValue();
	});
}

dojo.addOnLoad(addEditor);
dojo.addOnLoad(uploadFormInit);

