// settings for the pictures move
hiSpeed = 6;
medSpeed = 3;
lowSpeed = 0.5;
speed = lowSpeed;

// initialise
pozInit = -1			// initial position of the first left element
firstElement = 0;		// index of the first left displayed element 
elementWidth = 139;		// width of the element

// define an array to keep the refferences to the objects that have to be moved
elements = Array ();


var lastScrollTop = 0;
var lastScrollLeft = 0;
var scrollTop = 0;
var scrollLeft = 0;
var divContent = null;
var divFooter = null;
var divArrange = null;
var IE6 = (navigator.userAgent.indexOf ("MSIE 6.0") != -1);


// MOUSE COORDINATES
// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Variables to hold mouse x-y pos.s
var mX = 0
var mY = 0

// Main function to retrieve mouse x-y pos.s
function getMouseXY (e) {
	if (IE) { // grab the x-y pos.s if browser is IE
		mX = event.clientX + document.body.scrollLeft;
		mY = event.clientY + document.documentElement.scrollTop;
	} else {  // grab the x-y pos.s if browser is NS
		mX = e.pageX;
		mY = e.pageY;
	}  
	// catch possible negative values in NS4
	if (mX < 0){mX = 0}
	if (mY < 0){mY = 0}  
	return true;
}

function windowWidth ()
{
	var wWidth = 0;
	if( typeof( window.innerWidth ) == 'number' ) 
	{
		//Non-IE
		wWidth = window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		wWidth = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
		wWidth = document.body.clientWidth;
	}
	
	return wWidth;
}
function windowHeight ()
{
	var wHeight = 0;
	if( typeof( window.innerHeight ) == 'number' ) 
	{
		//Non-IE
		wHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientHeight || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		wHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientHeight || document.body.clientHeight ) ) {
	//IE 4 compatible
		wHeight = document.body.clientHeight;
	}
	
	return wHeight;
}

// function for setting the position of the elements. 
function setPoz ()
{
	// get the content of the original div
	if (!divContent)
	{
		divContent = document.getElementById ("moving").innerHTML;
    divArrange = document.getElementById ("arrange");
    if (IE6)
    {
      divFooter = document.getElementById ("footer");
      divFooter.style.position = "absolute";
      divTrafic = document.getElementById ("traficro");
      divTrafic.style.position = "absolute";
      
      divArrange.style.height = "133px";
    }
    else
      divArrange.style.height = "0px";
	}


 	wHeight = windowHeight ();
	wWidth = windowWidth ();
	
	//window.status = document.documentElement.scrollLeft + ",  " + document.body.clientWidth + ",  " + wWidth;
	
  if (document.documentElement.scrollLeft )
  if (IE)
    if (IE6)
     document.documentElement.scrollLeft = Math.min (document.documentElement.scrollLeft, document.body.clientWidth - wWidth);
    else
     document.documentElement.scrollLeft = Math.min (document.documentElement.scrollLeft, document.documentElement.scrollWidth - wWidth);
  else
     document.documentElement.scrollLeft = Math.min (document.documentElement.scrollLeft, document.documentElement.scrollWidth - wWidth+5);
		
	scrollTop = document.documentElement.scrollTop;
  scrollLeft = document.documentElement.scrollLeft;


	if (IE6 && (lastScrollTop != scrollTop || lastScrollLeft != scrollLeft))
	{		
    divFooter.style.bottom = "-1px";
		divFooter.style.left = "-1px";
    divTrafic.style.bottom = "132px";
		divTrafic.style.left = "-1px";
  }

	// find out how many elements there are
	if (!elements.length || elements.length * elementWidth < wWidth )
	{
		if (elements.length * elementWidth < wWidth )		// add elements if there are not enough
		{
			ha = document.getElementById ("moving").innerHTML;
			document.getElementById ("moving").innerHTML = ha + divContent;
		}
		
		for (i = 0; i < document.getElementById ("moving").getElementsByTagName("a").length; i++)
			document.getElementById ("moving").getElementsByTagName("a")[i].id = "a"+i;

		for (i=0; i<200; i++)
		{
		    ob=document.getElementById ("a"+i);
			if (ob)
			{
				elements [i] = ob;			
			}
			else
				break;
		}
	}
		
	// set elements position
	for (i = 0; i < elements.length; i++)
	{
		ob= elements [ (i+firstElement) % elements.length ];
		if (ob)
		{
			pozitia = pozInit + i*elementWidth;
			ob.style.left = pozitia +"px";
			//ob.style.top = (6 - i*103) +"px";
		}
	}
	
	// check if the first left element is out of the container
	// if so, set the second left element as first. 
	// As a consequence, the former first element will be placed at the end of the row
	if ( pozInit < -elementWidth )
	{
		pozInit = -1;
		firstElement = ( firstElement + 1 ) % elements.length;
	}
	// check if the first left element is already the container
	// if so, set the last element as first
	// As a consequence, the former last element will be placed at the beggining of the row
	if ( pozInit >= 0 )
	{
		pozInit = - elementWidth;
		firstElement = ( firstElement - 1 ) % elements.length;
		if (firstElement < 0)
			firstElement = elements.length - 1;
	}

	// increase the initial position of the row
	pozInit += speed;
	
	
	
	var inArea = false
	
  if (!window.pageYOffset)  // IE
  { 
    if (IE6)
    	inArea = mY - scrollTop > wHeight - 133 && mY - scrollTop < wHeight - 33;
    else
    	inArea = mY - scrollTop > wHeight - 133 && mY - scrollTop < wHeight - 33;
  }
  else
    inArea = mY - window.pageYOffset > wHeight - 133 && mY - window.pageYOffset < wHeight - 33;
	
	
	var status = 0;
	
	// check for mouse position and set the row speed accordingly
	if (inArea)	// mouse is over the row. 
	{
		if (mX <= wWidth * 0.25)
		{
			speed = hiSpeed;
			status=1;
		}
    else if (mX > wWidth * 0.25 && mX < wWidth * 0.45)
		{
    	speed = medSpeed;
			status=2;
		}
    else if (mX > wWidth * 0.75)
		{
    	speed = -hiSpeed;
			status=3;
		}
    else if (mX > wWidth * 0.55 && mX < wWidth * 0.75)
		{
    	speed = -medSpeed;
			status=4;
		}
    else
		{
    	speed = 0;
    }
	}
	else							// mouse is outside the row
	{
		speed = -lowSpeed;
	}

//  window.status = status;

//  window.status = lastScrollTop + ",  " + scrollTop;

	if (IE6 && (lastScrollTop != scrollTop || lastScrollLeft != scrollLeft))
	{
		divFooter.style.bottom = "0px";
		divFooter.style.left = scrollLeft + "px";

    divTrafic.style.bottom = "133px";
		divTrafic.style.left = scrollLeft + "px";

		lastScrollTop = scrollTop;
		lastScrollLeft = scrollLeft;
		
//		window.status=scrollLeft;
		
  }



  if (document.Show)
  {	
    document.Show.MouseX.value = mX;
    document.Show.MouseY.value = mY;
    document.Show.clientHeight.value = "w"+document.documentElement.clientWidth;
    document.Show.scrollHeight.value = "w"+document.documentElement.scrollWidth;
    document.Show.windowHeight.value = "w"+windowWidth ();
    document.Show.YOffset.value = "w"+window.pageXOffset;
    document.Show.scrollTop.value = scrollTop;
    document.Show.scrollLeft.value = scrollLeft;
    document.Show.inArea.value = inArea;
  }

	// recall te function after a while
	setTimeout (setPoz, 1);
}

