/* ------------------------------------------------------------ */
/*                                                              */
/*                         global param                         */
/*                                                              */
/* ------------------------------------------------------------ */

var _loc  = location.pathname;
var _path = _loc.substring(0,_loc.lastIndexOf('/')+1);
var _lv1path = _path.split('/')[1];
var _lv2path = _path.split('/')[2];
var _lv3path = _path.split('/')[3];
var _pwd  = _path.split('/')[_path.split('/').length-2];
var _html = _loc.substring(_loc.lastIndexOf('/')+1);
var _swfAry   = new Array();
var isIE  = /*@cc_on!@*/0;
var isIE6 = isIE && !window.XMLHttpRequest;
var $j = jQuery.noConflict();





/* ------------------------------------------------------------ */
/*                                                              */
/*                       import libraries                       */
/*                                                              */
/* ------------------------------------------------------------ */

document.write('<script type="text/javascript" src="/_common/js/lib/jquery.easing.js"></script>');
document.write('<script type="text/javascript" src="/_common/js/lib/jquery.preload.min.js"></script>');
document.write('<script type="text/javascript" src="/_common/js/lib/jquery.jcarousellite.min.js"></script>');
document.write('<script type="text/javascript" src="/_common/js/lib/colorbox/jquery.colorbox.js"></script>');
document.write('<link rel="stylesheet" type="text/css" href="/_common/js/lib/colorbox/colorbox.css" media="screen" />');
document.write('<script type="text/javascript" src="/_common/js/lib/jquery.flatheights.js"></script>');

// PNGFIX --------------------------------------------------------
if(isIE6) {
  document.write('<style type="text/css">#header .shop, #header h1, #header-category, #nav-local li, #nav-tabs li, .list-index li a.item, a.item img, .list-banner li img, dl.list-awards dt, .list-promo h2, .list-promo h2 span, #breadcrumbs li, #breadcrumbs li.brand a, .caution, #home-recommend, #home-outlet, #home-mail { behavior:url(/_common/js/lib/iepngfix/iepngfix.htc) }</style>');
  document.write('<script type="text/javascript" src="/_common/js/lib/iepngfix/iepngfix_tilebg.js"></script>');
  try {document.execCommand('BackgroundImageCache', false, true);} catch(e) {}  // ie6 background-image reload issue
}





/* ------------------------------------------------------------ */
/*                                                              */
/*                      jQuery initialize                       */
/*                                                              */
/* ------------------------------------------------------------ */

