var x, y;

var not_throb = 1;
var equip_index;
var west_index;

var imgs_index   = ['1016','992','976','960','944','928','896','864','832','800','768','736','704','640','576','512'];
//var path_to_pics = 'pres.pics/';
var path_to_pics = '';

var beta  = 150/567;
var gamma = 301/567;
var beta_x, gamma_x;


var slide_load_boolean;

function initialise(){
    slide_initialise();//NOTE : 
                       //This function AND setting value of alpha parameter is in file  slide.parameter.js
                       //*********************************************************************************
    x = parseInt(850*alpha);
    y = parseInt(1100*alpha);

    var ob;
    ob = gEID('slide_background');
    ob.style.width  = parseInt(x)+'px';
    ob.style.height = parseInt(y)+'px';

    gEID('sld_l').style.left = parseInt((x*100)/567)+'px';
    gEID('sld_r').style.left = parseInt((x*467)/567 - 67)+'px';

    set0();

    slide_order_index = 0;

    for (var c = 0; c < slide_order.length; c++) { slide_load_boolean[c] = 0;}
    if( slide_order_index + 2 < slide_order.length && !slide_load_boolean[slide_order_index + 2] ){ 
	slide_load_boolean[slide_order_index + 2] = 1;
	do_image_pre_load( slide_order[ slide_order_index + 2] );
    }
}//end of initialise




function sld_reset(order_increment){
    var prev_slide = slide_order[ slide_order_index ];

    slide_order_index += order_increment;

    if( slide_order_index < 0 || (slide_order_index + 1) > slide_order.length ){}// Do Final slide
    else{ // go through given slide order
	var next_slide = slide_order[ slide_order_index ];
	reset(prev_slide,next_slide);



	if( slide_order_index + 2 < slide_order.length && 
	    !slide_load_boolean[slide_order_index + 2] ){ 
	    //Carry out image preloading for future slides
	    slide_load_boolean[slide_order_index + 2] = 1;
	    do_image_pre_load( slide_order[ slide_order_index + 2] );
	}
    }
}//End of sld_reset




function reset(prev_slide,next_slide){

    if( next_slide < 20 ){
	if( next_slide < 5 ){

	    switch( next_slide ){
	    case 0 : set0(); break;
	    case 1 : set1(0,"fade in blurb");          break;
      	    case 2 : set1(0,"straight to next slide"); break;
	    case 3 : set3(0); break;
	    case 4 : set4(0); break;
	    }
	}
	else if( next_slide < 10 ){
	    
	    switch( next_slide ){
	    case 5 : doub_1100x680_raise('ivanpahbx','ivanpah','ivanpah.jpg'); break;
	    case 6 : doub_1100x680_raise('yumasanluisbx','yumasanluis','Yuma.SanLuis.jpg'); break;
	    case 7 : set7(); break;
	    case 8 : set2(0); break; 
	    case 9 : simp_1100x680_raise('nmairportbx','nmairport','NM.TX.ElPaso.sec.jpg'); break;
	    default : set0(0); 
	    }
	}
	else if( next_slide < 15 ){

	    switch( next_slide ){
	    case 10 : simp_1100x680_raise('almiassbx','almiass','Al.Miss.ss1.jpg'); break;
	    case 11 : simp_1100x680_raise('yumacirclebx','yumacircle','Yuma.circles.jpg'); break;
	    case 12 : simp_1100x680_raise('bakeoilbx','bakeoil','Bake.Oil.jpg'); break;
	    case 13 : set3(0); break;
	    case 14 : set4(0); break;
	    default : set0(0); 
	    }
	}
	else{
	}
    }
    else if( next_slide < 40 ){
	if( next_slide < 25 ){
	}
	else if( next_slide < 30 ){
	}
	else if( next_slide < 35 ){
	}
	else{
	}
    }
    else if( next_slide < 60 ){
	if( next_slide < 45 ){
	}
	else if( next_slide < 50 ){
	}
	else if( next_slide < 55 ){
	}
	else{
	}
    }

    //////////////////////////////////

    if( prev_slide < 20 ){
	if( prev_slide < 5 ){

	    switch( prev_slide ){
	    case 0 : unset0(); break;
	    case 1 : hhide('solen'); break;
	    case 2 : hhide('solen'); break;
	    case 3 : unset3(); break;
	    case 4 : unset4(); break;
	    default : unset0(); 
	    }
	}
	else if( prev_slide < 10 ){
	    switch( prev_slide ){
	    case 5 : doub_hide('ivanpahbx','ivanpahparks'); break;
	    case 6 : doub_hide('yumasanluisbx','yumasanluisparks'); break;
	    case 7 : unset7(); break;
	    case 8 : unset2(); break;

	    case 9 : hhide('nmairportbx');  break;
	    default : unset0(); 
	    }
	}
	else if( prev_slide < 15 ){
	    switch( prev_slide ){
	    case 10 : hhide('almiassbx'); break;
	    case 11 : hhide('yumacirclebx'); break;
	    case 12 : hhide('bakeoilbx'); break;
	    case 3 : unset3(); break;
	    case 4 : unset4(); break;
	    default : unset0(); 
	    }
	}
	else{
	}

    }
    else if( prev_slide < 40 ){
	if( prev_slide < 25 ){
	}
	else if( prev_slide < 30 ){
	}
	else if( prev_slide < 35 ){
	}
	else{
	}
    }
    else if( prev_slide < 60 ){
	if( prev_slide < 45 ){
	}
	else if( prev_slide < 50 ){
	}
	else if( prev_slide < 55 ){
	}
	else{
	}
    }

}//End of reset




