/*
 *  トップページ
 */


// 時間オブジェクト
var dateObj = new Date();

// SWFObjectの設定
var flashvars = {
  _cache: dateObj.getTime()
};
var params = {
  wmode: "transparent",
  allowfullscreen: true
};
var attributes = {
  wmode: "transparent"
};

// <a href="[URI]" rel="external">にtarget="_blank"と同じ効果を付与
addEvent(window, "load", function(){
  var anchors = document.getElementsByTagName("a");
  for(var i=0; i<anchors.length; i++){
    var a = anchors[i];
    if(a.getAttribute("rel", true) == 'external'){
      a.target = '_blank';
    }
  }
});


/*
 * メインナビのプルダウン
 */
var pulldownNavi = {
  hideSpeed: 400,
  showSpeed: 600,
  hideWait: 600,
  zIndex: 5,
  zIndexDefault: 5,
  pulledCount: 0,
  timer: [],

  // 出したり消したり
  action: function(tid, mode, caller){
    var pulled = $(document.getElementById(tid));
    var caller = $(caller);

    // タイマーがあればクリア
    if(pulldownNavi.timer[tid] !== undefined){
      this.clearTimer(tid);
    }

    // 表示用z-indexをリセット
    if(this.pulledCount === 0 || this.zIndex >= 10000){
      this.zIndex = this.zIndexDefault;
    }

    if(mode == "show"){
      caller.bind("mouseout", {tid:tid, mode:'hide'}, this.setTimer);
      caller.bind("mouseover", {tid:tid}, this.clearTimerEvent);
      pulled.bind("mouseout", {tid:tid, mode:'hide'}, this.setTimer);
      pulled.bind("mouseover", {tid:tid}, this.clearTimerEvent);
      pulled.css("z-index", ++this.zIndex);
      pulled.fadeIn(this.showSpeed);
      this.pulledCount++;
    }else{
      caller.unbind("mouseout", this.setTimer);
      caller.unbind("mouseover", this.setTimer);
      pulled.unbind("mouseout", this.setTimer);
      pulled.unbind("mouseover", this.setTimer);
      pulled.css("z-index", this.zIndexDefault);
      pulled.fadeOut(this.hideSpeed);
      this.pulledCount--;
    }

  },

  /*
   * 以下、タイマーによりグローバル呼び出されるメソッド群
   */

  // 非表示タイマーセット
  setTimer: function(event){
    pulldownNavi.timer[event.data.tid] = setTimeout("pulldownNavi.action('"+event.data.tid+"', '"+event.data.mode+"')", pulldownNavi.hideWait);
  },

  // 非表示タイマーをクリア
  clearTimer: function(tid){
    if(pulldownNavi.timer[tid] !== undefined){
      clearTimeout(pulldownNavi.timer[tid]);
      pulldownNavi.timer[tid] = undefined;
    }
  },

  // 同上 *こちらはjQueryイベントオブジェクトを取得
  clearTimerEvent: function(event){
    pulldownNavi.clearTimer(event.data.tid);
  }
};


/*
 * イベント追加
 */
function addEvent(elm, evType, fn, useCapture) {
  if (elm.addEventListener) {
    elm.addEventListener(evType, fn, useCapture);
    return true;
  }
  else if (elm.attachEvent) {
    var r = elm.attachEvent('on' + evType, fn);
    return r;
  }
  else {
    elm['on' + evType] = fn;
  }
}

/*
 *  クラスで取得
 */
document.getElementsByClassName = function (className) {
  var i, j, eltClass;
  var objAll = document.getElementsByTagName ? document.getElementsByTagName("*") : document.all;
  var objCN = new Array();
  for (i = 0; i < objAll.length; i++) {
    eltClass = objAll[i].className.split(/\s+/);
    for (j = 0; j < eltClass.length; j++) {
      if (eltClass[j] == className) {
        objCN.push(objAll[i]);
        break;
      }
    }
  }
  return objCN;
};


/*
 *  子ノードを削除
 */
function sweepChildren(node){
  var children  = node.childNodes;
  for(var i=0; childNode = children[i]; ){
    node.removeChild(childNode);
  }
}


/*
 * パラメータ取得
 */

function getParams(query){

  if(!query){
    query = location.search;
  }

  var ret = {};
  if(query && query.indexOf('?') === 0){
    query = query.slice(1);
    pairs = query.split('&');
    for(var i=0; pairs[i]; i++){
      var pair = pairs[i].split('=');
      var key = pair[0], value = pair[1];
      if(key !== undefined && value !== undefined){
        ret[key]  = value;
      }
    }
  }
  return ret;
}

/***                          ***/
/*** XMLHttpRequest関連ツール ***/
/***                          ***/

// XMLHttpRequestを作成
function createXMLHttp(){
  return browser.isIE() ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
}


/***                    ***/
/*** ブラウザ関連ツール ***/
/***                    ***/

