var oddObjArray = new Array(); //tracks all child nodes in article for odd sized type and images

var line_height = font_size*LINE_HEIGHT_MOD; // column hieghts are deteremined by the line height, this is the critical variable
var column_position = 0; // page position
var column_height_total = 0; // ??
var delta = 153;
var ihtml = "";// de cautat alta solutie
var bodyNode; // body tag id should fix this and id the body by getElementsByTagName
var initflag = false;
var FLAG_SHORT = false;
var viewer_height = null; // height of the text viewer
allImagePositions = new Array();
allParagraphPositions = new Array();

// short for getElementById
function gId(o){  return document.getElementById(o);}

// this is work around a bug in early version of the Mozilla engine(in Netscape 6.0)
// the bug does not track object offsets properly
// this method should be called when the fonts are resized, or any other
// event which would affect offsets
function st_imageCount()
	{
	var obj = gId("at0");
	var allImages = obj.getElementsByTagName("img");
	
	for (var imgCounter = 0; imgCounter < allImages.length; imgCounter++)
		{
		allImagePositions[imgCounter] = Math.abs(allImages[imgCounter].offsetTop);
		}
	}

//places positions of paragraphs into an array
function st_paragraphCount()
	{
	var obj = gId("at0");
	if (obj == null) return;
	var allParagraphs = obj.getElementsByTagName("p");
	
	nsFix = 0;
	
	for (var pCounter = 0; pCounter < allParagraphs.length; pCounter++)
		{
		//Netscape 6.01(and probably lower) starts the first paragraph out at a number other than
		//0, although it should be 0, this is a fix for that problem
		if (pCounter == 0) nsFix = Math.abs(allParagraphs[pCounter].offsetTop);
		allParagraphPositions[pCounter] = Math.abs(allParagraphs[pCounter].offsetTop)-nsFix;
		}
	}	
	
	
// controls display of images	
function st_displayImages()
	{
	//var imgOut = ""; //this string contains the HTML the be written out to create the images
	if (gId("at0") == null) return;
	var obj = gId("at0");
	var allImages = obj.getElementsByTagName("img");
	
	
	var start = Math.abs(-1*(viewer_height*(column_position)));
	var end = Math.abs((viewer_height*(COLUMN_NUM+column_position)));
	var imgOut = "";//start+" | "+end+"<br><br>";
	
	for (var imgCounter = 0; imgCounter < allImages.length; imgCounter++)
		{	
			var tempTop = allImagePositions[imgCounter];
			var imgObj = allImages[imgCounter];
			
			/*
			This is a fix for mozilla, but appears to have been fixed in Mozilla version .9.4 (or earlier)
			Since the offsetTop seems to be adjusted based also on the parent's position
			*/
			
			var comment = start+" | "+end+" | "+tempTop;
			
			window.status = comment +" "+(tempTop >= start && tempTop <= end);
			
			//an image has been found
			if (tempTop >= start && tempTop <= end)
				{
			
				if (!IMG_INLINE)
					{
					//you will want to alter this to fit your layout needs
					imgOut += "<div><img src=\""+allImages[imgCounter].src+"\"><br>"+allImages[imgCounter].getAttribute('custom')+"</div>";
					//imgOut =imgOut+ "<div><br>"+allImages[imgCounter].getAttribute('custom')+" "+tempTop+" | "+imgCounter+"</div>";
					}
				if (IMG_INLINE)
					{
					
					allImages[imgCounter].style.width="255";
					allImages[imgCounter].style.height="153";
					}
				}
		}
	// draw the images
	if (!IMG_INLINE)
		{
		gId(IMG_VIEWER).style.display = "none"
		gId(IMG_VIEWER).innerHTML = imgOut;
		gId(IMG_VIEWER).style.display = "block"

		}
	
	}

// makes sure that a paragraph starts on the first line, to be flush with the top
// this currently is buggy in layouts with more than 2 columns

