/**
 * @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();
 
	//sNav
	$("#sNav").each(function(){
		$(this).find("li.l1:first").addClass("first-child");
	});
 	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);
	
	//fadehover
	$("a.fadehover img, .fadehoverArea a img").stop().live('mouseover',
		function(){ $(this).stop().animate({"opacity":0.7}, 300); }
	);
	$("a.fadehover img, .fadehoverArea a img").stop().live('mouseleave',
		function(){ $(this).stop().animate({"opacity":1}, 300); }
	);
	
	//api用に現在のURLを取得
	$("#mixi a").attr("data-url" , location.href);

	//footer hot items
	var n = $("#hotItemArea ul 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++){
		$("#hotItemArea ul li").eq(shuffledarr[i]).addClass('show');
	}
	$("#hotItemArea ul 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 = '<p class="img"><a class="fadehover" href=' + items[0].url + '><img src="'+ items[0].pic + '?w=80&h=96"></a></p>';
				var spec = '<p>品番：' + items[0].serial + "<br/>カラー：" + items[0].colors + '<br/>サイズ：' + items[0].sizes + '</p>';
				var price = '<p class="price">&yen;' + addFigure(items[0].price) + '(税込)</p>';
				var btn = '<p><a href="' + items[0].url + '" ><img src="/imgs/hotitems/bt-store.gif" alt="最新の在庫チェック / オンライン購入 " width="226" height="28" class="ahover"></a></p>';
				$( "#storeItemArea" ).html( img + '<div class="desc">' + spec + price + btn +'</div>');
				$( "#storeItemArea" ).find(".ahover").easyOver();
				$(".checkLink").html('※品切れの場合がございますので、在庫については各店舗へお問合せいただくか、<a href="'+ items[0].url +'" >こちらより在庫をご確認ください。</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)
							.attr("target", "_brank")
							.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").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;
}

