if(navigator.userAgent.indexOf("iPhone")>-1) {
  addEventListener("load",function(){ setTimeout(autoScroll,0) },false);
}

/**
*
*/
function autoScroll() {
  window.scrollTo(0,0.9);
}

/**
*
*/
function imageSetup(w,h) {
  //Direct to iPhone function if on the iPhone
  if(navigator.userAgent.indexOf("iPhone")>-1) {
    iPhoneOrientationChange(w,h);
    return;
  }

  var navBarHeight=45;
  var viewPartWidth
  var viewPartHeight
 
  if(typeof window.innerWidth != 'undefined') {
    //The more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    viewPartWidth = window.innerWidth;
    viewPartHeight = window.innerHeight-navBarHeight;
  }else if (typeof document.documentElement != 'undefined') {
    //E6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
    viewPartWidth = document.documentElement.clientWidth;
    viewPartHeight = document.documentElement.clientHeight-navBarHeight;
  }

  var screenRatio=viewPartWidth/viewPartHeight;
  var imgRatio=w/h;
  var imgCell=document.getElementById("theImage");
  
  if(w>h) { //Landscape Image

    if(imgRatio<screenRatio) {
      imgCell.width=Math.floor(viewPartHeight*imgRatio);
      imgCell.height=viewPartHeight;
    
    } else {
      imgCell.width=viewPartWidth;
      imgCell.height=Math.floor(viewPartWidth*(1/imgRatio));
    }

  } else { //Portrait Image

    if(imgRatio>screenRatio) {
      imgCell.width=viewPartWidth;
      imgCell.height=Math.floor(viewPartWidth*(1/imgRatio));
      
    } else {
      imgCell.width=Math.floor(viewPartHeight*imgRatio);
      imgCell.height=viewPartHeight;
    }

  }
}

function iPhoneOrientationChangeMain() {
    var logo=document.getElementById("theLogo");

    switch(window.orientation)
    {
      case -90: //Landscape orientated
        logo.setAttribute("orient","landscape");
        break;

      case 0: //Portrait orientated
        logo.setAttribute("orient","portrait");
        break;
        
      case 90: //Landscape orientated
        logo.setAttribute("orient","landscape");
        break;

      case 180: //Portrait orientated
        logo.setAttribute("orient","portrait");
        break;

    }
    autoScroll();
}


/*
* For iPhone resizing especially on orientation change
*/
function iPhoneOrientationChange(w,h) {
  var navBarHeight=45;

  //--- iPhone sizes ---
  var portraitWidth=320;
  var portraitHeight=416-navBarHeight;

  var landscapeWidth=480;
  var landscapeHeight=268-navBarHeight;

  var screenRatio=portraitWidth/portraitHeight;
  var imgRatio=w/h;
  var imgCell=document.getElementById("theImage");

  if(w>h) { /**** Landscape Image ****/

    switch(window.orientation)
    {
      case -90: //Landscape orientated
      case 90:
        imgCell.height=landscapeHeight
        imgCell.width=Math.floor(landscapeHeight*imgRatio);
        break;
      default: //Portrait orientated
        imgCell.width=portraitWidth;
        imgCell.height=Math.floor(portraitWidth*(1/imgRatio));
    }

  } else { /**** Portrait Image *****/

    switch(window.orientation)
    {
      case 0: //Portrait orientated
      case 180:
        imgCell.height=portraitHeight;
        imgCell.width=Math.floor(portraitHeight*imgRatio);
        break;
      default: //Landscape orientated
        imgCell.height=landscapeHeight;
        imgCell.width=Math.floor(landscapeHeight*imgRatio);
    }
  }

  autoScroll();

}