function set0(){

    var ob                   = gEID('iri_pos');
    ob.style.left            = (-65 + parseInt(0.5*x))+'px';
    ob.style.top             = (-40 - parseInt(0.5*y))+'px';
    gEID('iri').style.width  = '168px';
    hhide('back');


}//end of set0
function unset0(){}

function do_image_pre_load(load_slide){

    var imgs;

    switch( load_slide ){
    case 2 : imgs = []; break;
    case 3 :  
	imgs = ['USA.1016.s.jpg','USA.992.s.jpg','USA.976.s.jpg','USA.960.s.jpg','USA.944.s.jpg','USA.928.s.jpg','USA.896.s.jpg','USA.864.s.jpg','USA.832.s.jpg','USA.800.s.jpg','USA.768.s.jpg','USA.736.s.jpg','USA.704.s.jpg','USA.640.s.jpg','USA.576.s.jpg','USA.512.s.jpg','s.ber.cty.out.s.gif','New.Hamp.s.ber.cty.out.s.gif']; break;
    case 4 :  
	imgs = ['map.SoCal.smaller.jpg','san.Bern.boundary.gif','parks.gif','bases.gif']; break;
    case 5 :  
	imgs = ['ivanpah.jpg','ivanpah.parks.gif']; break;
    case 6 :  
	imgs = ['Yuma.SanLuis.jpg','Yuma.SanLuis.gif']; break;
    case 7 :  
	imgs = ['DoD.QDR.2010.jpg','adr_mullen.crop.s.jpg','Marine.Corp.E2O.Exped.Energy.Office.png']; break;
    case 8 :  
	imgs = ['engines.jpg','electrical.generators.jpg','receivers.jpg','heat.tanks.jpg','mirrors.jpg','acres.jpg']; break;
    case 9 :  
	imgs = ['NM.TX.ElPaso.sec.jpg']; break;
    case 10 :  
	imgs = ['Al.Miss.ss1.jpg']; break;
    case 11 :  
	imgs = ['Yuma.circles.jpg']; break;
    case 12 :  
	imgs = ['Bake.Oil.jpg']; break;
     default : imgs = []; 
    }

    for (var c = 0; c < imgs.length; c++) {
	var img = new Image();
	img.src = path_to_pics + imgs[c];
    }
}//End of do_image_pre_load




