// Om det redan finns ett window.onload-event så hakar vi på det här på det.
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent (scroller);

function scroller() {
	window.scrollTo(0,1);
}



/* Kontrollera rotation av iPhone */
/*
addEventListener("load", function() {
        setTimeout(updateLayout, 0);
    }, false);
*/


/* Browsersniffning av iPhone */
var agent=navigator.userAgent.toLowerCase();
var is_iphone = ((agent.indexOf('iphone')!=-1));
if (is_iphone) {  }


/* Sätt olika attribut på <body> beroende på om iPhone är stående eller liggande. */
var currentWidth = 0;
var agent=navigator.userAgent.toLowerCase();
var is_iphone = ((agent.indexOf('iphone')!=-1));
if (is_iphone) {
	function updateLayout() {
		if (window.innerWidth != currentWidth) {
			
			currentWidth = window.innerWidth;
	 
			var orient = currentWidth == 320 ? "portrait" : "landscape";
			document.body.setAttribute("orientation", orient);
			//alert(orient); /* Debug */
			//setTimeout(function() { window.scrollTo(0, 1); }, 100);            
		}
	}
/* Ändra storlek på alla bilder */
	function fixImgs(whichId, maxW) {
	var pix=document.getElementById(whichId).getElementsByTagName('img');
	  for (i=0; i<pix.length; i++) {
		w=pix[i].width;
		h=pix[i].height;
		if (w > maxW) {
		  f=1-((w - maxW) / w);
		  pix[i].width=w * f;
		  pix[i].height=h * f;
		}
	  }
	}

setInterval(updateLayout, 400);
addLoadEvent(updateLayout);

addLoadEvent ( function() {
		var orient = document.getElementsByTagName('body')[0].getAttribute('orientation')
		if (orient=="portrait") {var width = 284} else {var width = 444}
		fixImgs('content', width)
		}
	);
setInterval ( function() {
		var orient = document.getElementsByTagName('body')[0].getAttribute('orientation')
		if (orient=="portrait") {var width = 284} else {var width = 444}
		fixImgs('content', width)
		}
	, 400);
}

/*
function checkorientation() {
	window.alert(document.getElementsByTagName('body')[0].getAttribute('orient'));
}

addLoadEvent (checkorientation);
*/

/* Ändra storlek på alla bilder */
/*
var agent=navigator.userAgent.toLowerCase();
var is_iphone = ((agent.indexOf('iphone')!=-1));
if (is_iphone) { 
	function fixImgs(whichId, maxW) {
	var pix=document.getElementById(whichId).getElementsByTagName('img');
	  for (i=0; i<pix.length; i++) {
		w=pix[i].width;
		h=pix[i].height;
		if (w > maxW) {
		  f=1-((w - maxW) / w);
		  pix[i].width=w * f;
		  pix[i].height=h * f;
		}
	  }
	}
addLoadEvent ( function() {
		var orient = document.getElementsByTagName('body')[0].getAttribute('orientation')
		if (orient=="portrait") {var width = 284} else {var width = 444}
		fixImgs('content', width)
		}
	);
}
*/
