// Copyright 2010 TS Solutions Pty Ltd
function wwFly(objid, final_x, final_y, time_left)
{
// calc how many steps, and time of step in ms
   var x_step=0, y_step=0, no_steps=0, timeleft=0;
	 var step_time=wwControl.quantum;
	 var x,y, obj, step_time=0;
	 obj=document.getElementById(objid);
	 if(!obj)
	 {
//	   alert("No object for id='"+objid+"'");
		 return;
	 }
	 var d0=GetTopLeft(obj);
	 x=d0.left;
	 y=d0.top;
	 final_x=parseInt(final_x);
	 final_y=parseInt(final_y);
	 time_left=parseInt(time_left);  // in ms
	 no_steps=Math.max((final_x-x),(final_y-y));
	 step_time=parseInt(time_left/no_steps);
	 if(step_time<wwControl.quantum)
	 {
		 step_time=wwControl.quantum;;
		 no_steps=time_left/wwControl.quantum;
	 }
	 if(no_steps>0)
	 {
			x_step=Math.round((final_x-x)/no_steps);
			y_step=Math.round((final_y-y)/no_steps);
			// 
			if(x_step!=0)
			{
				x=parseInt(x+x_step);
				obj.style.left=x+"px";
			}
			if(y_step!=0)
			{
				y=parseInt(y+y_step);
				obj.style.top=y+"px";
			}
			// calc new x and y, and time_step
			if((x_step!=0 || y_step!=0) && time_left>0) 
			{
				window.setTimeout("wwFly('"+objid+"',"+final_x+","+final_y+","+(time_left-step_time)+")",step_time);
			}
	 }
	 else
	 {
			obj.style.left=final_x+"px";
			obj.style.top=final_y+"px";
	 }
}
//
function wwFly_tick(obj, final_x, final_y, time_left)
{
   var no_steps=0, timeleft=0;
	 var step_time=wwControl.quantum;
	 var d0=GetTopLeft(obj);
	 no_steps=parseInt(time_left/step_time);
	 if(no_steps>0)
	 {
			obj.style.left=parseInt(d0.left+Math.round((final_x-d0.left)/no_steps))+"px";
			obj.style.top=parseInt(d0.top+Math.round((final_y-d0.top)/no_steps))+"px";
	 }
	 else
	 {
			obj.style.left=final_x+"px";
			obj.style.top=final_y+"px";
	 }
}
//
function wwEffectClass(p0_objid,p_array, time_left)
{
	this.objid=p0_objid;
	this.tick=function() {};
	this.timeleft=parseInt(time_left);
	this.parameters=p_array;
	this.effectRoutine=function () {};
	this.onComplete=function () {};
	this.effectName="";
}
function wwFlyClass(obj_id, x, y, time_left)
{
	var self=this;
	this.effectName="wwFly";
	this.timeleft=time_left;
	this.objid=obj_id;
	var obj=document.getElementById(obj_id);
	var d0=GetTopLeft(obj);
	if(x=="" || x==NaN) x=d0.left;
	if(y=="" || y==NaN) y=d0.top;
  var current_x=d0.left;
	var current_y=d0.top;
	var no_steps=parseInt(self.timeleft/wwControl.quantum);
	var final_x=x;
	var final_y=y;
	var step_size_x=(final_x-current_x)/no_steps;
	var step_size_y=(final_y-current_y)/no_steps;
	var step_time=wwControl.quantum;
	this.start=function ()
	{
		  try
			{
				wwControl.dbgwrite(self.effectName+".start timeleft="+self.timeleft+" x="+final_x+" y="+final_y);
		  	if(self.timeleft>0) wwControl.addEffect(self);
			}
			catch (err)
			{
				alert(err.description);
			}
	}
	this.tick=function()
	{
	 if(self.timeleft>0)
	 {
			if(step_size_x!=0)
			{
				current_x+= step_size_x;
			  obj.style.left=parseInt(current_x)+"px";
			}
			if(step_size_y!=0)
			{
				current_y+=step_size_y;
				obj.style.top=parseInt(current_y)+"px";
			}
	 }
	 else
	 {
			obj.style.left=parseInt(final_x)+"px";
			obj.style.top=parseInt(final_y)+"px";
	 }
	 this.timeleft-=wwControl.quantum;
	 if(this.timeleft<=0) this.onComplete();
	}
	this.onComplete=function () {};
}
function wwFadeClass(obj_id, start_opacity, final_opacity, time_left)
{
	this.effectName="wwFade";
	this.obj=document.getElementById(obj_id);
	if(this.obj==null) alert("Unable to find element id='"+obj_id+"'");
	this.level=parseInt(start_opacity);
	this.finalopacity=parseInt(final_opacity);
	this.timeleft=parseInt(time_left);
	this.onComplete=function () {};
	var opacitystep=(final_opacity-start_opacity)/(time_left/wwControl.quantum);
//	wwControl.dbgwrite("wwFadeClass new opacitystep="+opacitystep);
	this.tick=function ()
	{
	  //wwControl.dbgwrite("wwFadeClass tick opacitystep="+opacitystep);
		this.level=this.level+opacitystep; //Math.round((this.finalopacity-this.level)/(this.timeleft/wwControl.quantum));
//	  wwControl.dbgwrite("wwFadeClass new level="+this.level);
		if(this.level<0) this.level=0;
		if(this.level>100) this.level=100;
		this.obj.style.filter="alpha(opacity="+this.level+")";
			// set the opacity for safari prior to 1.2
		this.obj.style.KHTMLOpacity = this.level/100;
		// set the opacity for safari 1.2 and higher and newer firefox
		this.obj.style.opacity = this.level/100;
		// set the opacity for firefox and netscape
		if (this.level == 100) this.level = 99.999;
		this.obj.style.MozOpacity = this.level/100;
		this.timeleft-=wwControl.quantum;
	  if(this.timeleft<=0) this.onComplete();
//		  wwControl.dbgwrite("wwFadeClass.tick timeleft="+this.timeleft);
	}
	this.start=function ()
	{
//		  wwControl.dbgwrite("wwFadeClass.start timeleft="+this.timeleft);
		  if(this.timeleft>0) wwControl.addEffect(this);
	}
}

