var classFix;
if (document.all){
	classFix = "className";
} else {
	classFix = "class";
}
var columnMode = 3;
var originalHeight;
var lineHeight = 16;
var currentPos = 0;
var col3Width = 300;
var heightOfColumns = 300;
function getHeight(obj){
if (obj == "window"){
		if (window.innerHeight){
			return window.innerHeight;
			
		} else {
			
			return 640;
		}
	} else {
		obj = document.getElementById(obj);
		if (obj.offsetHeight){
			return obj.offsetHeight;
		}
	}
}
function articleSetup(){
	var fontSize = 12;
    
    var cv1 = 12;
	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.getElementById("articleBody");
		var artText = obj.cloneNode(true);
		artText.setAttribute("id", "at" + i);
		artText.style.display = "block";
		artText.style.top = "0px";
		artText.style.left = col3Width*i+i*20+"px";
		artText.style.fontSize = fontSize + "px";
		artText.style.lineHeight = lineHeight + "px";
		col.appendChild(artText);		
	}
}
function layoutArticles(){	
	var parentHeight = getHeight("articleParent");
	for (var i = 0; i < columnMode; i++){
		var obj = document.getElementById("at" + i);
		if (parentHeight > (2 * lineHeight)){
			obj.style.top = -1 * (parentHeight * (i + currentPos))+'px';
		}
	}
	articlePages();
}
function resetArticle(){
	currentPos = 0;
	layoutArticles();
}
function setSnap(mod){
	if (mod == null){
		mod = 0;
	}
	var snap = lineHeight * Math.round((getHeight("window") - mod) / lineHeight)+'px';
	if (snap < (lineHeight * 10)){
		snap = lineHeight * 10+'px';
		if (window.scrollTo){
			window.scrollTo(0, 75);
		}
	} else if (window.scrollTo){
		window.scrollTo(0, 0);
	}
	return snap;
}
function setArticleHeight(){
	if (columnMode > 1){
		if (document.getElementById("articleBody") != null){
			document.getElementById("articleParent").style.height = setSnap(heightOfColumns);
			var tHeight = getHeight("at1");
			parentHeight = getHeight("articleParent");	
			while ((parentHeight * (currentPos + (columnMode - 1))) > tHeight && currentPos > 0){
				currentPos = currentPos - 1;
			}
		}
	}
}
function articlePages(){
	var parentHeight = getHeight("articleParent");
	var tHeight = getHeight("at1");
	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("tc_page_nav_middle");
	obj.innerHTML = "Pagina " + pagesCurrent + " din " + pagesTotal;
    
    // button activation/deactivation
    var prevButtonBG = document.getElementById("tc_prev_button_bg");
    var prevButtonText = document.getElementById("tc_prev_button_text");
    var nextButtonBG = document.getElementById("tc_next_button_bg");
    var nextButtonText = document.getElementById("tc_next_button_text");
    if (pagesTotal > 1) {
        if (pagesCurrent == 1) {
            // activate next button
            nextButtonText.style.color = "#000";
			document.getElementById("pnon").style.display='block';
			document.getElementById("pnoff").style.display='none';
            nextButtonBG.onmouseover = function () {
                nextButtonBG.style.backgroundColor = "#eoeoeo";
                nextButtonBG.style.cursor = "pointer";
            }
            nextButtonBG.onmouseout = function () {
                nextButtonBG.style.backgroundColor = "#eoeoeo";
            }
            nextButtonBG.onclick = function () { nextPage(); }
            // deactivate prev button
			document.getElementById("ppon").style.display='none';
			document.getElementById("ppoff").style.display='block';
            prevButtonText.style.color = "#eoeoeo";
            prevButtonBG.onmouseover = function () {
                prevButtonBG.style.backgroundColor = "#eoeoeo";
                prevButtonBG.style.cursor = "";
            }
        } 
        
        if (pagesCurrent > 1 && (pagesCurrent <= pagesTotal)) {
            // activate prev button
			document.getElementById("ppon").style.display='block';
			document.getElementById("ppoff").style.display='none';
            prevButtonText.style.color = "#000";
            prevButtonBG.onmouseover = function () {
                prevButtonBG.style.backgroundColor = "#eoeoeo";
                prevButtonBG.style.cursor = "pointer";
            }
            prevButtonBG.onmouseout = function () {
                prevButtonBG.style.backgroundColor = "#eoeoeo";
            }
            prevButtonBG.onclick = function () { prevPage(); }
            if (pagesCurrent == pagesTotal) {
                // deactivate next button
				document.getElementById("pnon").style.display='none';
			document.getElementById("pnoff").style.display='block';
                nextButtonText.style.color = "#ccc";
                nextButtonBG.onmouseover = function () {
                    nextButtonBG.style.backgroundColor = "#eoeoeo";
                    nextButtonBG.style.cursor = "";
                }
            } else {
                // activate next button
                nextButtonText.style.color = "#000";
                nextButtonBG.onmouseover = function () {
                    nextButtonBG.style.backgroundColor = "#eoeoeo";
                    nextButtonBG.style.cursor = "pointer";
                }
                nextButtonBG.onmouseout = function () {
                    nextButtonBG.style.backgroundColor = "#eoeoeo";
                }
                nextButtonBG.onclick = function () { nextPage(); }
            }
        }
    }
}

function nextPage(){
	tHeight = getHeight("at1")
	var parentHeight = getHeight("articleParent");
	if ((parentHeight * (currentPos + columnMode)) < tHeight){
		currentPos = currentPos + columnMode;
	}
	layoutArticles();
}
function prevPage(){
	currentPos = currentPos - columnMode;
	if (currentPos < 0){
		currentPos = 0;
	}
	layoutArticles();
}
function threeColumn(){
	var fontSize = 12;
    var cv1 = 12;
	currentPos = 0;
	columnMode = 3;
	colWidth = col3Width;

	if (fontSize > 18){
		fontSize = 18;
	}
	var obj1 = document.getElementById("at0");
	obj1.style.zIndex = 5;
	obj1.style.display = "block";
	obj1.style.width = colWidth;
	obj1.style.left = 0;
	
	var obj2 = document.getElementById("at1");

	obj2.style.zIndex = 5;
	obj2.style.display = "block";
	obj2.style.width = colWidth;
	obj2.style.left = colWidth + 20+'px';

	var obj3 = document.getElementById("at2");

	obj3.style.zIndex = 5;
	obj3.style.display = "block";
	obj3.style.width = colWidth;
	obj3.style.left = 2 * (colWidth + 20)+'px';

}

function initArticle(){
	if (document.getElementById("articleBody") != null){
		articleSetup();
		setArticleHeight();
		threeColumn();
		layoutArticles();
	} else {
		alert("Can not find articleBody node");
	}
}



//function windowResize(){
	//setArticleHeight();
	//layoutArticles();
//}

//window.onresize = windowResize;