var imageloader;
var imageLoaderActive=0;
var autoPlay=1;
var galleryIndex=-1;
var galleryTimer=0;
var opacityTimer=0;
var galleryOldIndex=-1;
var galleryDelay=4000;
var galleryFadeSpeed=11;
var opacity=100;
var galleryPause=0;
function galleryDeselect(){
if(galleryOldIndex>=0)document.getElementById("galleryMenuItem"+galleryOldIndex).className="";
}
function openImage(i,objId){

autoPlay=0;
galleryPausePic();
window.clearTimeout(galleryTimer);	
//galleryIndex=i;
loadImage(objId,i);
}
function loadImage(objId,i,scroll){
if(imageLoaderActive==1){
	window.clearTimeout(opacityTimer);	
}

	galleryOldIndex=galleryIndex;
	galleryIndex=i;
	imageLoaderActive=1;
	imageloader = new Image();
	imageloader.onload = function() {
	galleryDeselect();
	document.getElementById("clock").style.visibility ="hidden";
	document.getElementById("galleryMenuItem"+i).className="select";
		if(scroll){					scrollGalleryMenu(-13-document.getElementById("galleryMenuItem"+galleryIndex).offsetLeft+parseInt(document.getElementById("galleryMenuFrame").offsetWidth)/2);
		}
		fadeOutIn(objId);
		
		};
		document.getElementById("clock").style.visibility ="visible";
	imageloader.src = galleryImages[i];	
	

}

function fadeIn(objId) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += galleryFadeSpeed;
     opacityTimer=window.setTimeout("fadeIn('"+objId+"')", 1000/24);
	  
    }
	else {
	imageLoaderActive=0;
	document.getElementById("galleryText").innerHTML=document.getElementById("galtext"+galleryIndex).innerHTML;
	if(autoPlay==1&&galleryImages.length>1&&!galleryPause){
		galleryTimer=window.setTimeout("playNext('"+objId+"')",  galleryDelay);
		}
	}
  }
}

