var ConfiguredViewProcess = Class.create({
  CLASSDEF: {
      name: 'ConfiguredViewProcess'
  },
  
  initialize: function CPVAP_initialize(configuredViewArea, productProcess) {
    this.id = productProcess.id;
    this.configuredViewArea = configuredViewArea;
    this.productProcess = productProcess;
    //build the div that will contain the 
    this.htmlId = d.getNextId();
    if(d.mode != DESIGNER_MODE_VIEW_CUSTOM_PRODUCT) {
      this.managePaneFull = document.createElement("DIV");
      this.managePaneFull.id = "mp_c_" + this.htmlId;
	  this.managePaneFull.className="manage_pane_width";
      this.managePaneFull.style.display = "none";
      
      this.managePaneFull.innerHTML = '<h3 class="manage_pane_header" style="display:none;" id="mp_h_' + this.htmlId + '">' + productProcess.process.name + '</h3>' +
      '<div id="mp_' + this.htmlId + '" class="manage_pane_body"></div>';
        
      configuredViewArea.managePane.appendChild(this.managePaneFull);
      
      this.managePane = $('mp_' + this.htmlId);
      this.managePaneHeader = $('mp_h_' + this.htmlId);
    }
    this.items = new MapList();
    this.visibleItems = new MapList();
    this.enabled = false;
    
  },
  
  reSortZOrder: function CPVAP_reSortZOrder() {
    
    this.items.resort();
    
    for(var i=0;i<this.items.list.size();i++) {
      var item = this.items.list[i];
      item.pos = i;
      
      if(i==0) {
        if(!item.isFirstZ) {
          //item.titleZUp.src = d.pathPrefix + "/images/mp/move_up_off.gif";
          //item.titleZUp.className="off";
          item.titleZDown.className="off";
          //this.items[i].titleZUp0.src = d.pathPrefix + "/images/mp/move_up_off.gif";
          item.isFirstZ = true;
        }
      } else if(item.isFirstZ) {
        //item.titleZUp.src = d.pathPrefix + "/images/mp/move_up_on.gif";
        //item.titleZUp.className=null;
        item.titleZDown.className=null;
        //this.items[i].titleZUp0.src = d.pathPrefix + "/images/mp/move_up_on.gif";
        item.isFirstZ = false;
      }
      
      if(i==this.items.list.size()-1) {
        if(!item.isLastZ) {
          //item.titleZDown.src = d.pathPrefix + "/images/mp/move_down_off.gif";
          //item.titleZDown.className="off";
          item.titleZUp.className="off";
          //this.items[i].titleZDown0.src = d.pathPrefix + "/images/mp/move_down_off.gif";
          item.isLastZ = true;
        }
      } else if(item.isLastZ) {
        //item.titleZDown.src = d.pathPrefix + "/images/mp/move_down_on.gif";  
        //item.titleZDown.className=null;
        item.titleZUp.className=null;
        //this.items[i].titleZDown0.src = d.pathPrefix + "/images/mp/move_down_on.gif";
        item.isLastZ = false;
      }
    }
  },
  
  initItems: function CPVAP_initItems() {
    for(var i=0; i < this.items.list.size(); i++) {
      var item = this.items.list[i];
      log("initing item:" + item.id);
      item.initialiseManagePane();
      item.addToDesigner(false);
      item.showPanel();
      log("inited item:" + item.id);
    }
    this.reSortZOrder();
  },
  
  addItem: function CPVAP_addItem(item) {
    if (item.itemVisible()) this.visibleItems.add(item);
    this.items.add(item);
    this.configuredViewArea.registerItem(item);
  },
  
  removeItem: function CPVAP_removeItem(item) {
    if (item.itemVisible()) this.items.remove(item.id);
    this.visibleItems.remove(item.id);
    this.configuredViewArea.deRegisterItem(item);
  },
  
  //called by the interface
  addNewItem: function CPVAP_addNewItem(newItem) {
    
    //set the top items up arrow to on
    if((this.items.list.size() > 0)&&(this.items.list[this.items.list.size()-1].titleZUp!=null)) {
      //this.items.list[0].titleZUp.src = d.pathPrefix + "/images/mp/move_up_on.gif";
      this.items.list[0].titleZUp.className=null;
    }
    this.addItem(newItem);
    newItem.initialiseManagePane();
    newItem.addToDesigner(false);
    newItem.showPanel();
    this.reSortZOrder();
    d.currentProductType.updatePrice();
  },
  
  deleteItem: function CPVAP_deleteItem(item) {
    this.removeItem(item);
    this.reSortZOrder();
    if(this.visibleItems.list.size() == 0) {
      this.configuredViewArea.updateAllowedProcesses();
      this.configuredViewArea.showManagementPanels();
    }
    d.currentProductType.updatePrice();
  },
  
  isUsed: function CPVAP_isUsed() {
    return (this.items.list.size() > 0);
  },
  
  hasVisibleItems: function CPVAP_hasVisibleItems() {
    return (this.visibleItems.list.size() > 0);
  },
  
  isUsingItemOfType: function CPVAP_isUsingItemOfType(type) {
    for(var i=0; i < this.items.list.length; i++) {
      if(this.items.list[i].asset.getItemType() == type) {
        return true;
      }
    }
    return false;
  },
  
  showPanel: function CPVAP_showPanel(useHeader, onlyIfHasItems) {
    if(useHeader) {
      this.managePaneHeader.style.display="";
    } else {
      this.managePaneHeader.style.display="none";
    }
    if(onlyIfHasItems && !this.isUsed()) {
      this.managePaneFull.style.display="none";
    } else {
      this.managePaneFull.style.display="";
    }
  },
  
  hidePanel: function CPVAP_hidePanel() {
    if(this.managePaneFull != null) {
      this.managePaneFull.style.display="none";
    }
  },
  
  select: function CPVAP_select() {
    
  },
  
  remove: function CPVAP_remove() {
    for(var i=0;i<this.items.length;i++) {
      this.items[i].remove();
    }
    if(this.managePaneFull != null) {
      this.managePaneFull.parentNode.removeChild(this.managePaneFull);
      this.managePaneFull = null;
    }
  },
  
  getNextZOrder: function CPVAP_getNextZOrder() {
    var maxZ = -1;
    for(var i=0;i<this.items.list.size();i++) {
      if(this.items.list[i].zIndex > maxZ) {
        maxZ = this.items.list[i].zIndex;
      }
    }
    return maxZ+1;
  },
  
  buildManagePane: function CPVAP_buildManagePane(item) {
    var html = types[item.itemType];
    var re = new RegExp("\\[ID\\]", "g");
    html = html.replace(re,item.elId);

    var x = new Insertion.Top(this.managePane, html);
    
    //html = type_min.replace(re,item.id);
    //x = new Insertion.Top(this.managePanes[0], html);
  },
  
  //calcuate for price to do the printing this object stores..
  //colorType: the type of color (white/light/dark)
  //priceData: object to store the price breakdown in (priceData.extras)
  //returns total this views costs to decorate
  calculatePrice: function CPVAP_calculatePrice(colorType, fullPriceData, priceData, percentMarkup) {
    
    var processPriceData = {
      type: 3,
      process: this,
      priceType: 0,
      extras: []
    };
    var price = 0.0;
    var stitchCount = 0;
    var incompleteStitchCount = false; //do we have a stitch count for all the items?
    var digitizationFees = 0;
    if(this.productProcess.process.isWilcomEMB()) {
      //get the thread count of each item used....
      
      if(this.items.list.size() == 0) {
        incompleteStitchCount = true;
      } else {
        for(var i=0;i < this.items.list.size(); i++) {
          var item = this.items.list[i];
          if((item.isWilcomEMB)||(item.digitize)) {
            log("emb item, item.digitize=" +item.digitize + ", item.digitizationFee=" + item.digitizationFee + ", item.stitchCount=" + item.stitchCount);
            if((item.digitize)&&(item.digitizationFee == null)) {
              item.updateDigitizationCosts();
            }
            
            if(item.stitchCount != null) {
              stitchCount += item.stitchCount;
            } else {
              log("Found incomplete stitch count");
              incompleteStitchCount = true;
            }
          }
          //We now display as separate cart item
          //if(item.digitize) {
          //  digitizationFees += item.digitizationFee;
          //}
        }
      }
      if(incompleteStitchCount) {
        var defaultStitchCount = this.productProcess.productTypeProcess.defaultStitchCount;
        if(defaultStitchCount==null) {
          defaultStitchCount = 0;
        }
        if(defaultStitchCount > stitchCount) {
          log("Reset stitch count from " + stitchCount + " to " + defaultStitchCount);
          stitchCount = defaultStitchCount;
        }
      }
      
      
    }
    
    if(this.productProcess.productTypeProcess.priceMode == 0) { //lwd
      price = this.productProcess.prices[colorType];
      processPriceData.priceType = colorType;
      processPriceData.priceDelta = this.productProcess.prices[colorType] - this.productProcess.prices[0];
      
    } else if(this.productProcess.productTypeProcess.priceMode == 1) { //single price
      price = this.productProcess.prices[0];
      processPriceData.priceType = -1;
      processPriceData.priceDelta = 0;
    } else { //lookup tables
      processPriceData.priceType = -2;
      
      var priceTable = this.productProcess.priceTable;
      var embPriceData = priceTable.calcPrice(stitchCount, this.configuredViewArea.configuredProduct.qty);
      
      price = embPriceData[0];
      processPriceData.threadCountLower = embPriceData[1];
      processPriceData.threadCountUpper = embPriceData[2];
      processPriceData.priceDelta = 0;
      
      
      log(priceData);
      //TODO: get digitisation costs
    }
    if(digitizationFees>0) {
        var digitizationPriceData = {
          type: 4,
          process: this,
          total: digitizationFees * percentMarkup,
          extras: []
        };
        processPriceData.extras.push(digitizationPriceData);
        //split the price accross the qty to give an item cost...
        itemDigPrice = digitizationFees / this.configuredViewArea.configuredProduct.qty;
        price += itemDigPrice;
    }
    
    if(fullPriceData.unUsedDecorationCost > 0) {
      if(fullPriceData.unUsedDecorationCost > price) {
        fullPriceData.unUsedDecorationCost -= price;
        price = 0;
      } else {
        price -= fullPriceData.unUsedDecorationCost;
        fullPriceData.unUsedDecorationCost = 0;
      }
    }
    
    //add decoration library asset charges
    for (var i=0; i < this.items.list.length; i++) {
      var item = this.items.list[i];
      if(item.asset.decorationLibraryId != 0) {
        log('trying to calculate cost from process');
        var itemcost = item.asset.itemCost();
        log("item cost " + itemcost);
        price = price + itemcost;
        log('price is now ' + price);
      }
    };
    
    if(price > 0) {
      processPriceData.total = price * percentMarkup;
      priceData.extras.push(processPriceData);
    }
    return price;
  }
});
