/*
This file contains all other user interaction functions:
switching to the 3 column layout, switching to the 1 column
layout, increasing and decreasing the font, setting the 
window resize, etc.
*/

var classFix;
if (document.all){
	classFix = "className";
} else {
	classFix = "class";
}

// these values come from the cookie
var columnMode = 1;

// set some defaults values
var originalHeight;
var lineHeight = 16;
var currentPos = 0;
var tHeight = 0;

// use these values to manipulate the width of the text columns
// col3Width is used in the 3 column layout and col1Width is 
// used in the 1 column layout.
var col3Width = 210;
var col1Width = 421;
var colWidth = 0;

// use this number to manipulate the height of the article text box
// it is counter intuitive: smaller is taller, bigger is shorter
var heightOfColumns = 250;

var articlePhoto = null;

// returns the height of the current window
function getHeight(obj){
	if (obj == "window"){
		if (window.innerHeight){
			return window.innerHeight;
		} else {
			return document.getElementById("bodyNode").offsetHeight;
		}
	} else {
		obj = document.getElementById(obj);
		if (obj.offsetHeight){
			return obj.offsetHeight;
		}
	}
}
	
// makes the columns and copies the text node into the newly
// created columns ac is the div holding the cloned node - at
function articleSetup(){
	// get the fontSize value from the cookie
	var cv1 = readCookie("ar06userdata");
	cookieValues = parseCookie(cv1);
	var fontSize = cookieValues[1];
	var parentDiv = document.getElementById("articleParent");
	var currentPos = 0;
	for (var i = 0; i < 3; i++){
		var col = document.createElement("div");
		col.setAttribute("id", "ac" + i);
		//col.setAttribute("class", "artCol");
		col.setAttribute(classFix, "artCol");
		parentDiv.appendChild(col);
		var obj = document.body;
		var artText = obj.cloneNode(true);
		artText.setAttribute("id", "at" + i);
		artText.style.display = "block";
		artText.style.top = "0px";
		artText.style.fontSize = fontSize + "px";
		artText.style.lineHeight = lineHeight + "px";
		col.appendChild(artText);		
	}
	/*if (document.getElementById("articlePhoto")){
		articlePhoto = document.getElementById("articlePhoto");
	}*/
}

// Gets the font set by the cookie & sets it
function articleRememberFont(){
	// get the fontSize value from the cookie
	var cv1 = readCookie("ar06userdata");
	cookieValues = parseCookie(cv1);
	var fontSize = cookieValues[1];
	document.body.style.fontSize = fontSize + "px";
}

// moves the columns into position, but adjust the top
function layoutArticles(){	
	var parentHeight = getHeight("articleParent");
	for (var i = 0; i < columnMode; i++){
		var obj = document.getElementById("at" + i);
		//make sure at least 2 rows of article text are available 
		if (parentHeight > (2 * lineHeight)){
			obj.style.top = -1 * (parentHeight * (i + currentPos));
		}
	}
	/*if (articlePhoto != null){
		layoutPhoto();
	}*/
	articlePages();
}

// resets the article to the first "page"
function resetArticle(){
	currentPos = 0;
	layoutArticles();
}

// returns the snap to align article; this adjusts the height of the 
// article window with a number divisible by line height. the minium 
// number of rows is 10
function setSnap(mod){
	if (mod == null){
		mod = 0;
	}
	var snap = lineHeight * Math.round((getHeight("window") - mod) / lineHeight);
	if (snap < (lineHeight * 10)){
		snap = lineHeight * 10;
		if (window.scrollTo){
			window.scrollTo(0, 75);
		}
	} else if (window.scrollTo){
		window.scrollTo(0, 0);
	}
	return snap;
}

// clips the parent layer to the defined visible area
// if space allows include bottom, otherwise clip out most of bottom
function setArticleHeight(){
	if (columnMode > 1){
		if (document.body != null){
			document.getElementById("articleParent").style.height = setSnap(heightOfColumns);
			tHeight = getHeight("at1");
			parentHeight = getHeight("articleParent");
			//check to make sure page is still visible
			while ((parentHeight * (currentPos + (columnMode - 1))) > tHeight && currentPos > 0){
				currentPos = currentPos - 1;
			}
			layoutArticles();
		}
	}
}

