// JavaScript Document:

function playSound(id,duration)
{
if(g_SoundEnabled==false)
	return;
if(g_isIE8)
	{
	document.all.iesound.src="sound/"+id;
	return;
	}
	
var obj=document.getElementById(id);
if(obj==null)
	return;

try
{
obj.play();
}
catch(e)
	{
	return;
	}

if(duration!=null)
	setTimeout("stopSound(\""+id+"\")",duration);
}

function stopSound(id)
{
if(g_isIE8)
	{
	document.all.iesound.src='';
	return;
	}


var obj=document.getElementById(id);
if(obj==null)
	return;
try
	{
	obj.pause();
	}
catch(e)
	{
	}
}

function ra_Animation(parent_div_id,id,framewidth,frameheight,framecount,duration) 
{
/*
Purpose: Class to play animation sequences
Programmer: Reginald J. Armond
Date: September 23, 2011
Rev: Oct 5, 2011
*/
var parent_id=parent_div_id;
this.instance_id=id;
this.instance_name=id; // in IE 7,8 global variable iteration is not possible
var isIE8   = (jQuery.browser.version<=8);
var A=[];
var anim_duration=duration;
var frame_width=framewidth;
var frame_height=frameheight;
var frame_count=framecount;
var cur_frame=0;
var timer_id=null;
var playing=false;
var div_id=id;
var div_id_image=id+"_img";
var cb_EOS=null;
var scale=1.0;
var obj_div=null;
var obj_image=null;
// private

function createAnimationContainer()
	{
	var obj=$("#"+parent_id);
	if(obj==null)
		{		
		return false;
		}
		
	var S=obj.html();
	
	if(S.indexOf("\""+div_id+"\"")>0)	
		return true;
		
	S="<div id=\""+div_id+"\" style=\"position:absolute; top:0px; left:0px; top:0px; left:0px; width:0px; height:0px; z-index:0;\"><img id=\""+div_id_image+"\" src=\"\" style=\"width:1px; height:1px\" /></div>";
	
	// alert("adding "+div_id+" to parent container "+S);
	
	obj.append(S);
	
	obj_div=$('#'+div_id);
	obj_image=$('#'+div_id_image);
	
	return true;
	}

function createNewFrame()
	{
	return {x:0,y:0,duration:Math.round(anim_duration/frame_count),opacity:1.0,callback:null,layer:null,loop_frame:null,loop_count:0,loop_count_cur:0,bSelected:false,img:null,pfn:null,url:null};
	}
	
function selectFrames(selector,b)
	{
	
	if(A.length<1)
		{
		var i;
		for(i=0;i<frame_count;i++)			
			A[i]=createNewFrame();			
		}
	
		
	if(typeof(selector)=="number")
		selector=selector.toString();
		
	if(selector=="*")		
		selector="0-"+(A.length).toString();
		
	var a_sel=selector.split(",");
	var i,j,a,ipos=0,obj;
	if(b==null)
		b=true;
	for(i=0;i<a_sel.length;i++)
		{
		a=a_sel[i];
		ipos=a.indexOf("-");
		if(ipos>0)
			{
			var iStart,iEnd,ii;
			iStart=parseInt(a.substring(0,ipos));
			iEnd=parseInt(a.substring(ipos+1));
			for(ii=iStart;ii<iEnd;ii++)				
				A[ii].bSelected=b;				
			}
		else
			A[parseInt(a)].bSelected=b;
		}
	}

function resetLoopCounters()
	{
	selectFrames("*",true);
	var ii,fi;
	for(ii=0;ii<A.length;ii++)
		{
		fi=A[ii];
		if(fi!=null)
			{
			if(fi.loop_frame!=null)				
				fi.loop_count_cur=fi.loop_count;				
			}
		}
	}

function processObjects(selector,oset)
	{
	var i,obj,ii;
	selectFrames("*",false);
	selectFrames(selector,true);
	for(i=0;i<A.length;i++)
		{
		obj=A[i];
		if(obj.bSelected)
			{			
			for(ii in oset)				
				obj[ii]=oset[ii];				
			}
		}
	}
// public

this.setScale=function(scale_factor)
	{
	scale=scale_factor;
	}

this.setDuration=function(selector,duration)
	{
	processObjects(selector,{duration:duration});
	}
	
this.setOpacity=function(selector,opacity)
	{
	processObjects(selector,{opacity:opacity});
	}
	
this.setCallback=function(selector,cb_fn)
	{
	processObjects(selector,{callback:cb_fn});
	}
	
this.setCallback_EndOfSequence=function(cb_fn)
	{
	cb_EOS=cb_fn;
	}
	
this.setPosition=function(selector,x,y)
	{
	processObjects(selector,{x:x,y:y});
	}
	
this.setLayer=function(selector,layer)
	{
	processObjects(selector,{layer:layer});
	}
	
this.addFrame=function(image)
	{
	if(image==null)
		return false;
	var obj=createNewFrame();
	var pt=typeof(image);
	var img_type=".gif!.jpg!.png!";
	var url_type=".htm!.html!.php!.asp!";
	var ipos=-1;
	if(pt=="string")
		{
		var ext=image.toLowerCase()+"!";
		ipos=ext.lastIndexOf(".");
		if(ipos>-1)
			{			
			ext=ext.substring(ipos);			
			ipos=img_type.indexOf(ext);
			if(ipos>=0)
				pt="image";
			else
				{
				ipos=url_type.indexOf(ext);
				if(ipos>=0)
					pt="url";
				else
					pt="url";
				}
			}
		}
	

	if(pt=="function")
			{
			obj.pfn=image;
			}
	else
	if(pt=="image")
			{
			obj.img=new Image();
			obj.img.src=image;
			}
	else
	if(pt=="url")
		{
		obj.url=image;		
		}

	A[A.length]=obj;
	if(A.length>frame_count)
		frame_count=A.length;
	return true;
	}
	
this.setLoop=function(startframe,endframe,count)
	{
	processObjects(endframe.toString(),{loop_frame:startframe,loop_count:count});
	}

this.gotoFrame=function(n)
	{
	cur_frame=n;
	resetLoopCounters();
	}	
	
this.refresh=function()
	{
	if(cur_frame==null || timer_id==null)
		return false;
	this.showFrame(cur_frame);
	return true;
	}
	
this.showFrame=function(frame)
	{
	if(obj_div==null)
		{		
		createAnimationContainer();
		if(obj_div==null)
			obj_div=$('#'+div_id);		
		}
		
	if(obj_image==null)
		{		
		var S="<img id=\""+div_id_image+"\" src=\"\" style=\"width:1px; height:1px\" />";
		obj_div.html(S);
		obj_image=$("#"+div_id_image);
		}	
		
	if(A.length<1)		
		return false;	
		
	var obj=A[frame];
	
	if(obj==null)
		return false;	
	
		
	var X=Math.round(obj.x*scale);
	var Y=Math.round(obj.y*scale);
	var W=Math.round(frame_width*scale);
	var H=Math.round(frame_height*scale);
	
	// set displayed frame	
	obj_div.css('left',X.toString()+"px");
	obj_div.css('top',Y.toString()+"px");
	obj_div.css("width",W.toString()+"px");
	obj_div.css("height",H.toString()+"px");
		
	obj_image.css("width",W.toString()+"px");
	obj_image.css("height",H.toString()+"px");
	if(obj.img!=null)
		{
		obj_image.attr("src",obj.img.src);
		obj_div.css('display','block');
		}
	else
		{
		if(obj.pfn!=null)
			{
			obj_div.html(obj.pfn());
			obj_div.css('display','block');
			}
		else
		if(obj.url!=null)
			{			
			obj_div.load(obj.url);
			obj_div.css('display','block');
			}
		else
			obj_div.css('display','none');
		}
	
	// set opacity
	if(obj_div.opacity!=null && isIE8==false)
		{		
		obj_div.css('opacity',obj.opacity);
		obj_div.css('filter',"alpha(opacity="+Math.round(100.0*obj.opacity)+")");
		}
			
	if(obj.layer!=null)		
		obj_div.css('z-index',obj.layer);
	
	return true;
	}
	
this.isPlaying=function()
	{
	return (timer_id!=null);
	}
	
this.play=function(start)
	{
	if(start!=null)
		{
		cur_frame=start;
		playing=true;
		}
	else
		{
		if(playing==false)
			return;
		}
		
	if(cur_frame==0)
		{
		// real frame count
		frame_count=A.length;
		if(timer_id!=null)
			{
			clearTimeout(timer_id)	
			timer_id=null;
			}
		this.show();
		resetLoopCounters();
		}
		
	this.showFrame(cur_frame);
	
	var last_frame=cur_frame;
		
	if(cur_frame<frame_count)
		{
		var fi=A[cur_frame];
		if(this.instance_name==null)
			this.getInstanceName(this.instance_id);
		if(fi.callback)
			fi.callback(cur_frame);
		playing=true;
		timer_id=setTimeout(this.instance_name+".play()",fi.duration);		
		if(fi.loop_frame)
			{
			if(fi.loop_count_cur>0)
				{
				cur_frame=fi.loop_frame;
				// alert("fi.loop_count_cur: "+fi.loop_count_cur);
				fi.loop_count_cur--;				
				}
			else
				cur_frame++;
			}
		else
		 	cur_frame++;
		}
	else
		{
		timer_id=null;
		playing=false;
		if(cb_EOS)
			cb_EOS(cur_frame);		
		}	
	
	}
	
this.stop=function()
	{
	playing=false;
	if(timer_id!=null)
		{
		clearTimeout(timer_id);		
		timer_id=null;
		}
	}
	
this.hide=function()
	{
	var obj=$('#'+div_id);
	if(obj==null)
		{
		createAnimationContainer();
		obj=$('#'+div_id);
		if(obj==null)
			return;
		}
		
	obj.hide();
	}

this.show=function()
	{
	var obj=$('#'+div_id);
	if(obj==null)
		{
		createAnimationContainer();
		obj=$('#'+div_id);
		if(obj==null)
			return;
		}
	
	obj.show();
	}

this.playSound=function(soundFile,duration)
	{
	var div_name="div_sound_";
	var i,c,obj=null;
	for(i=0;i<soundFile.length;i++)
		{
		c=soundFile.charAt(i).toLowerCase();
		div_name+=((c>="a" && c<="z") || (c>="0" && c<="9")) ? c:"_"; 
		}
		
	if(this.instance_name==null)
			this.getInstanceName(this.instance_id);
			
				
	if(duration>0)
		{
		var divobj=document.createElement("div");
		var S="<audio preload=\"auto\" autoplay=\"autoplay\">";
		divobj.setAttribute("id",div_name);
		divobj.setAttribute("style","display:none");
		S+="<source src=\""+soundFile+".mp3\" type=\"audio/mpeg\" />";
		S+="<source src=\""+soundFile+".wav\" type=\"audio/wav\" />";
		S+="<source src=\""+soundFile+".ogg\" type=\"audio/ogg\" />";
		S+="</audio>";
		divobj.innerHTML=S;
		document.body.addChild(divobj);
		setTimeout(this.instance_name+".playSound(\""+soundFile+"\",0)",duration);		
		}
	else
		{
		var remove_div=document.getElementById(div_name);
		
		obj=document.body;
		obj.removeChild(remove_div);		
		}
	
	return true;
	}
	
this.getFrameHeight=function()
	{
	return frame_height;
	}

this.getInstanceName=function(id)
	{
	for(var i in window)
	{
	try
	{	
	if(window[i].instance_id==id)
		{
		// alert("found instance name: "+i);
		this.instance_name=i;
		return i;		
		}
	}
	catch(e)
		{
		}
	}
	return null;
	}


this.doInit=function()
	{
	var obj=$('#'+this.instance_id);
	if(obj==null)
		return false;
	if(this.instance_name==null)
		this.getInstanceName(this.instance_id);
	frame_width=parseInt(obj.css('width'));
	frame_height=parseInt(obj.css('height'));
	return true;
	}
	

}