function st_alignParagraphs()
	{
	var paragraphOffset = 0;
	for (var c = 0; c < COLUMN_NUM; c++)
		{
		
		var column = gId("at"+c);
		var allParagraphs = column.getElementsByTagName("p");
		
		// offset the column for any previous out of alignment paragraphs
		column.style.top =  (parseInt(column.style.top)-paragraphOffset)+"px";
		
		var start = Math.abs(-1*(viewer_height*(c+column_position)))+1-paragraphOffset;
		var end = start+line_height;
	
		for (var pCounter = 0; pCounter < allParagraphs.length; pCounter++)
			{	
				var pTop = allParagraphPositions[pCounter]-paragraphOffset;//Math.abs(allImages[imgCounter].offsetTop);
				var imgObj = allParagraphs[pCounter];
				
				//a paragraph is within the range
				if (pTop >= start && pTop <= end)
					{
					//adjust the top of the active column
					column.style.top = ((parseInt(column.style.top)-line_height))+"px";
					
					// increase the global paragraphOffset
					paragraphOffset = paragraphOffset+line_height;
					
					// for debugging
					//allParagraphs[pCounter].style.color = "#FF0000";					
					}
			}
		}
	
		
	}

/* returns the height, in pixels of object */

function st_getHeight(o)
	{
	if (o == "w") 
		{
		o = gId("bodyNode")
		if (window.innerHeight) return window.innerHeight
		else return parseInt(o.offsetHeight)
		}
	else {
	o = gId(o);
	if (o == null) return;// in mod normal nu ar fi trebuit dar e ceva busit
	if (o.offsetHeight) return o.offsetHeight
	 }
	}


/* setup 
	creates the columns

*/	
	
function st_columnSetup()
	{
	
	p = gId(VIEWER);
	p.innerHTML = "";
	column_position = 0;
		
	for (i=0; i < COLUMN_NUM; i++)
		{

		var o = gId(CONTENTS)
		var aT = document.createElement("div");
		aT.setAttribute("id","at"+i)
		aT.setAttribute("className","stText");
		aT.setAttribute("class","stText");
		aT.innerHTML = o.innerHTML;
			
		if (document.all) var cur = "hand"
		else  var cur = "pointer"
		if (i == 0 || i == COLUMN_NUM-1)	
			{
			aT.style.cursor = cur;
			//aT.onclick = st_columnClick;
			//aT.onmouseover = st_columnOver;
			//aT.onmouseout = st_columnOut;
			}
		
		aT.style.display = "block";
		p.appendChild(aT);
	}
	//POZA
		var aT = document.createElement("div");
		aT.setAttribute("id","poza");
		
		if (delta != 0) {
			aT.style.display = "block";
		} else {
			aT.style.display = "none";
			}
        if (COLUMN_NUM == 1) {
             aT.style.display = "inline";
        }	
		aT.setAttribute("className","stText");
		aT.setAttribute("class","stText");
		
		aT.innerHTML = "<table id=\"tabpoza\" width=\"100%\" bgcolor=\"#FFFFFF\" height=\"" + delta+"\"><tr><td style=\"background:url("+escape(picture_url)+");background-repeat:no-repeat;\"></td></tr></table>";
		p.appendChild(aT);
		
		//aT.setAttribute("class","stText");

	}



/* 
	lays out the columns 
	this is fired when ever the type faces change, "pages" turned, or window resized
*/	
	
