// 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 = 223;		// width of the element
var objArranged = 0;	// objects are not yet arranged
var orderChanged = true;



// 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 mainPageWidth = 910;

var pagePosition = 0;
var lastPagePosition = -100;

var lastWWidth = 0;
var lastWHeight = 0;

var speedStatus = 0;


// gallery
var interval = 25;

var g_lowSpeed = 1;
var g_midSpeed = 3;
var g_highSpeed = 5;

var padding = 20;

var divGallery = null;
var galleryWidth = 0;
var galleryHeight = 0;
var galleryX = 0;
var galleryY = 0;


var hasGallery = true;

var divGalleryPictures = null;

var thePictures = Array ();
var nbPictures = 0;
var thePicturesSize = Array ();
var thePictureTotalSize = 0;

// set the orientation. default horizontal i.e. vert= false
var vert = false;


// 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
var IE6 = (navigator.userAgent.indexOf ("MSIE 6.0") != -1);

// 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 (!event)
		return;
	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, arrange different elements according to the browser used
	// this is to be done only once
	if (!divContent)
	{
//		alert ("aaa");
		divContent = document.getElementById ("moving").innerHTML;
	    divArrange = document.getElementById ("arrange");
		divFooter = document.getElementById ("footer");
		divFooter.style.display = "block";

		if (IE6)
			divFooter.style.position = "absolute";

		// duplicate elements in array in case they are not enough
		if (1)	// no condition right now. We only duplicate the orriginal elements				//(!elements.length || elements.length * elementWidth < wWidth )
		{
			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;
			}
		}
	}

	// calculate window sizes
 	wHeight = windowHeight ();
	wWidth = windowWidth ();
	
	if (IE6 && lastWWidth != wWidth)
	{
		divFooter.style.width = "900";
	}


	
	// calculate page position
	pagePosition = Math.floor((wWidth - mainPageWidth) / 2) + 10;
	
	// if the page width changed
	if (pagePosition != lastPagePosition)
	{
		//alert ("changed!");
	}

	
	// make some corrections to the scroll so that it behaves better
	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;

	// re-arange the bottom div in IE6 (first time with a dummy value and next with the right value)
	if (IE6 && (lastScrollTop != scrollTop || lastScrollLeft != scrollLeft))
	{		
	    divFooter.style.bottom = "-1px";
		divFooter.style.left = "-1px";
		divFooter.style.width = "100%";
  	}

	//window.status = document.documentElement.scrollLeft + ",  " + document.body.clientWidth + ",  " + wWidth + ",   " +elements.length + ", " + elementWidth;

	// set elements position
	objArranged = 0;
	if ( ( objArranged < elements.length-1 ) || wWidth < mainPageWidth )
	{
		objArranged = 0;
		if (IE6)
			divFooter.style.width = "99%";

		
		for (i = 0; i < elements.length; i++)
		{
			ob= elements [ (i+firstElement) % elements.length ];
			if (ob)
			{
				pozitia = pagePosition  + pozInit + i*elementWidth;
				ob.style.left = pozitia +"px";
				//ob.style.top = (6 - i*103) +"px";
				
				if ( i < 4 )
					ob.style.display = "block";
				else if ( i == 4 )
					ob.style.display = speedStatus < 0 ? "none" : "block";
				else 
					ob.style.display = "none";
				
				objArranged +=1;
			}
		}
	
		// 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 )
		{
//			window.status = "este! " + pozInit +", " + pagePosition +", " + elementWidth;
			pozInit = - 1;
			firstElement = ( firstElement + 1 ) % elements.length;
			
			orderChanged = true;
		}

		// 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;

			orderChanged = true;
		}
	
		// 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;

	
	
	if (IE6 && (lastScrollTop != scrollTop || lastScrollLeft != scrollLeft ))
	{
		divFooter.style.bottom = "0px";
		divFooter.style.left = scrollLeft + "px";

		lastScrollTop = scrollTop;
		lastScrollLeft = scrollLeft;
		
//		window.status=scrollLeft;
		
  	}

//	if (IE6 && divFooter.style.width != "100%")
//		divFooter.style.width = "100%";

	
	if ( wWidth < mainPageWidth || !orderChanged )
	{
		orderChanged = false;
		
		if (wWidth >= mainPageWidth)
		{
			speed = -lowSpeed;
			speedStatus = 0;
		}
		else
		{
			// check for mouse position and set the row speed accordingly
			if (inArea)	// mouse is over the row. 
			{
				if (mX <= wWidth * 0.25)
				{
					speed = hiSpeed;
					speedStatus=1;
				}
			    else if (mX > wWidth * 0.25 && mX < wWidth * 0.45)
				{
		    		speed = medSpeed;
					speedStatus=2;
				}
			    else if (mX > wWidth * 0.75)
				{
		    		speed = -hiSpeed;
					speedStatus=3;
				}
			    else if (mX > wWidth * 0.55 && mX < wWidth * 0.75)
				{
		    		speed = -medSpeed;
					speedStatus=4;
				}
			    else
				{
		    	speed = 0;
	    		}
			}
			else							// mouse is outside the row
			{
				speed = -lowSpeed;
				speedStatus = 0;
			}
		}
	}
	else
	{
		speed = 0;
		speedStatus = -1;
	}

//  window.status = status;

