﻿// Se l'oggetto DOL non è definito lo definisce
if (typeof DOL == "undefined" || !DOL) {
	var DOL = function() {
		return {
			_DEBUG: true,
			rawurl: window.location.href,
			host: window.location.protocol + "//" + window.location.hostname,
			path: window.location.pathname,
			qs: function() {
				var _qs = {}
				var hu = window.location.search.substring(1);
				var gy = hu.split("&");
				var ft = null;
				for (var i=0; i<gy.length; i++) {
					ft = gy[i].split("=");
					_qs[ft[0]] = ft[1]
				}
				return _qs;
			}(),
			hash: window.location.hash.substr(1),
			settings: {}
		}
	}();
}

DOL.timer = DOL.timer || function() {
	var t = {};
	return {
		start: function(name, time, callback) {
			if (typeof callback != "function") {
				callback = function(){}
			}
			t[name] = setInterval(callback, time)
		},
		stop: function(name) {
			clearInterval(t[name])
		}
	}
}();


DOL.cache = DOL.cache || function() {
	var c = {};
	return {
		load: function(key) { 
			return (typeof(c[key]) != 'undefined') ? c[key] : false;
		},
		save: function(key, val) { 
			c[key] = val;
		},
		remove: function(key) {
			if(typeof(c[key]) != 'undefined') {
				delete c[key];
			}
		}
	}
}();


DOL.loader = DOL.loader || function() {
	var s = {};
	function _has(scriptname) {
		return (typeof(s[scriptname]) != 'undefined') ? s[scriptname] : false;
	};
	function _register(scriptname, attr) {
		s[scriptname] = attr || true;
	};
	function _load(url, callback, cache, async) {
		cache = cache || false;
		async = async || false;
		$.ajax({
			async: async,
			type: "GET", 
			url: url, 
			success: callback, 
			dataType: "script", 
			cache: cache 
		}); 
	};
	return {
		cssload: function(url) {
			return $('head').append('<link rel="stylesheet" href="' + url + '" type="text/css" />');
		},
		jsload: function(url) {
			if (!_has(url)) {
				_load(url, function(){
					_register(url);
				}, true, true);
			}
		},
		requires: function(url, callback, async) {
			var result =  null;
			if (typeof callback == "undefined") {
				callback = function(){}
			}
			if (url.isArray()) {
				$.each(url, function(i, u){
					if(_has(u)) {
						return false
					} else {
						_load(u, function(){
							_register(u);
						}, true, false)
					}
				});
				setTimeout(function(){
					result = callback.call(this);
				}, 200);
			} else {
				if (_has(url)) {
					result = callback.call(this)
				} else {
					_load(url, function(){
						_register(url);
						result = callback.call(this)
					}, true, async)
				}
				return result;
			}
		}
	}
}();

//WRAPPA LA CONSOLE DI FIREBUG
DOL.debug = DOL.debug || function() {
	//var _methods = ["assert", "log", "debug", "info", "warn", "error", "dir", "dirxml", "count", "trace", "group", "groupEnd", "time", "timeEnd", "profile", "profileEnd"]
	return {
		console: function(method) {
			if (window.console && console[method] && DOL._DEBUG && $.browser.mozilla) {
				console[method].apply(this, [].splice.call(arguments,1))
			}
		},
		log: function() {
			console["log"].apply(this, [].splice.call(arguments,1))
		}
	}
}();

// GEOLOCALIZZA L'IP DELL'UTENTE CON LE API DI GOOGLE
DOL.location = DOL.location || function(){
	var cl = google.loader.ClientLocation
	if (cl != null) {
		return {
			lat: cl.latitude,
			lon: cl.longitude,
			city: cl.address.city,
			country: cl.address.country,
			country_code: cl.address.country_code,
			region: cl.address.region
		}
	} else {
		return null
	}
}()


DOL.setup = DOL.setup || function() {
	DOL.cache.save("baselibs_status", "init");
	DOL.loader.jsload("http://scripts.dol.it/libs/base.js");
	DOL.loader.jsload("http://scripts.dol.it/libs/utility.js");
	DOL.loader.jsload("http://scripts.dol.it/libs/widgets.js");
	for (var i=0; i<DOL._MODS.length; i++) {
		DOL.loader.jsload("http://scripts.dol.it/libs/" + DOL._MODS[i] + ".js");
	}
	for (var i=0; i<DOL._STYLES.length; i++) {
		DOL.loader.cssload("http://scripts.dol.it/css/" + DOL._STYLES[i] + ".css");
	}
	DOL.cache.save("baselibs_status", "loaded");
	if ((typeof _INIT != "undefined") && (typeof _INIT == "function")) {
		setTimeout(_INIT, 1000)
	}
}





DOL._DEBUG = (typeof _DEBUG != "undefined") ? _DEBUG : false;
DOL._GOOGLEAPI = (typeof _GOOGLEAPI != "undefined") ? _GOOGLEAPI : [];
DOL._MODS = (typeof _MODS != "undefined") ? _MODS : [];
DOL._STYLES = (typeof _STYLES != "undefined") ? _STYLES : [];

google.load("jquery", "1");
for (var i=0; i<DOL._GOOGLEAPI.length; i++) {
	google.load(DOL._GOOGLEAPI[i].name, DOL._GOOGLEAPI[i].version);
}
google.setOnLoadCallback(DOL.setup)





