/*
  Author: Moises Beltran
  About: ShipOnDay, Given warehouse and onhand QTY,
         Will determine Whether to show out of stock message
         or to update div with countdown
*/
//include your own plugins
cpo.require('UtcTime','utc_time')

var ShipOnDay = Base.extend({
  constructor: function(update_div,warehouse,on_hand) {
    this.update_div = '#'+update_div;
    this.warehouse = warehouse;
    this.on_hand = on_hand;
  },
  init: function(){
    try { this.update_message();}
    catch(e){/*alert(e);*/}
  },
  time: null,
  warehouse: "",
  on_hand: 0,
  update_div: "",
  container: null,
  on_hand_zero: "Out of Stock.",
  w3not0: "Processed today. Ships in 2-3 business days. Expedited delivery not available.",
  w1not0After5: "In Stock. Ships tomorrow.",
  w1not0Weekend: "In stock.",
  w1not0Before5: "In Stock. Order in the next <b><span id='time_count_down' class='order-in-the-next'></span></b> and your order ships today!",
  //will determine which message to display
  update_message: function(){
    this.container = $(this.update_div)
    if(this.on_hand == 0){
      this.container.replaceWith(this.on_hand_zero);
    }
    else if(this.on_hand > 0 && this.warehouse == "w3"){
      this.container.replaceWith(this.w3not0);
    }
    else if(this.on_hand > 0 && this.warehouse == "w1"){
      this.time = new UtcTime();
      //our warehouse is ET US, -5
      if((this.time.is_friday() == true && this.time.withinWindow() == false) || this.time.is_saturday() == true){
	    //Its either saturday or friday after 5:00 PM EST
	    this.container.replaceWith(this.w1not0Weekend);
	  }
	  else if(this.time.is_sunday() == true){
		this.container.replaceWith(this.w1not0After5);
	  }
      else if(this.time.withinWindow()){
        this.container.replaceWith(this.w1not0Before5);
         //we will update the container every minute with updated time
        $('#time_count_down').replaceWith(this.time.time)
      }
      else
        this.container.replaceWith(this.w1not0After5);
     
    } 
  }
});
