﻿(function($) {
	$.fn.StErrorPopup = function(settings) {
 	settings = jQuery.extend({
		inlineValidation: true,	
		scroll:true,
		promptPosition: "topRight"	// OPENNING BOX POSITION, IMPLEMENTED: topLeft, topRight, bottomLeft, centerRight, bottomRight
	}, settings);	
	$.StErrorPopup.settings = settings;
};	
$.StErrorPopup = {
defaultSetting: function(caller) {		// NOT GENERALLY USED, NEEDED FOR THE API, DO NOT TOUCH
    settings = {
        inlineValidation: true,
        scroll: true,
        promptPosition: "topRight" // OPENNING BOX POSITION, IMPLEMENTED: topLeft, topRight, bottomLeft, centerRight, bottomRight
    }
    $.StErrorPopup.settings = settings;
},
buildPrompt: function(caller, promptText) {			// ERROR PROMPT CREATION AND DISPLAY WHEN AN ERROR OCCUR
		if(!$.StErrorPopup.settings){
			$.StErrorPopup.defaultSetting()
		}
		deleteItself = "." + $(caller).attr("id") + "formError"
	
		if($(deleteItself)[0]){
			$(deleteItself).stop();
			$(deleteItself).remove();
		}
		var divFormError = document.createElement('div');
		var formErrorContent = document.createElement('div');
		linkTofield = $.StErrorPopup.linkTofield(caller)
		$(divFormError).addClass("formError")
		
		$(divFormError).addClass(linkTofield);
		$(formErrorContent).addClass("formErrorContent");
		
		$("body").append(divFormError);
		$(divFormError).append(formErrorContent);
			
		if($.StErrorPopup.showTriangle != false){		// NO TRIANGLE ON MAX CHECKBOX AND RADIO
			var arrow = document.createElement('div');
			$(arrow).addClass("formErrorArrow");
			$(divFormError).append(arrow);
			if($.StErrorPopup.settings.promptPosition == "bottomLeft" || $.StErrorPopup.settings.promptPosition == "bottomRight"){
			$(arrow).addClass("formErrorArrowBottom")
			$(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($.StErrorPopup.settings.promptPosition == "topLeft" || $.StErrorPopup.settings.promptPosition == "topRight"){
				$(divFormError).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>');
			}
		}
		$(formErrorContent).html(promptText)
	
		callerTopPosition = $(caller).offset().top;
		callerleftPosition = $(caller).offset().left;
		callerWidth =  $(caller).width();
		inputHeight = $(divFormError).height();
	
		/* POSITIONNING */
		if($.StErrorPopup.settings.promptPosition == "topRight"){callerleftPosition +=  callerWidth -30; callerTopPosition += -inputHeight -10; }
		if($.StErrorPopup.settings.promptPosition == "topLeft"){ callerTopPosition += -inputHeight -10; }
		
		if($.StErrorPopup.settings.promptPosition == "centerRight"){ callerleftPosition +=  callerWidth +13; }
		
		if($.StErrorPopup.settings.promptPosition == "bottomLeft"){
			callerHeight =  $(caller).height();
			callerleftPosition = callerleftPosition;
			callerTopPosition = callerTopPosition + callerHeight + 15;
		}
		if($.StErrorPopup.settings.promptPosition == "bottomRight"){
			callerHeight =  $(caller).height();
			callerleftPosition +=  callerWidth -30;
			callerTopPosition +=  callerHeight + 15;
		}
		$(divFormError).css({
			top:callerTopPosition,
			left:callerleftPosition,
			opacity:0
		})
		return $(divFormError).animate({"opacity":0.87},function(){return true;});	
	},
	updatePromptText : function(caller,promptText,type) {	// UPDATE TEXT ERROR IF AN ERROR IS ALREADY DISPLAYED
		
		linkTofield = $.StErrorPopup.linkTofield(caller);
		var updateThisPrompt =  "."+linkTofield;
		
		$(updateThisPrompt).find(".formErrorContent").html(promptText);
		callerTopPosition  = $(caller).offset().top;
		inputHeight = $(updateThisPrompt).height();
		
		if($.StErrorPopup.settings.promptPosition == "bottomLeft" || $.StErrorPopup.settings.promptPosition == "bottomRight"){
			callerHeight =  $(caller).height();
			callerTopPosition =  callerTopPosition + callerHeight + 15;
		}
		if($.StErrorPopup.settings.promptPosition == "centerRight"){  callerleftPosition +=  callerWidth +13;}
		if($.StErrorPopup.settings.promptPosition == "topLeft" || $.StErrorPopup.settings.promptPosition == "topRight"){
			callerTopPosition = callerTopPosition  -inputHeight -10;
		}
		$(updateThisPrompt).animate({ top:callerTopPosition });
	},
	linkTofield : function(caller){
		linkTofield = $(caller).attr("id") + "formError";
		linkTofield = linkTofield.replace(/\[/g,""); 
		linkTofield = linkTofield.replace(/\]/g,"");
		return linkTofield;
	},
	closePrompt : function(caller,outside) {						// CLOSE PROMPT WHEN ERROR CORRECTED
		if(!$.StErrorPopup.settings){
			$.StErrorPopup.defaultSetting()
		}
		if(outside){
			$(caller).fadeTo("fast",0,function(){
				$(caller).remove();
			});
			return false;
		}
		linkTofield = $.StErrorPopup.linkTofield(caller);
		closingPrompt = "."+linkTofield;
		$(closingPrompt).fadeTo("fast",0,function(){
			$(closingPrompt).remove();
		});
	}
}
})(jQuery);
