DeepLinker = {
	fileext: ".aspx",

	redirectedHashUrls: false,
	/**
	 * This function needs to be run BEFORE Flash.convertDeepUrls, otherwise an infinite loop could happen.
	 */
	redirectHashUrls: function() {
		var parse = DeepLinker.parseUrl(window.location.hash.toString());
		if (parse) {
			var loc = DeepLinker.transformUrl("/"+parse);
			window.location = loc;
			return true;
		}
		return false;
	},

	transformUrl: function(hashedUrl) {
		var protocol = window.location.protocol;
		var host = window.location.host;
		return protocol + "//" + host + hashedUrl;
	},
	
	convertIndexToDefault: function(url) {
		if (url.substr(url.length - 6, url.length) == "/index" ) {
			return (url.substr(0, url.length-5) + "default");
		} else {
			return url;
		}
	},
	
	getHashPos: function(url) {
		return (url.indexOf("#") != -1);
	},
	
	/**
	 * Incoming flash URLS have no extension, but incoming ajax urls might.
	 * Also, Incoming URLS might be named index or default.  This function fixes the rigamarole.
	 */
	parseUrl: function(url) {
		if (DeepLinker.getHashPos(url) === false) return false;

		var parsed = DeepLinker.convertIndexToDefault(url.substr(DeepLinker.getHashPos(url)+1));
		if (parsed.substr((parsed.length - DeepLinker.fileext.length), parsed.length) !== DeepLinker.fileext) {
			return parsed + DeepLinker.fileext;
		} else {
			return parsed;
		}
	},

	/**
	 * Turn a parsed url into a fully qualified URL
	 */
	parseFullUrl: function (url) {
		return "http://www.budweiser.com/"+DeepLinker.parseUrl(url);
	},
	
	/**
	 * Handle our deep linking urls.
	 * when the user comes in, and they have an existing hash URL, this need to be transformed into a non-hash url, and the user directed there.
	 *   This handles all ajax cases, wehre the has URL points to a page.  (i.e. footer links)
	 * when the user comes in, and they have no existing hash URL, it needs to be transformed into a hash url
	 *   This handles all flash cases, where a user comes in on a deep url, and we need to pass that info into flash.
	 *   The deep level conversion should only happen once.
	 */
	convertedDeepUrls: false,
	convertDeepUrls: function() {
		if (!DeepLinker.convertedDeepUrls && !window.location.hash) {
			window.location.hash = DeepLinker.buildHash(window.location);
			DeepLinker.convertedDeepUrls = true;
		}
	},

	buildHash: function(url) {
		
		var language = document.getElementsByTagName('html')[0].getAttribute('lang');
		var hostlength = DeepLinker.getPathPos(); //determine length of host (ex. http://localhost = 16)
		var delimiter = "/";
		
		if (typeof url != "String") {
			url = url.toString();
		}

		//kill query string, it hoses things
		if (url.indexOf('?') >0) {
			url = url.substr(0, url.indexOf('?'));
		}
		
		url = url.substr(hostlength,(url.length - hostlength)); //create string of path for deeplink
		if(url.substr((url.length - DeepLinker.fileext.length), url.length) == DeepLinker.fileext){ //checks if there is a file extension in the URI and that it matches the default fileext set
			url = url.substr(0,url.length - DeepLinker.fileext.length); //remove the file extension from the end of the path - 0 (zero) indicates the start of the string
		}
		//check if cookies are disabled
		if(DeepLinker.cookieCheck() == false){
			/*
			 * Position of the token for cookie disabled users on this server
			 * 1 is the position in the array when a split is done and url starts with "/" position 0 in the array is blank (before the "/")
			 */
			serverTokenPosition = 1; //see above 
			var urlarray = url.split(delimiter); //explode the url
			var tempdelimiter = delimiter; //set temp delimiter as we will set it to blank on the last cycle in the for() loop
			url = delimiter; //set starting delimiter
			for(var i = 0;i < urlarray.length; i++){
				if(i != serverTokenPosition && urlarray[i] != ""){
					if(i == (urlarray.length-1)){//drop training slash (length of array minus one is the last array value
						tempdelimiter = ""; //set to blank for last cycle so there isn't a trailing slash
					}
					url = url + urlarray[i] + tempdelimiter;
				}
			}
			
		}
		
		//checks to see if there is any path (if the uri is set to root (http://localhost)) 
		if(url.length <= 1){
			url = "/"+language+"/index"; //set to default of home-page index
		}
		
		//checks if only the language is set
		var urlarray = url.split(delimiter); //get array seperated by URL delimiter
		if(urlarray[1] == language && urlarray.length <= 3){ //check if the url is deeper than the home page
			if(urlarray[2] != undefined && urlarray[2] == ""){ //if the array appears to be deeper (because of trailing slash) last node of array is blank
				url = url + "index"; //set with no delimiter as it should be there based on above logic
			}else if(urlarray[2]==undefined){ // if there is no 3rd array node it must be an index (there was no trailing slash falling out of above logic
				url = url + delimiter + "index"; //add index + delimiter
			}//all other cases are ignored.
		}
		return DeepLinker.convertDefaultToIndex(url);
	},

	cookieCheck: function(){
		//internal function to check if the bud cookie is set
		var check = function(){
			if((Cookie.get('AspxAutoDetectCookieSupport') != "") || (Cookie.get('s_cc') != "")){
				return true; //exists
			}else{
				return false; //no exists
			}
3		};
		if(check() == false){
			var expires = new Date() + 5; //set expires for 5 days from now
			//Cookie.set('budCookieCheck','true',expires); //set the cookie
			if(check() == true){
				return true; //cookie was set
			}else{
				return false; //cookie was not set
			}
		}else{
			return true; //cookie already exists
		}
	},

	getPathPos: function() {
		return (window.location.protocol + "//" + window.location.host).length;
	},
	
	convertDefaultToIndex: function(url) {
		if (url.substr((url.length - 8), url.length) == "/default" ) {
			return (url.substr(0, url.length-7) + "index");
		} else {
			return url;
		}
	}
}


Cookie = {
	/**
	 * Set a cookie to the path /
	 */
	set: function(cName, value, expireDays){
		var exDate=new Date();
		exDate.setDate(exDate.getDate()+expireDays);
		document.cookie=cName+ "=" +escape(value)+
		((expireDays==null) ? "" : ";path=/;expires="+exDate.toGMTString());
	},

	/**
	 * Check for a cookie 
	 */
	get: function(cName){
		if (document.cookie.length > 0) {
			var cStart = document.cookie.indexOf(cName + "=");
			if (cStart != -1) {
				cStart = cStart + cName.length + 1;
				var cEnd = document.cookie.indexOf(";", cStart);
				if (cEnd==-1) {
					cEnd=document.cookie.length;
				}
				return unescape(document.cookie.substring(cStart,cEnd));
		    }
		}
		return "";
	}
}

DeepLinker.redirectHashUrls();
DeepLinker.convertDeepUrls();
