//Extend WYMeditor

 //selected content  ->  wym._iframe.contentWindow.getSelection();


WYMeditor.editor.prototype.buzzle = function() {
	var wym = this;
	
 //pre init zeugs 
	wym._options.toolsItems =  [{'name': 'Bold', 'title': 'Strong', 'css': 'wym_tools_strong'}, 
                     {'name': 'Italic', 'title': 'Emphasis', 'css': 'wym_tools_emphasis'},
                     {'name': 'Superscript', 'title': 'Superscript', 'css': 'wym_tools_superscript'},
                     {'name': 'Subscript', 'title': 'Subscript', 'css': 'wym_tools_subscript'},
                     {'name': 'InsertOrderedList', 'title': 'Ordered_List', 'css': 'wym_tools_ordered_list'},
                     {'name': 'InsertUnorderedList', 'title': 'Unordered_List', 'css': 'wym_tools_unordered_list'},
                     {'name': 'Indent', 'title': 'Indent', 'css': 'wym_tools_indent'},
                     {'name': 'Outdent', 'title': 'Outdent', 'css': 'wym_tools_outdent'},
                     {'name': 'Undo', 'title': 'Undo', 'css': 'wym_tools_undo'},
                     {'name': 'Redo', 'title': 'Redo', 'css': 'wym_tools_redo'},
                     {'name': 'Unlink', 'title': 'Unlink', 'css': 'wym_tools_unlink'},
                    ];
//buttons
  
	var html = "<li class='wym_tools_link'>"			//create link button
			+ "<a name='link' href='#' title='Hyperlink einf&uuml;gen'>"
			+ "Link"
			+ "</a></li>"
			+"<li class='wym_tools_image'>"		//upload image button
			+ "<a name='images' href='#' title='Bild einf&uuml;gen'>"
			+ "insert Image"
			+ "</a></li>"
			+ "<li class='wym_tools_floatLeft'>"		//float left button
			+ "<a name='floatLeft' href='#' title='Textumluss links'"
			+ " style='background-image:"
			+ " url(" + wym._options.basePath +"plugins/buzzle/icon_floatleft.gif)'>"
			+ "floatleft"
			+ "</a></li>"
			+ "<li class='wym_tools_floatRight'>"			//float right button
			+ "<a name='floatRight' href='#' title='Textumluss rechts'"
			+ " style='background-image:"
			+ " url(" + wym._options.basePath +"plugins/buzzle/icon_floatright.gif)'>"
			+ "floatright"
			+ "</a></li>"
			+ "<li class='wym_tools_table'>"			//insert table button
			+ "<a name='InsertTable' href='#' title='Tabelle einf&uuml;gen'>"
			+ "Table"
			+ "</a></li>";
		   
		   
	$(wym._box).find(wym._options.toolsSelector + wym._options.toolsListSelector).append(html);	//append buttons
		
		
	$(wym._box).find('li.wym_tools_link a').click(function(){					// addlink popup
		$("#buzzWym").html('<div>Url:<input id="linkurl" /></div>').dialog({
			title:'link einfuegen',
			buttons: {"Ok":function(){
						url = $('#linkurl').val();			
						wym.wrap('<a href='+url+'>', '</a>');
						$("#buzzWym").dialog('close');	
					 }
			} 
		});				
		
	});
	
	$(wym._box).find('li.wym_tools_table a').click(function(){			// tabelle einfuegen
		$("#buzzWym").dialog({ title:'tabelle einf&uuml;gen' });
		//code goes here
	});
	
	
	$(wym._box).find('li.wym_tools_floatLeft a').click(function(){			//floatleft
		wym.toggleClass("floatLeft","img");
	});
    $(wym._box).find('li.wym_tools_floatRight a').click(function(){			//floatright
		wym.toggleClass("floatRight","img");
	});

	
    $(wym._box).find('li.wym_tools_image a').click(function(){				    //upload image popup
		$.ajax({type: "POST",url: "lib/upload-file.php", data:{'getImages':'1'},success: function(data){
			$("#buzzWym").html(data);
			picAddDel();			
		}});		
        $("#buzzWym").dialog({
			title:'Bild einf&uuml;gen / hinaufladen',
			modal:false,
            width:550,
            height:400,
            dialogClass:'ajaxUpload',
		    buttons: {"Bild raufladen":function(){}},
            open:function(){ 
				$.each($("button"),function(i,val){
				        //alert($(this).children("span").html());
					if ($(this).children("span").html() == "Bild raufladen"){                                         
						new AjaxUpload($(this),{  
							action: 'lib/upload-file.php',                           
                            name: 'uploadfile',  			    //Name of the file input box  
                            onSubmit: function(file, ext){  
								if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){        // check for valid file extension                            
									 showError('Es sind nur JPG, PNG oder GIF Datein erlaubt.');  
									return false;  
                                }  
                                showLoader();
                            },  
                            onComplete: function(file, response){       //On completion clear the status 						
                                hideLoader();     		
                                if(response==="success"){    //Add uploaded file to list								 
                                    $("#buzzWym").append('<img src="data/thumbnails/'+file+'" alt="" />');
                                } else {  
                                    showError('Konnte das Bild nicht hochladen.'+response);
                                } 
								picAddDel();								
                            }  
                        }); 
                    }
                });
            }
	    });
	});

		
	
	  
//resize iframe 
if(wym._box){
	wym._iframe.height=wym._box.height()-40;
	wym._iframe.width=wym._box.width()+20;
}
	
	
//wenn no ka helper div irgndwo is appenden
if($('body').find("#buzzWymHelp").length == 0) 
{
$('body').append('<div id="buzzWymHelp"></div>');
}



function picAddDel()
{

$.each($("#buzzWym").find("img"), function(){
				if(!$(this).hasClass('del'))
				{
					$(this).addClass('del');
					$(this).before('<div style="background-color:red;width:40px;height:30px;position:absolute;"id="buzzWymDelImg" ></div>');					
				}
			});
			$("body").delegate("#buzzWymDelImg","click",function(){
				var name = $(this).next("img").attr('src');
				$(this).next("img").detach();
				$(this).detach();
				$.ajax({type: "POST",url: "lib/upload-file.php", data:{delImage:name},success: function(data){
				}});
				return false;
			}); 


}



function showNotice(text)
{
$('#buzzWymHelp').html(text).dialog({
	modal:true,
	title:'Info',
	buttons:{
		"ok":function(){ 
			$(this).dialog('close');		
		},
	}
});

}


function showError(text)
{
$('#buzzWymHelp').html(text).dialog({
	modal:true,
	title:'Sorry...',
	buttons:{
		"ok":function(){ 
			$(this).dialog('close');		
		},
	}
});
}


function showLoader()
{
$('#buzzWymHelp').html('<div><center><img src=' + wym._options.basePath +'plugins/buzzle/ajax-loader.gif></center></div>').dialog({
	modal:true,
	title:'loading...'
}).appendTo("body");
}

function hideLoader()
{
$('#buzzWymHelp').dialog('close');
}


};
