/*
 * Resize extension
 */
(function($,h,c){var a=$([]),e=$.resize=$.extend($.resize,{}),i,k="setTimeout",j="resize",d=j+"-special-event",b="delay",f="throttleWindow";e[b]=250;e[f]=true;$.event.special[j]={setup:function(){if(!e[f]&&this[k]){return false}var l=$(this);a=a.add(l);$.data(this,d,{w:l.width(),h:l.height()});if(a.length===1){g()}},teardown:function(){if(!e[f]&&this[k]){return false}var l=$(this);a=a.not(l);l.removeData(d);if(!a.length){clearTimeout(i)}},add:function(l){if(!e[f]&&this[k]){return false}var n;function m(s,o,p){var q=$(this),r=$.data(this,d);r.w=o!==c?o:q.width();r.h=p!==c?p:q.height();n.apply(this,arguments)}if($.isFunction(l)){n=l;return m}else{n=l.handler;l.handler=m}}};function g(){i=h[k](function(){a.each(function(){var n=$(this),m=n.width(),l=n.height(),o=$.data(this,d);if(m!==o.w||l!==o.h){n.trigger(j,[o.w=m,o.h=l])}});g()},e[b])}})(jQuery,this);

// namespace
this.com = this.com || {};
com.jobrapido = {};

// The logger can be enabled setting logLevel
com.jobrapido.Logger = function() {
	var _logLevel = "";
	var _enabled =  true;

	// ------------
	//  PUBLIC API
	// ------------
	this.setLevel = function(level) {
		_logLevel = level;
	}
	
	this.error = function() {
	    _log("error", arguments);
	}
	
	this.warn = function() {
	    if (_logLevel != "error" && _logLevel!="") _log("warn", arguments);
	}
	
	this.info = function() {
	    if (_logLevel != "warn" && _logLevel != "error" && _logLevel!="") _log("info", arguments);
	}
	
	this.debug = function() {
	    if (_logLevel == "debug" && _logLevel!="") _log("debug", arguments);
	}
	
	// ---------------
	//    PRIVATE
	// ---------------
	function _log(level, arguments) {
	    if (window.console) {
		    try {
		    	if (typeof  window.console[level] === "function") {
		        	window.console[level].apply(window.console, arguments);
		    	} else {
			    	window.console.log(level+":"+arguments);
		    	}
		    } catch(e) {
		    }
	    }
	};
}


// Jobrapido Web Toolkit
com.jobrapido.WebToolkit = function() {
	var _istances = [];
	// ------------
	//  PUBLIC API
	// ------------
	this.$ = function(selector, context) {
		if (!context) {
			context = document;
		}
		return jQuery(selector, context);
	}
		
	this.$DataTable = function(selector, options) {
		return _getInstance("datatable",selector, options);
	}
	
	this.$Form = function(selector, options) { 
		return _getInstance("form",selector, options);
	}
	
	this.$NotificationArea = function(selector, options) {
		return _getInstance("notificationarea",selector, options);
	}

	// ---------------
	//    PRIVATE
	// ---------------
	var _getInstance = function(type, selector, options) {
		if (_istances[selector]==undefined) {
			switch(type) {
				case "datatable":
					_istances[selector] = new com.jobrapido.datatable.DataTable(jQuery(selector), selector, options);
				break;
				case "form":
					_istances[selector] = new com.jobrapido.form.Form(jQuery(selector), options);
				break;
				case "notificationarea":
					_istances[selector] = new com.jobrapido.notification.NotificationArea(jQuery(selector), options);
				break;
			}
		}
		return _istances[selector];
	}
}

// GLOBAL OBJECTS
var log = new com.jobrapido.Logger();
var jrt = new com.jobrapido.WebToolkit();

/*
 * String extension
 *
 For perfomance reason this function is directly written in Dialog.Jobletter.JS
 (function(){ 
	String.prototype.capitalize = function(){
		return this.replace(/\b([^-\s]*)\b/g, function(word){
			return upperFirstLetter(lower(word));
		})
	}
    
	function lower(word){
		return word.toLowerCase();
	}
    
	function upperFirstLetter(word){
		return word.substr(0,1).toUpperCase() + word.substr(1);
	}
})();
*/
 
/*
 * Date extension
 */
Date.dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
Date.abbrDayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
Date.monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
Date.abbrMonthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
Date.firstDayOfWeek = 1;
Date.format = 'dd/mm/yyyy';
Date.fullYearStart = '20';