function st_columnLayout()
	{	
	viewer_height = st_getHeight(VIEWER);
		
	for (var i = 0; i < COLUMN_NUM; i++)
		{				
			o = gId("at"+i);
			st_moveColumnLeftPosition(i);
			if (viewer_height > 2*line_height){
				//o.style.top = -1*(viewer_height*(i+column_position))+"px";
				var coltop = -1*(viewer_height*(i+column_position)-((column_position/2)*delta));
				o.style.top = coltop+"px";
				if (i == 1) {
					coltop += delta;
					o.style.top = coltop+"px";
				}
			}
		}
	var o = gId("poza");
	o.style.top = "0px";//-1*(viewer_height*(i+column_position))+"px";
	if (COLUMN_NUM > 1) {
		o.style.left = gId("at1").style.left;
		
	//o.style.height = "200px";
	}
	tC = column_height_total/(viewer_height-delta/2); 
	tP = Math.ceil(tC); 
	tPos = (column_position+COLUMN_NUM)/COLUMN_NUM; 
	o = gId("PP");
	oo = gId("P2");
	
	st_alignParagraphs();
	
	// if the current page is > than the total pages
	if ((Math.round(tPos)) > (Math.ceil(tP/COLUMN_NUM)))
		{
		st_screenPrevious();
		}
	//customize you page number here
	var pagx = Math.round(tPos);
	var pagy = Math.ceil(tP/COLUMN_NUM);
	if (o != null) {
    	o.innerHTML = ""+(pagx)+"/"+(pagy);
    	oo.innerHTML = ""+(pagx)+"/"+(pagy);
   	
	if (pagx == pagy) {
		gId("inddreapta").innerHTML = "<img src=\"./img/common/button_inainte_dsbl.gif\"/ width=\"52\" height\"13\"/>";
		gId("inddreapta1").innerHTML = "<img src=\"./img/common/button_inainte_dsbl.gif\"/ width=\"52\" height\"13\"/>";
		} else {

			gId("inddreapta").innerHTML = "<a title=\"inainte\" href=\"javascript:st_screenNext();\"><img src=\"./img/common/button_inainte.gif\"/ width=\"52\" height\"13\" alt=\"inainte\"/></a>";
			gId("inddreapta1").innerHTML = "<a title=\"inainte\" href=\"javascript:st_screenNext();\"><img src=\"./img/common/button_inainte.gif\"/ width=\"52\" height\"13\" alt=\"inainte\"/></a>";
		}
	if (pagx == 1) {
		gId("indstanga").innerHTML = "<img src=\"./img/common/button_inapoi_dsbl.gif\"/ width=\"52\" height\"13\"/>";
		gId("indstanga1").innerHTML = "<img src=\"./img/common/button_inapoi_dsbl.gif\"/ width=\"52\" height\"13\"/>";
		} else {
			gId("indstanga").innerHTML = "<a title=\"inapoi\" href=\"javascript:st_screenPrevious();\"><img src=\"./img/common/button_inapoi.gif\"/ width=\"52\" height\"13\" alt=\"inapoi\"/></a>";
			gId("indstanga1").innerHTML = "<a title=\"inapoi\" href=\"javascript:st_screenPrevious();\"><img src=\"./img/common/button_inapoi.gif\"/ width=\"52\" height\"13\" alt=\"inapoi\"/></a>";
		}
	}
	st_displayImages();

	}
	

// adjust the column height to the line height to avoid cropping a column mid line

function st_columnSnapHeight(m) 
	{
	if (m == null) m =0;
	if (m == 0) return 0;
	if (line_height <10 ) return m;
	//s = line_height*Math.round((m)/line_height)
	s = line_height*Math.round((m)/line_height)
	if (s < line_height*1) s = line_height*1 
	return s;
	}
	
	
function st_columnSetHeight()
	{
	
	adjust_divs_height();
	if (COLUMN_NUM > 1)
	    {
		
	if (gId(CONTENTS) != null)
		    {
    		gId(VIEWER).style.height = st_columnSnapHeight(NAVIGATION_AREA)+"px";
    		column_height_total = st_getHeight("at1");	viewer_height = st_getHeight(CONTENTS)
    		while ((viewer_height*(column_position+COLUMN_NUM-1)) > column_height_total && column_position > 0)column_position= column_position-1
    		st_columnLayout();
			}
	    } else {
	   
	        gId(VIEWER).style.height = st_columnSnapHeight(NAVIGATION_AREA)+"px";
    		column_height_total = st_getHeight("at0");	viewer_height = st_getHeight(CONTENTS)
     		//gId("at0").style.width = "542px";
    		while ((viewer_height*(column_position+COLUMN_NUM-1)) > column_height_total && column_position > 0)column_position= column_position-1
            st_columnLayout();

	    }
	}
	
	