// returns the number of screens an article spans, ie the number of pages
function articlePages(){
	var parentHeight = getHeight("articleParent");
	var totalColumns = tHeight / parentHeight;
	var totalPages = Math.ceil(totalColumns);
	var tPos = (currentPos + columnMode) / columnMode;
	var pagesTotal = Math.ceil(totalPages / columnMode);
	var pagesCurrent = Math.round(tPos);
	var obj = document.getElementById("pagenumbers");
	obj.innerHTML = "Page " + pagesCurrent + " : " + pagesTotal;
}

// displays next page icon when mousing over column
function nextPageOver(){
	var parentHeight = getHeight("articleParent");
	if ((parentHeight * (currentPos + columnMode)) < tHeight){	
		changeImage('next','/images/icon/nextHot.gif');
	}
}

// displays next page icon when mousing over column
function prevPageOver(){
	if (currentPos > 0){	
		changeImage('prev','/images/icon/prevHot.gif');
	}
}

// hides mouse icon when leaving the columns
function hidePrevPageOver(){
	changeImage('prev','/images/icon/prevCool.gif');
}

function hideNextPageOver(){
	changeImage('next','/images/icon/nextCool.gif');
}

// adjusts the article to display the next page
function nextPage(){
	//tHeight = getHeight("at0")
	var parentHeight = getHeight("articleParent");
	// check to make sure page is still visible 
	if ((parentHeight * (currentPos + columnMode)) < tHeight){
		currentPos = currentPos + columnMode;
	}
	layoutArticles();
}

// adjusts the article to display the previous page
function prevPage(){
	currentPos = currentPos - columnMode;
	if (currentPos < 0){
		currentPos = 0;
	}
	layoutArticles();
}

// user event to trigger the one column change
function eventOneColumn(){
	currentPos = 0;
	columnMode = 1;
	colWidth = col1Width;
	
	obj = document.getElementById("at0");
	obj.style.width = colWidth;
	
	obj = document.getElementById("articleParent");
	obj.style.height = getHeight("at0");
	
	obj = document.getElementById("at1");
	obj.style.display = "none";
	
	obj = document.getElementById("at2");
	obj.style.display = "none";

	parentHeight = getHeight("articleParent");

	for (var i = 0; i < columnMode; i++){
		obj = document.getElementById("at" + i);
		obj.style.top = -1 * (parentHeight * (i + currentPos));
	}
	articlePages();

	document.getElementById("next").style.display = "none";
	document.getElementById("prev").style.display = "none";
}

// user event to trigger the three column change
function eventThreeColumn(){
	if (window.scrollTo){
		window.scrollTo(0,0);
	}
	// save columnMode value in the cookie
	var cv = "columnMode:3&fontSize:" + cookieValues[1] + "&clippings:" + cookieValues[2];
	createCookie("ar06userdata",cv,7);

	threeColumn();
	setArticleHeight();
	articlePages();
}

// switches to three Column mode
function threeColumn(){

	// get the fontSize value from the cookie
	var cv1 = readCookie("ar06userdata");
	cookieValues = parseCookie(cv1);
	var fontSize = cookieValues[1];
	
	currentPos = 0;
	columnMode = 3;
	colWidth = col3Width;

	if (fontSize > 18){
		fontSize = 18;
	}
	var obj1 = document.getElementById("at0");
	//obj1.style.cursor = "pointer";
	obj1.style.cursor = "hand";
	obj1.style.zIndex = 5;
	obj1.style.display = "block";
	obj1.style.width = colWidth;
	obj1.style.left = 0;
	obj1.onmousemove = prevPageOver;
	obj1.onmouseout = hidePrevPageOver;
	obj1.onmousedown = prevPage;
	obj1.onmouseup = hidePrevPageOver;
	
	var obj2 = document.getElementById("at1");
	//obj2.style.cursor = "default";
	obj2.style.cursor = "default";
	obj2.style.zIndex = 5;
	obj2.style.display = "block";
	obj2.style.width = colWidth;
	obj2.style.left = colWidth + 16;
	obj2.onmousemove = null;
	obj2.onmouseout = null;
	obj2.onclick = null;
	obj2.onmouseup = null;

	var obj3 = document.getElementById("at2");
	//obj3.style.cursor = "pointer";
	obj3.style.cursor = "hand";
	obj3.style.zIndex = 5;
	obj3.style.display = "block";
	obj3.style.width = colWidth;
	obj3.style.left = 2 * (colWidth + 16);
	obj3.onmousemove = nextPageOver;
	obj3.onmouseout = hideNextPageOver;
	obj3.onclick = nextPage;
	obj3.onmouseup = hideNextPageOver;			

	document.getElementById("next").style.display = "block";
	document.getElementById("prev").style.display = "block";
}