function set1(count,irithm_blurb){
    var ob = gEID('invt');
	
    if( count > 6){
	if( count == 52){ 
	    hide(ob); //hide 'invents'

	    visi(gEID('back')); // make back-to-last-slide button visible

	    //make irithm logo small and move it to bottom left
	    gEID('iri').style.width = '84px';
	    ob = gEID('iri_pos');
	    ob.style.left           = '0px';
	    ob.style.top            = '-14px';

	    // make visible 'Electrical power ...', position it, prepare for it's fadein and then fade it in.
	    ob = gEID('solen');
	    visi(ob);
	    ob.style.left       = (-400  + parseInt(0.5*x))+'px';
	    ob.style.top        = (7  + parseInt(0.5*y))+'px';
	    if( irithm_blurb == "fade in blurb" ){ fadein(0,'solen');}// Fadein irithm promotional catch phrase
	    else{                                  sld_reset(+1);}    // Forget catch phrase, go straight to next slide
	}
    }
    else if( count > 0){
	ob.style.left       = (-143 + 25*count + parseInt(0.5*x))+'px';
    }
    else{ // count == 0 
	visi(ob);
	ob.style.left       = (-143  + parseInt(0.5*x))+'px';
	ob.style.top        = (7  + parseInt(0.5*y))+'px';
    }

    if( count < 52){ count++; setTimeout("set1("+count+",'"+irithm_blurb+"');",33); }
}//end of set1

function fadein(count,id){
    var ob = gEID(id);
	
    var l = 4*(64 - count);
    ob.style.color = 'rgb('+l+','+l+','+l+')';

    if( count < 64){ count++; setTimeout("fadein("+count+",'"+id+"');",33); }
}//end of fadein




function set2(count){
   if( count > 6){
	if( count == 8){
 
	    var ob = gEID('acres');
	
	    visi(ob);
	    ob.style.left       = (-219  + parseInt(0.5*x))+'px';
	    ob.style.top        = (62  + parseInt(0.5*y))+'px';
	    ob.style.color      = 'rgb(128,128,128)';
	    equip_index = 1; equip();

	    ob = gEID('tenmil');	
	    visi(ob);
	    ob.style.left       = (-225  + parseInt(0.5*x))+'px';
	    ob.style.top        = (-27  + parseInt(0.5*y))+'px';
	    ob.style.color      = 'rgb(255,255,255)';
	    not_throb           = 0;
	    throb(0,1,'tenmil');
	}
    }

    if( count < 8){ count++; setTimeout("set2("+count+");",33); }
}//end of set2
function unset2(){ doub_hide('tenmil','thirmil'); doub_hide('eitymil','acres'); not_throb = 1; }

function equip(){
    var ob = gEID('acresimg');
    
    switch( equip_index ){
    case 1 : newsrcimg('acresimg','engines.jpg'); break;
    case 2 : newsrcimg('acresimg','electrical.generators.jpg'); break;
    case 3 : newsrcimg('acresimg','receivers.jpg'); break;
    case 4 : newsrcimg('acresimg','heat.tanks.jpg'); break;
    case 5 : newsrcimg('acresimg','mirrors.jpg'); break;
    default : newsrcimg('acresimg','acres.jpg');
    }
    equip_index++;
    if( equip_index == 7 ){ equip_index = 1;}
}//end of equip



function throb(count,inc,id){

    if( !not_throb ){
	var ob = gEID(id);
	
	//	var l = 2*(128 - count);
	//	ob.style.color = 'rgb('+l+','+l+','+l+')';

	//	if( inc == 1 ){
	//	    if( count < 127){ count++; }else{ inc = -1;} 
	//	}

	var l = 255 - 3*count;
	//	ob.style.color = 'rgb('+l+','+l+','+l+')'; //varies greys
	ob.style.color = 'rgb(255,'+l+','+l+')';//varies reds

	if( inc == 1 ){
	    if( count < 70){ count++; }else{ inc = -1;} 
	}
	else{
	    if( count > 0 ){ count--; }
	    else{ 
		hide(ob);
		if( id == 'eitymil' ){ id = 'tenmil';}
		else{
		    if( id == 'thirmil' ){ id = 'eitymil';}
		    if( id == 'tenmil' ){ id = 'thirmil';}
		}
		ob = gEID(id);
		visi(ob);
		ob.style.left       = (-225  + parseInt(0.5*x))+'px';
		ob.style.top        = (-27  + parseInt(0.5*y))+'px';
		ob.style.color      = 'rgb(255,255,255)';

		inc =  1;
	    } 
	}
	setTimeout("throb("+count+","+inc+",'"+id+"');",33); 
    }

}//end of throb


