<!-- javascript for tt @johnny -->

// 設定
var tt_sw = 0; // ttが有効か否か
var tt_flg = 1; // ttが開いてるか否か
var disp_obj = new Array(); // このページで表示されるTTのデータが入る
var disp_id = 0; // 現在表示されてるTT
var bi_obj = new Array();
var cover_flag = 0;

var tt_set = {
  path : "/kyoto/tool_tip/",
  img : {
    tab_up : "tab_up.gif",
    tab_up_ro : "tab_up_o.gif",
    tab_down : "tab_down.gif",
    tab_down_ro : "tab_down_o.gif",
    btn_help : "btn_help.gif",
    btn_help_ro : "btn_help_o.gif",
    btn_on : "btn_on.gif",
    btn_on_ro : "btn_on_o.gif",
    btn_off : "btn_off.gif",
    btn_off_ro : "btn_off_o.gif"
  }
}

Array.prototype.shuffle = function(){
  var i = this.length;
  while(i){
    var j = Math.floor(Math.random() * i);
    var t = this[--i];
    this[i] = this[j];
    this[j] = t;
  }
  return this;
}

$(function(){

  $.each($.browser, function(k, v){
    if(k == "version"){
      bi_obj["v"] = v;
    }else{
      if(v == true){
        bi_obj["k"] = k;
      }
    }
  });

  if(bi_obj["k"] != "msie" || bi_obj["v"] >= 6){
    $("body").append("<div id=\"site_concierge_wrap_\" class=\"c-fix\"><div id=\"site_concierge_wrap\"><div id=\"site_concierge\"></div></div></div>");
    //$.getJSON("/kyoto/tool_tip/tt_data/data.php?url="+window.location.pathname, function(json){
    $.ajax({
      url : "/kyoto/tool_tip/tt_data/data.php?url="+window.location.pathname,
      async : false,
      dataType : "json",
      success : function(json){
        $.each(json, function(key){
          $.each(this, function(key2){
            this.shuffle();
            $.each(this, function(key3){
              this.title = get_response_text(this.title);
              this.data = get_response_text(this.data);
              disp_obj.push(this);
            });
          });
        });
        $("#site_concierge").setTemplateURL(tt_set.path + "/tt_tpl/tt.tpl", null, {filter_data : false} );
        $("#site_concierge").processTemplate(disp_obj[0]);
        tt_change(disp_id);
      }
    });
    //});
  }

});

function tt_change(dispid){
  $("#site_concierge .entry-text").setTemplateURL(tt_set.path + "/tt_tpl/data.tpl", null, {filter_data : false});
  $("#site_concierge .entry-text").processTemplate(disp_obj[dispid]);
  $("#site_concierge .entry-text .photo").find("img").each(function(){
    var mt_img_str = $(this).attr("src");
    if(!mt_img_str){
        $(this).remove();
    }
  });
  $("#site_concierge .entry-text .link").each(function(){
    var mt_link = $(this).attr("href");
    var mt_target = $(this).attr("target");
    if(mt_link){
      if(/#/.test(mt_link)){
        mt_link = mt_link.replace('#', '?tt#');
        $(this).attr("href", mt_link);
      }else{
        mt_link = mt_link + "?tt";
        $(this).attr("href", mt_link);
      }
    }
    if(mt_target == 2){
      $(this).attr("target", "_blank");
    }else{
      $(this).removeAttr("target");
    }
  });
  //$('#content_cover').css("height", $('#site_concierge .content').height());
  $("#site_concierge .entry-text").fadeIn('fast');
}

function tt_open(){
  if(!tt_flg){
    $("#site_concierge .content").slideDown("normal");
    $("#content_cover").slideDown("normal");
    $("#tt_id_tab").attr("src",tt_set.path + "img/" + tt_set.img.tab_down);
    $("#tt_id_tab").mouseover(function(){$("#tt_id_tab").attr("src",tt_set.path + "img/" + tt_set.img.tab_down_ro)});
    $("#tt_id_tab").mouseout(function(){$("#tt_id_tab").attr("src",tt_set.path + "img/" + tt_set.img.tab_down)});
    tt_flg = 1;
  }
}