$j(function($){

  // API:faq | accessory | related ------------------------------------------------------------
  if(_lv3path!='') {
    $('a[href=#tab-support]').one('click',function(){
      $('#tab-support #faq').load('/_api/faq/'+_pwd,function(){
        $(this).find('.list-q a').click(function(){
          var target = $(this).attr('href');
          $(target).doScroll();
        });
        $('.list-qa dd.back a').click(function(){
          $('#faq').doScroll();
        });
        if ($('#tab-support #faq').children().size()<1 && $('#tab-support #doc').size()<1) {
          $('#tab-support #faq').html('<p class="center mB30">※現在、サポート情報はありません。</p>');
        }
      });
    });
    $('#footer #promo-accessory').load('/_api/accessory/'+_pwd,function(){
      var earpad = $(this).find('a.item[href$="_earpad/"]');
      earpad.attr('href',earpad.attr('href')+'#item_'+_pwd);
    });
    $('#footer #promo-related').load('/_api/related/'+_pwd);
  }

  // #nav-index -------------------------------------------
  $('#nav-index li a').click(function(){
    var target = $(this).attr('href');
    $(target).doScroll();
  });

  // #nav-main -------------------------------------------
  $('#nav-main li a[href*=/' + _path.split('/')[2] + '/]').addClass('on');
//  $('#nav-main li a').blend({speed:350,opacity:1});
  
  // #nav-local ------------------------------------------------------------
  $('#nav-local ul').clone(true).prependTo('#nav-footer');
  $('#nav-tabs ul').clone(true).prependTo('#nav-footer');
  $('#nav-footer ul a.br').each(function(){
    $(this).html($(this).text());
  });

  // breadcrumbs ------------------------------------------------------------
  $('body[id!="shop"] #breadcrumbs').breadcrumbs();

  // #nav-gallery ------------------------------------------------------------
  $('#nav-gallery:not(.top) li:first-child').css('marginLeft',(48*((3-$('#nav-gallery li').size()%3)%3)+1)+'px');

  // [ Utility ] : img hover : toggle -------------------------------------------
  $('.imghover, .list-banner').imghover();
  $('.imgtoggle').imgtoggle();

  // [ Utility ] : .first,.last --------------------------------------------------------
  $('li:first-child, dt:first-child, dd:nth-child(2), div > .column:first').addClass('first');
  $('li:last-child, dt:last-child, dd:nth-child(2), div > .column:last').addClass('last');
  $('#content ul,#content ol,.stripe,.item').each(function(i) {
    $('>li:even,>tbody>tr:even',this).addClass('even');
    $('>li:odd,>tbody>tr:odd',this).addClass('odd');
  });

  // [ Utility ] : dl.column ------------------------------------------------------------
  $('dl.column,dl.columnR').each(function(i) {
    var dtWidth = [];
    $(this).children('dt').each(function(j) {
      dtWidth.push($(this).width()+5);
    });
    var max = Math.max.apply(null,dtWidth);
    $(this).children('dt').width(max);
    var marginLR = ($(this).hasClass('columnR')) ? 'marginRight' : 'marginLeft';
    $(this).children('dd').css(marginLR,max+10);
  });

  // [ Utility ] : window -----------------------------------------------------
  $('a.blank, area.blank, a.pdf').click(function(){ window.open(this.href, null,'scrollbars=yes,resizable=yes,status=yes,directories=yes,menubar=yes,toolbar=yes'); return false; });
  $('.close a, a.close').click(function(){ window.close(); return false; });

  // colorbox ------------------------------------------------------------
  $('.colorbox').colorbox();
  $('.colorbox_youtube').colorbox({iframe:true, innerWidth:640, innerHeight:360});
  $('.colorbox_movie').colorbox({iframe:true, innerWidth:640, innerHeight:525});
  $('.colorbox_iframe').colorbox({iframe:true, width:"80%", height:"80%"});
  $('.colorbox_inline').colorbox({iframe:true, width:"80%", href:"#modal"});

  // tabs --------------------------------------------------------
  $('div.tabs').tabs();
  $('div.tabs a.awards_badge').click(function(){
    $('#nav-local a[href="#tab-awards"]').click();
  });
  
  // HERO Rotation ------------------------------------------------------------
  $('#hero .list-hero-trio').doRotate();

  // HERO area map ------------------------------------------------------------
  $('#m_hero area').hover(function(){
    var img = $(this).attr('href').split('#');
    $('#hero .hero img').attr('src','img/'+img[1]+'.jpg');
  });
  
  // Gallery Title ------------------------------------------------------------
  $('.list-gallery li').each(function(){
    var title = $('a',this).attr('title');
    $(this).append(title);
  });

  // flatheight ------------------------------------------------------------
  $(window).load(function() {
    $('.flatheight').each(function(i) {
      $(this).children('li:not(.append), section').flatHeights();
      $(this).find('.flatheight_target').flatHeights();
    });
  });
  
});





/* ------------------------------------------------------------ */
/*                                                              */
/*                         iPad issue                           */
/*                                                              */
/* ------------------------------------------------------------ */
// http://dev.jquery.com/ticket/6446
// remove once it's fixed
if ( /webkit.*mobile/i.test(navigator.userAgent)) {
  (function($) {
      $.fn.offsetOld = $.fn.offset;
      $.fn.offset = function() {
        var result = this.offsetOld();
        result.top -= window.scrollY;
        result.left -= window.scrollX;
        return result;
      };
  })(jQuery);
}