function st_screenNext()
	{
	viewer_height = st_getHeight(VIEWER)
	//var coltop = -1*(viewer_height*(i+column_position)-((column_position/2)*delta));
	if ( ((viewer_height*(column_position+COLUMN_NUM))-(((column_position+2)/2)*delta)) < column_height_total)column_position= column_position+COLUMN_NUM;
	st_columnLayout();
	}
	
function st_screenPrevious()
	{
	column_position= column_position-COLUMN_NUM;
	if (column_position < 0) column_position = 0
	st_columnLayout()
	}

//returns the event object
function st_getEventObj(e) {
	if (e == null) e = event;
	if (e.srcElement) obj = e.srcElement;
	else obj = e.target;

	var count = 0;
	do {
	obj = obj.parentNode;
	} 
	while ((obj.id == null || obj.id == "") && count++ < 2); //work around for Moz
	 
	return obj;
	}


// for interface highlighting
// you will want to customize these two method
function st_over(direction)
	{
	obj = document.getElementById("page"+direction);
	obj.style.color = "#FF0000"
	}
	
function st_out(direction)
	{
	obj = document.getElementById("page"+direction);
	obj.style.color = "#000000"
	
	}

// called when the mouse is over the column
// add events like navigation button changes here
function st_columnOver(e)
	{
	// if there is more than one column
	if (COLUMN_NUM > 1)
		{
		var cur = document.all ? "hand" : "pointer";
		obj = st_getEventObj(e);
		if (obj.id.indexOf("0") > -1) // if the first column is moused over
			{
			var hilite = (column_position > 0);
			obj.style.cursor = hilite ? cur : "default";
			if (hilite) 
				try {st_over("previous");}
				catch(error){}
			}

		if (obj.id.indexOf(COLUMN_NUM-1) > -1) //if the last column is moused over
			{
			//var coltop = -1*(viewer_height*(i+column_position)-((column_position/2)*delta));	
			var hilite = ((viewer_height*(column_position+COLUMN_NUM)-(((column_position+2)/2)*delta)) <
			column_height_total);
			obj.style.cursor = hilite ? cur : "default";
			if (hilite) 
				try {st_over("next");}
				catch(error){};
			}
		}
	}
	
	
function st_columnOut(e)
	{
	if (e == null) return;
	// if there is more than one column
	if (COLUMN_NUM > 1)
		{
		
		obj = st_getEventObj(e);
		if (obj.id.indexOf("0") > -1) // if the first column is moused out
			{
			try {st_out("previous");}
				catch(error){}
			}

		if (obj.id.indexOf(COLUMN_NUM-1) > -1) //if the last column is moused out
			{
	   		try {st_out("next");}
				catch(error){}
	   		}
  		}
	}


// called when a column has been clicked on
// routes event to appropriate method, or cancles if user is clicking a link or copying text

function st_columnClick(e)
	{
	return;
	if (e != null) event = e;
		if (event.srcElement) obj = event.srcElement;
		else obj = event.target;
	
	if (obj.nodeName.toLowerCase() == "a" || obj.parentNode.nodeName.toLowerCase() == "a")
		{
		// route to the link; //
		//if (obj.nodeName.toLowerCase() == "a") document.location = obj.getAttribute("href");
		//if (obj.parentNode.nodeName.toLowerCase() == "a") document.location = obj.parentNode.getAttribute("href");
		}
		
	// object clicked is not a link
	
	else
		{
		//parse through the parent objects looking for an object with an id
		// this needs to be improbed to detect only objects with an id of "at"
		var tcounter = 0;
		if (obj.id == null || obj.id == "")
			{
			do {
				obj = obj.parentNode;
				}			
			while ((obj.id == null || obj.id == "") && tcounter < 10);
			}
		
		// if there is more than one column		
		if (COLUMN_NUM > 1)
			{
			// if the first column is clicked
			if (obj.id.indexOf("0") != -1) st_screenPrevious();
			
			//if the last column is clicked
			if (obj.id.indexOf(COLUMN_NUM-1) != -1) st_screenNext();
			}
		
		// different method will need to be used for single column mode
		
		if (COLUMN_NUM == 1)
			{
			
			}
		}
	}



