// -----------------------------------------------------------------------------------
//
//	Martijn Smeets Fotografie Standard Functions v0.5
//  by Martijn Smeets - http://www.martijnsmeetsfotografie.nl
//	Last Modification: February 24, 2010
//
//	Contents:
//	Viewport
//	Styleswitcher
//
// -----------------------------------------------------------------------------------



//
// Viewport script
// Check if the browser's viewport is big enough and set styles accordingly
//

function viewport() {

	var viewportheight;
	var minimumheight = 640;			// the required height of the viewport
	var offset = minimumheight / 2;		// position of the centeredContainer div
 
 	// Determine the viewport height 
	// Standards compliant browsers (webkit/mozilla/netscape/opera/IE7)
	if (typeof window.innerHeight != 'undefined') {
		viewportheight = window.innerHeight
	}
	// IE6 in standards compliant mode
	else if (typeof document.documentElement != 'undefined' && document.documentElement.clientHeight != 0) {
		viewportheight = document.documentElement.clientHeight
	}
	// Older versions of IE
	else {
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}	

	// If the viewport height is larger than the required height
	if (viewportheight < minimumheight ) {
		document.getElementById('body').style.overflow = 'auto';
		document.getElementById('html').style.overflow = 'auto';
		document.getElementById('centeredContainer').style.top = offset+'px';
	}
	else {
		document.getElementById('body').style.overflow = 'hidden';
		document.getElementById('html').style.overflow = 'hidden';
		document.getElementById('centeredContainer').style.top = 50+'%';
	}
}





//
// Styleswitcher script
// Used to switch to a different stylesheet by means of cookies
//

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}


function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}


function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}


function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}


function readCookie(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;
}


window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}


window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}


// if cookie exists, use the saved style. If not, use the standard style.
var cookie = readCookie("style");
var styleTitle = cookie ? cookie : getPreferredStyleSheet();
if (styleTitle != null) setActiveStyleSheet(styleTitle);