var nbrOfPortraits = 12;
var nbrOfLandscapes = 9;
  
  function preload_image(imgObj,imgSrc)
  {
    if (document.images)
	  {
	    eval(imgObj+' = new Image()');
	    eval(imgObj+'.src = "'+imgSrc+'"');
	  }
  }
  
	function preload_slides()
  {
	  for(i=1; i<=nbrOfPortraits; i++)
    {
		  document.write('<img src="images/slides/portrait/'+i+'.jpg" class="hiddenElement">');
			document.write('<img src="images/slides/portrait/'+i+'C.jpg" class="hiddenElement">');
	  }
		for(i=1; i<=nbrOfLandscapes; i++)
    {
		  document.write('<img src="images/slides/landscape/'+i+'.jpg" class="hiddenElement">');
			document.write('<img src="images/slides/landscape/'+i+'C.jpg" class="hiddenElement">');
	  }
  }
	
  function changeImage(imgName,imgObj)
  {
  	document.images[imgName].src = eval(imgObj+".src");
  }

  preload_image('imageA1','images/homeOn.png');
  preload_image('imageA2','images/homeOff.png');
  preload_image('imageA3','images/homeOnR.png');
  preload_image('imageA4','images/homeOffR.png');

  preload_image('imageB1','images/aboutUsOn.png');
  preload_image('imageB2','images/aboutUsOff.png');
  preload_image('imageB3','images/aboutUsOnR.png');
  preload_image('imageB4','images/aboutUsOffR.png');

  preload_image('imageC1','images/musicOn.png');
  preload_image('imageC2','images/musicOff.png');
  preload_image('imageC3','images/musicOnR.png');
  preload_image('imageC4','images/musicOffR.png');

  preload_image('imageD1','images/eventsOn.png');
  preload_image('imageD2','images/eventsOff.png');
  preload_image('imageD3','images/eventsOnR.png');
  preload_image('imageD4','images/eventsOffR.png');

  preload_image('imageE1','images/contactOn.png');
  preload_image('imageE2','images/contactOff.png');
  preload_image('imageE3','images/contactOnR.png');
  preload_image('imageE4','images/contactOffR.png');
	
	preload_slides();
	
	function randomNumber(low,high)
	{
	  return low + ((high - low)  * Math.random());
	}
	
	function selectImage(which)
	{
	  if(which=='P')
		{
		  var picNbr = Math.round(randomNumber(1,nbrOfPortraits));
		  var URL = 'images/slides/portrait/' + picNbr;
		  return URL;
		}
		else
		{
		  var picNbr = Math.round(randomNumber(1,nbrOfLandscapes));
			var URL = 'images/slides/landscape/' + picNbr;
		  return URL;
		}
	}
	
	function loadSlides()
	{
	  loadTopSection();
		loadBottomSection();
	}

	function loadTopSection()
	{
	  document.getElementById('img1').src = selectImage('P') + '.jpg';
		document.getElementById('img2').src = selectImage('L') + '.jpg';
		document.getElementById('img3').src = selectImage('P') + '.jpg';
		document.getElementById('img4').src = selectImage('L') + '.jpg';
		document.getElementById('img5').src = selectImage('P') + '.jpg';
	}
	
	function loadBottomSection()
	{
	  document.getElementById('img6').src = selectImage('P') + '.jpg';
		document.getElementById('img7').src = selectImage('L') + '.jpg';
		document.getElementById('img8').src = selectImage('P') + '.jpg';
		document.getElementById('img9').src = selectImage('L') + '.jpg';
		document.getElementById('img10').src = selectImage('P') + '.jpg';
	}
	
	var divPosition   = 0;  // nbr of pixels from slideDiv position
	var divStepSize   = 1;  // in pixels
	var direction     = 0;  // 0 is in up postion, 1 is in down position
	var skipFirstTime = 0;
	var whichStep     = 0;
	var speedUp       = 1  // 1 is speeding up movement, 0 is slowing down
	var forTesting1   = 0;
	var forTesting2   = 0;
	
	function moveSlideDiv()
	{
	  
		if (divStepSize==1 && whichStep>=9 && speedUp==1){ divStepSize = 2;	whichStep = 0;		}
		if (divStepSize==2 && whichStep>=19 && speedUp==1){divStepSize = 3;	whichStep = 0;		}
		if (divStepSize==3 && whichStep>=29 && speedUp==1){divStepSize = 4;	whichStep = 0;		}
		if (divStepSize==4 && whichStep>=58 && speedUp==1){divStepSize = 3;	whichStep = 0; speedUp=0;		}
		if (divStepSize==3 && whichStep>=29 && speedUp==0){divStepSize = 2;	whichStep = 0;		}
		if (divStepSize==2 && whichStep>=19 && speedUp==0){divStepSize = 1;	whichStep = 0;		}
		if (divStepSize==1 && whichStep>=9 && speedUp==0){ divStepSize = 1;	whichStep = 0;speedUp = 1;		
			if(direction==0)
			{
			  direction=1;
				loadTopSection();
		  }
			else
			{
			  direction=0;
		    loadBottomSection();
			}
			return; // we're done
		}
		
		if (direction==0)
		{
		  divPosition -= divStepSize;
		}
		else
		{
		  divPosition += divStepSize;
		}
		whichStep++;
		document.getElementById("innerSlideDiv").style.top = divPosition;
		
		setTimeout("moveSlideDiv()",5);
	}
	
	function startSlideMover()
	{
	  if (skipFirstTime == 0)
		{
		  skipFirstTime = 1;
		}
		else
		{
	    moveSlideDiv();
		}
		setTimeout("startSlideMover()",5000); // every 5 secs
	}
	
	// this function is for the ShowImages page. Is called when user clicks arrow
	var isSliding     = 0;
	var slidePosition = 160;
	var whichHorizontalStep = 1;
	var moveLeft      = 0;
	var viewingNbr    = 1;
	var imagePosition = 0;
	var fadeStep      = 18;
	
	function moveSlide(leftOrRight)
  { 
		if((viewingNbr==1)&&(leftOrRight=="L")) return;  //ignore
		if((viewingNbr==(nbrOfPortraits+nbrOfLandscapes-1))&&(leftOrRight=="R")) return; //ignore
		if(isSliding==1) return;
		isSliding = 1;
		(leftOrRight=="L") ? moveLeft = 1 :	moveLeft = 0;
		(leftOrRight=="L") ? viewingNbr-- : viewingNbr++;
		if(leftOrRight=="R") preLoadNextImage();
		startHorizontalSlideMove();
  }	
	
	function preLoadNextImage()
	{
		// we're preloading two images ahead for smooth transitions
		// first checking if the image exists
		if((viewingNbr+2)<=(nbrOfPortraits+nbrOfLandscapes-1))
		document.getElementById("hidden1").src = 'images/slides/Full/' + (viewingNbr+2) + '.jpg';
	}
	
	function startHorizontalSlideMove()
	{
	  // this function is called 18 times
		// in 50 calls we need to move the div 79 pix (75 for image and 4 margin), 
		// we want to start slow and end slow: 2x1, 4x5 ,6x6, 4x5, 2x2
		
		fadeImageOut();
		
	  if(whichHorizontalStep==1)															step=2;
		if(whichHorizontalStep==2)															step=3;
		if((whichHorizontalStep>=3) &&(whichHorizontalStep<6))	step=4;
		if((whichHorizontalStep>=7) &&(whichHorizontalStep<12))	step=6;
		if((whichHorizontalStep>=13)&&(whichHorizontalStep<16))	step=5;
		if(whichHorizontalStep>=17) 														step=2;
		
		(moveLeft) ? (slidePosition+=step) : (slidePosition-=step);
		document.getElementById("slidePane").style.left = slidePosition;
		whichHorizontalStep++;
		
		if(whichHorizontalStep<18)
			setTimeout("startHorizontalSlideMove()",30);
		else
		{
			isSliding = 0;
			whichHorizontalStep = 1;
			if(imagePosition==45)
				imagePosition = 160;
			else
			  imagePosition = 45;
			document.getElementById("fullImage").src = 'images/slides/Full/' + viewingNbr + '.jpg';
			document.getElementById("enlargedImage").style.left = imagePosition;
			setTimeout("fadeImageIn()",200);
		}
	}
	
	function fadeImageOut()
	{
		var imageObject = document.getElementById("fullImage").style;
    currentOpacity = whichHorizontalStep * (5.88);
    imageObject.opacity      = (100-currentOpacity)/100;
	  imageObject.MozOpacity   = (100-currentOpacity)/100;
	  imageObject.KhtmlOpacity = (100-currentOpacity)/100;
    imageObject.filter       = "alpha(opacity=" + (100-currentOpacity) + ")";
	}
	
	function fadeImageIn()
	{
		fadeStep--;
		var imageObject = document.getElementById("fullImage").style;
    currentOpacity = fadeStep * (5.88);
    imageObject.opacity      = (100-currentOpacity)/100;
	  imageObject.MozOpacity   = (100-currentOpacity)/100;
	  imageObject.KhtmlOpacity = (100-currentOpacity)/100;
    imageObject.filter       = "alpha(opacity=" + (100-currentOpacity) + ")";
		if(fadeStep>1)
		  setTimeout("fadeImageIn()",30);
		else
		{ 
			fadeStep = 18;
			return;
		}
	}
	
	function changeSlide(imageId,inOrOut,orientation)
	{
	  // finding out what image has been loaded
	  var whichImageFull = document.getElementById(imageId).src;
		// the first part of the image source is http://www.the-curb.com/images/slides/portrait/ or
		// http://www.the-curb.com/images/slides/landscape/ depending on the orientation
		if(orientation=='P')
		{
		  var whichImage = whichImageFull.substring(47,whichImageFull.length);
		}
		else
		{
		  var whichImage = whichImageFull.substring(48,whichImageFull.length);
		}
		// now changinf from BW -> color or C -> BW
		if(inOrOut==1) // to color
		{
		  var imageNameDisect = whichImage.split('.');
		  var newSource = imageNameDisect[0] + 'C.' + imageNameDisect[1];
		}
	  else // to BW
		{
		  var imageNameDisect = whichImage.split('C');
		  var newSource = imageNameDisect[0] + imageNameDisect[1];
		}
		if(orientation=='P')
		{                                         
  		document.getElementById(imageId).src = 'images/slides/portrait/' + newSource;
		}
		else
		{
		  document.getElementById(imageId).src = 'images/slides/landscape/' + newSource;
		}
		return;
	}
	
	function launchImager()
	{
		window.open('showImages.php','imager','width=540,height=580');
		return;
		
	}
