if(typeof vector == "undefined") var vector = new Object();
vector.VecPrice = function(ns,api){
  this.imgPath = './js/vecprice_btn_cart.jpg';
  this.headTag = document.getElementsByTagName('head')[0];
  this.isIE = (undefined !== window.ActiveXObject);
  this.styleTag = document.createElement('style');
  this.styleID = "vectorStyleCss";
  this.styleTag.id = this.styleID;
  this.headTag.appendChild(this.styleTag);
  var ownerNode = (this.isIE)? "owningElement" : "ownerNode" ;
  for(var i=0;i<document.styleSheets.length;i++){
    if(document.styleSheets[i][ownerNode].id == this.styleID){
      this.CSS = document.styleSheets[i];
    }
  }
  this.ns = ns;
  this.titleTxt = '製品'
  this.leadTxt = 'この製品の価格表です。'
  this.html = new Array();
  this.numbers =  new Array();
  this.appendID  = new String();
  this.apiURL = (api)? api : 'http://search2.vector.co.jp/vsearch/api/nprice_api.php?';
  this.colors = {
    "border"   : "#9eccf0",
    "bg_head"  : "#edf7ff",
    "bg_body"  : "#ffffff",
    "link"     : "#025ce6",
    "headText" : "#1d80e6",
    "text"     : "#333333",
    "hr"       : "#286eb4",
    "strong"   : "#c50004"
  };
};
vector.VecPrice.prototype = {
  addVariable : function(n,v){
    switch(n){
      case "title" : this.titleTxt = v ;break;
      case "lead"  : this.leadTxt = v; ;break;
      case "image" : this.imgPath = v ;break;
      case "srnumber" : this.numbers = v ;break;
      case "id" : this.appendID = v;break;
    }
  },
  set : function(){
    this.getJSON();
  },
  write : function(html){
    document.getElementById(this.appendID).innerHTML = html;
  },
  addCSS : function(selector,propaty){
    if(this.isIE){
      this.CSS.addRule(selector,propaty);
    }else{
      this.CSS.insertRule(selector+' {'+propaty+'}',0);
    };
  },
  parseJSON : function(data){
    var pTable = '#sptable_'+this.ns;
    // CSS Rule
    this.addCSS(pTable,'margin:5px auto; width:98%; border:1px solid '+this.colors.border+'; border-collapse:separate; /border-collapse:collapse;');
    this.addCSS(pTable+' table','width:100%;');
    this.addCSS(pTable+' *','font-family:"メイリオ"; font-size:12px;');
    this.addCSS(pTable+' thead td','padding:5px; background:'+this.colors.bg_head+';');
    this.addCSS(pTable+' thead span','font-size:11px;');
    this.addCSS(pTable+' tfoot td','color:'+this.colors.headText+'; text-align:right; font-size:10px; padding:3px 5px; background:'+this.colors.bg_head+';');
    this.addCSS(pTable+' #sptable_'+this.ns+'_title','color:'+this.colors.headText+';');
    this.addCSS(pTable+'_in','padding:3px 5px; background:'+this.colors.bg_body+';');
    this.addCSS(pTable+'_in thead td','font-size:10px; background:'+this.colors.bg_body+' !important;');
    this.addCSS(pTable+'_in tbody td','padding:5px; vertical-align:middle; border-top:1px dotted '+this.colors.hr+';');
    this.addCSS(pTable+'_in tbody strong','font-size:14px; font-weight:bold; color:'+this.colors.strong+';');
    this.addCSS(pTable+' a','color:'+this.colors.link+';');
    this.addCSS('.sptable_in_title','font-size:18px !important;');
    this.addCSS('.sptable_in_price','color:'+this.colors.text+'; width:54%;');
    this.addCSS('thead .sptable_in_name','color:'+this.colors.text+';');
    this.addCSS('thead .sptable_in_price','text-align:left !important;');
    this.addCSS('tbody .sptable_in_price','text-align:right;');
    this.addCSS('.sptable_in_btn','text-align:center;');
    // Create HTML
    this.html.push('<table id="sptable_'+this.ns+'"><thead id="sptable_'+this.ns+'_title"><tr><td>\
      「<b class="sptable_in_title">'+this.titleTxt+'</b>」価格表<br/>\
      <span>'+this.leadTxt+'</span></td>\
      </tr>');
    this.html.push('</thead><tbody><tr><td id="sptable_'+this.ns+'_in"><table><thead><tr><td class="sptable_in_name">商品名</td><td class="sptable_in_price">価格</td><td class="sptable_in_btn"></td></tr></thead><tbody>');
    var item = data.retval;
    for(var i=0;i<item.length;i++){
      this.html.push('<tr>\<td class="sptable_in_name"><a href="'+this.decode(item[i].link_url)+'">'+this.decode(item[i].sr_name)+'</a></td>');
      if(item[i].sp_discount == '0'){
        this.html.push('<td class="sptable_in_price">通常'+this.decode(item[i].sr_price)+'円</td>');
      }else{
        this.html.push('<td class="sptable_in_price">通常'+this.decode(item[i].sr_price)+'円が<b>'+item[i].sp_discount+'%</b>OFF!<strong>'+this.decode(item[i].sp_date)+'</strong>まで<strong>特価'+this.decode(item[i].sp_price)+'円</strong></td>');
      }
      this.html.push('<td class="sptable_in_btn"><a href="'+this.decode(item[i].link_url)+'"><img src="'+this.imgPath+'" alt="詳細/購入"></a></td></tr>');
    };
    this.html.push('\
    </tbody></table></td></tr></tbody><tfoot>\
        <tr><td colspan="3">※全て税込価格となります。</td></tr>\
      </tfoot></table>\
    ');
    this.write(this.html.join('\n'));
  },
  decode : function(str){
    str = decodeURIComponent(str);
    var reg = new RegExp(/\+/g),
        restr = str,
        decStr = '';
    if(str.match(reg)){
      decStr = '&nbsp;'
      restr = str.replace(reg,decStr);
    };
    return restr;
  },
  getJSON : function(){
    var vals = this.numbers;
    var script = document.createElement('script');
    script.src = this.apiURL+'&vals='+vals+'&callback='+this.ns+'.parseJSON';
    this.headTag.appendChild(script);
  }
};
var VecPrice = vector.VecPrice;
