function ramp_SmartPhoneHandler(config) {

	init = function() {
		if ("undefined"==config || null==config)
			config = {};

            //the order these load in in important
            //URL of the top-most mobile home page, typically m.domain.com
		setDefault(config,"mobileHome",null);
            //action to take if the redirect is unsuccessful in finding a mobile version of the landing page. Options are "home", meaning "redirect to the mobile home page" or ... anything else, which will simply leave the user on the originally requested URL
		setDefault(config,"onNoMobileVersion","home");
            //allow override of standard landing page redirect logic. Setting this value to "home" redirect all mobile requests to the mobile home page
		setDefault(config,"onMobileDetect",null);
		setDefault(config,"deviceKeys",getDeviceMap());
		setDefault(config,"hosted",false);
		setDefault(config,"guid",getGUID());
		setDefault(config,"autoLoad",true);
		setDefault(config,"mobilePreview","/preview");
		setDefault(config,"rootPath",getRootPath());
		setDefault(config,"pagePath",getPagePath());
		setDefault(config,"test",false);
		setDefault(config,"themeKey",null);//force a theme
		setDefault(config,"nocookie",false);//Don't remember anything
		setDefault(config,"sitename",null);//The name of the site that this page is for
		setDefault(config,"noredir",false);//Signal that an attempt to redir has already been made
		setDefault(config,"m_n",getUrlParam("m_n",null));//Signal a redirect has already occured and a mobile version was not found
        
		if (config.autoLoad)
			load();
	}; //end init

	setDefault = function(obj,prop,d) {
        if("undefined" == typeof(obj[prop]) || null == obj[prop]) {
            obj[prop] = d;
        }
	}; //end setDefault

	getPagePath = function () {
		if (config.hosted)
			return window.location.pathname;
		else {
			var pageUrl = window.location.href;
                //use a canonical link if present
			var links = document.getElementsByTagName("link");
			for (var i=0;i<links.length;i++){
				if (links[i].rel=="canonical")
					pageUrl = links[i].href; break;
			}
			return config.mobilePreview+"?dispatch=true&curl=" + encodeURI(pageUrl);
		}
	}; //end getPagePath

	getRootPath = function () {
		return "http://" + window.location.host + "/";
	}; //end getRootPath

	getGUID = function () {
		if (config.guid)
			return config.guid;

		if (document.getElementsByTagName) {

			var metaNames = new Array("ramp.guid","guid","dc.identifier","fb.page_id");
			var metaProps = new Array("fb:page_id"); //property is used by FB and is part of the RDFa spec

			var metas = document.getElementsByTagName("meta");
			for (var i=0; i<metas.length;i++) {
				var meta = metas[i];
				var name = meta.getAttribute("name");
				var prop = meta.getAttribute("property");
				var content = meta.content;

				for (var n=0;n<metaNames.length;n++) {
					if (metaNames[n]==name) {
						return content;
					}
				}
				for (var p=0;p<metaProps.length;p++) {
					if (metaProps[p]==prop)
						return content;
				}
			}

		}

		return window.location.href;

	};//end getGUID

	getDeviceMap = function() {
		var deviceKeys = new Array();

            //set key to theme, value to device RegEx
		deviceKeys["mobile"] = /iPhone|iPod|Android/i;
            //deviceKeys["ipad"] = /iPad/i;
            //deviceKeys["bb"] = /Blackberry9530/i;
            //deviceKeys["palm"] = /Pre/i;
            //deviceKeys["mobile"] = /Android/i;

            //this pattern is here just for testing
            //deviceKeys["mac"] = /Macintosh/i;

		return deviceKeys;
	};//end getDeviceMap

	getUrlParam = function ( name,d ) {
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( window.location.href );
        if( results == null )
            return d;
        else
            return results[1];
	};

	doRedirect = function (url) {
		if (config.test)
            alert("redirect to = \n" + url);
        else if(config.noredir)
            return;
		else
            window.location = url;
        
	};//end doRedirect

	noMobileVersion = function () {
		if (config.m_n) {
                //see what the default is
			if (config.onNoMobileVersion) {	
				if (config.onNoMobileVersion && config.mobileHome) {
					doRedirect(config.mobileHome);
				}
			}
			return true;
		}
		return false;
	};

    load = function() {
            //first thing, see if they're on a mobile device
		if (!isMobile())
			return;

            //next, see if they should just be redirected to the top-level. This should be true on all non-indexed content
		if (config.onMobileDetect=="home" || (noMobileVersion() && config.onNoMobileVersion=="home")) {
			if (config.mobileHome && config.mobileHome!="")
				doRedirect(config.mobileHome);
			return;
		} 


            //check for existing redirect cookie
		var cookieKey = "sp_redir"+config.guid;
        var cookie = config.nocookie?null:getCookie(cookieKey);


            //here just for testing
		if (config.test && cookie) {
			var what = prompt("Use cookie value, or redo?\n\nType: redo OR cookie","redo");
			if (what=="redo") {rmCookie(cookieKey); cookie=null; }
		};


		if (cookie) {
			doRedirect(unescape(cookie));
		} else {
			var themeKey = getThemeKey();
			if (themeKey) {
				var redir = getRedirectUrl(themeKey);
				setCookie(cookieKey, redir);
				doRedirect(redir);
			}
        }

    }; // end load


	getRedirectUrl = function (themeKey) {
		var mobileUrl = config.rootPath + (config.sitename?"/"+config.sitename+"/":"") + themeKey + config.pagePath;
		return mobileUrl;
	};

        //use this function to match a device to a theme
    getThemeKey = function(agent) {
		var key = config.themeKey;

        if ("undefined"==agent || null==agent)
            agent=navigator.userAgent;

		if (!key) {
			for (var key in config.deviceKeys) {
				if(agent.match(config.deviceKeys[key])) { config.themeKey=key; };
			}
		}
        if(!config.themeKey) {
            return null;
        } else {
            return "device/" + config.themeKey;
        }
    }; //end getThemeKey

        //returns true/false
    isMobile = function(agent) {
            
        if ("undefined"==agent || null==agent)
            agent=navigator.userAgent;

        for (var key in config.deviceKeys) {
            if(agent.match(config.deviceKeys[key])) { return true; };
        }
            
        return false;
    };
    ramp_SmartPhoneHandler.prototype.isMobile = isMobile;
    ramp_SmartPhoneHandler.prototype.doRedirect = doRedirect;
    
    getCookie = function(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;
    }; //end getCookie

	setCookie = function (c_name,value,exdays) {
		var exdate=new Date();
		exdate.setDate(exdate.getDate() + exdays);
		var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
		document.cookie=c_name + "=" + c_value;
	}; //end setCookie


	rmCookie = function (name) {
		document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
	}; //end rmCookie


        //initialize the config values
	init();
}