/* ------------------------------------------------------------ */
/*                                                              */
/*                        breadcrumbs                           */
/*                                                              */
/* ------------------------------------------------------------ */
(function($){
  $.fn.breadcrumbs = function() {
    var lv1label={'hk':'harman/kardon','marklv':'Mark Levinson','revel':'REVEL AUDIO','crystal':'Crystal Cable'};
    var lv2label={'hifi':'HiFiスピーカー', 'mm':'マルチメディアスピーカー', 'home':'ホームシステム', 'headphone':'ヘッドホン', 'car':'カーオーディオ', 'accessory':'アクセサリ', 'hifiphone':'HiFiヘッドホン', 'miniphone':'ミニヘッドホン', 'canalphone':'カナルタイプイヤホン', 'inearphone':'インイヤーホン', 'ncphone':'ノイズキャンセルヘッドホン', 'mm':'マルチメディアスピーカー', 'preamp':'プリアンプ', 'poweramp':'パワーアンプ', 'player':'プレイヤー', 'processor':'AVプロセッサー', 'turntable':'ターンテーブル', 'tonearm':'トーンアーム', 'connect':'インターコネクトケーブル', 'speaker':'スピーカーケーブル', 'digital':'デジタルケーブル', 'phono':'フォノケーブル', 'firewire':'Firewireケーブル', 'hdmi':'HDMIケーブル', 'video':'AVケーブル', 'about':'ブランドについて'};
    var lv1 = ((typeof lv1label[_lv1path]=='undefined') ? _lv1path.toUpperCase():lv1label[_lv1path]);
    var lv2 = ((typeof lv2label[_lv2path]=='undefined') ? '':lv2label[_lv2path]);
    var crumbs = '';
    if (lv1 != '') crumbs += '<li class="brand"><a href="/'+_lv1path+'/" title="'+lv1+'">'+lv1+'</a></li>';
    if (lv2 != '' && (_lv3path != '' || (_html !='index.html' && _html != ''))) crumbs += '<li><a href="/'+_lv1path+'/'+_lv2path+'/">'+ lv2 +'</a></li>';
    crumbs += ( '<li class="here"><span title="このページ">' + $('title').html().split('|')[0] + '</span></li>');
    $(this).prepend('<ol>'+crumbs+'</ol>');
  }
})(jQuery);





/* ------------------------------------------------------------ */
/*                                                              */
/*                           tabs                               */
/*                                                              */
/* ------------------------------------------------------------ */
(function($){
  $.fn.tabs = function() {
    return this.each(function(nav) {
      var node = $(this);
      var nav  = $('#nav-local,#nav-tabs,#nav-tabs2,#nav-footer');
      var intro = $('ul li:first-child a',nav).attr('href').split('#tab-')[1];
      var defaultTab = (location.search) ? location.search.split('?')[1] : intro;
      node.children().hide();
      node.children('#tab-'+defaultTab).show();
      if (defaultTab=='support') {
        $('#nav-local a[href=#tab-support]').click();
      }
      nav.find('li').removeClass('on');
      nav.find('li:has(a[href="#tab-'+defaultTab+'"])').addClass('on');
      nav.find('li a[href*="#tab"]').click(function() {
        if (!$(this).parent('li').is('.on')) {
          var re = /([_\-\w]+$)/i;
          var target = '#'+re.exec(this.href)[1];
          if ($(target).size() > 0) {
//            node.find('div[id^=tab-]:visible').fadeOut('fast',function(){$(target).fadeIn('fast');});
            node.find('div[id^=tab-]:visible').hide();
            $(target).show();
            nav.find('li').removeClass('on');
            nav.find('li a[href='+target+']').parent('li').addClass('on');
            if ($(this).parents('div').attr('id') == 'nav-footer') {
              $('#header').doScroll();
            }
          }
          return false;
        }
      });
    });
  }
})(jQuery);





/* ------------------------------------------------------------ */
/*                                                              */
/*                          doScroll                            */
/*                                                              */
/* ------------------------------------------------------------ */
(function($){
  $.fn.extend({
    doScroll: function() {
      $('html,body').animate({scrollTop: $(this).offset().top}, 500);
    }
  });
})(jQuery);





/* ------------------------------------------------------------ */
/*                                                              */
/*                          doRatate                            */
/*                                                              */
/* ------------------------------------------------------------ */
(function($){
  $.fn.extend({
    doRotate: function() {
      var main = $(this).prev();
      var btnGoAry = new Array();
      for (var i=1;i<=(main.find('li').size());i++) {
        btnGoAry.push('#ahero'+i);
        $(this).append('<a id="ahero'+i+'"></a>');
      }
      main.jCarouselLite({
        speed: 600,
        circular: false,
        visible: 1,
        vertical: 1,
        easing: 'easeOutCubic',
        btnGo: btnGoAry
      });
      $(this).jCarouselLite({
        mouseWheel: 0,
        auto: 7000,
        speed: 600,
        circular: true,
        visible: 3,
        start: 1,
        hoverPause: true,
        beforeStart: function(obj) {
          target = (obj.index()==1) ? $('#ahero'+btnGoAry.length) : $('#ahero'+(obj.index()-1)) ;
//          target = (obj.index()>2) ? $('#ahero'+(obj.index()-2)) : $('#ahero'+(btnGoAry.length-obj.index()%2));
          target.click();
        }
      });
    }
  });
})(jQuery);





