var user_agent = navigator.userAgent;
user_agent = user_agent.toLowerCase();  //avoid case-sensitivity
if(user_agent.match("android") || user_agent.match("iphone")){
	var redirect = true;
	//check url for parameter to not redirect
	if(document.baseURI.match("q=full")){
		redirect=false;
		deleteCookie("site_context");
		createCookie("site_context","desktop",1000000)
	}
	else{
		//check cookie
		var context = readCookie("site_context");
		if(context){
			if(context ==="desktop"){
				redirect = false;
			}
		}
	}
	
	if(redirect){
		var url = window.location.href;
		var new_url = "";
		if(url.match("www.aids.gov")){
			new_url = url.replace("www.aids.gov","m.aids.gov/touch")
		}
		else if(url.match("aids.gov")){
			new_url = url.replace("aids.gov","m.aids.gov/touch")
		}
//		if(url.match("www.stage.aids.icfi.com")){
//			new_url = url.replace("www.stage.aids.icfi.com","www.stage.aids.icfi.com/Touch")
//		}
//		else if(url.match("stage.aids.icfi.com")){
//			new_url = url.replace("stage.aids.icfi.com","stage.aids.icfi.com/Touch")
//		}

		window.location=new_url;
	}
	else{
		//add the link to go to mobile site
		createMobileLink(user_agent);
	}
}


function createMobileLink(user_agent){
		var mobileLink = document.createElement("a");
		mobileLink.id = "mobile_link";
		mobileLink.style.top = "0px";
		mobileLink.style.color = "white";
		mobileLink.style.display = "block";
		mobileLink.style.textAlign = "center";
		mobileLink.style.position = "relative";
		mobileLink.style.width = "100%";
		mobileLink.innerHTML = "Mobile Site";
		if(user_agent.match("android") || user_agent.match("iphone")){
			var new_url="";
			var url = window.location.href;
			if(url.match("www.aids.gov")){
				new_url = url.replace("www.aids.gov","m.aids.gov/touch")
			}
			else if(url.match("aids.gov")){
				new_url = url.replace("aids.gov","m.aids.gov/touch")
			}
			
//			if(url.match("www.stage.aids.icfi.com")){
//				new_url = url.replace("www.stage.aids.icfi.com","www.stage.aids.icfi.com/Touch")
//			}
//			else if(url.match("stage.aids.icfi.com")){
//				new_url = url.replace("stage.aids.icfi.com","stage.aids.icfi.com/Touch")
//			}
			mobileLink.href = new_url;
		}
	
		document.body.appendChild(mobileLink);
		mobileLink.onclick = mobileLinkClick;	
	
}
function mobileLinkClick(){
	//delete any instances of site_context and create another one that references the mobile site
	deleteCookie("site_context");
	createCookie("site_context","mobile",1000000);
}

/* The following functions to create, read, and delete cookies
is courtesy Peter-Paul Koch, http://www.quirksmode.org/js/cookies.html */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function deleteCookie(cookie_name){
	var cookie_date = new Date ( );  // current date & time
	  cookie_date.setTime ( cookie_date.getTime() - 1 );
	  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function readCookie(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;
}