//moves the column into horizontal position
function st_moveColumnLeftPosition(i){
        if (COLUMN_NUM * i == 1) return;
    	o = gId("at"+i) ;
	    o.style.left = (i*(parseInt(o.offsetWidth)+10))+"px";
    	if (COLUMN_NUM > 1) {
        	if (i == 1) {
    		        gId("poza").left = o.style.left;
            }
	    } else {
	        gId("poza").style.display = "none";
	    }
}


var loop_counter = 0; //used to check for possible infinite loop 

function st_fontSetSize(TEMP_LINE_HEIGHT_MOD)
	{
	

	line_height = Math.round(font_size+(LINE_HEIGHT_MOD*font_size));
	
	if (COLUMN_NUM == 2) {
    	for (var i = 0; i < COLUMN_NUM; i++) 
    		{ 
    		o = gId("at"+i);
    			try {
	    			o.style.fontSize = font_size+"px";
    				o.style.lineHeight = line_height+"px" 
    			} catch(Error) {
    				return;
    				} 
    	    //scalam si poza corespunzator
    		delta  = st_columnSnapHeight(delta);
	        gId("poza").style.height = delta;
        	gId("tabpoza").style.height = delta;
        	adjust_divs_height() ;
	    	}  
	    } else {
	             o = gId(CONTENTS);
    	         try {
	    			o.style.fontSize = font_size+"px";
    				o.style.lineHeight = line_height+"px" 
        		 } catch(Error) {
    	    			return;
    		     }
	         
	         
	}
	//set paragraph margins
	tObject = gId(VIEWER);
	
	
	//ie version
	if (document.styleSheets[0].addRule != null)
		{
		document.styleSheets[1].rules.item(0).style.marginBottom =  line_height+"px";
		}
	
	var o = gId("stStyle");
	if (o.sheet != null)
		{
		o.sheet.cssRules[0].style.marginBottom = line_height+"px";
		}
		
	// if there is a rounding error in the line height then adjust the LINE_HEIGHT_MOD

	while ((st_getHeight("at0")/line_height) - Math.round(st_getHeight("at0")/line_height) != 0)
		{
		loop_counter++;
		if (loop_counter > 10) 
			{
			break;
			}
	
		LINE_HEIGHT_MOD = LINE_HEIGHT_MOD+.1;
		if (LINE_HEIGHT_MOD > MAX_LINE_HEIGHT) LINE_HEIGHT_MOD = ORIGINAL_LINE_HEIGHT_MOD;
		st_fontSetSize();
		}
		
	//st_paragraphCount();		
	//st_imageCount();
       if (COLUMN_NUM == 2) {
	        st_columnSetHeight();
	   }
	}
	
function st_fontLarger()
	{
	font_size = font_size+2;
	if (font_size > font_size_MAX) {font_size = font_size_MAX;}
	st_fontSetSize();
	}
	
function st_fontSmaller()
	{
	if (font_size % 2 == 1) {
	    	font_size = font_size-1;
	} else {
    	font_size = font_size-2;
    }
	if (font_size < font_size_MIN) font_size = font_size_MIN;
	st_fontSetSize();
	}

function st_init()
	{
	adjust_divs_height();
	if (document.createElement != null)
		{
		bodyNode = gId("bodyNode");
	
		if (gId(CONTENTS) != null)
			{
			//Mac IE like CONTESTS to have the stText style
			//gId(CONTENTS).setAttribute("class","stText");
			//gId(CONTENTS).setAttribute("className","stText");
			
			st_columnSetup();
	
			st_columnSetHeight(); 
			st_fontSetSize(); 
			
			st_columnLayout();
			
			gId(CONTENTS).style.display = "none";
			}
		
		document.onmousemove = st_NetscapeFix;
		
		window.onresize = st_columnSetHeight;
		}
	try	
		{
		// this is the clippings for smokinggun.com
		// you should take this out
		initClippings();
		}
	catch(error)
		{
	}
	//st_fontLarger();
	}
	

