/* parseUri JS v0.1.1, by Steven Levithan <http://stevenlevithan.com>
Splits any well-formed URI into the following parts (all are optional):
----------------------
- source (since the exec method returns the entire match as key 0, we might as well use it)
- protocol (i.e., scheme)
- authority (includes both the domain and port)
  - domain (i.e., host; can be an IP address)
  - port
- path (includes both the directory path and filename)
  - directoryPath (supports directories with periods, and without a trailing backslash)
  - fileName
- query (does not include the leading question mark)
- anchor (i.e., fragment) */
function parseUri(sourceUri){
	var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"],
		uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri),
		uri = {};
	
	for(var i = 0; i < 10; i++){
		uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
	}
	
	/* Always end directoryPath with a trailing backslash if a path was present in the source URI
	Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key */
	if(uri.directoryPath.length > 0){
		uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
	}
	
	return uri;
}

var mappings = new Array();
addMapping("/index.htm", "/home.htm");
addMapping("/PAGES/keyFeatures.htm", "/PAGES/principales.htm");
addMapping("/PAGES/solutions_linesofBusiness.htm", "/PAGES/metiers.htm");
addMapping("/PAGES/solutions_loans.htm", "/PAGES/solutions_credits.htm");
addMapping("/PAGES/solutions_propmanag.html", "/PAGES/immobilier.htm");
addMapping("/PAGES/solutions_realEstate.htm", "/PAGES/financement.htm");

function addMapping(first, second){
	var mapping = new Object();
	mapping.first = first;
	mapping.second = second;
	mappings.push(mapping);
}

function getMapping(from){
	for( var i=0; i < mappings.length; i++){
		if( mappings[i].first == from ){
			return mappings[i].second;
		}
		if( mappings[i].second == from ){
			return mappings[i].first;
		}
	}
	return from;
}




function changeLang(){
    var newUrl = getNewUrl(document.location.href);
    document.location = newUrl;
}

function isBlank(str){
	if(str == null) return true;
	else if(str.length == 0 ) return true;
	else return false;
}

function getNewUrl(currentHref){
	var currentUrl = parseUri(currentHref);
	var newDomain = "http://";
	newDomain += (currentUrl.domain.toLowerCase() == "www.cassiopae.com")? "www.cassiopae.fr": "www.cassiopae.com";
	var newUrl = newDomain;
	
	//Check mappings for any inconsistancies
	var path = getMapping(currentUrl.path);
	if( !isBlank(path)) newUrl += path;		 
    return newUrl; 
}