function set3(){ 
    var ob = gEID('westUSA');
    ob.style.top        = (-290  + parseInt(0.5*y))+'px';
    ob.style.left       = (-410  + parseInt(0.5*x))+'px';
    visi(ob);
    ob.style.overflow = 'visible';
    ob = gEID('WUSA');
    ob.src  =  path_to_pics  + 'USA.' + imgs_index[0] + '.s.jpg';

    west_index = 1;
}//end of set3()
function unset3(){ DOWNimg('TWUSA'); DOWNimg('NH'); hhide('westUSA');}



function ani_to_West_USA(count){
    var ob;

    switch( west_index ){
    case 1 : 
	if( count == 0 ){ srcpixgif('TWUSA'); srcpixgif('NH'); }
	gEID('WUSA').src =  path_to_pics  + 'USA.' + imgs_index[count] + '.s.jpg';
	if( count < imgs_index.length){ count++; setTimeout("ani_to_West_USA("+count+");",62); }
	else{
	    west_index = 2;
	}
	break;
    case 2 :
	newsrcimg('TWUSA','s.ber.cty.out.s.gif');
	ob = gEID('posTWUSA');
	ob.style.top     = '-564px';
	ob.style.left    = '121px';
	west_index = 3;
	break;
    case 3 :
	newsrcimg('NH','New.Hamp.s.ber.cty.out.s.gif');
	ob = gEID('posNH');
	ob.style.top     = '-650px';
	ob.style.left    = '121px';
	west_index = 1;
	break;
    default : break; 
    }
}//End of ani_to_West_USA


function set4(){ 
    var ob = gEID('SoCalbx');
    ob.style.top        = (-385  + parseInt(0.5*y))+'px';
    ob.style.left       = (-704  + parseInt(0.5*x))+'px';

    visi(ob);
    ob.style.overflow = 'visible';
    newsrcimg('SoCal','map.SoCal.smaller.jpg');

    ob = gEID('posSBcoun');
    ob.style.top     = '-752px';
    ob.style.left    = '121px';

    newsrcimg('SBcoun','san.Bern.boundary.gif');
    visi( gEID('SBcoun') );

}//end of set4()
function unset4(){ doub_hide('SoCalbx','SBcoun'); doub_hide('SoCalparks','SoCalbases');}

function bringUPparks(){ 
    ob = gEID('posSoCalparks');
    ob.style.top     = '-838px';
    ob.style.left    = '121px';
    ob = gEID('SoCalparks');
    ob.src =  path_to_pics + 'parks.gif';
    visi(ob);
}//End of bringUPparks
function bringUPbases(){ 
    ob = gEID('posSoCalbases');
    ob.style.top     = '-924px';
    ob.style.left    = '123px';
    ob = gEID('SoCalbases');
    ob.src =  path_to_pics + 'bases.gif';
    visi(ob);
}//End of bringUPbases
function DOWNparksbases(){DOWNimg('SoCalparks'); DOWNimg('SoCalbases');}//DOWNparksbases

function srcpixgif(el){    gEID(el).src =  path_to_pics + 'pix.gif';}
function newsrcimg(el,im){ gEID(el).src =  path_to_pics + im;}


function doub_1100x680_raise(slide_bx,slide_id,slide){ sl_1100x680_raise(-385,-704,slide_bx,slide_id,slide);}
function simp_1100x680_raise(slide_bx,slide_id,slide){ sl_1100x680_raise(-342,-583,slide_bx,slide_id,slide);}
function sl_1100x680_raise(top,left,slide_bx,slide_id,slide){
    var ob = gEID(slide_bx);
    ob.style.top        = (top  + parseInt(0.5*y))+'px';
    ob.style.left       = (left + parseInt(0.5*x))+'px';
    
    visi(ob);
    ob.style.overflow = 'visible';
    newsrcimg(slide_id,slide);
}//end of 1100x680_raise