function stopPlay(objId){
autoPlay=1-autoPlay;
galleryPausePic();
if(autoPlay==0){
		window.clearTimeout(galleryTimer);	
	}
else playNext(objId);
}
function playNext(objId){
	if(galleryPause==1)return;
	i=(galleryIndex+1)%galleryImages.length;	
	//scrollGalleryMenu(-10-document.getElementById("galleryMenuItem"+galleryIndex).offsetLeft+parseInt(document.getElementById("galleryMenuFrame").offsetWidth)/2);
	loadImage(objId,i,1);	
}
function fadeOutIn(objId) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity >= 0) {
      setOpacity(obj, opacity);
      if(galleryPause==0)opacity -= galleryFadeSpeed;
      opacityTimer=window.setTimeout("fadeOutIn('"+objId+"')",  1000/24);
    }
	else{	
	obj.src=imageloader.src;
	opacity=50;
	fadeIn(objId);
	}
  }
}
function setOpacity(obj, opacity1) {
  opacity1 = (opacity1 == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity1+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity1/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity1/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity1/100;
}
var galleryTimer;
var galleryStart,galleryDistination,galleryEndTime,galleryStartTime,gallerySpeed=100/1000;
function scrollGalleryMenuPage(d){
var curX=parseInt(document.getElementById("galleryMenuContent").style.left);
var frame=parseInt(document.getElementById("galleryMenuFrame").offsetWidth);

var newX=curX-frame*d;


//alert(content);
if(autoPlay==1)stopPlay("im");
scrollGalleryMenu(newX);
}
var tmp=new Image();
tmp.src="images/scrollplay.gif";
function galleryPausePic(){
if(autoPlay)document.getElementById("gallerypause").src="images/scrollpause.gif";
else document.getElementById("gallerypause").src="images/scrollplay.gif";
}
function scrollGalleryMenu(x){
var cell=25;
var frame=parseInt(document.getElementById("galleryMenuFrame").offsetWidth);
var content=parseInt(document.getElementById("galleryMenuContent").offsetWidth);
//content=Math.round(content/cell)*cell;
x=Math.round(x/cell)*cell;

if(frame>content)x=0;
else{
if(x>0)x=0;
//alert(content);
if(x<-content+frame){
	x=-content+frame;
	}
}

galleryStartTime=new Date().getTime();
galleryStart=parseInt(document.getElementById("galleryMenuContent").style.left);
galleryDistination=x;
var dist=galleryDistination-galleryStart; 
var timeD=Math.abs(dist/gallerySpeed);

galleryEndTime=galleryStartTime+timeD;
scrollGalleryMenuStep();
}
function scrollGalleryMenuStep(){
var curTime=new Date().getTime();
if(curTime<galleryEndTime){

	document.getElementById("galleryMenuContent").style.left=galleryStart+(curTime-galleryStartTime)*(galleryDistination-galleryStart)/(galleryEndTime-galleryStartTime);
	var galleryTimer1= window.setTimeout("scrollGalleryMenuStep()",  1000/24);
	}
else document.getElementById("galleryMenuContent").style.left=galleryDistination;
}
function pausePlay(objId){
if(galleryImages.length<2)return;
if(autoPlay==1){
galleryPause=1;
galleryPausePic();
//window.clearTimeout(galleryTimer);
//fadeIn(objId);
	}
}
function continuePlay(objId){
if(galleryImages.length<2)return;
galleryPause=0;
//autoPlay=1;galleryPausePic();
//
if(autoPlay==1){
galleryPause=0;
galleryPausePic();
	window.clearTimeout(galleryTimer);
	//playNext(objId);	
	galleryTimer=window.setTimeout("playNext('"+objId+"')",  galleryDelay/3);
	}	

}
var sizeOfSplash=298;
var splashDist=0;
var splashStartTime,splashEndTime,splashOnScroll=0;
function splashScroll(d){
if(splashOnScroll==1)return;

var curX=parseInt(document.getElementById("splashProjectsContent").style.left);
var frame=parseInt(document.getElementById("splashProjectsFrame").offsetWidth);
var content=parseInt(document.getElementById("splashProjectsContent").offsetWidth);
var x=curX+sizeOfSplash*d;
/*if(x>0)x=0;
//alert(content);
if(x<-content+frame){
	x=-content+frame;
	}
*/
//alert(x+" vs "+(-(content-frame+sizeOfSplash-1)));
if(x<0&&x>-(content-frame+sizeOfSplash-1))
{splashOnScroll=1;
	splashDist=x;
	splashStart=curX;
	splashStartTime=new Date().getTime();
	splashEndTime=splashStartTime+700;
	splashStep();
	}
}
function splashStep(){

var curTime=new Date().getTime();
if(curTime<splashEndTime){
var t0,t1,x0,x1;
var mid=splashStartTime+650;

if(curTime<mid){
	t0=splashStartTime;
	t1=mid;
	x0=splashStart;
	x1=splashDist+(splashDist-splashStart)*0.03;
	}
else{
	t0=mid;
	t1=splashEndTime;
	x0=splashDist+(splashDist-splashStart)*0.03;
	x1=splashDist;
	
}
var t=t1-t0;
var b=(x1-x0)*3.0/2/t/t;
var a=-b/3/t;
var ct=curTime-t0;
var x=x0+1.0*a*ct*ct*ct+1.0*b*ct*ct;
//alert(splashDist+" vs "+(a*ct*ct*ct));
//alert(splashStart+1.0*a*(curTime-t0)^3/3+1.0*b*(curTime-t0)^2/2);
document.getElementById("splashProjectsContent").style.left=x;//splashStart+(curTime-splashStartTime)*(splashDist-splashStart)/(splashEndTime-splashStartTime);
	var galleryTimer1= window.setTimeout("splashStep()",  1000/24);
	}
else {
	document.getElementById("splashProjectsContent").style.left=splashDist;
	splashOnScroll=0;
	}
}


