;baseUrlQurey = "/iibbase"; //SETUP NEEDED PATH
rootUrl = location.protocol+"//"+location.hostname + baseUrlQurey;
jsBase = rootUrl+"/Iibjs";

/**
 * Initialization on load
 */
(function(jQ) {
	/**
	 * Error alert dialog
	 */
	var alrtDiv = null;
	var frMess = null;
	/**
	 * Log or alert
	 */
	jQ.log = function(message) {
  		if(window.console) {
     		console.log(message);
  		} else {
  			jQ.alert(message);
  		}
	};
	
	jQ.alert = function(mess){
		if(alrtDiv == null){
			alrtDiv = jQ("<div></div>").css("display", "none").attr("id", "dialog").attr("title", "Error!").
				append(jQ("<span></span>").addClass("ui-icon ui-icon-alert").css({'float':'left', 'margin':'0 7px 20px 0'})).
				append(jQ("<p></p>"));
			jQ("body").append(alrtDiv);
		}
		//insert new content
		jQ("p", alrtDiv).text(mess);
		//show the dialog
		alrtDiv.dialog({
			bgiframe: true,
			modal: true,
			buttons: {
				Ok: function() {
					jQ(this).dialog('close');
				}
			}
		});
		alrtDiv.dialog('open');
	};
	/**
	 * Shows small message  on the pot of the page
	 */
	jQ.friendlyMessage = function(mess){
		frMess = jQ("<div><div>").addClass("topFloafingMessage").text(mess).appendTo(jQ("body")).fadeIn("slow", function(){
			setTimeout(function() {
				jQ(frMess).fadeOut("slow",function(){
						jQ(frMess).remove();
					});
			} , 2500);
		});
	}
	
	jQ.addJs = function(srcUrl){ //alert(srcUrl);
		$("<script></script>").attr({
			type: "text/javascript",
			src: srcUrl
		}).appendTo("head");
	};
	
	jQ.redirect = function(url){
		document.location = url;
	}
})(jQuery);

/**
 * Ajax functionality
 */
(function(jQ) {
	/**
	 * Images
	 */
	jQ.STD_LOAD_IMG = jQ("<img></img>").attr("src",jsBase+"/images/stdLoader.gif");;
	/**
	 * default options
	 */
	var Defaults = {
		url: document.location,
	 	data: {},
	 	type: "POST", 
	 	dataType: "json",
	 	cache: true, 
	 	contentType: "application/x-www-form-urlencoded", 
	 	global: false,
	 	timeout: 10000,
	 	//actions
//	 	function (data, type) {
//		  // do something
//		  // return the sanitized data
//		  return data;
//		}
	 	dataFilter: false,
	 	//events
//	 	function (XMLHttpRequest, textStatus) {
//		  this; // the options for this ajax request
//		}
	 	beforeSend: false, 
	 	complete: false,
//	 	function (XMLHttpRequest, textStatus, errorThrown) {
//		  // typically only one of textStatus or errorThrown 
//		  // will have info
//		  this; // the options for this ajax request
//		}
	 	error: false, 
//	 	function (data, textStatus) {
//		  // data could be xmlDoc, jsonObj, html, text, etc...
//		  this; // the options for this ajax request
//		}
	 	success: false,
	 	
	 	//security
	 	username: false,
	 	password: false
	 };
	/**
	 * Options that will be used for ajax work
	 */
	var Options = new Object();
	/**
	 * The object of an element that is requesting ajax involvment.
	 */
	var elObject = null;
	/**
	 * Process ajax request
	 */
	jQ.fn.ijajax = function(options, obj){
		//save the object to internal.
		elObject = obj;
		Options = jQ.extend({}, Defaults, options);
		//we are relying on the same design pattern of an element object
		// it means that all the needed event function are predefined.
		//setup all needed events handlers
		Options.beforeSend = function(XMLHttpRequest){
			if(jQ.isFunction(elObject.onBeforeSend)){
				return elObject.onBeforeSend();
			}
		};
		Options.success = function(data, textStatus){
			if(jQ.isFunction(elObject.onSuccess)){
				return elObject.onSuccess(data);
			}
		};
		Options.error = function(XMLHttpRequest, textStatus, errorThrown){
			if(jQ.isFunction(elObject.onError)){
				return elObject.onError(XMLHttpRequest.responseText);
			}
		};
		//called after the request finishes (after success or error are executed)
		Options.complete = function(XMLHttpRequest, textStatus){
			if(jQ.isFunction(elObject.onComplete)){
				return elObject.onComplete();
			}
		};
		//alert(Options.url);
		//send
		jQ.ajax(Options);
	}
})(jQuery);

//FORMS
(function(jQ){
	jQ.Form = {
		obj:null,
		validate:function(){
			jQ(":input", jQ.Form.obj).each(function(){
				jQ.Form.input.validate(this);
			});
		},
		input:{
			validate:function(inObj){
				min = jQ(inObj).attr("min");
				if(typeof min == 'undefined'){
					return;
				}
				if(jQ(inObj).val().length < min){
					jQ(inObj).addClass("notValidInput");
				} else {
					jQ(inObj).removeClass("notValidInput");
				}
			}
		}
	};

	jQ.fn.Form = function(){
		//check if the element is a form
		if(!jQ(this).is("form")){
			$.alert("Expecting form element!");
			return this;
		}
		jQ.Form.obj = this;
		jQ(":input", jQ.Form.obj).each(function(){
			//validate each on blur
//			jQ(this).blur(function(){
//				jQ.Form.input.validate(this);
//			});
		});
		jQ(this).submit(function(){
			jQ.Form.validate();
			if(jQ(".notValidInput", jQ.Form.obj).length > 0){
				$.alert("Not valid data!");
				return false;
			}
			return true;
		});
	};

})(jQuery);
////////////////////
//END FORMS
////////////////////