/* ------------------------------------------------------------ */
/*                                                              */
/*                       image switcher                         */
/*            [ filename.ext <-> filename_on.ext ]              */
/*                                                              */
/* ------------------------------------------------------------ */
(function($){
  $.fn.extend({
    imghover: function() {
      return this.each(function() {
        var node = $(this);
        if (!node.is('img')&&!node.is(':image')){
          node.find('img,:image').imghover();
          return;
        }
        node.hover(function() {
          node.addClass('hover');
          if (!node.hasClass('on')) $.fn.imgOn(node);
        },function() {
          node.removeClass('hover');
          if (!node.hasClass('on')) $.fn.imgOff(node);
        });
      });
    },
    imgtoggle: function() {
      return this.each(function() {
        var node = $(this);
        if (!node.is('img')&&!node.is(':image')){
          node.find('img,:image').imgtoggle();
          return;
        }
        node.click(function() {
          if (node.hasClass('on')) {
            $.fn.imgOff(node);
          } else {
            if (!node.hasClass('hover')) $.fn.imgOn(node);
          }
          node.toggleClass('on');
        });
      });
    },
    imgOn: function(node) {
      if(node.attr('src').match(/^(.+)_on(\.[a-z]+)$/)) return;
      if(isIE6 && node.context.style.filter) {
        node.context.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+node.context.style.filter.split('"')[1].replace(/^(.+)(¥.[a-z]+)$/,'$1_on$2')+'",sizingMethod="scale")';
      } else {
        node.attr('src',node.attr('src').replace(/^(.+)(\.[a-z]+)$/,'$1_on$2'));
      }
    },
    imgOff: function(node) {
      if(isIE6 && node.context.style.filter) {
        node.context.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+node.context.style.filter.split('"')[1].replace(/^(.+)_on(¥.[a-z]+)$/,'$1$2')+'",sizingMethod="scale")';
      } else {
        node.attr('src',node.attr('src').replace(/^(.+)_on(\.[a-z]+)$/,'$1$2'));
      }
    }
  });
})(jQuery);





/* ------------------------------------------------------------ */
/*                                                              */
/*                      Flash  -  SWFObject                     */
/*                                                              */
/* ------------------------------------------------------------ */
// <div id="swf1Holder"></div>
// <script type="text/javascript">_swf({type:'promo'});</script>
// <script type="text/javascript">_swf({w:'940',h:'210',id:'swf2',swf:'/_common/swf/feature.swf',param:'_v=Fp&flag=1',bgcolor:'#990000'});</script>

function _swf(obj) {
  if (typeof obj == 'undefined') return null;
  if (_swfAry.toString().indexOf(obj.type)>-1) return null;

  var expressInstall = '/_common/swf/expressInstall.swf';
  var flvars = {};
  var params = {};
  var attrib = {};

  switch (obj.type) {
    case 'feature':
      prm    = {w:'100%',h:'500',id:'swf1',swf:'/_common/swf/feature.swf'};
      flvars = {_f:_fileName};
      params = {wmode:'window',bgcolor:'#009900',scale:'showAll'};
      break;
    default:
      prm    = obj;
      if (prm.param) { var pAry=prm.param.split('&'); for (var i=0;i<pAry.length;i++) {var a=pAry[i].split('='); flvars[a[0]] = a[1];} };
      params = {bgcolor:obj.bgcolor,scale:obj.scale,salign:obj.salign,wmode:obj.wmode};
  }
  if (typeof prm == 'undefined') return null;

  /* --- [4] swf version --- */
  prm.v = (obj.v) ? obj.v : '9.0.28';
  if(!swfobject.hasFlashPlayerVersion(prm.v)){
  }

  /* --- [6] flvars --- */
  flvars._p = _path;
  if (location.search) { var sAry=location.search.substr(1).split('&'); for (var i=0;i<sAry.length;i++) {var a=sAry[i].split('='); flvars[a[0]] = a[1];} };

  /* --- [7] params --- */
  params.bgcolor = (params.bgcolor) ? params.bgcolor : '#ffffff';
  params.scale   = (params.scale)   ? params.scale :   'noScale';
  params.salign  = (params.salign)  ? params.salign :  'TL';
  params.wmode   = (params.wmode)   ? params.wmode :  'transparent';
  params.allowFullScreen = 'true';

  /* --- [8] attrib --- */
  attrib.id = prm.id;

  swfobject.embedSWF(prm.swf, prm.id+'Holder', prm.w, prm.h, prm.v, expressInstall, flvars, params, attrib);
}

