/*-------------------------------------------------------------------
Name: SiteFooter (Global Footer) Region Panel JS
$Author: dgasior $
$DateTime: 2011/08/01 10:35:34 $
$Revision: #2 $
-------------------------------------------------------------------*/

/**
 * Set a region cookie based on selected country, and redirect based on business rules
 * @param {String} regioncode adobe specific geo paths
 * @returns nothing
 * @author David Gasior
 */
(function($) {
	adobe.fn.changeRegionFooter = function(regioncode) {
		if ($.string(regioncode).startsWith('be_')) {
			$.cookies.set('international', regioncode, {domain: 'adobe.com', hoursToLive: 8760});
			$.cookies.set('storeregion', 'be', {domain: 'adobe.com', hoursToLive: 8760});
		}
		else if ($.string(regioncode).startsWith('ca')) {
			$.cookies.set('international', regioncode, {domain: 'adobe.com', hoursToLive: 8760});
			$.cookies.set('storeregion', 'ca', {domain: 'adobe.com', hoursToLive: 8760});
		}
		else if ($.string(regioncode).startsWith('eeur')) {
			$.cookies.set('international', regioncode, {domain: 'adobe.com', hoursToLive: 8760});
			$.cookies.set('storeregion', 'eu', {domain: 'adobe.com', hoursToLive: 8760});
		}
		else if ($.string(regioncode).startsWith('hk_')) {
			$.cookies.set('international', regioncode, {domain: 'adobe.com', hoursToLive: 8760});
			$.cookies.set('storeregion', 'cn', {domain: 'adobe.com', hoursToLive: 8760});
		}
		else if ($.string(regioncode).startsWith('lu_')) {
			$.cookies.set('international', regioncode, {domain: 'adobe.com', hoursToLive: 8760});
			$.cookies.set('storeregion', 'lu', {domain: 'adobe.com', hoursToLive: 8760});
		}
		else if ($.string(regioncode).startsWith('uk')) {
			$.cookies.set('international', regioncode, {domain: 'adobe.com', hoursToLive: 8760});
			$.cookies.set('storeregion', 'gb', {domain: 'adobe.com', hoursToLive: 8760});
		}
		else {
			$.cookies.set('international', regioncode, {domain: 'adobe.com', hoursToLive: 8760});
			$.cookies.set('storeregion', regioncode, {domain: 'adobe.com', hoursToLive: 8760});
		}
	
		var currURL = window.location.pathname;
		var currPath = currURL;
		var geoArray = ["africa","ap","at","au","be_en","be_fr","be_nl","bg","br","ca","ca_fr","ch_de","ch_fr","ch_it","cn","cz","de","dk","eeurope","ee","es","fi","fr","hk_en","hk_zh","hr","hu","ie","il_en","in","it","jp","kr","la","lt","lu_de","lu_en","lu_fr","lv","mena","mx","nl","no","nz","pl","pt","ro","ru","si","se","sea","sk","tr","tw","ua","uk"];
	
		$.each(geoArray, function() {
			if ($.string(currURL).startsWith('/'+this+'/')) {
				currPath = currURL.replace('/'+this+'/','/');
				return false;
			}
		});
	
		if (($.string(currPath).startsWith('/cfusion')) && (regioncode == 'us')) {
			newURL = "/";
			homeURL = "/";
		}
		else if (($.string(currPath).startsWith('/cfusion')) && (regioncode != 'us')) {
			newURL = "/" + regioncode + "/";
			homeURL = "/" + regioncode + "/";
		}
		else if (regioncode != 'us') {
			newURL = "/" + regioncode + currPath;
			homeURL = "/" + regioncode + "/";
		}
		else {
			newURL = currPath;
			homeURL = "/";
		}
	
		$.ajax({
			url: newURL,
			type: 'HEAD',
			error:
				function(){
					window.location = homeURL;
				},
			success:
				function(){
					window.location = newURL;
				}
		});
	};
})(jQuery); 
