/*****************************************************************************
 *
 *  TITLE:		AbleEngine - Core Library
 *  MODIFIED:	2011.06.16
 *  AUTHOR: 	Graham Wheeler
 *	REQUIRES:	jQuery 1.5+
 *	INCLUDES:	Imtech Date Utils
 *
 ****************************************************************************/

jQuery.support.noCloneEvent = !!window.addEventListener;

var Imtech=Imtech||{};Imtech.Utils={date:{"1033":{monthsLong:["January","Febraury","March","April","May","June","July","August","September","October","November","December"],monthsShort:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],daysLong:["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],daysShort:["Mon","Tue","Wed","Thu","Fri","Sat","Sun"],patterns:{d:"M/d/yyyy",D:"dddd, MMMM dd, yyyy",f:"dddd, MMMM dd, yyyy H:mm tt",F:"dddd, MMMM dd, yyyy H:mm:ss tt",g:"M/d/yyyy H:mm tt",G:"M/d/yyyy H:mm:ss tt",m:"MMMM dd",o:"yyyy-MM-ddTHH:mm:ss.fff",s:"yyyy-MM-ddTHH:mm:ss",t:"H:mm tt",T:"H:mm:ss tt",U:"dddd, MMMM dd, yyyy HH:mm:ss tt",y:"MMMM, yyyy"},tt:{AM:"AM",PM:"PM"},clockType:12},"1043":{monthsLong:["januari","febrauri","maart","april","mei","juni","juli","augustus","september","oktober","november","december"],monthsShort:["Jan","Feb","Mar","Apr","Mei","Jun","Jul","Aug","Sep","Okt","Nov","Dec"],daysLong:["maandag","dinsdag","woensdag","donderdag","vridag","zaterdag","zondag"],daysShort:["ma","di","wo","do","vr","za","zo"],patterns:{d:"d-M-yyyy",D:"dddd d MMMM yyyy",f:"dddd d MMMM yyyy H:mm",F:"dddd d MMMM yyyy H:mm:ss",g:"d-M-yyyy H:mm",G:"d-M-yyyy H:mm:ss",m:"dd MMMM",o:"yyyy-MM-ddTHH:mm:ss.fff",s:"yyyy-MM-ddTHH:mm:ss",t:"H:mm",T:"H:mm:ss",U:"dddd d MMMM yyyy HH:mm:ss",y:"MMMM, yyyy"},tt:{AM:"",PM:""},clockType:24}}};Date.prototype.format=function(k,i){var b=this.toString();var a=Imtech.Utils.date[i];if(typeof(a)!=="undefined"){var g=typeof(a.patterns[k])!=="undefined"?a.patterns[k]:k;b=g.replace(/yyyy/g,this.getFullYear());b=b.replace(/yy/g,(this.getFullYear()+"").substring(2));var f=this.getMonth();b=b.replace(/MMMM/g,a.monthsLong[f].escapeDateTimeTokens());b=b.replace(/MMM/g,a.monthsShort[f].escapeDateTimeTokens());b=b.replace(/MM/g,f+1<10?"0"+(f+1):f+1);b=b.replace(/(\\)?M/g,function(m,l){return l?m:f+1});var c=this.getDay();b=b.replace(/dddd/g,a.daysLong[c].escapeDateTimeTokens());b=b.replace(/ddd/g,a.daysShort[c].escapeDateTimeTokens());var h=this.getDate();b=b.replace(/dd/g,h<10?"0"+h:h);b=b.replace(/(\\)?d/g,function(m,l){return l?m:h});var e=this.getHours();if(a.clockType==12){if(e>12){e-=12}}b=b.replace(/HH/g,e<10?"0"+e:e);b=b.replace(/(\\)?H/g,function(m,l){return l?m:e});var d=this.getMinutes();b=b.replace(/mm/g,d<10?"0"+d:d);b=b.replace(/(\\)?m/g,function(m,l){return l?m:d});var j=this.getSeconds();b=b.replace(/ss/g,j<10?"0"+j:j);b=b.replace(/(\\)?s/g,function(m,l){return l?m:j});b=b.replace(/fff/g,this.getMilliseconds());b=b.replace(/tt/g,this.getHours()>12||this.getHours()==0?a.tt.PM:a.tt.AM)}return b.replace(/\\/g,"")};String.prototype.escapeDateTimeTokens=function(){return this.replace(/([dMyHmsft])/g,"\\$1")};

/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

var AE = { version: "2.1.1" };
AE.console = {
	outputBuffer: [],
	log: function(text) {
		this.outputBuffer.push(text);
		if (this.outputBuffer.length > 4096) {
			// Discard older entries 
			this.outputBuffer.shift();
		}
	},
	print: function(text) {
		if (text) {
			this.outputBuffer.push(text);
		}
		while(this.outputBuffer.length > 0) {
			try {
				window.console.log(this.outputBuffer.shift());
			} catch(ex) { break; }
		}
	},
	toString: function(separator) {
		var sep = separator || "\n";
		return this.outputBuffer.join(sep);
	},
	clear: function() { this.outputBuffer = []; }
};
AE.queryStringParams = {};
AE.queryString = jQuery('script[src*="ableengine.core.js"]').attr("src");
AE.queryString = AE.queryString.substring(AE.queryString.indexOf("?") + 1);
jQuery.each(AE.queryString.split("&"), function(index, p){
	try {
		var kv = p.split('=');
		AE.queryStringParams[kv[0]] = kv[1];
	} catch(ex) {}
});
AE.currentPath = location.href.toString().substring(location.href.indexOf(".com/")+5);
AE.pathSegments = AE.currentPath.split('/');
AE.rootPath = AE.queryStringParams['rootPath'] || "/";
AE.debug = AE.queryStringParams['debug'] || true;
AE.mouse = { x: 0, y: 0 };
AE.select = jQuery;
AE.Plugins = {
	
	Add: function(plugin) {
		this[plugin.name] = plugin;
		if (AE.debug) { AE.console.print("Detected Plugin: "+plugin.name); }
		if (plugin.attachToEvents !== undefined) {
			AE.select.each(plugin.attachToEvents.split(","), function(index, ev) {
				switch (ev) {
					case "WINDOW_LOAD": {
						AE.select(window).load(function() {
							try {
								if (AE.debug) { AE.console.print(plugin.name+" applied to selector: 'body'"); }
								plugin.init(AE.select('body'));
								jQuery.event.trigger("PLUGIN_"+plugin.name+"_INIT");
							} catch (ex) {
								if (AE.debug) { AE.console.print("Unable to initialize Plugin: "+plugin.name+" ("+ex+")"); }
							}
						});
						break;
					}
					case "PAGE_LOAD": {
						AE.select(document).ready(function() {
							try {
								if (AE.debug) { AE.console.print(plugin.name+" applied to selector: 'body'"); }
								plugin.init(AE.select('body'));
								jQuery.event.trigger("PLUGIN_"+plugin.name+"_INIT");
							} catch (ex) {
								if (AE.debug) { AE.console.print("Unable to initialize Plugin: "+plugin.name+" ("+ex+")"); }
							}
						});
						break;
					}
					default: {
						AE.select(document).bind(ev, function(e, scope) {
							try {
								if (AE.debug) { AE.console.print(plugin.name+" applied to selector: '"+(scope.selector||scope)+"'"); }
								plugin.init(AE.select(scope));
								jQuery.event.trigger("PLUGIN_"+plugin.name+"_INIT");
							} catch (ex) {
								if (AE.debug) { AE.console.print("Unable to initialize Plugin: "+plugin.name+" ("+ex+")"); }
							}
						});
						break;
					}
				}
			});
		}
	},
	Running: 0
	
};

//	AE.require()
//	Injects a resource reference (such as a script or stylesheet) into the document
AE.require = function(fileName, fileType) {
	
	try {

		if (fileType == undefined) {
			if (fileName.indexOf(".css") != -1) { fileType = "CSS"; }
			else if (fileName.indexOf(".js") != -1) { fileType = "JS"; }
			else {
				throw("Invalid file type specified.");
			}
		}

		var isRemote = (fileName.indexOf("http") == 0);

		switch (fileType) {

			case "CSS": {
				
				if (jQuery('link[href*="+fileName+"]').length > 0) {
					return;
				} else {
					jQuery("head").append('<link rel="stylesheet" type="text/css" href="'+((isRemote)?'':AE.rootPath)+fileName+'">');
				}
				break;
				
			} 
			
			case "JS": {
	
				if (jQuery('script[src*="+fileName+"]').length > 0) {
					return;
				} else {
					jQuery("head").append('<script type="text/javascript" src="'+((isRemote)?'':AE.rootPath)+fileName+'"></script>');
				}
				break;
				
			} 
			
			default: {
				throw("Reached the unreachable code.");
				break;
			}
			
		}
		
	} catch(ex) {

		AE.console.print("Error in AE.require():");
		AE.console.print(ex);
		
	}

};


AE.findNearest = function(selector, seekFrom) {
	var target = AE.select(seekFrom).nextAll(selector+":first");
	var seekparent = AE.select(seekFrom).parent();
	if (target.length == 0 && seekparent.length > 0) {
		target = AE.findNearest(selector, seekparent.get(0));
	}
	return target;
};


AE.numberToCurrency = function(number, decimalSeparator, thousandsSeparator, nDecimalDigits){

		//default values
    var decimalSeparator = decimalSeparator || '.';
    var thousandsSeparator = thousandsSeparator || ',';
    var nDecimalDigits = nDecimalDigits || 2;

    var num = number.toFixed(nDecimalDigits), //limit/add decimal digits
        parts = RegExp('^(-?\\d{1,3})((\\d{3})+)\\.(\\d{'+ nDecimalDigits +'})$').exec(num); //separate begin [$1], middle [$2] and decimal digits [$4]

    if (parts) { //number >= 1000 || number <= -1000
        return parts[1] + parts[2].replace(/\d{3}/g, thousandsSeparator + '$&') + decimalSeparator + parts[4];
    } else {
        return num.replace('.', decimalSeparator);
    }
};


AE.Plugins.Add({

	name: "FirstLast",
	attachToEvents: "PAGE_LOAD,DOM_UPDATED",
	init: function(scope) {
		AE.select("img:first-child ~ *:header", scope ).addClass("offset-first-child");
		AE.select("*:last-child", scope ).addClass("last-child");
	}

});


/************************************************************\
*
*		Page Load Event - Main Script Init
*
\************************************************************/
AE.select(function() {
						
	AE.select("html").addClass("js-enabled");
	
	AE.select(document).mousemove(function(e) {
		AE.mouse.x = e.pageX || e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		AE.mouse.y = e.pageY || e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	});
	
	AE.select(window).resize(function() {
		AE.select(document).trigger("VIEWPORT_CHANGED", [document]);
	});
	
	AE.select(document).scroll(function() {
		AE.select(document).trigger("VIEWPORT_CHANGED", [document]);
	});

});

AE.select(window).load(function() {

	if (AE.debug) { AE.console.print(); }
																	
});


