var cartdomain = 'http://ekcec.web-staffs.com';
//var cartdomain = 'http://localhost:3000';
var templatedir = '/cart/';



Function.prototype.asynchronize = function() {
  var f = this;
  return function() {
    var resultcb = function() {};
    var failcb = function(e) { throw e };
    if (typeof arguments[arguments.length - 1] == 'function') {
      resultcb = Array.prototype.pop.apply(arguments);
      if (typeof arguments[arguments.length - 1] == 'function') {
        failcb = Array.prototype.pop.apply(arguments);
      }
    }
    try {
      var result = f.apply(this, arguments);
      setTimeout(function() { resultcb(result) }, 0);
    }
    catch (e) {
      setTimeout(function() { failcb(e) }, 0);
    }
  };
};



var Cartboxs = function() {
  var self = arguments.callee;
  if(self.instance == null){
      this.initialize.apply(this,arguments);
      self.instance = this;
  }
  return self.instance;
  //this.initialize();
}
Cartboxs.prototype = {
  count: 0,
  boxs: [],
  initialize: function() {
    
  },
  addBox: function(box) {
    this.boxs.push(box);
    this.count++;
  }
};

var CartBox = function(code, id, sw) {
  this.initialize(code, id, sw);
}
CartBox.prototype = {
  code: '',
  id:   '',
  sw:   '',
  html: '',
  domain: cartdomain,
  initialize: function(code, id, sw) {
    this.code = code || '';
    this.id   = id || '';
    this.sw   = sw || '';
      //alert('Initialize  Code='+this.code+':id='+ this.id +':sw='+this.sw);
  },
  getJSONP: function(box) {
    var url = this.domain + "/store/get_cart_box_jsonp/"+ 
              this.code + "/{callback}";
        //alert('URL=' + url);
    j$.getJSONP(url, function(item){
      if (box.sw == 'b') {
        box.html = renderBigCart(item);
      } else if (box.sw == 's') {
        box.html = renderSmallCart(item);
      } else if (box.sw == 'l') {
        box.html = renderlistCart(item);
      } else {
        box.html = renderlistCart(item);
      }
      //alert('GetJSONP HTML：' + box.html + ' ::: ' + box.id + ' : ' + box.sw);
      box.show();
    });
  },
  show: function(){
    //$('#'+this.id).html(this.html);
    //--jQuery の方式だと何故かIEでcartbox_lテンプレートの中身がうまく反映されない
    //  ので通常のJavaScriptに変更
    //  レンダリング自体は出来ている
    //alert('CartBox ID=' + this.id + "\n  html: "+this.html);
    //var nakami = document.getElementById(this.id).innerHTML;
    //alert('Nakami ID=' + this.id + "\n  html: "+nakami);
    document.getElementById(this.id).innerHTML = this.html; 
  }

};

function renderBigCart(item, res) {
  var bigcartTemplate = j$.createTemplateURL(templatedir + "cartbox.tpl?" + Math.random().toString(36).slice(-8));
  return bigcartTemplate.get(item[0], {}, window);
}
function renderSmallCart(item, res) {
  var smallcartTemplate = j$.createTemplateURL(templatedir + "cartbox_s.tpl?" + Math.random().toString(36).slice(-8));
  return smallcartTemplate.get(item[0], {}, window);
}
function renderlistCart(item, res) {
  var listcartTemplate = j$.createTemplateURL(templatedir + "cartbox_l.tpl?" + Math.random().toString(36).slice(-8));
  var res = listcartTemplate.get(item[0], {}, window);
    //alert(res);
  return res;
}


function AddCartbox(code, id, sw) {
  cbs = new Cartboxs();
  cbs.addBox(new CartBox(code, id, sw));
}

window.onload = function () {
  for (j = 0; j < cbs.boxs.length; j++) {
      cbs.boxs[j].getJSONP(cbs.boxs[j]);
  }
}

/*
$(document).ready(function() {
  //alert('Cartboxs count=' + cbs.count);
  for (j = 0; j < cbs.boxs.length; j++) {
      cbs.boxs[j].getJSONP(cbs.boxs[j]);
  }
});
*/


