if(typeof tws_Global == 'undefined' || typeof tws_Global === null) {
	var tws_Global = {
		loaded: [],
		callbacks: [],
		
		scriptLoaded: function(objName)
		{
			tws_Global.loaded.push(objName);
			setTimeout("tws_Global.runCallbacks()",1);
		},
		
		runCallbacks: function()
		{
			for(var i =0 ; i < tws_Global.callbacks.length; i++)
			{
				if(tws_Global.callbacks[i] !== true)
				{
					try
					{
						tws_Global.callbacks[i]();
						tws_Global.callbacks[i] = true;
					}
					catch(e)
					{
						try { console.log(e); } catch(e) {}
					}
				}
			}
		},
		
		setCallback: function(func)
		{
			try
			{
				if(!func())
				{
					tws_Global.callbacks.push(func);
				}
			}
			catch(e)
			{
				tws_Global.callbacks.push(func);
			}
		}
	}
};

var tws_GEO =
{
    baseURL : "http://geo.tibaco.net/",
	cookieName	: "tws_GEO",
	doc	: null,
	xml : null,
	
	setSiteID: function(siteID)
    {
    	tws_Global.siteID = siteID;
    },
	
    getData: function() {
    	if(!this._readCookie('tws_geo')) {
            var script = document.createElement('script');
            script.id = 'tws_geo';
            script.src = this.baseURL + 'locateip?output=js';
            script.type = 'text/javascript';
            document.getElementsByTagName("head")[0].appendChild(script);
    	} else {
    		this.xml = this._readCookie('tws_geo');
    	}
    },

	getIPAddress: function() {
		if (this._setData()) {
            ip = this.doc.getElementsByTagName("geo")[0].getAttribute("ip");
        }
        if(typeof ip != 'undefined') {
        	return ip;
        }
        return null;
	},

	getRegionCode: function() {
		if (this._setData()) {
            code = this.doc.getElementsByTagName("region")[0].getAttribute("code");
        }
        if(typeof code != 'undefined') {
        	return code;
        }
        return null;
	},

	getAreaName: function() {
		if (this._setData()) {
            area = this.doc.getElementsByTagName("area")[0].getAttribute("code");
        }
        if(typeof area != 'undefined') {
        	return area;
        }
        return null;
	},

	getCountryCode: function() {
		if (this._setData()) {
            country = this.doc.getElementsByTagName("country")[0].getAttribute("code");
        }
        if(typeof country != 'undefined') {
        	return country;
        }
        return null;
	},

	getCountryName: function() {
		if (this._setData()) {
            country = this.doc.getElementsByTagName("country")[0].nodeValue;
        }
        if(typeof country != 'undefined') {
        	return country;
        }
        return null;
	},

	getCityName: function() {
		if (this._setData()) {
            city = this.doc.getElementsByTagName("city")[0].nodeValue;
        }
        if(typeof city != 'undefined') {
        	return city;
        }
        return null;
	},

	getContinentCode: function() {

	},

	getLongitude: function() {
		if (this._setData()) {
            longitude = this.doc.getElementsByTagName("longitude")[0].nodeValue;
        }
        if(typeof longitude != 'undefined') {
        	return longitude;
        }
        return null;
	},

	getLatitude: function() {
		if (this._setData()) {
            latitude = this.doc.getElementsByTagName("latitude")[0].nodeValue;
        }
        if(typeof latitude != 'undefined') {
        	return latitude;
        }
        return null;
	},

	_setData: function() {
		if (!this._readCookie('tws_geo') && this.xml != null)
			this._createCookie('tws_geo',this.xml);
		if (this.doc != null) {
			return true;
		} else if (this.doc == null && this.xml != null) {
			this.doc = this._stringToXML(this.xml);
			if(this.doc) {
				return true;
			}
		}

		return false;
    },

    _createCookie: function(name,value) {
		document.cookie = name+"="+value+"; path=/";
	},

	_readCookie: function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},

	_stringToXML: function(string)
    {
        var xmlDoc;
        if (window.ActiveXObject)
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.loadXML(string);
        }
        else if (document.implementation && document.implementation.createDocument)
        {
            var domParser = new DOMParser();
            xmlDoc = domParser.parseFromString(string, 'application/xml');
        }
        return xmlDoc;
    },
    
    loaded: tws_Global.scriptLoaded('tws_GEO')

};

tws_GEO.getData();