/**
 * @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");
	  });
	  
	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
	var footCatArr = [
		"#hotItemArea li.mens",
		"#hotItemArea li.womens",
		"#hotItemArea li.style"
	];
	for(var i=0; i<footCatArr.length; i++) {
		var rand = Math.floor(Math.random()*6);
		$(footCatArr[i]).each(function(index){
			if(index == rand) {
				$(this).show().addClass("show");
			}
		});
	}
	$(".show:last").addClass("last");

/* SideBtArea */
	$(".base")
		.append("<ul id='SideBtArea'><li class='icon'><a href='http://twitter.com/UnitedArrows' target='_blank'><img src='/common/imgs/base/side-bt-twitter.gif' width='23' height='23'></a></li><li class='icon'><a href='http://www.facebook.com/UNITEDARROWS.jp' target='_blank'><img src='/common/imgs/base/side-bt-facebook.gif' width='23' height='23'></a></li><li><a href='/news/atom.xml'><img src='/common/imgs/base/side-bt-rss.gif' width='23' height='23'></a></li></ul>")
			.find("#SideBtArea")
				.find("img")
					.easyOver()
				.end()
			.end()
		.end();

 });
})(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="imghover" 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="235" height="25" 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)
							.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;
}