//
function GetTopLeft(elm)
{
	var x, y = 0;
//set x to elm’s offsetLeft
	x = parseInt(elm.offsetLeft);
//set y to elm’s offsetTop
	y = parseInt(elm.offsetTop);
//set elm to its offsetParent
	elm = elm.offsetParent;
//use while loop to check if elm is null
// if not then add current elm’s offsetLeft to x
//offsetTop to y and set elm to its offsetParent
	while(elm != null && elm !=undefined)
	{
		x = x + parseInt(elm.offsetLeft);
		y = y + parseInt(elm.offsetTop);
		elm = elm.offsetParent;
	}
	return {top:y, left: x};
}
//

function wwSetOpacity(objid, level)
{
	level=parseInt(level);
	if(level<0) level=0;
	if(level>100) level=100;
	obj=document.getElementById(objid);
	obj.style.filter="alpha(opacity="+level+")";
	if (level == 100) level = 99.999;
	// set the opacity for firefox and netscape
	obj.style.MozOpacity = level/100;
		// set the opacity for safari prior to 1.2
	obj.style.KHTMLOpacity = level/100;
	// set the opacity for safari 1.2 and higher and newer firefox
	obj.style.opacity = level/100;
}

//
// fade from obj0 to obj1 in time x ms
// first set obj2 behind obj1, ie ensure obj1.z > obj2.z

function crossfade(opacity, objid0, objid1) 
{
	opacity=parseInt(opacity);
	if (opacity <= 100) 
	{
      wwSetOpacity(objid0, opacity);
      wwSetOpacity(objid1, 100 - opacity);
      opacity -= 1;
      window.setTimeout("crossfade("+opacity+",'"+objid0+"', '"+objid1+"')", 30);
	}
}

