// page moving 
function gotop() {
  location.href='http://'+location.host;
}


//$(document).ready(getTag);
function getTag() {
  var url ;
  var id = '';
  var status = '';
  urls = '/article/tag/ajax';
  var divtb = document.getElementById('tag_list');
  if (divtb){
    var str = location.pathname;
    var url = /^\/article\/detail\/(\d+)/;
    if (str.match(url)) {
        var m = str.match(url);
        id = m[1];
    }
    url = /^\/(%|[A-Za-z0-9])*\/article\/detail\/(\d+)/;
    if (str.match(url)) {
        var m = str.match(url);
        id = m[2];
    }
    id = 'article_id=' + id;
    id += '&rand=' + Math.random();
    $("#tag_list").load(urls + '?' + id);
  }
}


$(document).ready(show_toolmenu);
function show_toolmenu(){
	// check url
	if (location.pathname.match(/^\/user\/regist/)) {
		return;
	}
	
  if (document.getElementById('toolmenu')){
    var id = '';
    id += '&rand=' + Math.random();
    if ($("#toolmenu") == null) return;
    $("#toolmenu").load("/headermenu/",{method: 'post', parameters: id, asynchronous: 1 });
  }
}


// url utility
function getq(key) {
  var r = '';
  var re = new RegExp('\\?.*'+key+'=([0-9a-z_\\-%]+)', 'gi');
  var m = re.exec(location.href);
  if (m != undefined) {
    if (m.length > 0) {
      r = m[1];
    }
  }
  return r;
}


// パネルのon/off
function togglePanel(id) {
  var d = document.getElementById(id);
  if (d) {
    if (d.style.display=='') {
      d.style.display='none';
    } else {
      d.style.display='';
    }
  }
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}


function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; 
  document.MM_sr=new Array;
  for(i=0;i<(a.length-2);i+=3)
    if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


// デフォルトの前処理
function init() {}


function getaddr(enddom, startdom, user, islink)
{
  var email = user + '@' + startdom + enddom;
  var link = email;
  if (islink)
    link = '<a href="mailto:'+email+'">'+email+'</a>';
  document.write(link);
}

// ログイン
function gologin(cid) {
  if (cid == undefined) {
    cid = 'markezine_regist';
  }
  location.href='https://sems.shoeisha.com/users/login/markezine?cid='+cid+'&ref='+encodeURIComponent(location.href)
}
// 会員登録
function goregist(cid) {
  if (cid == undefined) {
    cid = 'markezine_regist';
  }
  location.href='/user/regist/?cid='+cid+'&ref='+encodeURIComponent(location.href);
}


// SiteCatalyst
// arena: event5, evar14
// hottopics: event5, evar14
function arena_click(o){
  var s=s_gi(s_account);
  s.linkTrackVars='products,eVar14,events';
  s.linkTrackEvents='event5';
  s.events='event5';
  s.eVar14= o;
  s.products = o;
  s.tl(this,'o','Arena Click');
}


function hottopics_click(o){
  var s=s_gi(s_account);
  s.linkTrackVars='products,eVar14,events';
  s.linkTrackEvents='event5';
  s.events='event5';
  s.eVar14= o;
  s.products = o;
  s.tl(this,'o','Hottopics Click');
}


function sumitem_click(o,t){
  var a = new parseUri(o.href);
  var adid = a.itemID(o.href);
  var s=s_gi(s_account);
  s.linkTrackVars='products,eVar15,events';
  s.linkTrackEvents='event4';
  s.events='event4';
  s.eVar15= adid;
  s.products = adid;
  s.tl(this,'o','Ad Click');
}


function partner1_click(o){
  var s=s_gi(s_account);
  s.linkTrackVars='products,eVar16,events';
  s.linkTrackEvents='event9';
  s.events='event9';
  s.eVar16=o;
  s.tl(this,'o','Partner Click');
}


function partner2_click(o){
  var s=s_gi(s_account);
  s.linkTrackVars='products,eVar16,events';
  s.linkTrackEvents='event10';
  s.events='event10';
  s.eVar16=o;
  s.tl(this,'o','Partner Click');
}


function ov_click_count(v1, v2, v3) {
  var s=s_gi(s_account);
  s.linkTrackVars='products,eVar19,eVar20,events';
  s.linkTrackEvents='event4';
  s.events='event4';
  s.products = 'ov;' + v1;
  s.eVar19= v2;
  s.eVar20= v3;
  s.tl(this,'o','Overture Click');
}


