/** >   File header
**********************************************************************
*      COPYRIGHT (C)                                          K&M Soft
*
*      The copyright to the sourcecode herein is the property of
*      K&M Soft.
*
*      The sourcecode may be used and/or copied only with the written
*      permission from K&M Soft or in accordance with the terms and
*      conditions stipulated in the agreement/contract under which
*      the source have been supplied.
*
**********************************************************************

**********************************************************************
*
*    Filename: csScripts.js
*      Author: Kent Granström
* Description: Photo gallery functionality
*
*********************************************************************/

String.prototype.trim = function() { return this.replace( /^\s+|\s+$/, "" ); }
window.onload         = function() { stripedTable(); }

/* Deals with the scrollable table of images        */
/* http://www.alistapart.com/articles/zebratables/  */
function removeClassName (elem, className)
{
	elem.className = elem.className.replace(className, "").trim();
}

function addCSSClass (elem, className)
{
	removeClassName (elem, className);
	elem.className = (elem.className + " " + className).trim();
}

function stripedTable()
{
	if (document.getElementById && document.getElementsByTagName)
	{
	 var allTables = document.getElementsByTagName('table');
		if (!allTables) { return; }

		for (var i = 0; i < allTables.length; i++)
		{
			if (allTables[i].className.match(/[\w\s ]*scrollTable[\w\s ]*/))
			{
				var trs = allTables[i].getElementsByTagName("tr");
				for (var j = 0; j < trs.length; j++)
				{
					removeClassName(trs[j], 'alternateRow');
					addCSSClass(trs[j], 'normalRow');
				}
				for (var k = 0; k < trs.length; k += 2)
				{
					removeClassName(trs[k], 'normalRow');
					addCSSClass(trs[k], 'alternateRow');
				}
			}
		}
	}
}

// Deals with the soft transition of the images
function BlendTransIt(imgName, altText)
{
    var aSlide = document.blendIMG;
    aSlide.style.filter = "blendTrans(duration = 1)";
    aSlide.filters.blendTrans.Apply();
    aSlide.src = imgName;
    aSlide.alt = altText;
    aSlide.filters.blendTrans.Play();
}


// ###########################################
// New handling of opacity to work on more than IE browsers. 
//change the opacity for different browsers
function changeOpac(opacity, id)
{
	var obj           = document.getElementById(id).style; 
	obj.opacity       = (opacity / 100);
	obj.MozOpacity    = (opacity / 100);
	obj.KhtmlOpacity  = (opacity / 100);
	obj.filter        = "alpha(opacity=" + opacity + ")";
}

// Change the image
function changeImage(imgId, imgFile)
{
  // Make new image
  document.getElementById(imgId).src = imgFile;	  
}

function Fade(imgId, imgFile, opacStart, opacEnd, ms)
{
	// Speed for each frame
	var speed = Math.round(ms / 100);
	var timer = 0;

  // Fade out the old image
  for (i = opacStart; i > opacEnd; i--) 
	{
		setTimeout("changeOpac(" + i + ",'" + imgId + "')", timer * speed);
		timer++;
	}

  // Activate the new image
	setTimeout("changeImage('" + imgId + "', '" + imgFile + "')", timer * speed);

  // Fade in the new image
	for (i = opacEnd; i < opacStart; i++) 
	{
    // Fade in the new image
		setTimeout("changeOpac(" + i + ",'" + imgId + "')", timer * speed);
		timer++;
  }  
  // Set final bit.....  
	setTimeout("changeOpac(" + opacStart + ",'" + imgId + "')", timer * speed);
} 


function BlendIMG(imgId, imgFile)
{
  var opacity = document.getElementById(imgId).style.opacity;

  if (opacity ==  1 || opacity == 100)
  {
    Fade(imgId, imgFile, 100, 0, 800);
  }
}

// #################################
//-- begin: Scroller's Algorithm -->
// #################################
function initNewsScroller()
{
  if (document.getElementById)
  {
    ns6div            = document.getElementById('iens6div');
    ns6div.style.top  = sheight + "px";
    ns6div.innerHTML  = singletext[0];
    sizeup            = ns6div.offsetHeight;
    ns6scroll();
  }
  else if (document.layers)
  {
    ns4layer      = document.ns4div.document.ns4div1;
    ns4layer.top  = sheight;
    ns4layer.document.write(singletext[0]);
    ns4layer.document.close();
    sizeup        = ns4layer.document.height;
    ns4scroll();
  }
  else if (document.all)
  {
    iediv = iens6div;
    iediv.style.pixelTop = sheight + "px";
    iediv.innerHTML = singletext[0];
    sizeup=iediv.offsetHeight;
    iescroll();
  }
}

function iescroll()
{
  if (iediv.style.pixelTop > 0 && iediv.style.pixelTop <= sspeed)
  {
    iediv.style.pixelTop = 0;
    setTimeout("iescroll()", spause);
  }
  else if (iediv.style.pixelTop >= sizeup * -1)
  {
    iediv.style.pixelTop -= sspeed + "px";
    setTimeout("iescroll()", 100);
  }
  else
  {
    if (ii == singletext.length - 1)
      ii = 0;
    else 
      ii++;
    iediv.style.pixelTop = sheight + "px";
    iediv.innerHTML = singletext[ii];
    sizeup = iediv.offsetHeight;
    iescroll();
  }
}

function ns4scroll()
{
  if (ns4layer.top > 0 && ns4layer.top <= sspeed)
  {
    ns4layer.top = 0;
    setTimeout("ns4scroll()", spause);
  }
  else if (ns4layer.top >= sizeup * -1)
  {
    ns4layer.top -= sspeed;
    setTimeout("ns4scroll()", 100);
  }
  else
  {
    if (ii == singletext.length - 1)
      ii = 0;
    else
      ii++;
    ns4layer.top = sheight;
    ns4layer.document.write(singletext[ii]);
    ns4layer.document.close();
    sizeup = ns4layer.document.height;
    ns4scroll();
  }
}

function ns6scroll()
{
  if (parseInt(ns6div.style.top) > 0 && parseInt(ns6div.style.top) <= sspeed)
  {
    ns6div.style.top = 0;
    setTimeout("ns6scroll()", spause);
  }
  else if (parseInt(ns6div.style.top) >= sizeup * -1)
  {
    ns6div.style.top = parseInt(ns6div.style.top) - sspeed + "px";
    setTimeout("ns6scroll()", 100);
  }
  else
  {
    if (ii == singletext.length - 1)
      ii = 0;
    else
      ii++;
    ns6div.style.top = sheight + "px";
    ns6div.innerHTML = singletext[ii];
    sizeup = ns6div.offsetHeight;
    ns6scroll();
  }
}
//-- end: Scroller Algorithm -->
