// JavaScript Document:

function playSound(id,duration)
{
if(g_isIE8)
	{
	document.all.iesound.src="sound/"+id;
	return;
	}
	
var obj=document.getElementById(id);
if(obj==null)
	return;

try
{
obj.play();
}
catch(e)
	{
	alert("error Loading sound :"+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(id,framewidth,frameheight,framecount,duration,iname) 
{
/*
Purpose: Class to play animation sequences
Programmer: Reginald J. Armond
Date: September 23, 2011
Rev: Oct 3, 2011
*/
this.instance_id=id;
this.instance_name=iname; // in IE 7,8 global variable iteration is not possible

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 div_id=id;
var div_id_image=id+"_img";
var parent_div_id="background";
var cb_EOS=null;
var scale=1.0;
var obj_div=null;
var obj_image=null;
// private
function createAnimationContainer(sParentDiv,sAnimDiv)
	{
	alert("trying to create: "+sAnimDiv);
	var anim_div=document.createElement("div");
	anim_div.setAttribute("style","position:absolute; top:0px; left:0px; top:0px; left:0px; width:0px; height:0px; opacity: 1.0; filter: alpha(opacity=100);");
	anim_div.setAttribute("id",sAnimDiv);
				
	var obj=document.getElementById(sParentDiv);
	if(obj==null)
		return false;	
	obj.appendChild(anim_div);
	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};
	}
	
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)
	{
	var obj=createNewFrame();
	obj.img=new Image();
	obj.img.src=image;
	A[A.length]=obj;
	if(A.length>frame_count)
		frame_count=A.length;
	}
	
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(){
	var obj=$('#'+div_id);
	if(cur_frame==null || timer_id==null){
	//if(obj.css('display') == 'none'){
		return false;
	}
	if(!cur_frame){
		cur_frame = framecount;	
	}
	this.showFrame(cur_frame);
	return true;
}
	
this.showFrame=function(frame){
	if(obj_div==null)
		obj_div=$('#'+div_id);
		
	if(obj_div==null){
		createAnimationContainer(parent_div_id,div_id);
		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;
	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);
	
	// set opacity
	if(obj_div.opacity!=null){
		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;
		
	if(cur_frame==0)
		{
		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);
		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
		{
		if(cb_EOS)
			cb_EOS(cur_frame);
		timer_id=null;
		}	
	
	}
	
this.stop=function(){
	if(timer_id!=null){
		clearTimeout(timer_id);
		timer_id=null;
	}
}
	
this.hide=function(){
	var obj=$('#'+div_id);	
	obj.hide();
}

this.show=function(){
	var obj=$('#'+div_id);
	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.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;
	}
	

}


// my doModal

var g_currentModalInfo={source:null,value:null};

function doModal(source_div,cb_clickout)
	{
	var obj=$('#modal_container');
	
	if(source_div!=null)
		{
	
		var S=$("#"+source_div).html();
		$("#"+source_div).html('');				
		
		g_currentModalInfo.source=source_div;
		g_currentModalInfo.value=S;		
	
		obj.show();
		
		obj=$('#modal_over');
		obj.html(S);
		
		obj=$('#modal_under');
		obj.click(cb_clickout);
		
		}
	else
		{
		$("#"+g_currentModalInfo.source).html(g_currentModalInfo.value);
		g_currentModalInfo.source=null;		
		obj.hide();
		obj=$('#modal_over');
		obj.html('');
		}
	
	}
	
/*function CookieJar(sName)
	{
	this.name_cookiejar=sName;
	this.separator_item=";";
	this.separator_keyvalue="=";
	this.cookie_values=null;
	
	// interface
	this.Load=loadvalues; // load values from name_cookiejar
	this.Save=savevalues; // save values from name_cookiejar
	this.Value=getkeyvalue; // sKey
	this.Set=setkeyvalue;   // sKey, sValue	
	
	// Basic functions
	
	this.SetCookie=basicsetcookie; // sName,sValue
	this.GetCookie=basicgetcookie; // sName
	this.getCookieVal=basicgetcookieval; // offset
	
	
function basicgetcookieval (offset) 
   {
   var endstr = document.cookie.indexOf (";", offset);

   if (endstr == -1)      endstr = document.cookie.length;

   return unescape(document.cookie.substring(offset, endstr));
   }

function basicgetcookie (name) 
   {
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;

   var i = 0;

   while (i < clen) 
      {
      var j = i + alen;
      if (document.cookie.substring(i, j) == arg)      return this.getCookieVal (j);

      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0)       break; 
      }

   return "";
   }

function basicsetcookie (name, value) 

   {
   t=new Date();   
   t.setTime(t.getTime() +  (24 * 60 * 60 * 1000 * 365)); 	

   var argv = basicsetcookie.arguments;
   var argc = basicsetcookie.arguments.length;
   var expires =  t; // (2 < argc) ? argv[2] : null;
   var path = (3 < argc) ? argv[3] : null;
   var domain = (4 < argc) ? argv[4] : null;
   var secure = (5 < argc) ? argv[5] : false;

	path="/";

   document.cookie = name + "=" + escape (value) +
   	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
     	((path == null) ? "" : ("; path=" + path)) +
     	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");

   }

	
	
	function loadvalues()
		{
		var sCookie=this.GetCookie(this.name_cookiejar);
		if(sCookie=="")
			return;
		if(this.cookie_values==null)
			this.cookie_values=new Array();
			
		var aKV=sCookie.split(this.separator_item);
		var i;
		var kv;
		for(i=0;i<aKV.length;i++)
			{
			kv=aKV[i].split(this.separator_keyvalue);
			this.cookie_values[kv[0]]=kv[1];
			}		
		}
		
	function savevalues()
		{
		if(this.cookie_values==null)
		 	return;
		var i;
		var sCookie="";
		for(i in this.cookie_values)
			{
			if(sCookie!="")
				sCookie+=this.separator_item;
			sCookie+=i;
			sCookie+=this.separator_keyvalue;
			sCookie+=this.cookie_values[i];
			}
		this.SetCookie(this.name_cookiejar,sCookie);
		}
		
	function getkeyvalue(sKey)
		{
		var sValue;
		if(this.cookie_values==null)
			return "";
		return (this.cookie_values[sKey]!=null) ? this.cookie_values[sKey]:"";
		}
		
	function setkeyvalue(sKey, sValue)
		{
		if(this.cookie_values==null)
			this.cookie_values=new Array();
		
		this.cookie_values[sKey]=sValue;
		return true;
		}
	}*/