//  window.status = lastScrollTop + ",  " + scrollTop;




  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;
  }



	if (IE6 && lastWWidth != wWidth)
	{
		divFooter.style.width = "100%";
		lastWWidth = wWidth;
	}



//	lastPagePosition = pagePosition;
	//window.status += lastPagePosition;
	if (pagePosition != lastPagePosition)
	{
//		objArranged = 0;
//		alert ("aa");
//		lastPagePosition = pagePosition;
	}
  
	// recall te function after a while
	setTimeout (setPoz, 5);
}

function hideCongrat ()
{
	document.getElementById ("felicitare").style.display = "none";
}








function setPositions (firstPicture, firstPosition)
{
	
	if (!document.getElementById)
		return;
		
	if (!hasGallery)
		return
		
	if (!divGalleryPictures)
		if (document.getElementById ("picturesGalleryPictures"))
		{
			document.getElementById ("picturesGallery").style.display = "block";
			
			divGalleryPictures = document.getElementById ("picturesGalleryPictures");
			thePictures = divGalleryPictures.getElementsByTagName("a");
			nbPictures = thePictures.length;
			
			for (i=0; i<nbPictures; i++)
			{
				thePictures[i].getElementsByTagName("img")[0].selSize = vert ? thePictures[i].getElementsByTagName("img")[0].height : thePictures[i].getElementsByTagName("img")[0].width
				thePicturesSize [i] = thePictures[i].getElementsByTagName("img")[0].selSize;
				thePictureTotalSize += thePicturesSize [i];
			}
			
			
		}
		else
		{
			hasGallery = false;
			return;
		}

		
	if (firstPicture == undefined)
		firstPicture = 0;
		
	if (firstPosition == undefined)
		firstPosition = 0;
		
	//window.status = findPos (document.getElementById ("picturesGallery", true)) + ", Mouse: " + mX + ", " +mY;	//firstPicture +", " + firstPosition;

	actualPosition = firstPosition;
	
	
	
	for (i = firstPicture; i < nbPictures + firstPicture; i++)
	{
		j = i % nbPictures;
		
		if (i == firstPicture + 1)
			secondPosition = actualPosition;
			
		if (vert)
			thePictures[j].style.top = actualPosition+"px";
		else
			thePictures[j].style.left = actualPosition+"px";
		
		actualPosition += thePicturesSize[j] + padding;
	}
	
	direction = getSpeed () / Math.abs (getSpeed());
	
	if (firstPosition > 0 )
	{
		firstPicture = (nbPictures + firstPicture - direction ) % nbPictures;
			
		firstPosition -= padding + thePicturesSize [firstPicture];
	}
	if (secondPosition < 0 )
	{
		firstPicture = (nbPictures + firstPicture - direction ) % nbPictures;
			
		firstPosition = secondPosition;
	}
	
	
	firstPosition += getSpeed ();
	
	setTimeout ("setPositions ("+firstPicture+", "+firstPosition+")", interval);

}

function getSpeed ()
{
	if (!divGallery)
	{
		divGallery = document.getElementById ("picturesGallery");
		
		galleryWidth = divGallery.offsetWidth;
		galleryHeight = divGallery.offsetHeight;
		galleryX = findPos (divGallery)[0];
		galleryY = findPos (divGallery)[1];
	}

	var gallerySize = vert	?	galleryHeight	:	galleryWidth;

	if ( gallerySize > thePictureTotalSize )
		return 0;
		
	g_speed = g_lowSpeed
	
	//window.status = secondPosition; //(mY - galleryY) +" >= 0  && " + (mY - galleryY) +" <= " + galleryHeight +" && " + (mX - galleryX) + " >= 0 && " + (mX - galleryX) +" <= " + gallerySize;
	
	if (mY - galleryY >= 0  && mY - galleryY <= galleryHeight && mX - galleryX >= 0 && mX - galleryX <= galleryWidth)
	{
	
		var mCoord 			= vert ? mY 		: mX;
		var galleryCoord 	= vert ? galleryY 	: galleryX;
		
		//window.status = "inside";
		switch (true)
		{
			case (mCoord - galleryCoord < gallerySize / 5):
				g_speed = g_highSpeed;
			break;
			case (mCoord - galleryCoord >= gallerySize / 5 && mCoord - galleryCoord < 2 * gallerySize / 5):
				g_speed = g_midSpeed;
			break;
			case (mCoord - galleryCoord >= 2 * gallerySize / 5 && mCoord - galleryCoord < 3 * gallerySize / 5):
				g_speed = 0;
			break;
			case (mCoord - galleryCoord >= 3 * gallerySize / 5 && mCoord - galleryCoord < 4 * gallerySize / 5):
				g_speed = -g_midSpeed;
			break;
			case (mCoord - galleryCoord >= 4 * gallerySize / 5):
				g_speed = -g_highSpeed;
			break;
		}
	}
	else
	{
		//window.status = "outside";
	}
	return g_speed;
} 


function findPos(obj, absolute) 
{
	if (absolute == undefined)
		absolute = false;
		
	var curleft = curtop = 0;
	if (obj.offsetParent)
		do 
		{
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
		} 
		while (obj = obj.offsetParent);

	if (!absolute)
		return [curleft, curtop];
	else
		return [curleft - pageXOffset, curtop - pageYOffset];
}


