

var isMobile = false;
var isDesktop = false;


// navigate to a new URL
var jump = function(dest) {
	document.location = dest
}


var store = {}
try {
	store = localStorage
}
catch(e){}


$(document).ready(function() {

	
	// Detect the magic force= query parameter and store it so it persists across pages
	var q = document.location.search;
	var m = q.match(/force=([^&]*)/);
	if(m) 
		store.force = m[1];


	var ua = navigator.userAgent;
	var pw = window.screen.width	// pixel width


	if(/(iPhone|iPod|iPad|Android|Blackberry)/.test(ua)) {
		// We have a clear indication - very high confidence that this will be the right decision
		isMobile = true, isDesktop = false
	}
	else {
		// otherwise, just guess based on screen resolution, and cross our fingers
		if(pw <= 480)
			isMobile = true, isDesktop = false;
		else
			isDesktop = true, isMobile = false;
	}


	// override with the "force" value if it's been stored
	var force = store.force || ""
	if(force == "mobile") 
		isMobile = true, isDesktop = false;
	else
	if(force == "desktop") 
		isDesktop = true, isMobile = false;
	else
		; // proceed to making best guess


	// lastly, if "mobileEnabled" is false, mobile is not an option
	if(!mobileEnabled) 
		isMobile = false, isDesktop = true;




	
	/* herein lies the magic that makes the desktop login screen transform into a mobile login screen */
	if(isLoginPage && isMobile) {

		$(".hw-gen-page").hide();
		var ch = $("#content-block-1").html();
		ch = ch.replace(/my user name &amp; password./, "me");
		ch = ch.replace(/your user name or password./, "");
		ch = ch.replace(/Forgot/, "Forgot username/password?");
		ch = ch.replace(/Sign In/, "<h2>Sign In</h2>");
		ch = ch.replace(/Sign Up/, "<h2>Sign Up</h2>");
		$(".hw-gen-page").html("");

		$("body").append($("<div id=mobile></div>"))

		$("#mobile").append($("<div class=masthead><div class=logo><img src='/icons/CME/mobile/masthead.png'></div><div class=searchtab><img class=btn_search src='/icons/CME/mobile/search-tab.png'></div></div>"));

		$("#mobile").append($("<div style='margin: 1em;'>"+ch+"</div>"))

		$(".hw-gen-page").hide();
		$("#pageid-ac > #header").hide();
		$("#pageid-ac #footer").hide();
		$("*").removeAttr("width")
		$("*").removeAttr("bgcolor");
		$("div").removeAttr("margin")
		$("font").removeAttr("size");
		$("font").removeAttr("face");
		$("font").removeAttr("color");
		$("div#mobile > div > font > table").css("margin", "0 0 2em 0");
		$(".cme-login-forgot-userpass").css("text-align", "right").css("font-size", "90%").attr("colspan", "3")

		$.each($("form"), function(i, form) {
			if(form.name == "UserSignIn"){
				var t = $(form).children("table").get(0)
				var $c = $(t.rows[0].cells[3])
				var h = $c.html()
				$c.hide();
				$c.attr("width", "1px");
				$d = $("<div class='notebox'>"+h+"</div>")
				$d.appendTo($(form))
				$(t).removeAttr("width")
			}
		})


		var curYear = new Date().getFullYear();
		$("#mobile").append($("<div class=footer><a href='/misc/terms.dtl'>Copyright &copy; "+curYear+" by the "+society+"</a> </div>"));

		$(".cme-login-signin-arrow").remove()
		$(".cme-login-signup-arrow").remove()
		$(".cme-login-signup-small-arrow").remove()


		$("#mobile").fadeIn()

	}
	else {
		

		// this handles the header-banner click that acts like a "home" button
		$(".masthead").click(function() { jump("/") })


		// This causes the search form to appear when the icon is clicked
		$(".btn_search, .search_results_header").click(function(ev) {
			$(".search_entry").slideToggle(200)
			return false;
		})


		// Do some misc things based on mobile vs. desktop
		if(isMobile) {

			if(false) {
				// Hack to redirect vj content from broken dev url to working prod url for testing
				var $vj = $("#vj-content")
				if($vj) {
					var href = $vj.attr("src");
					if(href) {
						href = href.replace(/dev\./, "www.");
						$vj.attr("src", href);
					}
				}
			}


			//$("#vj-content").attr("width", "auto"); //pw+"px");
			//$(document.body).attr("width", "500px");
			$("#cme_error_page table").removeAttr("width");
			$("#cme_error_page td").removeAttr("width");
			$("p.error td").removeAttr("bgcolor");
		}
		else
		if(isDesktop) {
		}


		// lastly, show the appropriate div based on what was decided, and empty the other
		if(isMobile) {
			$("#desktop").html("")
			$("#mobile").fadeIn()
		}
		else {
			$("#mobile").html("")
			$("#desktop").fadeIn()
		}

	}



})