/* netscape fix  */	

function init() {
      if (COLUMN_NUM == 1)  return;
      if(gId(CONTENTS) == null)  return;
      if (ihtml =="") {
          ihtml = gId(CONTENTS).innerHTML;
      }
     

         
       // quit if this function has already been called
       if (initflag) return;
     
       // flag this function so we don't do the same thing twice
       initflag = true;
         if ( FLAG_SHORT) {
          change_layout(1);
          
           FLAG_SHORT = false;
          return;
      } 
       // create the "page loaded" message
       st_init();
   };

	
function st_NetscapeFix()
	{
	st_columnSetHeight();
	document.onmousemove = null;
	}


// adds a new style to hide the source text
function st_preload()
	{
	document.writeln("<style type=\"text/css\">");
	document.writeln("#"+CONTENTS+" {display:block}");
	//set images to 0 in size in the VIEWER
	document.writeln("#"+VIEWER+" img {	display:inline; 	height:0px; 	width:0px;");
	document.writeln("</style>");
	}

//Take the images ant the title and set the their heghts t line_heught_multiple
function adjust_divs_height() {
	var i=1;
	var titlu = "imagine"+i;
	while (gId(titlu) != null) {
	
//var h_imagine = st_getHeight(titlu);
var h_imagine = 123;
var noua_h = st_columnSnapHeight(h_imagine);
try { 
	gId(titlu).style.height = noua_h+"px";
                                                       	//alert(h_imagine + " " + noua_h);
} catch(error) {}
		
		i=i+1;
		titlu = "imagine"+i;
		
	}
	
}


function setPictureUrl(the_url) {
	picture_url = the_url;
	if (the_url == "") {
		delta = 0;
		}
	if (COLUMN_NUM == 0) {
		delta = 0;
		the_url = ""
		}
}

function change_layout(cols)  {

    init();
    if ((COLUMN_NUM == cols) &&(FLAG_SHORT == false))  {
        return;
    }
    COLUMN_NUM = cols;
    if (COLUMN_NUM == 1) {
        if (picture_url != "") {
            xtra = "<img height=\"153\" style=\"float:right;margin:10px;\" src = \""+picture_url+"\"  />";
        } else {
            xtra = "";
        }
        //gId(CONTENTS).innerHTML  = xtra  + ihtml; 
    	gId(CONTENTS).setAttribute("class","");
		gId(CONTENTS).setAttribute("className","");
		gId(CONTENTS).style.display = "block";
       
        if (gId("at0") != null) {
           gId(VIEWER).removeChild(gId("at0"));
        }
        if (gId("at1") != null) {
            gId(VIEWER).removeChild(gId("at1"));
        }
        if (gId("poza") != null) {
            gId(VIEWER).removeChild(gId("poza"));
        }
        gId(VIEWER).style.height = gId(CONTENTS).style.height;
         if (gId("navigare") != null) {
            gId("navigare").style.display = "none";
            gId("navigare1").style.display = "none";
         }
    }
    
    if (cols != 1) { 
        //gId(CONTENTS).innerHTML  = ihtml;
        if (gId("navigare") != null) {
            gId("navigare").style.display = "block";
            gId("navigare1").style.display = "block";
        }
        initflag = false;
        init();
    } 
}
       st_preload();
  //  init();//try to start the script at fast as possible

   /* for Mozilla -execute script when DOM is Loaded */
//   if (document.addEventListener) {
//       document.addEventListener("DOMContentLoaded", init, null);
//   }

   /* for Internet Explorer - same thing*/
   /*@cc_on @*/
   /*@if (@_win32)
       document.write("<script defer src=./js/ie_onload.js><"+"/script>");
   /*@end @*/

   /* for other browsers */
   //window.onload = init;

//st_fontSmaller();


