/**
 * @name main.js
 * @fileOverview
 * @version 1.1
 * @description
 * <p>(c) United Arrows Ltd. Licensed <a href="http://ja.wikipedia.org/wiki/GNU_General_Public_License">GNU General Public License</a>.</p>
 */

//jQuery.noConflict();

/**
 * config
 */
(function($){
$(function() {
//bodyのクラスにブラウザ情報を追加
$.addClassUA();
//easyOverのターゲット設定
$("img.ahover, .ahoverArea img").easyOver();
//IE5,6にてPNG有効化
//$.autoEnablePNG();
  //HTML5タグ有効化
  $.enableHTML5();
//ポップアップリンクに置換
$(".commonPop").easyPop();
//アンカーリンクをスムージング
$("a[href^=#]").smoothScroll();
//heightline
   $(".separator").each(function(){
    $(this).heightline(".box");
  });

//スマートフォンのPC切り替えリンクの書き換えとCOOKIEの発行
if($('#DisplayArea')){
	var $PcLink = $('#DisplayArea a');
	var $urlpc = $(location).attr('href').slice($(location).attr('href').indexOf('jewelchanges.jp/sp')+18);

	$PcLink.removeAttr('href').attr('href',$urlpc);

	$PcLink.click(function(){
		document.cookie = "showPC=1;path=/";
	});
}


var _tabArr = [];
var _currentNum = -1;
var _currentTab;
function _tabClicked(num) {
if(_currentTab) {
$(_currentTab).parent().removeClass("open");
_currentTab.$block.hide(0.3*1000);
}
var nextTab = _tabArr[num];
if(_currentTab==nextTab) {
_currentTab = null;
} else {
if(nextTab) nextTab.$block.show(0.3*1000);
_currentTab = nextTab;
$(_currentTab).parent().addClass("open");
}
}
$("#sNav .storeNav li.l2>a").each( function(index){
var $this = $(this);
_tabArr.push(this);
this.num = index;
this.$parent = $this.parent();
this.$block = $("ul", this.$parent);
$this.click(function(){
_tabClicked(this.num);
return false;
});

//選択中の子がいたら開く
if(this.$parent.find( "a.on:first" ).size() > 0 ) _tabClicked(this.num);
});
//未選択の場合は一番上（東京）を開く
if(!_currentTab) _tabClicked(0);




//api用に現在のURLを取得
$("#mixi a").attr("data-url" , location.href);


//append radius
$('.radius02').each(function(){
  $(this)
  .css('position','relative')
  .append('<div class="radiusImg tl"><img src="/common/imgs/base/radius02-tl.png"></div>')
  .append('<div class="radiusImg tr"><img src="/common/imgs/base/radius02-tr.png"></div>')
  .append('<div class="radiusImg bl"><img src="/common/imgs/base/radius02-bl.png"></div>')
  .append('<div class="radiusImg br"><img src="/common/imgs/base/radius02-br.png"></div>')
  .find('div.radiusImg')
  .css('position','absolute')
  .end()
  .find('div.tl')
  .css('top','0px')
  .css('left','0px')
  .find('img').css('vertical-align','top').end()
  .end()
  .find('div.tr')
  .css('top','0px')
  .css('right','0px')
  .find('img').css('vertical-align','top').end()
  .end()
  .find('div.bl')
  .css('bottom','0px')
  .css('left','0px')
  .find('img').css('vertical-align','bottom').end()
  .end()
  .find('div.br')
  .css('bottom','0px')
  .css('right','0px')
  .find('img').css('vertical-align','bottom').end()
  .end()
  .end();
  $.autoEnablePNG();
});

//footer hot items
var n = $("#f-hotitems li").length;
var arr = [];
for(var i=0; i<n; i++){ arr[i] = i; }
var shuffledarr = [];
var randomIndex;
for(var i=0; i<n; i++){
  randomIndex = Math.floor(Math.random()*arr.length);
  shuffledarr[i] = arr[randomIndex];
arr.splice(randomIndex, 1);
}
for(var i=0; i<7; i++){
$("#f-hotitems li").eq(shuffledarr[i]).addClass('show');
}
$("#f-hotitems li:not(.show)").remove();


 });
})(jQuery);


