$ = jQuery;
jQuery.fn.extend({
    admin : function(){

		//reposition edit images;
		function rePos(elems){
			$.each($(elems),function(){

				var id = $(this).attr('id');
				var pos = $(this).position();
				var width = $(this).width();

				//Edit Img
				$("#admin_" + id)
				//position it
				.css({

					position : "absolute",
					top: pos.top + 2,
					left: pos.left + width - 18,
					opacity : 0.7

				})
			});
		}
		if($(this).css('position') == "static")
			$(this).css('position','relative');

		var php_script = "inc/ajax_handler.php";
		var modifyImg = '<img src="img/edit.png" title="Modifier" class="edit" alt="edit" />';


		$.each($(this).find('.admin'),function(){

			var pos = $(this).position();
			var width = $(this).width();

			//make it editable
            $(this).hover(
                function(){
                    //$(this).attr('contentEditable','true');
                    //$(this).attr('designMode','on');
                    $(this).css('border','1px dotted gray');
                    var id = $(this).attr('id');
                    $('[id=admin_'+id+']').fadeIn('fast');
                },
                function(){
                    //$(this).attr('contentEditable','false');
                    //$(this).attr('designMode','off');
                    $(this).css('border','0px dotted gray');
                    var id = $(this).attr('id');
                    //$('#admin_'+id).fadeOut('fast');
                }
            );


			var id = $(this).attr('id');

			//Edit Img
			$(modifyImg)

			//add specific id
			.attr('id',"admin_"+id)

			//position it
			.css({

				position : "absolute",
				top: pos.top + 2,
				left: pos.left + width - 18,
				opacity : 0.7

			})

			//insert it
			.insertAfter(this)

			//click on edit
			.click(

				function(){
					//get the id of the element to be modified
					var editImg = this;
					var id = $(editImg).attr('id').replace('admin_','');
					var tag = $('#' + id).get(0).nodeName;
					if(tag != "IMG"){
                        $('#savebtn').attr('rel',id);
                        $('#savebtn').unbind('click').click(function(){
                            //if(!confirm("Êtes vous sûre de vouloir sauvegarder ?")) return false;
                            hideModif();
                            html = editor.getData();
							console.log(html);
                            html = html.replace(/<p>/g,"");
                            html = html.replace(/<\/p>/g,"<br>");
							console.log(html);
                            $('[id=' + id + ']').html(html);
                            $(editImg).attr('src','img/loader.gif');
                            var data = {};
                            data[id] = html;
                            $.post(php_script, data ,function(){
                                $(editImg).attr('src','img/accept.png');
                                setTimeout(function(){
                                    $(editImg).attr('src','img/edit.png');
                                },3000)
                            });
                        });
						var html = $('#' + id).html();//.replace(/&lt;/g,'<').replace(/&gt;/g,'>');;
                        editor.setData(html);
                        showModif();
                        return false;
						//if it's an image
					}else{
						$('.upload-div iframe.upload-iframe').attr('src','upload.html').load(function(){
							$(this).contents().find('.upload-form').attr('action',php_script);
							$(this).contents().find('.upload-input').attr('name',id);
						});

						$('.upload-div').fadeIn();

						$('.upload-mask').css('opacity','0.7').fadeIn();
						$('.upload-mask, .close-upload').click(function(){
							$('.upload-mask').fadeOut();
							$('.upload-div').fadeOut();
						});
					}
			        return false;
				}

			);

			$(this).bind('keyup load',function(){
				rePos($(this).parent().find('.admin'));
			});

		});

	}
});