function initArticle(){
	if (document.body != null){
		articleSetup();
		setArticleHeight();
		if (columnMode == 1){
			eventOneColumn();
		} else if (columnMode == 3){
			threeColumn();
		} else {
			alert("columnMode does not exist");
		}
		//setFaceSize();
		layoutArticles();
	
		//attempt to get article to NOT scroll when space bar is pressed in IE
		//obj = document.getElementById("articleParent");
		//obj.onkeyup = eventArticleFocus;
		//obj.onscroll = testArticle;
	} else {
		alert("Can not find p-maincontent node");
	}
}

// increases the font and adjusts the article layout
function increaseFont(){
	// get the current value from the cookie
	var cv1 = readCookie("ar06userdata");
	cookieValues = parseCookie(cv1);
	var currentFontSize = parseInt(cookieValues[1]);
	var newFontSize = currentFontSize + 1;
	if (newFontSize > 18){
		newFontSize = 18;
	}
	// save new value in the cookie
	var cv = "columnMode:" + cookieValues[0] + "&fontSize:" + newFontSize + "&clippings:" + cookieValues[2];
	createCookie("ar06userdata",cv,7);
	document.body.style.fontSize = newFontSize + "px";
	// re-draw article
	lineHeight = newFontSize + Math.round(.3 * newFontSize);
	/*
	for (var i = 0; i < 3; i++){
		obj = document.getElementById("p-maincontent" + i);
		obj.style.fontSize = newFontSize + "px";
		obj.style.lineHeight = lineHeight + "px";
	}
	*/
	setArticleHeight();
}

// increases the font and adjusts the article layout
function decreaseFont(){
	// get the current value from the cookie
	var cv1 = readCookie("ar06userdata");
	cookieValues = parseCookie(cv1);
	var currentFontSize = parseInt(cookieValues[1]);
	var newFontSize = currentFontSize - 1;
	if (newFontSize < 11){
		newFontSize = 11;
	}
	// save new value in the cookie
	var cv = "columnMode:" + cookieValues[0] + "&fontSize:" + newFontSize + "&clippings:" + cookieValues[2];
	createCookie("ar06userdata",cv,7);
	document.body.style.fontSize = newFontSize + "px";
	// re-draw article
	lineHeight = newFontSize + Math.round(.3 * newFontSize);
	/*
	for (var i = 0; i < 3; i++){
		obj = document.getElementById("p-maincontent" + i);
		obj.style.fontSize = newFontSize + "px";
		obj.style.lineHeight = lineHeight + "px";
	}	
	*/
	setArticleHeight();
}

// adjust article layout when the window is resized
function windowResize(){
	setArticleHeight();
}

window.onresize = windowResize;

// this toggles the format between one and three column layouts
// checks the current value from the cookie and does that opposite
function toggleFormat(){
	var format = columnMode;
	if (format == 1){
		eventThreeColumn();
	} else if (format == 3){
		eventOneColumn();
	} else {
		// do nothing
	}
}

// this might be used, should offset the image if there is one
function layoutPhoto(){
	//find if the last column is visible
	if (columnMode != 1){
		var obj = document.getElementById("at" + (columnMode - 1));
		if ((parseInt(obj.style.top) + obj.offsetHeight) < (parentHeight)){
			colHeight = getHeight("at0");
			graphicOffset = (columnMode - 1) * parentHeight;
			articlePhoto.style.marginTop = lineHeight;
			articlePhoto.style.top = colHeight - ((parentHeight * currentPos) + graphicOffset);
			if (parseInt(articlePhoto.style.top) < 0){
				articlePhoto.style.marginTop = 0;
				articlePhoto.style.top = 0;
			}
			if (columnMode == 2){
				articlePhoto.style.left = (1) * colWidth + 20 + "px";
			}
			if (columnMode == 3){
				articlePhoto.style.left = (2) * colWidth + 36 + "px";
			}
			articlePhoto.style.visibility = "visible";
		} else {
			articlePhoto.style.visibility = "hidden";
			//put the graphic below the first column which
		}
	}
}

<!--     

     // Mouseover effect on left hand menu
function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else
      window.onload = fnc;
  }
}