/** olsAPI連携
*
**/
function  api(){
var param_url = "/common/js/proxy.php";
var param_api_url = "http://store.united-arrows.co.jp/fd/getitem.html";
var param_api_list_url = "http://store.united-arrows.co.jp/fd/getlist.html";
var param_type = "GET";
var param_dataType = "json";

/**
* gidを指定して商品を1件取得
* @param gid:商品ID
* @param cb:コールバック定義
*/
this.getItemFromId = function( id , cb ){

var self = arguments.callee;
self.callBack = function( data ){

if(data != null && data.items.length > 0) {
var items = data.items;

var img = '<dl class="clear"><dt><a class="imghover" href="' + items[0].url + '" target="_blank"><img src="'+ items[0].pic + '?w=80&h=96"></a></dt>';
var spec = '<dd><p>品番：' + items[0].serial + "<br/>カラー：" + items[0].colors + '<br/>サイズ：' + items[0].sizes + '</p>';
var price = '<p class="price">&yen;' + addFigure(items[0].price) + '(税込)</p></dd></dl>';
var btn = '<div class="buy"><a href="' + items[0].url + '" target="_blank"><img src="/imgs/hotitems/bt-store.gif" alt="最新の在庫チェック / オンライン購入 "></a></div>';
$( "#storeItemArea" ).html( img + '<div class="desc">' + spec + price + btn +'</div>');
$( "#storeItemArea" ).find(".ahover").easyOver();
$(".checkLink").html('※品切れの場合がございますので、在庫については各店舗へお問合せいただくか、<a href="'+ items[0].url +'" target="_blank">こちらより在庫をご確認ください。</a>');

}else{
$( "#storeItemArea" ).parent().remove();
}
}
if( cb ){
self.callBack = cb; 
}
param = {
url: param_url ,
type: param_type ,
dataType: param_dataType ,
data: {
url: param_api_url + "?gids=" + id
}
,success: self.callBack
}
$.ajax( param );
}

/**
* パラメータを複数指定して商品リストを取得
* @param params:パラメータ
* @param cb:コールバック定義
* @param cnt:取得件数
*/
this.getItemList = function( params , cb , cnt){

var self = arguments.callee;
var c = 5; //デフォルト取得件数
if( cnt ){
c = cnt; 
}
var param_c = "&count="+c;
self.callBack = function( data ){
if(data != null && data.items.length > 0) {
var items = data.items;
//var html = '<ul class="imghoverArea">';
var $ul = $("<ul/>");
$( ".relatedList" ).html( $ul );
$ul.css("display","none");
$ul.addClass("imghoverArea");

for(var i = 0; i < items.length; i++) {
var $div = $("<div/>");
$div
.addClass("img")
.append("<a/>")
.find("a:last")
.attr("href", items[i].url)
.append('<img src="'+ items[i].pic + '?w=110&h=132" alt="">');
var $title = $("<p/>");
$title
.addClass("title")
.html(items[i].name);
var $price = $("<p/>");
$price.html('¥' + addFigure( items[i].price ));
var $li = $("<li/>");
$ul.append($li);
$li
.append($div)
.append($title)
.append($price);
}
$(".relatedList li").last().addClass("last");
$ul.fadeIn();
}else{
$( ".relatedList" ).parent().remove();
}
}
if( cb ){
self.callBack = cb; 
}

param = {
url: param_url ,
cache: false,
type: param_type ,
dataType: param_dataType ,
data: {
url: param_api_list_url + "?" + params + param_c
}
,success: self.callBack
}
$.ajax( param );
} 

}

