var pop;
var index=0;
var index2=-1;
var loop=1;
var stop=0;
var items=Array();
var status=0;
var playTimeout;
var pauseTimeout;

jQuery(window).load(function(){


if(jQuery("#greeting").length==0)return;

 index=0;
 index2=-1;
 pop = Popcorn("#greeting");

 var selects=jQuery(".word");
 selects.each(function(idx,el) {
    var start=parseFloat(jQuery(this).data('start'), 10)+0.00;
    var stop=parseFloat(jQuery(this).data('stop'), 10)+0.00;
    //alert(start+":"+stop);
    items.push(Array(start,stop));
	jQuery(this).click(function() {
                        if(!jQuery(this).hasClass("selected"))jQuery(this).addClass("selected");
			index=jQuery('.word').index(jQuery(this));
			clearTimeout(pauseTimeout);
			clearTimeout(playTimeout);
			pop.pause();
			play1();
	});
//alert(start+0.01);
  pop.cue( start+0.01, function() {
	var pos=items[index][0];
	var time=(items[index][1]-items[index][0])*1000-12;
	clearTimeout(pauseTimeout);
	clearTimeout(playTimeout);
	pauseTimeout=setTimeout(pause1,time);
        setClass1();
  });

 });
 pop.play();
});


function setClass1(){
  //return;
        //if(index==6)alert(0);
  jQuery(".word").each(function(idx,el){
    if(jQuery(this).hasClass("selected"))jQuery(this).removeClass("selected");
    if(jQuery(this).hasClass("selected"))jQuery(this).removeClass("selected");
    //console.log(idx);
    if(jQuery('.word').index(jQuery(this))==index){
         jQuery(this).addClass("selected");
         if(!jQuery(this).hasClass("read"))jQuery(this).addClass("read");
         //jQuery('body').scrollTo(jQuery(this));
         //alert(jQuery(this).offset().top);
        //console.log(jQuery(window).height()+":"+jQuery(this).offset().top);
        if(jQuery(this).offset().top>jQuery(window).height()-100)jQuery('html, body').animate({
            scrollTop: jQuery(this).offset().top-150
        }, 500);
        if(jQuery(this).offset().top<200)jQuery('html, body').animate({
            scrollTop: jQuery(this).offset().top-100
        }, 500);
    }
  });
  //if(index==6)alert(1);
}

function setHis1(){
  //return;
  var hisp=storage.getItem("p"+ppid);
  if(hisp===null)hisp=",";
  if(hisp.indexOf(","+index+",")<0){
      hisp+=index;
      hisp+=",";
  }
  storage.setItem("p"+ppid,hisp);
}

function clearClass(){
  jQuery(".word").each(function(idx,el){
    if(jQuery(this).hasClass("selected"))jQuery(this).removeClass("selected");
  });
}
  
  function play1(){
        //alert(index);
	var pos=items[index][0]*1.00;
	var time=(items[index][1]-items[index][0])*1000;
	pop.play(pos);
	clearTimeout(pauseTimeout);
	clearTimeout(playTimeout);
	//pauseTimeout=setTimeout(pause1,time);
	//setTimeout(check, 1000);
	status=1;
	setClass1();
}
function pause1(){
  pop.pause();
  setClass1();
  setHis1();
  status=2;
  index++;
  if(index>=items.length)index=0;
  //alert(index);
  clearTimeout(pauseTimeout);
  clearTimeout(playTimeout);
  playTimeout=setTimeout(play1, 2000);
  //alert(playTimeout);
}
function check(){
  setClass1();
  setHis1();
}