function tt_close(){
  if(tt_flg){
    $("#site_concierge .content").slideUp("normal");
    $("#content_cover").slideUp("normal");
    $("#tt_id_tab").attr("src",tt_set.path + "img/" + tt_set.img.tab_up);
    $("#tt_id_tab").mouseover(function(){$("#tt_id_tab").attr("src",tt_set.path + "img/" + tt_set.img.tab_up_ro)});
    $("#tt_id_tab").mouseout(function(){$("#tt_id_tab").attr("src",tt_set.path + "img/" + tt_set.img.tab_up)});
    tt_flg = 0;
  }
}

function tt_tgl(){
  if(tt_sw){
    tt_flg ? tt_close() : tt_open();
  }
}

function tt_next(){
  if(disp_id + 1 < disp_obj.length){
    disp_id++;
    $("#site_concierge .entry-text").fadeOut("fast");
    tt_change(disp_id);
  }
}

function tt_back() {
  if(disp_id > 0){
    disp_id--;
    $("#site_concierge .entry-text").fadeOut("fast");
    tt_change(disp_id);
  }
}

function tt_help(){
  alert("help click");
}

function tt_on(){
  $("#tt_onoff_button").mouseover(function(){$("#tt_onoff_button").attr("src",tt_set.path + "img/" + tt_set.img.btn_off_ro)});
  $("#tt_onoff_button").mouseout(function(){$("#tt_onoff_button").attr("src",tt_set.path + "img/" + tt_set.img.btn_off)});
  if(!tt_sw){
    tt_open();
  }
  tt_sw = 1;
}

function tt_off(){
  if(tt_sw){
    tt_close();
  }
  $("#tt_onoff_button").mouseover(function(){$("#tt_onoff_button").attr("src",tt_set.path + "img/" + tt_set.img.btn_on_ro)});
  $("#tt_onoff_button").mouseout(function(){$("#tt_onoff_button").attr("src",tt_set.path + "img/" + tt_set.img.btn_on)});
  tt_sw = 0;
}

function tt_sw_tgl(){
  if(!tt_sw){
    tt_sw = 1;
    $("#site_concierge .box-content .entry-text").fadeTo(3000, 1.0, tt_close);
    $('#content_cover').css("height", $('#site_concierge .content').height());
  }
}

function tt_init_start(){
  if(!cover_flag && bi_obj["k"] == "msie" && bi_obj["v"] == 6){
    $("#site_concierge_wrap").prepend("<div id=\"cover\"><div id=\"cover_area\"><iframe src=\"javascript:false;\" id=\"tab_cover\"></iframe><iframe src=\"javascript:false;\" id=\"content_cover\"></iframe></div></div>");
    cover_flag = 1;
  }
  $("#tt_id_tab").attr("src",tt_set.path + "img/" + tt_set.img.tab_down);
  $("#tt_id_tab").mouseover(function(){$("#tt_id_tab").attr("src",tt_set.path + "img/" + tt_set.img.tab_down_ro)});
  $("#tt_id_tab").mouseout(function(){$("#tt_id_tab").attr("src",tt_set.path + "img/" + tt_set.img.tab_down)});
  $("#site_concierge #tab_area").click(tt_tgl);
  $("#site_concierge #tt_prev").click(tt_back);
  $("#site_concierge #tt_next").click(tt_next);
  tt_sw_tgl();
}

function get_response_text(req){
  var text = req;
  if(navigator.appVersion.indexOf( "KHTML" ) > -1){
    var esc = escape(text);
    if(esc.indexOf("%u") < 0 && esc.indexOf("%") > -1){
      text = decodeURIComponent(esc);
    }
  }
  return text;
}