//carouselBox高さ調整
function carouselBoxHeightSet(){
if( $(".carouselBoxArea").length > 0 ){
$(".carouselBoxArea").each( function(){
own = $(this);
var c_height = 0;
own.find( "li" ).each( function(){
c_height = Math.max( c_height, $(this).height() );
});
c_height+=20;
own.css("height" , c_height+"px");
own.find(".carouselBoxWrapper,.carouselBox,.caroufredsel_wrapper,#carouselBoxAreaOthers").css( "height" , c_height+"px" );
own.find("li").css( "height" , c_height+"px" );
});
}
}

//otherBox高さ調整
function othersBoxHeightSet(){
if( $(".carouselBoxArea").length > 0 ){
$(".carouselBoxArea").each( function(){
own = $(this);
var c_height = 0;
own.find( "li" ).each( function(){
c_height = Math.max( 0, 190 );
});
c_height+=0;
own.css("height" , c_height+"px");
own.find(".carouselBoxWrapper,.carouselBox,.caroufredsel_wrapper,#carouselBoxAreaOthers").css( "height" , c_height+"px" );
own.find("li").css( "height" , c_height+"px" );
});
}
}

//sotherBox高さ調整
function recommendBoxHeightSet(){
if( $("#carouselBoxRecommendSection .carouselBoxArea").length > 0 ){
$("#carouselBoxRecommendSection .carouselBoxArea").each( function(){
own = $(this);
var c_height = 0;
own.find( "li" ).each( function(){
c_height = Math.max( 0, 136 );
});
c_height+=0;
own.css("height" , c_height+"px");
own.find(".carouselBoxWrapper,.carouselBox,.caroufredsel_wrapper,#carouselBoxAreaOthers").css( "height" , c_height+"px" );
own.find("li").css( "height" , c_height+"px" );
});
}
}

//sotherBox高さ調整
function rankingBoxHeightSet(){
if( $("#carouselBoxRankingSection .carouselBoxArea").length > 0 ){
$("#carouselBoxRankingSection .carouselBoxArea").each( function(){
own = $(this);
var c_height = 0;
own.find( "li" ).each( function(){
c_height = Math.max( 0, 136 );
});
c_height+=0;
own.css("height" , c_height+"px");
own.find(".carouselBoxWrapper,.carouselBox,.caroufredsel_wrapper,#carouselBoxAreaOthers").css( "height" , c_height+"px" );
own.find("li").css( "height" , c_height+"px" );
});
}
}

//arrivalsBox高さ調整
function arrivalsBoxHeightSet(){
if( $("#carouselBoxNewSection .carouselBoxArea").length > 0 ){
$("#carouselBoxNewSection .carouselBoxArea").each( function(){
own = $(this);
var c_height = 0;
own.find( "li" ).each( function(){
c_height = Math.max( 0, 90 );
});
c_height+=0;
own.css("height" , c_height+"px");
own.find(".carouselBoxWrapper,.carouselBox,.caroufredsel_wrapper,#carouselBoxNew").css( "height" , c_height+"px" );
own.find("li").css( "height" , c_height+"px" );
});
}
}



//rotherBox高さ調整
function rothersBoxHeightSet(){
if( $("#carouselBoxDetailSection .carouselBoxArea").length > 0 ){
$("#carouselBoxDetailSection .carouselBoxArea").each( function(){
own = $(this);
var c_height = 0;
own.find( "li" ).each( function(){
c_height = Math.max( 0, 90 );
});
c_height+=0;
own.css("height" , c_height+"px");
own.find(".carouselBoxWrapper,.carouselBox,.caroufredsel_wrapper,#carouselBoxAreaOthers").css( "height" , c_height+"px" );
own.find("li").css( "height" , c_height+"px" );
});
}
}



//金額表示　３桁毎に,を挿入
function addFigure(str) {
　var num = new String(str).replace(/,/g, "");
　while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
　return num;
}