addOnloadEvent(function() {
  var items = document.getElementsByTagName("div");
  for (var i = 0; i < items.length; i++) {
    if (items[i].className=="menuoff") {
      items[i].onmouseover = function() {
        this.style.backgroundColor = '#7C6D60';
        this.style.backgroundImage = "url" + "('/assets/menu_shadow.jpg')";
      };
      items[i].onmouseout = function() {
        this.style.backgroundColor = '';
        this.style.backgroundImage = "url" + "('/assets/menu_line.gif')";
      };
    }
  }
} );

     // Stylesheet change

function setCookie(name, value) {
expires = new Date();
expires.setYear(expires.getYear() + 1900 + 2);
var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "" )+ "; path=/";
document.cookie = curCookie;
}

function getCookie(name) {
var prefix = name + "=";
var nullstring = "";
var cookieStartIndex = document.cookie.indexOf(prefix);
if (cookieStartIndex == -1)
return nullstring;
var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
if (cookieEndIndex == -1)
cookieEndIndex = document.cookie.length;
return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

var whichskin = getCookie("skinthesite");
if (whichskin == "Default") {
document.write('<link rel="stylesheet" href="../themes/PhilipsINR/css/style.css" type="text/css" media="screen" title="Default">')
} else if (whichskin == "Larger") {
document.write('<link rel="stylesheet" href="/assets/style_larger.css" type="text/css" media="screen" title="Larger">')
} else {
document.write('<link rel="stylesheet" href="../themes/PhilipsINR/css/style.css" type="text/css" media="screen" title="Default">')
}

// function for changing stylesheets using document.styleSheets
function setStyleSheet(theme){
	setCookie("skinthesite",theme);
	for ( i = 0; i < document.styleSheets.length; i++ ) {
		if ( document.styleSheets[i].title ) {
			document.styleSheets[i].disabled = true;
		if ( document.styleSheets[i].title == theme )
              document.styleSheets[i].disabled = false;
		}
	}
	window.location.reload(false);
}

     // Email pop-up window - English

   function popupsendeng(title){
    var docurl = document.location.href;
    var sendto = "/english/sendfriend/index.php?docurl="+docurl+"&section="+sectiontitle;
    var mywin = window.open(sendto,'sendtofriend','width=500,height=514, directories=no,location=no,menubar=no,scrollbars=yes, status=no,toolbar=no,resizable=yes');
    mywin.focus();
    window.name = "main";
   }


     // Email pop-up window - French

   function popupsendfr(title){
    var docurl = document.location.href;
    var sendto = "/francais/sendfriend/index.php?docurl="+docurl+"&section="+sectiontitle;
    var mywin = window.open(sendto,'sendtofriend','width=500,height=550, directories=no,location=no,menubar=no,scrollbars=yes, status=no,toolbar=no,resizable=yes');
    mywin.focus();
    window.name = "main";
   }


     // quiz pop-up windows

function openquizwin(link,name,width,height) {
window.open(link,name,'toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=yes,scrollbars=yes,width='+width+',height='+height);
window.name = "main";
}


     // All other pop-up windows

function openwin(link,name,width,height) {
window.open(link,name,'toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=yes,scrollbars=no,width='+width+',height='+height);
window.name = "main";
}

     // FAQs reveal



function findPosY(obj)
{
  var curtop = 0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      curtop += obj.offsetTop
      obj = obj.offsetParent;
    }
  } else if (obj.y)
    curtop += obj.y;
  return curtop;
}

function hidecell(id)
{
  var element = document.getElementById(id);
  element.style.display = "none"
}

function showcell(id)
{
  var element = document.getElementById(id);
  hidecells();
  element.style.display = "block";
}


function setPosT(obj) 
{
  setTimeout(function(){setPos(obj)},100);
}

function setPos(obj)
{ 
  var y = findPosY(obj);
  scrollTo(0,y);
}

/* The following functions are used on the FAQ page */

function togglecell(id)
{
  var element = document.getElementById(id);
  if (element.style.display == "none"){
    showcell(id)
  } else {
    hidecell(id)
  }
}

function hidecell(id)
{
  var element = document.getElementById(id);
  element.style.display = "none"
}

function hidecells()
{
  var allCells = document.getElementsByTagName("div")
  for (var k=0;k<allCells.length;k++) { 
    var cell = allCells[k];
    if(cell.className == "faqhide") {
      cell.style.display = "none" 
    } 
  } 
}

function showcell(id)
{
  var element = document.getElementById(id);
  hidecells();
  element.style.display = "block";
}

   // -->