//-----------------------------------------------------------------------------
var parseUri =  function (str) {
  this.url = str;
  this.options = {
    strictMode: false,
    key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
    q:   {
      name:   "queryKey",
      parser: /(?:^|&)([^&=]*)=?([^&]*)/g
    },
    parser: {
      strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
      loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
    }
  }
  
  this.parse = function(){
    var o   = this.options,
      m   = o.parser[o.strictMode ? "strict" : "loose"].exec(this.url),
      uri = {},
      i   = 14;

    while (i--) uri[o.key[i]] = m[i] || "";

    uri[o.q.name] = {};
    uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
      if ($1) uri[o.q.name][$1] = $2;
    });

    return uri;
  }
  
  this.hier = function(strD){
    var u = strD;
    var c = u.split('/');
    c[0] = 'home';
    for(i = 0; i < c.length; i++){
     if(c[i].length == 0){
      c.splice(i,1);
     }
    }
    if (c[c.length - 1].match(/[^0-9]/g)) {
    }else{
      c.splice(c.length - 1,1);
    }
    return c.join(':');
    
  }

  this.type = function(strD){
    var u = strD;
    var c = u.split('/');
    c[0] = 'home';
    for(i = 0; i < c.length; i++){
     if(c[i].length == 0){
      c.splice(i,1);
     }
    }
    return 
    
  }

  
  this.itemID = function(strD){
    var u = strD;
    var c = u.split('/');
    c.reverse();
    for(i = 0; i < c.length; i++){
     if(c[i].length == 0){
      c.splice(i,1);
     }
    }
    if (c[0].match(/[^0-9]/g)) {
      return '';
      }else{
      return c[0];
    }
  }
  
};


function goprint() {
  var mode = getq('mode');
  var u = location.pathname;
  if (mode != 'print') {
    u = location.pathname + '?mode=print';
  }
  location.href = u;
}


// ユーザ取得
function sc_user(){
  var jx=jQuery.ajax({
    url: "/api/user",
    dataType: "json",
    async: false
   }).responseText;
  var result = eval('(' + jx + ')');
  return result.shopper_id;
}
// リファラードメイン取得
function sc_referrer(){
  if(document.referrer.replace(/http[s]?:\/\/([^\/]+?)\/.*/i,"$1") == location.hostname){
    return "";
  }
  return document.referrer.replace(/http[s]?:\/\/([^\/]+?)\/.*/i,"$1");
}


// ---- SiteCatalyt Click Events ----//

// <a href="#" 
//    ref="sc" 
//    scCategory="AreaTest" 
//    scVars="14" 
//    scVals="Data" 
//    scEvents="5"  
//    scProducts="%TITLE%" 
//    scLabel="test">Test</a>
// 
//    
//    If you'd like to omit value of attributes 
//    in scProducts,scLabel,scCategory and scVals , 
//    you can set the following value to the attributes.
//    
//    %TITLE% = this anchor text
//    %LOC% = location.href(page url)
//    %HREF% = this anchor href
//    

$(function(){
  if ($("a") == null) return;
  $("a").click(function () {
    try {
      if (s_account == undefined) return;
    } catch(e) { return; }

    var ref = $(this).attr("ref");
    if (typeof(ref) == 'undefined' || ref != 'sc') return;
    
    // event
    var events = $(this).attr("scEvents");
    if(typeof(events) == 'undefined'){
        events = '';
    } else {
      events = events.split(',');
    }

    // category
    var category = $(this).attr("scCategory");
    if(typeof(category) == 'undefined'){
        category = '';
    } 
    category = rep_val(category, $(this));
    
    // products
    var products = $(this).attr("scProducts");
    if(typeof(products) == 'undefined'){
        products = $(this).text();
    }
    products = rep_val(products, $(this));

    // eVar
    var eVars = $(this).attr("scVars");
    if(typeof(eVars) == 'undefined'){
        eVars = '';
    } else {
      eVars = eVars.split(',');
    }
    
    // eVal
    var eVals = $(this).attr("scVals");
    if(typeof(eVals) == 'undefined'){
        eVals = '';
    } else {
      eVals = eVals.split(',');
    }
    
    // LABEL
    var linkname = $(this).attr("scLabel");
    if(typeof(linkname) == 'undefined'){
        linkname = $(this).text();
    }
    linkname = rep_val(linkname, $(this));
    
    var s=s_gi(s_account);

    // Set linkTrackVars
    var linkTrackVars = '';
    for ( var i=0; i < eVars.length  ; i++  ){
      linkTrackVars +=  "eVar" + eVars[i];
      if (i != eVars.length -1 ){
         linkTrackVars += ",";
      }
    }
    if (products){
       if (linkTrackVars){
          linkTrackVars += ",products";
       } else {
          linkTrackVars += "products";
       }
    }
    if (events.length > 0){
       if (linkTrackVars){
          linkTrackVars += ",events";
       } else {
          linkTrackVars += "events";
       }
    }
    s.linkTrackVars = linkTrackVars  ;
    
    // Set linkTrackEvents
    var linkTrackEvents = '';
    for ( var i=0; i < events.length  ; i++  ){
      linkTrackEvents +=  "event" + events[i];
      if (i != eVars.length -1 ){
         linkTrackVars += ",";
      }
    }
    s.linkTrackEvents = linkTrackEvents;
    // Set events
    s.events = linkTrackEvents;
    // Set products
    s.products = category + ";" + products;
    
    // Set eVar99
    if ( eVars && eVals ){
       for ( var i=0; i < eVars.length  ; i++  ){
            eVals[i] = rep_val(eVals[i], $(this));
            eval( "s.eVar" + eVars[i] + "='" + eVals[i] + "'");
       }
       
    }
    
    s.tl(this,'o',linkname);
  });
  
  function rep_val(val, elem) {
    if (val) {
      val = val.replace('%TITLE%', elem.text(), 'g');
      val = val.replace('%HREF%', elem.attr('href'), 'g');
      val = val.replace('%LOC%', location.href, 'g');
    }
    return val;
  }
});