(function() {

	function add(name, method) {
		if( !Date.prototype[name] ) {
			Date.prototype[name] = method;
		}
	};
	
	add("isLeapYear", function() {
		var y = this.getFullYear();
		return (y%4==0 && y%100!=0) || y%400==0;
	});
	
	add("isWeekend", function() {
		return this.getDay()==0 || this.getDay()==6;
	});
	
	add("isWeekDay", function() {
		return !this.isWeekend();
	});
	
	add("getDaysInMonth", function() {
		return [31,(this.isLeapYear() ? 29:28),31,30,31,30,31,31,30,31,30,31][this.getMonth()];
	});
	
	add("getDayName", function(abbreviated) {
		return abbreviated ? Date.abbrDayNames[this.getDay()] : Date.dayNames[this.getDay()];
	});

	add("getMonthName", function(abbreviated) {
		return abbreviated ? Date.abbrMonthNames[this.getMonth()] : Date.monthNames[this.getMonth()];
	});

	add("getDayOfYear", function() {
		var tmpdtm = new Date("1/1/" + this.getFullYear());
		return Math.floor((this.getTime() - tmpdtm.getTime()) / 86400000);
	});
	
	add("getWeekOfYear", function() {
		return Math.ceil(this.getDayOfYear() / 7);
	});

	add("setDayOfYear", function(day) {
		this.setMonth(0);
		this.setDate(day);
		return this;
	});
	
	add("addYears", function(num) {
		this.setFullYear(this.getFullYear() + num);
		return this;
	});
	
	add("addMonths", function(num) {
		var tmpdtm = this.getDate();
		
		this.setMonth(this.getMonth() + num);
		
		if (tmpdtm > this.getDate())
			this.addDays(-this.getDate());
		
		return this;
	});
	
	add("addDays", function(num) {
		//this.setDate(this.getDate() + num);
		this.setTime(this.getTime() + (num*86400000) );
		return this;
	});
	
	add("addHours", function(num) {
		this.setHours(this.getHours() + num);
		return this;
	});

	add("addMinutes", function(num) {
		this.setMinutes(this.getMinutes() + num);
		return this;
	});
	
	add("addSeconds", function(num) {
		this.setSeconds(this.getSeconds() + num);
		return this;
	});
	
	add("zeroTime", function() {
		this.setMilliseconds(0);
		this.setSeconds(0);
		this.setMinutes(0);
		this.setHours(0);
		return this;
	});
	
	add("asString", function(format) {
		var r = format || Date.format;
		return r
			.split('yyyy').join(this.getFullYear())
			.split('yy').join((this.getFullYear() + '').substring(2))
			.split('mmmm').join(this.getMonthName(false))
			.split('mmm').join(this.getMonthName(true))
			.split('mm').join(_zeroPad(this.getMonth()+1))
			.split('dd').join(_zeroPad(this.getDate()))
			.split('hh').join(_zeroPad(this.getHours()))
			.split('min').join(_zeroPad(this.getMinutes()))
			.split('ss').join(_zeroPad(this.getSeconds()));
	});
	
	Date.fromString = function(s, format)
	{
		var f = format || Date.format;
		var d = new Date('01/01/1977');
		
		var mLength = 0;

		var iM = f.indexOf('mmmm');
		if (iM > -1) {
			for (var i=0; i<Date.monthNames.length; i++) {
				var mStr = s.substr(iM, Date.monthNames[i].length);
				if (Date.monthNames[i] == mStr) {
					mLength = Date.monthNames[i].length - 4;
					break;
				}
			}
			d.setMonth(i);
		} else {
			iM = f.indexOf('mmm');
			if (iM > -1) {
				var mStr = s.substr(iM, 3);
				for (var i=0; i<Date.abbrMonthNames.length; i++) {
					if (Date.abbrMonthNames[i] == mStr) break;
				}
				d.setMonth(i);
			} else {
				d.setMonth(Number(s.substr(f.indexOf('mm'), 2)) - 1);
			}
		}
		
		var iY = f.indexOf('yyyy');

		if (iY > -1) {
			if (iM < iY)
			{
				iY += mLength;
			}
			d.setFullYear(Number(s.substr(iY, 4)));
		} else {
			if (iM < iY)
			{
				iY += mLength;
			}
			d.setFullYear(Number(Date.fullYearStart + s.substr(f.indexOf('yy'), 2)));
		}
		var iD = f.indexOf('dd');
		if (iM < iD)
		{
			iD += mLength;
		}
		d.setDate(Number(s.substr(iD, 2)));
		if (isNaN(d.getTime())) {
			return false;
		}
		return d;
	};
	
	var _zeroPad = function(num) {
		var s = '0'+num;
		return s.substring(s.length-2)
	};
})();
