var $targ=false;
var $tool=false;
var $db=false;
var $sys;
var $pop=false;
var $pap=false;
var $ctl=false;
var $itg=false;
var $itc=false;
var $pp=false;
var $nav=1;
var $pag='home';
var $app='i_1_0';
var $click_flag=false
var $color='whi';
var $dirty=false;
var $fetch=false;
var $q_e;
var $q_t=false;
var $q_n=0;

var $timer;
var $slide=-1;
var $slide_last=0;
var $fade_flag=false;
var $fade_targ=false;

var ie6 = (window.external &&	typeof window.XMLHttpRequest == "undefined");
var $computer=true;

function go_key(e,ix){
	var key = e.which || e.keyCode;
  if(key==13){
		switch(ix){
			case 'fli':
				superfli_find('pdg');
				break;
		}
	}
}

function initialise(){
	$computer=true;computer();
	//
}
function slide_control(targ,ix){
	if($slide==-1){
		$slide=ix;
		app_fade(targ,ix);
	}else{
		$slide=-1;
	}
}
function slide_show(){
	app_fade(false,$slide);
}
function play_pause(force){
	ptg=ge('play');
	if($slide==-1){
		$slide=-1;
		var ls=0;
		if(ptg.className=='pause'){
			ls=last_i();
			var trg=ge('i_1_'+ls);
		}
		ptg.className='play';
		$slide=ls;
		clearTimeout($timer);
		setTimeout('slide_show()',100);		
	}else{
		clearTimeout($timer);
		$slide=-1;
		ptg.className='pause';
	}
}

function last_i(){
	for(var i=0;i<$maxi+1;i++){
		targ=ge('i_1_'+i);
		//alert(targ.id + " " + targ.style.display);
		if(targ.style.display=='block'){
			break;
		}
	}
	return i;
}


function last_i(){
	for(var i=0;i<$maxi+1;i++){
		targ=ge('i_1_'+i);
		//alert(targ.id + " " + targ.style.display);
		if(targ.style.display=='block'){
			break;
		}
	}
	return i;
}

var $previ;
//NAVIGATION
function app_switch(targ,ix){
	var mx=$maxi; //last of set
	if(ix==mx){ix=0;}else{ix++;}
	$app='i_1_'+ix;
	targ.style.display='none';	
	targ=ge($app);
	targ.style.display='block';	
	app_impos(targ,ix);
}
function app_direct(ix){
	ge($app).style.display='none';
	var targ=ge('i_1_'+ix);
	targ.style.display='block';	
	$app=targ.id;
	app_impos(targ,ix);
}
function app_impos(targ,ix){
	var img=ge('img_'+ix);
	if($app!='i_1_0'){
		targ.style.paddingTop=(450-img.offsetHeight)/2 + 'px';
		targ.style.width=img.offsetWidth + 'px';
	}

}

function app_fade(targ,ix){
	//$fade_flag=true;
	//if(ie6){app_switch(targ,ix);return false;}
	if($slide==-1){return false;}
	if(!targ){targ=ge('i_1_'+ix);}
	var mx=$maxi; //last of set
	if(ix==mx){ix=0;}else{ix++;}
	$slide=ix;
	$app='i_1_'+ix;
	targ.style.opacity=1;
	targ.FadeState = 2;
	$fade_targ=targ;
	fade(targ.id,ix); //fade out
}
function app_show(targ,ix){
	if($slide==-1){return false;}
	targ.style.display='none';	
	targ=ge($app);
	targ.FadeState = -2;
	targ.style.opacity=0;
	targ.style.filter = 'alpha(opacity =0)';
	targ.style.display='block';	
	app_impos(targ,ix);
	$slide=ix;
	$fade_targ=targ;
	fade(targ.id,-1); //fade in	
	if($slide>-1){$timer=setTimeout('slide_show()',2500);}
	//if($slide>-1){setTimeout('app_fade(false,$slide)',2500);}
}


function fly_loc(loc){
	var x=[];
	x['cmri']='https://www.ocrr.ca/cmri/cmri.php?log=4917&vv_loc=dmo';
	x['cvis']='http://ocrr.ca/cvis';	
	x['pdg' ]='http://qstatistic.com/pdg';	
	x['req' ]='http://qstatistic.com/req';	
	x['mpr' ]='http://myparentrewards.com/rew/i.php';	
	x['rac' ]='http://qstatistic.com/rac';	
	x['foto']='http://vukfoto.com';	
	location.href=x[loc];
}
function nav_action(act,ix,sub){
	switch(act){
		case 'home':break;
		case 'contact':break;
	}
	ge('nav_'+$nav).className='nav';
	ge('nav_'+ix).className='nav_sel';
	ge('m_'+$nav).style.display='none';
	targ=ge('m_'+ix);
	targ.style.display='block';
	targ=ge('play');
	if(ix==1){
		targ.style.display='block';
		if($slide==-1){play_pause(false);}	
	}else{
		if($slide>-1){play_pause(false);}
		targ.style.display='none';
	}
	$nav=ix;
	$pag=act;
}

function computer(){
	var ci=9;var n=Math.floor(Math.random()*ci);
	while($q_n==n){n=Math.floor(Math.random()*ci);}
	if(!$q_t){$q_t=new Array();var r=ge('tbl_hal').rows;for(var i=0;i<ci;i++){$q_t[i]=r[0].cells[i];}}
	$q_t[n].className='tdH'+Math.floor(Math.random()*6);$q_n=n;
	if($computer){setTimeout('computer()',33);}
}

var TimeToFade = 466.0;
function fade(id,ix){
	var tgf=ge(id);
  if(tgf.FadeState == null){
    if(tgf.style.opacity == null || tgf.style.opacity == '' || tgf.style.opacity == '1'){
      tgf.FadeState = 2;
    }else{
      tgf.FadeState = -2;
    }
  }
  if(tgf.FadeState == 1 || tgf.FadeState == -1){
    tgf.FadeState = tgf.FadeState == 1 ? -1 : 1;
    tgf.FadeTimeLeft = TimeToFade - tgf.FadeTimeLeft;
  }else{
    tgf.FadeState = tgf.FadeState == 2 ? -1 : 1;
    tgf.FadeTimeLeft = TimeToFade;
    setTimeout("animate_fade(" + new Date().getTime() + ",'" + id + "'," + ix + ")", 33);
  }  
}
function animate_fade(lastTick, id, ix){  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
  var tgf = ge(id);
  if(tgf.FadeTimeLeft <= elapsedTicks){
    tgf.style.opacity = tgf.FadeState == 1 ? '1' : '0';
    tgf.style.filter = 'alpha(opacity = ' + (tgf.FadeState == 1 ? '100' : '0') + ')';
    tgf.FadeState = tgf.FadeState == 1 ? 2 : -2;
		if(ix>-1 && $slide>-1){app_show(tgf,ix);} //end of fade
    return;
  }
  tgf.FadeTimeLeft -= elapsedTicks;
  var newOpVal = tgf.FadeTimeLeft/TimeToFade;
  if(tgf.FadeState == 1){
    newOpVal = 1 - newOpVal;
	}
  tgf.style.opacity = newOpVal;
  tgf.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
  setTimeout("animate_fade(" + curTick + ",'" + id + "'," + ix +  ")", 33); 
}

function admin(ix){
	if(ix=='frm'){
		$aji='frm';
		$ajx='valert(rsp);';
		ajax_fetch('inclu/zz_proc.php','');		
	}else if(ix=='clr'){
		$aji='clear_new';
		$ajx="location.href='i.php?sys='+$sys+'&db=2';";
		ajax_fetch('a_core.php','');		
	}
}