function wipe(amount, objid0, objid1)
{
	obj0=document.getElementById(objid0);
	obj1=document.getElementById(objid1);
	pwidth = obj0.width;
	pheight = obj1.height;
	xstep = pwidth / trans_amount * amount;
	ystep = pheight / trans_amount * amount;
	
	if (from == "left")
	{
		// clip by rect(top, right, bottom, left)
		obj1.style.clip = 'rect(' +	'0px, ' + Math.round(0+xstep) + 'px, ' + 	pheight + 'px, ' + 	'0px)';
	}
	else if (from == "right")
	{
		// clip by rect(top, right, bottom, left)
		obj1.style.clip = 'rect(' +	'0px, ' + pwidth + 'px, ' + pheight + 'px, ' + Math.round(pwidth-xstep) + 'px)';
	}
	else if (from == "top")
	{
		// clip by rect(top, right, bottom, left)
		obj1.style.clip = 'rect(' +	'0px, ' + pwidth + 'px, ' +	Math.round(ystep) + 'px, ' + '0px)';
	}
	else if (from == "bottom")
	{
		// clip by rect(top, right, bottom, left)
		obj1.style.clip = 'rect(' +	Math.round(pheight-ystep) + 'px, ' + 	pwidth + 'px, ' +	pheight + 'px, ' + '0px)';
	}
	else  // if center or none specified
	{
		xcenter = pwidth / 2; // so 640 / 2 = 320
		ycenter = pheight / 2; // so 480 / 2 = 240
		xstep = xcenter / trans_amount * amount;
		ystep = ycenter / trans_amount * amount;
		
		// clip by rect(top, right, bottom, left)
		obj1.style.clip = 'rect(' +
		Math.round(ycenter-ystep) + 'px, ' + 
		Math.round(xcenter+xstep) + 'px, ' +
		Math.round(ycenter+ystep) + 'px, ' + 
		Math.round(xcenter-xstep) + 'px)';
	}
	
	if (amount == 1) wwSetOpacity(objid1, 100);
	
	amount++;
	
	if (amount < trans_amount)
		window.setTimeout("wipe("+amount+", '"+from+"');", trans_time);
	else
    {
    	// reset clip
    	obj1.style.clip = 'rect(auto, auto, auto, auto)';
    	setTimeout('changepic();', trans_time);
    }

}

function zoomin(amount, to) // this function is a photo effect
{
	
	if (amount == 0)
	{
		zwidth = pictures[cur_pic].photo.width;
		zheight = pictures[cur_pic].photo.height;
		//testing.innerHTML = zwidth+", "+zheight;
		// Calculate slope.
		// Steps will be 1 each unless width is bigger than height
		// or height is bigger than width.  If that is the case
		// then the step for the bigger dimension will be the slope (ex: zwidth / zheight)
		// the 2 and the *2 is to make each step twice as big
		zxstep = 3;
		zystep = 3;
		if (zwidth > zheight)
			zxstep = (zwidth / zheight)*3;
		if (zheight > zwidth)
			zystep = (zheight / zwidth)*3;
	} 
	
	zwidth = zwidth + zxstep;
	zheight = zheight + zystep;
	
	//document.getElementById('testing').innerHTML = "xstep="+zxstep+", ystep="+zystep;
	
	document.getElementById('picture').width = zwidth;
	document.getElementById('picture').height = zheight;

  zzleft = parseFloat(document.getElementById('picture').style.left);
	zztop = parseFloat(document.getElementById('picture').style.top);
	
	//document.getElementById('testing').innerHTML = "left="+zleft+", top="+ztop;
	
	// Untouched is upperleft
	
	if (to == "upperright" || to == "right" || to == "lowerright")
		document.getElementById('picture').style.left = zzleft - zxstep + 'px';
	
	if (to == "top" || to == "bottom" || to == "center")
		document.getElementById('picture').style.left = zzleft - zxstep/2 + 'px';
	
	if (to == "lowerleft" || to == "bottom" || to == "lowerright")	
		document.getElementById('picture').style.top = zztop - zystep + 'px';
			
	if (to == "left" || to == "center" || to == "right")
		document.getElementById('picture').style.top = zztop - zystep/2 + 'px';
		
	amount++;

	if (amount <= effect_amount)
		ztimer = window.setTimeout("zoomin("+amount+", '"+to+"');", effect_time);
	else
	{
		// reset left and top of picture
			//document.getElementById('picture').style.left = 0;
			//document.getElementById('picture').style.top = 0;
	}
}

