(function($) {
$.fn.ILP_bubble = function(settings) {
	var classes = $(this).attr("class");
	var classSettings = {};
	if(classes.indexOf("ilpBubbleProp[") != -1) {
		classes = classes.substr(classes.indexOf("ilpBubbleProp["));
		classes = classes.substr(classes.indexOf("[")+1,classes.indexOf("]")-classes.indexOf("[")-1);
		var classArray = classes.split(",");
		for(i=0;i<classArray.length;i++) {
			var settingArray = classArray[i].split(":");
			classSettings[settingArray[0]] = settingArray[1];
		}
	}

	var settings = jQuery.extend({
		promptPosition: "topRight",
		extraClass: "green_popup",
		marginVertical: - 75,
		marginHorizontal: 2		
	}, classSettings, settings);	
	$.ILP_bubble.settings = settings;
	$(this).mouseenter(function(caller) {
		$.ILP_bubble.settings = settings;
		$.ILP_bubble.buildBubble(this,$(this).html());
	});
	$(this).mouseleave(function(caller) {
		$.ILP_bubble.hideBubble(this);
	});
}

$.ILP_bubble = {
	hideBubble : function(caller) {
		var linkTofield = $.ILP_bubble.linkTofield(caller);
		if($("div."+linkTofield).size() != 0) {
			return $("div."+linkTofield).animate({"opacity":0},function(){return true;});
		}
	},
	buildBubble : function(caller,promptText) {			// ERROR PROMPT CREATION AND DISPLAY WHEN AN ERROR OCCUR
		if(!$.ILP_bubble.settings){
			$.ILP_bubble.defaultSetting()
		}
		var linkTofield = $.ILP_bubble.linkTofield(caller);
		if($("div."+linkTofield).size() != 0) {
			return $("div."+linkTofield).animate({"opacity":1},function(){return true;});
		}
		var divILPBubble = document.createElement('div');
		var divILPBubbleContent = document.createElement('div');
		
		$(divILPBubble).addClass("ilp_bubble")
		
		$(divILPBubble).addClass($.ILP_bubble.settings.extraClass);
		
		$(divILPBubble).addClass(linkTofield);
		$(divILPBubbleContent).addClass("ilp_bubble_content");
		
		$("body").append(divILPBubble);
		$(divILPBubble).append(divILPBubbleContent);

		var arrow = document.createElement('div');
		$(arrow).addClass("ilp_bubble_arrow");
		$(divILPBubble).append(arrow);
		
		if($.ILP_bubble.settings.promptPosition == "bottomLeft" || $.ILP_bubble.settings.promptPosition == "bottomRight"){
			$(arrow).addClass("ilp_bubble_arrow_bottom")
			$(arrow).html('<div class="line1"><!-- --></div><div class="line2"><!-- --></div><div class="line3"><!-- --></div><div class="line4"><!-- --></div><div class="line5"><!-- --></div><div class="line6"><!-- --></div><div class="line7"><!-- --></div><div class="line8"><!-- --></div><div class="line9"><!-- --></div><div class="line10"><!-- --></div>');
		}
		if($.ILP_bubble.settings.promptPosition == "topLeft" || $.ILP_bubble.settings.promptPosition == "topRight"){
			$(divILPBubble).append(arrow);
			$(arrow).html('<div class="line10"><!-- --></div><div class="line9"><!-- --></div><div class="line8"><!-- --></div><div class="line7"><!-- --></div><div class="line6"><!-- --></div><div class="line5"><!-- --></div><div class="line4"><!-- --></div><div class="line3"><!-- --></div><div class="line2"><!-- --></div><div class="line1"><!-- --></div>');
		}
		
		$(divILPBubbleContent).html(promptText)
	
		callerTopPosition = $(caller).offset().top;
		callerleftPosition = $(caller).offset().left;
		callerWidth =  $(caller).width();
		callerHeight = $(divILPBubble).height();
	
		/* POSITIONNING */
		if($.ILP_bubble.settings.promptPosition == "topRight"){callerleftPosition +=  callerWidth + $.ILP_bubble.settings.marginVertical; callerTopPosition += -callerHeight + $.ILP_bubble.settings.marginHorizontal; }
		if($.ILP_bubble.settings.promptPosition == "topLeft"){ callerTopPosition += -callerHeight + $.ILP_bubble.settings.marginHorizontal; }
		
		if($.ILP_bubble.settings.promptPosition == "centerRight"){ callerleftPosition +=  callerWidth + $.ILP_bubble.settings.marginVertical; }
		
		if($.ILP_bubble.settings.promptPosition == "bottomLeft"){
			callerHeight =  $(caller).height();
			callerleftPosition = callerleftPosition;
			callerTopPosition = callerTopPosition + callerHeight + $.ILP_bubble.settings.marginHorizontal;
		}
		if($.ILP_bubble.settings.promptPosition == "bottomRight"){
			callerHeight =  $(caller).height();
			callerleftPosition +=  callerWidth + $.ILP_bubble.settings.marginVertical;
			callerTopPosition +=  callerHeight + $.ILP_bubble.settings.marginHorizontal;
		}
		$(divILPBubble).css({
			top:callerTopPosition,
			left:callerleftPosition,
			opacity:0
		})
		return $(divILPBubble).animate({"opacity":1},function(){return true;});	
	},
	linkTofield : function(caller){
		linkTofield = $(caller).attr("id");
		if(linkTofield == "" ) {
			linkTofield = $.ILP_bubble.genId();
			$(caller).attr("id",linkTofield);
		}
		linkTofield = "ilp_bubble_" + linkTofield;
		linkTofield = linkTofield.replace(/\[/g,""); 
		linkTofield = linkTofield.replace(/\]/g,"");
		return linkTofield;
	},
	genId : function(string_length) {
		var chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
		string_length = string_length ? string_length : 8;
		var randomstring = '';
		for (var i=0; i<string_length; i++) {
			var rnum = Math.floor(Math.random() * chars.length);
			randomstring += chars.substring(rnum,rnum+1);
		}
		return randomstring;
	}
}
})(jQuery);