// American Institute of Physics homepage spots show
// Liang Lang 
// All rights reserved.


var sp;
var ct;
var t;
kuclr="#FFCC66";
var current=0;
var spotid;
var sp1;

function spotsShow()
{
       ct=4;
       spotid=0;

//       sp1 = new Spry.Widget.SlidingPanels("ticker", {enableAnimation:false});
       sp1 = new Spry.Widget.SlidingPanels("ticker", {duration:800});

/*
	rnum = (Math.floor(Math.random()*ct));   
          spotid = parseInt(rnum);
*/
          if (spotid==0) {
                 spotFunction4('spot1','.thumb1','block','1');
	  }
          else if (spotid==1) {
                 spotFunction4('spot2','.thumb2','block','2');
	  }
          else if (spotid==2) { 
                 spotFunction4('spot3','.thumb3','block','3');
	  }
          else if (spotid==3) {
                 spotFunction4('spot4','.thumb4','block','4');
	  }
          else {
                 spotFunction4('spot1','.thumb1','block','1');
	  }

	sp = new Spry.Widget.SlidingPanels("ticker", {defaultPanel:spotid, duration:300});

        current = spotid;
        spotFunction6();

	Spry.$$("#thumb1").addEventListener("click", function(){
             spotFunction7();
          //   if (sp.getMyCurrentPanelIndex()==0) {}
          //    else {
                  spotFunction2(ct); 
		  spotFunction5('.thumb1','1');
		  sp.showPanel('spot1');
		  return false;
          //    }
        	}, false);



	Spry.$$("#thumb2").addEventListener("click", function(){
             spotFunction7();
             if (sp.getMyCurrentPanelIndex()==1) {}
             else {
                  spotFunction2(ct); 
		  spotFunction5('.thumb2','2');
		  sp.showPanel('spot2');
		  return false;
             }
		}, false); 
	Spry.$$("#thumb3").addEventListener("click", function(){
            spotFunction7();
             if (sp.getMyCurrentPanelIndex()==2) {}
              else {
                  spotFunction2(ct); 
		  spotFunction5('.thumb3','3');
		  sp.showPanel('spot3');
		  return false;
             }
          	}, false); 
	Spry.$$("#thumb4").addEventListener("click", function(){
            spotFunction7();
             if (sp.getMyCurrentPanelIndex()==3) {}
             else {
                  spotFunction2(ct);
		  spotFunction5('.thumb4','4');
		  sp.showPanel('spot4');
		  return false;
               }
		}, false); 

	Spry.$$("#spot1").addEventListener("click", function(){
             spotFunction7();
             sp.showPanel('spot1');
        });
 	Spry.$$("#spot2").addEventListener("click", function(){
             spotFunction7();
             sp.showPanel('spot2');
        });
 	Spry.$$("#spot3").addEventListener("click", function(){
             spotFunction7();
             sp.showPanel('spot3');
        });
	Spry.$$("#spot4").addEventListener("click", function(){
             spotFunction7();
             sp.showPanel('spot4');
        });
}


function spotFunction4(inparam1,inparam2,inparam3,inparam4) {

    var myst = spotFunction1(inparam1);

    if(myst) {
        myst.display = inparam3;
        Spry.$$(inparam2).addClassName("thumbClass"+inparam4);
        return true;
    } else {
        return false;
    }
} 

function spotFunction5(inparam,inparam2) {
  
 for (i=1; i<=ct; i++) {
     Spry.$$('.thumb'+i).removeClassName("thumbClass"+i);
     Spry.$$('.thumb'+i).removeClassName(".thumb"+i);     
   }
   Spry.$$(inparam).addClassName("thumbClass"+inparam2);
}

function spotFunction3(inparam1, inparam2) {

    var myst = spotFunction1(inparam1);

    if(myst) {
        myst.display = inparam2;
        return true;
    } else {
        return false;
    }
} 

function spotFunction1(subspot_1) {
    if(document.getElementById && document.getElementById(subspot_1)) {
        return document.getElementById(subspot_1).style;
    } else if (document.all && document.all(subspot_1)) {
        return document.all(subspot_1).style;
    } else if (document.layers && document.layers[subspot_1]) {
        return getObjN4(document, subspot_1);
    } else {
        return false;
    }
} 

function spotFunction2(inparam) {
    ct=inparam;
    for(counter= 1; counter <= ct; counter++) {
        spotFunction3('spot' + counter, 'block');
    }
}

function getObjN4(obj,name)
{
        var x = obj.layers;
        var foundLayer;
        for (var i=0;i<x.length;i++)
        {
                if (x[i].id == name)
                        foundLayer = x[i];
                else if (x[i].layers.length)
                        var tmp = getObjN4(x[i],name);
                if (tmp) foundLayer = tmp;
        }
        return foundLayer;
}

 Spry.Utils.addLoadListener(spotsShow);

function spotFunction6() {
//    var sp1 = new Spry.Widget.SlidingPanels("ticker", {enableAnimation:false});
//    var sp1 = new Spry.Widget.SlidingPanels("ticker", {duration:300});
      var spc = sp1.getContentPanelsCount();

      spotFunction2(ct); 
      spotFunction5('.thumb'+(current+1),(current+1));

     if (current==0) {
      sp1.showPanel(current);
      }
     else {sp1.showNextPanel();}

      t= setTimeout(function() {spotFunction6()}, 8000);
      if (current == spc-1) {
         current =0;
         return;
      }else {
         current = current+1;
      }
}

function spotFunction7() {
   if (typeof(t) == 'number') clearTimeout(t);
}
