 
brand.product.videoPlayer = { 
   config: {},
   lastCuePoint: 0,
   totalCuePoints: 0,
   text_step: generic.rbKeys.get("step"),
   text_of: generic.rbKeys.get("of"),
   
   init: function() {   
    console.log("brand.product.videoPlayer.init"); 
    if (!this.config.cue_points) return; //page with video include a /site/video/*.js file with the config 
 
    this.totalCuePoints = this.config.cue_points.length - 1;
    this.lastCuePointTime = Number(this.config.cue_points[this.totalCuePoints - 2].time);
 
    this.videoPlaceholder =  $("flash_placeholder");
     if (!this.videoPlaceholder) {
        return;
    } 
    
    this.drawVideoPlayer();
    this.processRelatedProducts();
    this.setInitialView(); 
    
    var self = this;
    generic.events.observe("videoPlayer:cuePoint", self.setCuePoint.bind(self)); 
  }, 
  
  drawVideoPlayer: function() {
    //console.log("brand.product.videoPlayer.drawVideoPlayer"); 
    var params = { 
       bgcolor: "#000000",
       flashvars: { 
       	conf_uri: this.videoPlaceholder.getAttribute("conf_uri"),
       	showTitle: "true" 
       }  
    };
   
    if (this.videoPlaceholder.getAttribute("color")=="white") {
    	params.flashvars.color = "white"; 
    } 
   
    var attr = {
        id: "howToVideoPlayer",
        name: "howToVideoPlayer",
        data: "/flash/_video_player/howToVideoPlayer_query.swf" 
    };
     
     generic.flash.embed(attr, params, "flash_placeholder"); 
  },
  
  processRelatedProducts: function() { 
     if (!$("vid_prods")) return; 
     //console.log("brand.product.videoPlayer.insertRelatedProducts"); 
         
     var prod_container = $("video_prod_container");
     $("video_prod_container").addClassName("hidden");  
     $$("#vid_prods .rel_prod").invoke('addClassName', "hidden");
     $$("#vid_prods .swatch_by_color .swatch_hex_container").invoke("show");
      
     brand.product.productDisplay.init({
                data: page_data.video_products,
                initButtons: true,
                altNodeId: true,
                altType: true,
                video_prod: true
            }); 
     
  },
   
  setInitialView: function() { 
     $("video_prod_container").addClassName("hidden");
     $("tip_title").innerHTML = "&nbsp;"; 
     $("tip_copy").innerHTML = (this.config.intro) ? this.config.intro : "&nbsp;";  
  },
  
  getTimeCode: function() { 
    var vp = $("howToVideoPlayer");
    if (!vp) { return "0000"; }
    console.log("Flash Player time: " + vp.queryTime() + " " + Math.round(vp.queryTime()));
    var time = Math.round(vp.queryTime()); 
    return time;
  },
  
  showCue: function(cidx) { 
	console.log("brand.products.videoPlayer.showCue cidx: " + cidx); 
	if (cidx==-1) {
	    this.setInitialView(); 
	    return;
	 } 
	 
	var cue = this.config.cue_points[cidx];    
	var completed = (cidx == this.totalCuePoints);
	  
	$("tip_title").innerHTML = (completed) ? "&nbsp;" : this.text_step + " " + (cidx+1) + " " + this.text_of + " " + this.totalCuePoints; 
	$("tip_copy").innerHTML = cue.copy;  
	
	var hasProducts = false;
	$$("#vid_prods .rel_prod").invoke('addClassName', "hidden");
	$$("#vid_prods .swatch_by_color .swatch_hex_container").invoke("hide");
  
	var str, product, sku;   
	cue.prods.each(function(catprodsku, idx) {   
	       console.log("product: "+catprodsku+" / "+$(catprodsku)); 
	       catprodsku = catprodsku.strip();
	       product = $(catprodsku);
	       str = catprodsku.split("PROD")[1].split("SKU"); 
	       sku = $("video_swatch_SKU"+str[1]);  
	       
	       if (!!product) { 
	        	   hasProducts = true;
	        	   $("vid_prods").appendChild(product);
	        	   product.removeClassName("hidden");    
	        	   if (!!sku) sku.style.display = "block"; 
	        } 
	});  
	    
	if (hasProducts) $("video_prod_container").removeClassName("hidden");  
  },
  
  setCuePoint: function(idx) { 
    //console.log("brand.products.videoPlayer.setCuePoint "); 
    var time = this.getTimeCode(); 
    if (time == "0000") { return; }
  
    var cuePoint = -1;  
    
    for (var i=0;i<=this.totalCuePoints;i++) { 
    	if (time >= this.config.cue_points[i].time) {
    	     if (i==this.totalCuePoints) cuePoint = i; //outro 
    	} else { 
    		cuePoint = i-1;   
    		break;
    	}
    }  
     
    this.showCue(cuePoint); 
  }  
} 
 
function playerIsReady() {
    var vp = $("howToVideoPlayer");
    if (vp && vp.queryTime) {
        //generic.events.fire("videoPlayer:cuePoint", []);
        clearInterval(intId);
    }
}
  
