/*-------------------------------------------------------------
 common
-------------------------------------------------------------*/
// var url = location.href;
// var uid = new Date().getTime();
// var isSSL = (location.protocol == "https:") ? true : false;
var hash = location.hash.split('#')[1];
var pid = $('body').attr('id');
var toRoot = './';

/*-------------------------------------------------------------
 ready?                                    required "jquery.js"
-------------------------------------------------------------*/
$(function(){
  getDepth();
  pid = $('body').attr('id');
  switch(pid){
    case 'style':
      linktoPopup('#collection','640','650','yes');
      break;
    case 'lineup':
      showInfo('#items');
      linktoPopup('#items','640','650','yes');
      break;
    case 'shoplist':
      linktoPopup('#area-menu','596','400','yes');
      break;
    case 'cbuk':
      showInfo('#items');
      linktoPopup('#items','640','550','yes');
      break;
    case 'popup':
      addImageChanger('#photo','#color');
      addImageChanger2('#photo','a.material-zoom');
      addCloseBtn();
      break;
    default:
      break;
  }

});

/*-------------------------------------------------------------
 get depth                                 required "jquery.js"
-------------------------------------------------------------*/
function getDepth(){
  var depth = $('body').attr('class').replace('level','');
  if(depth != 1) toRoot = '';
  var i = 1;
  while(i < depth){
    toRoot += "../";
    i++;
  }
}

/*-------------------------------------------------------------
 set Flash content            required "AC_RunActiveContent.js"
-------------------------------------------------------------*/
function setContent(item, arguments){
  var isPlayable = DetectFlashVer(9,0,0);
  if(isPlayable){
    var arg = (arguments) ? arguments : '';
    AC_FL_RunContent(
      'src','./_swf/'+item,
      'movie','./_swf/'+item,
      'width','1000',
      'height', 535,
      'FlashVars', arg,
      'quality','best',
      'play','true',
      'base','.',
      'bgcolor','#ffffff',
      'wmode','transparent',
      'allowScriptAccess','always',
      'pluginspage','http://www.macromedia.com/go/getflashplayer',
      'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0'
    );
  } else {
    //location.href = toRoot + "noflash.html";
    document.write('<p class="requiredFP">当サイトはAdobe Flashを使用しています。<br /><a href="http://get.adobe.com/jp/flashplayer/" target="_blank">こちら</a>より最新のAdobe Flash Playerをダウンロードしてください。</p>');
  }
}

/*-------------------------------------------------------------
 open Detail (from FLASH)                                      
-------------------------------------------------------------*/
function openDtl(img,color){
  var path = './item-detail/' + img + '.html';
  if(color) path = path + '#' + color;
  var conf  = 'width=640,height=520,menubar=no,toolbar=no,location=no,status=no,resizable=no,scrollbars=yes';
  window.open(path, '', conf);
}

/*-------------------------------------------------------------
 get recently news              required "jquery.js","jfeed.js"
-------------------------------------------------------------*/
function getRecentlyNews(articleNum){
  jQuery.getFeed({url:toRoot+'_common/_php/proxy.php',success:onComplete});
  function onComplete(feed) {
    var news = '<ul id="news-list">';
    for(var i=0; i<articleNum; i++) {
      var item = feed.items[i];
      var title = item.title;
      if(title.length >= 20) title = title.substr(0,20);
      news += '<li>'+title;
      news += '　<a href="' + item.link + '" target="_blank">&raquo; more</a>';
      news += '</li>';
    }
    news += '</ul>';
    $('#news>h2').after(news);
    }
}

/*-------------------------------------------------------------
 link to popup                             required "jquery.js"
-------------------------------------------------------------*/
function linktoPopup(target, w, h, scr){
  if(!scr) var scr = 'no';
  var conf  = 'width=' + w + ',height=' + h + ',menubar=no,toolbar=no,location=no,status=no,resizable=no,scrollbars=' + scr;
  var t = $(target).find('a')
          .attr('title','クリックでポップアップウィンドウを開きます')
          .click(function(){
             window.open(this.href, '', conf);
             return false;
           });
}

/*-------------------------------------------------------------
 show info                                 required "jquery.js"
-------------------------------------------------------------*/
function showInfo(target){
  var target = $(target).find('a');
  target.hover(function(){
    var info = $(this).find('span.data');
    var h = info.height();
    if(h > 30)
      info.css('margin-top',(h+6)*-1+'px');
    else
      info.css('margin-top','-36px');
  });
}

/*-------------------------------------------------------------
 init image set                            required "jquery.js"
-------------------------------------------------------------*/
function initImageSet(target){
  var itemNo = $('#name-and-price>h1').text();
  var defImg = target.attr('src');
  var chgImg = './_img/' + itemNo + '_' + hash + '.jpg';
  target.attr('src',chgImg)
  setTimeout(function(){if(target.height() < 294) target.attr('src',defImg);},1200);
}

/*-------------------------------------------------------------
 image changer                             required "jquery.js"
-------------------------------------------------------------*/
function addImageChanger(target,triggers){
  var target = $(target);
  var triggers = $(triggers).find('a');
  var images = new Array();
  for(var i=0; i<triggers.length; i++){
    images[i] = new Image();
    images[i].src = triggers[i];
  }

  triggers
   .attr('title','クリックで商品写真を変更します')
   .click(function(){
     target.attr('src',this.href);
     return false;
  });

  if(hash) initImageSet(target);
}

function addImageChanger2(target,trigger){
  var target = $(target);
  var images = new Array();
  images[0] = new Image();
  images[0].src = $(trigger).attr('href');
  $(trigger).click(function(){
    target.attr('src',this.href);
    return false;
  });
}

/*-------------------------------------------------------------
 add closebtn                              required "jquery.js"
-------------------------------------------------------------*/
function addCloseBtn(){
  var ins = $('<img src="./_img/btn-close.png" />')
            .attr({title:'ウィンドウを閉じる',alt:'close'})
            .click(function(){window.close();})
  $("#close-btn").append(ins);
}

/*-------------------------------------------------------------
 ie6 fix                                   required "jquery.js"
-------------------------------------------------------------*/
function iePngFix(){
  if(jQuery.browser.msie && jQuery.browser.version < 7) {
		//action for IE6
  }
}