var myEditor;

myEditor = new YAHOO.widget.SimpleEditor('text1', { 
	width: '450px', height: '60px', dompath: true, animate: false,
	toolbar: { 		
		buttons: [ 
			{group: 'textstyle', 
			buttons: [ 
				{type: 'select', label: 'Adobe Garamond Pro', value: 'namef',  
					menu: [	
							 {text: 'Adobe Garamond Pro'}
							,{text: 'Arial'}
							,{text: 'Arial Black'}
							,{text: 'Bell Gothic Std Black'}
							,{text: 'Bell MT'}
							,{text: 'Berlin Sans FB'}
							,{text: 'Boopee'}
							,{text: 'Cooper Black Std'}
							,{text: 'Comic Sans MS'}
							,{text: 'Courier New'}
							,{text: 'Garamond'}
							,{text: 'Georgia'}
							,{text: 'Gill Sans Ultra Bold'}
							,{text: 'Helvetica'}
							,{text: 'Hurry Up'}
							,{text: 'Old English'}
							,{text: 'Segoe Print'}
							,{text: 'Tahoma'}
							,{text: 'Times Roman'}
							,{text: 'Trebuchet MS'}
							,{text: 'Verdana'}
							
						]}, 
				/*{type: 'spin', label: '13', value: '13', range: [ 9, 75 ]}, */
				{type: 'select', label: '6', value: 'fontsize',  
					menu: [							
							{text: '6'},
							{text: '7'},
							{text: '8'},
							{text: '9'},
							{text: '10'},
							{text: '11'},
							{text: '12'},
							{text: '13'},
							{text: '14'},
							{text: '15'},
							{text: '16'},
							{text: '17'},
							{text: '18'},
							{text: '19'},
							{text: '20'},
							{text: '21'},
							{text: '22'},
							{text: '23'},
							{text: '24'},
							{text: '25'},
							{text: '26'},
							{text: '27'},
							{text: '28'},
							{text: '29'},
							{text: '30'},
							{text: '31'},
							{text: '32', checked: true},
							{text: '33'},
							{text: '34'},
							{text: '35'},							
							{text: '36'},
							{text: '37'},
							{text: '38'},
							{text: '39'},
							{text: '40'},
							{text: '41'},
							{text: '42'},
							{text: '43'},
							{text: '44'},
							{text: '45'}
						]},
				{type: 'separator'}, 
				{type: 'color', label: 'Font Color', value: 'forecolor pick', disabled:false},

				{type: 'separator'},

				{type: 'push', label: 'Bold', value: 'bold'}, 
				{type: 'push', label: 'Italic', value: 'italic'}
				//{type: 'push', label: 'Underline', value: 'underline'}				

			]}, 
			{group: 'textstyle1', 
			buttons: [ {type: 'push', label: 'Align Left CTRL + SHIFT + [', value: 'justifyleft'}, {type: 'push', label: 'Align Center CTRL + SHIFT + |', value: 'justifycenter'}, {type: 'push', label: 'Align Right CTRL + SHIFT + ]', value: 'justifyright'} ]}
		]}
		
		
	}); 

myEditor.on('toolbarLoaded', function() {
	
	
	this.toolbar.on('fontsizeClick', function(o) { 
			if (curren_inscription != null)
			{
				var txt = myEditor.getEditorHTML();
				var q = getRequestArray(txt);
				q['fontsize'] = o.button.value;				
				Lightbox.get_html('ajax_add_inscription',q);
				Lightbox.get_html_callback = function()
				{
					inscriptions[curren_inscription]['q'] = q;
					updatePicture(txt, script_url + 'upload/' + Lightbox.req.responseJS.filename, Lightbox.req.responseJS.width, Lightbox.req.responseJS.height, curren_inscription);
				}
			}
	        //alert(o.button.value); 
	        return false;
	    }); 	   
	   
	this.toolbar.on('namefClick', function(o) { 
		
		//alert('123');
        if (curren_inscription != null)
		{
			var txt = myEditor.getEditorHTML();
			var q = getRequestArray(txt);
			Lightbox.get_html('ajax_add_inscription',q);
			Lightbox.get_html_callback = function()
			{
				inscriptions[curren_inscription]['q'] = q;
				updatePicture(txt, script_url + 'upload/' + Lightbox.req.responseJS.filename, Lightbox.req.responseJS.width, Lightbox.req.responseJS.height, curren_inscription);
			}
		}
			
	});	    
	    
	this.toolbar.on('colorPickerClicked', function(o) { 
			cur_c = o.color;
			if (curren_inscription != null)
			{
				var txt = myEditor.getEditorHTML();
				var q = getRequestArray(txt);
				q['color'] = o.color;				
				Lightbox.get_html('ajax_add_inscription',q);
				Lightbox.get_html_callback = function()
				{
					inscriptions[curren_inscription]['q'] = q;
					updatePicture(txt, script_url + 'upload/' + Lightbox.req.responseJS.filename, Lightbox.req.responseJS.width, Lightbox.req.responseJS.height, curren_inscription);
				}
			}
	        //alert(o.color); 
	        return false;
	    });     
	
	this.toolbar.on('buttonClick', function(o) {
			//alert(o.button.value);
	        switch(o.button.value) {
	            case 'bold':
	            {
	                //Bold Code Here
	                italic = false;
	                if (bold) bold = false;
	                else bold = true;
	                underline = false;	                
	                break;
	            }
	            case 'italic':
	            {
	            	if (italic) italic = false;
	                else italic = true;
	                bold = false;
	                underline = false;	                
	                break;
	            }
	            case 'underline':
	            {
	                //My Button 2 Code Here
	                italic = false;
	                bold = false;
	                if (underline) underline = false;
	                else underline = true;	                
	                break;
	            }
	            case 'justifyleft':
	            {
	            	align = 0;
	            	break;
	            }
	            case 'justifycenter':
	            {
	            	align = 2;
	            	break;
	            }
	            case 'justifyright':
	            {
	            	align = 1;
	            	break;
	            }
	                
	        }
	        sendReq();
	        return true;
	    });
});

myEditor.render(); 

function getRequestArray(txt)
{
	var font = myEditor.toolbar.getButtonByValue('namef')._button.value;
	var fontsize = myEditor.toolbar.getButtonByValue('fontsize')._button.value;
	var color = cur_c;
	var to_ret = {'txt' : txt, 'font' : font, 'fontsize' : fontsize, 'color' : color, 'bold' : bold, 'italic' : italic, 'underline' : underline,'align' : align};
	return to_ret;
}

function sendReq()
{
	if (curren_inscription != null)
	{
		var txt = myEditor.getEditorHTML();
		var q = getRequestArray(txt);
		Lightbox.get_html('ajax_add_inscription',q);
		Lightbox.get_html_callback = function()
		{
			if (Lightbox.req.responseJS.filename != undefined)
			{
				inscriptions[curren_inscription]['q'] = q;
				updatePicture(txt, script_url + 'upload/' + Lightbox.req.responseJS.filename, Lightbox.req.responseJS.width, Lightbox.req.responseJS.height, curren_inscription);
			}
		}
	}
}