function doub_hide(el1,el2){ hhide( el1 ); hhide( el2 );}
function bringUPsingle(id){ 

    if( id == 'ivanpahparks' ){
	var top_str  = '-752px';
	var gif_str  = 'ivanpah.parks.gif';
    }
    if( id == 'yumasanluisparks' ){
	var top_str  = '-752px';
	var gif_str  = 'Yuma.SanLuis.gif';
    }

    var pos_str = 'pos' + id;

    var ob = gEID(pos_str);
    ob.style.top     = top_str;
    ob.style.left    = '121px';

    ob = gEID(id);
    ob.src =  path_to_pics + gif_str;
    visi(ob);
}//End of bringUPsingle
function DOWNimg(id){ srcpixgif(id); hhide(id);}//DOWNimg





function set7(){ 
    var ob;
    ob = gEID('QDRcover');
    ob.style.width  = parseInt(x)+'px';
    ob.style.height = parseInt(y)+'px';

    beta_x  = parseInt(beta*x);
    gamma_x = parseInt(gamma*x);
    ani2a(0);
}//end of set7()
function unset7(){ srcpixgif('slide_background'); doub_hide('MarineRFI2','MarineRFI'); doub_hide('AdMull1','geoenergy');}
function ani2a(count){

    if( count > 6){
	var y = parseInt(1100*alpha);
	var x = parseInt(y*(15-count)/8);
	gEID('QDRcover').style.top    = (y-x)+'px';
	gEID('QDRcover').style.height = x+'px';
    }
    else if( count == 0 ){
	newsrcimg('slide_background','DoD.QDR.2010.jpg');
	gEID('iri_pos').style.left    = '0px';
	gEID('iri_pos').style.top     = '-14px';
	gEID('iri').style.width       = '84px';
    }

    if( count < 15 + 1){ count++; setTimeout("ani2a("+count+");",30); }
}//end of ani2a



function ani2b(count,id){
    var ob = gEID(id);

    if( count > 0 ){
	if( count < 7){ 
	    ob.style.left  = parseInt(beta_x*(5-count)/5)+'px';
	    ob.style.width = parseInt(beta_x*(count)/5)+'px';

	    if( count == 1 && id == 'AdMull1' ){newsrcimg('slide_background','adr_mullen.crop.s.jpg');}
	}
	else{
	    var  z = parseInt(beta_x + beta_x*(count-5)/5);
	    if( z < (alpha*700)/0.8 ){ ob.style.width  = z+'px';}else{ ob.style.width  = parseInt( (alpha*710)/0.8 + 0.5)+'px';}
	}
	if( id == 'geoenergy' ){ SetOpacity(gEID('slide_background'),(100-count*4));}
    }
    else{
	ob.style.left   = beta_x+'px';
	ob.style.top    = gamma_x+'px';
	ob.style.zIndex = 1;
	visi(ob);
	ob.style.fontSize = parseInt( (alpha*22)/0.8 + 0.5)+'px';
    }

    if( count < 21){ count++; setTimeout("ani2b("+count+",'"+id+"');",30);}
}//end of ani2b

function ani2c(count,id){
    var ob = gEID(id);

    if( count > 0 ){
	if( count < 7){ 
	    ob.style.left  = parseInt(beta_x*(5-count)/5)+'px';
	    ob.style.width = parseInt(beta_x*(count)/5)+'px';

	    if( count == 1 && id == 'MarineRFI' ){ 
		newsrcimg('slide_background','Marine.Corp.E2O.Exped.Energy.Office.png');
		SetOpacity(gEID('slide_background'),100);
	    }
	}
	else{
	    var  z = parseInt(beta_x + beta_x*(count-5)/5);
	    if( z < (alpha*700)/0.8 ){ ob.style.width  = z+'px';}else{ ob.style.width  = parseInt( (alpha*710)/0.8 + 0.5)+'px';}
	}
	if( id == 'MarineRFI2' ){ SetOpacity(gEID('slide_background'),(100-count*4));}
    }
    else{
	ob.style.left   = beta_x+'px';
	ob.style.top    = gamma_x+'px';
	ob.style.zIndex = 1;
	visi(ob);
	ob.style.fontSize = parseInt( (alpha*22)/0.8 + 0.5)+'px';
    }

    if( count < 21){ count++; setTimeout("ani2c("+count+",'"+id+"');",30);}
}//end of ani2c


// /////////////////////// End of irithm code ////////////////////////