// ブラウザがIE(7以前)かどうかを判定
var browser = {
  IE: undefined,
  IE7: undefined,
  FF: undefined,

  // ブラウザがIEかどうかを判定
  isIE: function(){
    if(this.IE===undefined){
      this.IE = this.find("msie");
    }
    return this.IE;
  },

  // ブラウザがIE(7以前)かどうかを判定
  isIE7: function(){
    if(this.IE===undefined){
      var re = new RegExp("MSIE [4-7]");
      this.IE7 = this.find("msie") && re.test(navigator.appVersion) ? true : false;
    }
    return this.IE7;
  },

  // FF
  isFF: function(){
    if(this.FF===undefined){
      this.FF = this.find("firefox");
    }
    return this.FF;
  },

  // 小文字で検索
  find: function(str){
    return navigator.userAgent.toLowerCase().indexOf(str) >= 0;
  }
};


/***                          ***/
/*** クリップボード関連ツール ***/
/***                          ***/
addEvent(window, "load", function(){
  var hatePastes  = document.getElementsByClassName("preventPaste");
  for(var i=0; hatePastes[i]; i++){
    addEvent(hatePastes[i], "paste", preventPaste);
  }
});
function preventPaste(e){

  var alertText = "ペーストせず、確認しながら入力してください。";
  if(!browser.isFF()){
    var cb = new clipboard(e);
    var paste = cb.getClipboardData();
    if(paste){
      alert(alertText);
    }
    e.returnValue = false;
  }else{
    alert(alertText);
    e.preventDefault();
  }
  return false;
}

// クリップボードオブジェクト（FF非対応）
var clipboard = function(e){

  // クリップボードオブジェクト
  this.obj  = window.clipboardData ? window.clipboardData : e.clipboardData;

  // mimeタイプ
  this.mime = navigator.userAgent.toLowerCase().indexOf("msie") >= 0 ?
    "Text" : "text/plain";

  // クリップボードデータの取得
  this.getClipboardData = function(){
    return this.obj.getData(this.mime);
  };

  // クリップボードデータの設定
  this.setClipboardData = function(str){
    this.obj.setData(this.mime, str);
  };
};


/*
 *  BLOGで紹介ページ
 */


function changeTextColor(id){

  //var chkObj4 = document.getElementById("bgColor");
  //var vColor = chkObj4.value;


  var IniCol='#'+id.value;
  //var IniCol='#'+vColor;



  var NewCol = ccdlg.chooseColorDlg(IniCol);
  NewCol=NewCol.toString(16);
  NewCol="00000"+NewCol;
  NewCol=NewCol.substring(NewCol.length-6,NewCol.length);
  NewCol=NewCol;

  id.value=NewCol;
//  chkObj4.value =NewCol;

}
function doPreview(){
  changeHtml();
  document.getElementById("customizeform").submit();
}

function changeHtml(){


  var vlinkType = 1;
  var vopenWin = 1;
  var vshowBorder = 1;
  var vlargeImage = 1;
  var vPriceOpts = "";
  var vbgColor = "FFFFFF";
  var vfgColor = "000000";
  var vlinkColor = "0000FF";



  var radioList = document.getElementsByName("linkType");
  var chk = "";
  for(var i=0; i<radioList.length; i++){
    if (radioList[i].checked) {
      chk = radioList[i].value;
      break;
    }
  }
  if (chk == 1){
    vlinkType = 1;
  } else {
    vlinkType = 2;
  }




  var chkObj = document.getElementById("openWin");
  if (chkObj.checked) {
    vopenWin = 1;
  } else {
    vopenWin = "";
  }


  var chkObj2 = document.getElementById("showBorder");
  if (chkObj2.checked) {
    vshowBorder = 1;
  } else {
    vshowBorder = "";
  }

  var chkObj3 = document.getElementById("largeImage");
  if (chkObj3.checked) {
    vlargeImage = 1;
  } else {
    vlargeImage = "";
  }






  //vPriceOpts = document.customizeform.PriceOpts.value;

  var chkObj4 = document.getElementById("bgColor");
  vbgColor = chkObj4.value;

  var chkObj5 = document.getElementById("fgColor");
  vfgColor = chkObj5.value;

  var chkObj6 = document.getElementById("linkColor");
  vlinkColor = chkObj6.value;


  var chkObj7 = document.getElementById("PriceOpts");  
  if (chkObj7.selectedIndex == 1){
    vPriceOpts = "1";
  }

  // article_codeを取得 *2009-10-19 Suzuki
  if(!window._params){
    window._params  = getParams();
  }

  var str = 
'<iframe src="http://www.irhpress.co.jp/userreview/cm.php?article_code='+window._params.article_code+'&amp;linkType='+ vlinkType +'&amp;openWin='+ vopenWin +'&amp;showBorder='+ vshowBorder +'&amp;largeImage='+ vlargeImage +'&amp;PriceOpts='+ vPriceOpts +'&amp;bgColor='+ vbgColor +'&amp;fgColor='+ vfgColor +'&amp;linkColor='+ vlinkColor +'" style="width:120px;height:192px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" ></iframe>';


  var objTextarea = document.getElementById('served_html_box'); 
  objTextarea.value = str; 


}

