// Check if we are on a small screen
function CheckForSmallScreen(minWidth, minHeight)
{
   return(window.screen.height < minHeight || window.screen.width < minWidth);
}

// Check if the browser userAgent string is AppleWebKit
function CheckForWebKitUA()
{
   return(navigator.userAgent.search(/AppleWebKit/i) != -1);
}

// Check if the URL already has /iphone in it
function CheckForIPhoneURL()
{
   return(window.location.pathname.toString().search(/^\/iphone/i) != -1);
}

// Switch to the iphone UI if we're a webkit browser, are not already the /iPhone URL and are on a small screen
function RedirectIfSmallWebKitBrowser()
{
   var parms = window.location.search;
   if (parms.search(/autoPhoneRedirect=no|stripiPhoneCookie=yes/i) != -1)
   {
       SM.util.setCookie("autoPhoneRedirect", "no", 1);
   }
   else if (parms.search(/autoPhoneRedirect=yes/i) != -1)
   {
       SM.util.setCookie("autoPhoneRedirect", "yes", 1);
   }
   // check to see if redirection has been turned off via cookie
   var value = "";
   value = SM.util.getCookie("autoPhoneRedirect");
   if (value != "no")
   {
       if (CheckForWebKitUA() && !CheckForIPhoneURL() && CheckForSmallScreen(600,600))
       {
           window.location.replace("/iphone");
       }
   }
}

RedirectIfSmallWebKitBrowser();

