var ACWCookie = {
	'set': function(name, value, path, domain, expire) {
		var this_path = '';
		if (path != undefined) {
			this_path = '; path=' + path;
		}
		var this_domain = '';
		if (domain != undefined) {
			this_domain = '; domain=' + domain;
		}
		var this_expire = '';
		if (expire != undefined) {
			var d = new Date();
			d.setTime(d.getTime() + (86400000 * parseFloat(expire)));
			this_expire = '; expires=' + d.toGMTString();
		}
		return (document.cookie = name + '=' + value + this_path + this_domain + this_expire);
	},
	'get': function(name) {
		var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
		return (cookie ? unescape(cookie[2]) : null);
	},
	'erase': function(name) {
		var cookie = ACWCookie.get(name) || true;
		ACWCookie.set(name, '', '', '', -1);
		return cookie;
	},
	'accept': function() {
		if (typeof navigator.cookieEnabled == 'boolean') {
			return navigator.cookieEnabled;
		}
		ACWCookie.set('_test', '1');
		return (ACWCookie.erase('_test') === '1');
	}
};

var ACWPod = {
	'get': function() {
		return ACWCookie.get('POD');
	},
	'getCountry': function() {
		if (this.parse() != null) {
			return this.parse()[0].toUpperCase();
		}
		else {
			return 'US';
		}
	},
	'getLang': function() {
		if (this.parse() != null) {
			return this.parse()[1].toLowerCase();
		}
		else {
			return 'en';
		}
	},
	'getLocale': function() {
		if (this.parse() != null) {
			return this.getLang() + '_' + this.getCountry();
		}
		else {
			return 'en_US';
		}
	},
	'isTier0': function() {
		return (this.getLang() == 'en' || this.getLang() == 'de' || this.getLang() == 'fr' || this.getLang() == 'ja') ? true : false;
	},
	'parse': function() {
		var pod = ACWCookie.get('POD');
		if (pod != null && pod != undefined && pod != '' && pod.include('~')) {
			return pod.split('~');
		}
		else {
			return null;
		}
	},
	'set': function(pod) {
		ACWCookie.set('POD', pod, '/', '.apple.com', 28);
	}
};

var ACWTemplate = (typeof(Prototype) != 'undefined') ? Class.create({
	'initialize': function(container, jsonvars, template) {
		this.container = container;
		this.jsonvars = jsonvars;
		this.template = template;
		this.vars = '';
		this.module = '';
		this.request(container, jsonvars, template);
	},
	'request': function() {
		if ($(this.container)) {
			new Ajax.Request(this.jsonvars, {
				onSuccess: function(transport) {
					this.vars = transport.responseText.evalJSON();
				}.bind(this),
				onComplete: function() {
					this.loadModule();
				}.bind(this)
			});
			
			new Ajax.Request(this.template, {
				onSuccess: function(transport) {
					this.module = new Template(transport.responseText);
				}.bind(this),
				onComplete: function() {
					this.loadModule();
				}.bind(this)
			});
		}
	},
	'loadModule': function() {
		if (this.vars != '' && this.module != '') {
			$(this.container).update(this.module.evaluate(this.vars));
		}
	}
}) : null;

/* Deprecated functions */

var Pod = {
'getLocale': function() {
return ACWPod.getLocale();
}
};

function setPOD(str){
var today = new Date();
var expiry = new Date(today.getTime() + 28 * 24 * 60 * 60 * 1000); // plus 28 days
var expireVal = expiry.toGMTString();
document.cookie = 'POD' + "=" + str + "; path=/; domain=.apple.com; expires=" + expireVal;
}
