(function($){
 
    $.fn.extend({ 
         
        updateWidget: function() {

	    // Update the server with info about this widget
 
            return this.each(function() {


		var top = $(this).css('top');            
		var left = $(this).css('left');            
		var width = $(this).css('width');            
		var height = $(this).css('height');            

		var text = '';
		if ( $(this).hasClass('text')) {

			text = $(this).html();
		}

		var src = '';
		if ( $(this).hasClass('image')) {

			try {
				src = $(this).children('img:first-child').attr('src').replace(/^\/templates\/\d{1,5}\/images\//, '');
			} catch(ex) {
			}
		}

		var link = "";

		var domid = this.id;
		var domainname = document.domain;

		$.ajax({

			url: "update.php",
			data: "domid="+ domid +"&top="+ top  +"&left="+ left +"&width="+ width +"&height="+ height +"&text="+ text +"&src="+ src +"&link="+ link,
			error: function() {alert("Error updating server with new widget position!");},
		});


            });
        },

	deleteWidget: function() {

		// kill this widget

            return this.each(function() {

		var domid = this.id;
		var domainname = document.domain;
             

		$.ajax({

			url: "update.php",
			data: "delete=1&domainname="+ domainname +"&domid="+ domid, 
			error: function() {alert("Error updating server with new widget position!");},
		});
		
            });


	},

	getWidgetID: function(sugg) {

	    // get a unique domid for this widget


		var domainname = document.domain;
             


		var response = $.ajax({

			url: "getWidgetID.php",
			async: false,
			data: {domainname: domainname, sugg: sugg},
			error: function() {alert("Error updating server with new widget position!");},
		}).responseText.replace(/\n/, '');

		return response;



	},


    });
     

})(jQuery);

