
//イメージロードプラグイン
//imagesLoaded
$.fn.imagesLoaded = function(callback){
  var elems = this.filter('img'),
      len = elems.length,
      blank = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
      
  elems.bind('load.imgloaded',function(){
      if (--len <= 0 && this.src !== blank){
        elems.unbind('load.imgloaded');
        callback.call(elems,this);
      }
  }).each(function(){
     if (this.complete || this.complete === undefined){
        var src = this.src;
        this.src = blank;
        this.src = src;
     }
  });

  return this;
};

// shuffle()
(function($){
  $.fn.shuffle = function() {
    return this.each(function(){
      var items = $(this).children();
      return (items.length)
        ? $(this).html($.shuffle(items))
        : this;
    });
  }
 
  $.shuffle = function(arr) {
    for(
      var j, x, i = arr.length; i;
      j = parseInt(Math.random() * i),
      x = arr[--i], arr[i] = arr[j], arr[j] = x
    );
    return arr;
  }
})(jQuery);


// 画像サイズ調整
// imgResize();
function imgResize(s){
var img = s;
var rw = 132;
var rh = 201;
var w = img.width();
var h = img.height();
if(img.complete == false){ // 画像の読み込みが終わってないなら0.1秒後にやり直し。
	setTimeout(arguments.callee, 100);
	return;
}
		
if( rw <= w && rh <= h || rw > w && rh > h ) { //縦横両方大きい　もしくは 両方小さい
		
	if(w > h){ //横長
		var hiritsu	= rh / h; // 縮小率
		var afterWidth = w * hiritsu; // リサイズ後の横幅
 			
		if(rw > afterWidth){ //オーバーリサイズされる場合
			img.width(rw);
		} else {
			img.height(rh);
			//中央揃え
			var x = (afterWidth - rw) / 2;
 			img.css('margin-left',-x);
		}
	} else { //縦長
		var hiritsu	= rw / w; // 縮小率
		var hiritsu2 = rh / h; // 縮小率
		var afterHeight = h * hiritsu; // 縮小後の高さ
		var afterWidth = w * hiritsu2; // 縮小後の高さ
 			
		if(rh > afterHeight){ //オーバーリサイズされる場合
			img.height(rh);
			//中央揃え
			var x = (afterWidth - rw) / 2;
 			img.css('margin-left',-x);
		} else {
			img.width(rw);
		}
	}
 		
} else if( rw < w && rh > h ) { //横幅だけ大きい
	
		var hiritsu	= rh / h; // 縮小率
		var afterWidth = w * hiritsu; // リサイズ後の横幅
	img.height(rh);
	//中央揃え
	var x = (afterWidth - rw) / 2;
		img.css('margin-left',-x);
		
	} else if( rw > w && rh < h ) { //高さだけ大きい
	
		img.width(rw);
		
	} else { //イレギュラー
	
		img.height(201);
		img.width(132);
		
	}
}


$(function(){

	//
	// スライド
	//
	$('#skitter div.box_skitter').skitter({interval: 10000,animation: 'blindWidth',label: false,navigation: false,
		animateNumberOut: '',
		animateNumberOver: '',
		animateNumberActive: ''
	});
	$('#skitter div.box_skitter,#skitter div.container_skitter').css({'width': 700, 'height': 250});
	$('#skitter span.info_slide').css({'left':0,'top':225,'width': 700, 'height': '25px'});
	
    
    //
    // ショップニュース
    //
	$('#shop-news ul li:odd').addClass('odd');
	
	
	//
	// 新着商品
	//
	$('#item-list ul.tab li').click(function(){
		over_remove(); // _over状態ものを元に戻す
		var postfix = '_over';
		var img = $('img',this);
		var src = img.attr('src');
		var src_on = src.substr(0, src.lastIndexOf('.')) + postfix + src.substring(src.lastIndexOf('.'));
		img.attr('src', src_on);
	});
	$('#sort-all img').attr('src', 'common/img/top/tabl4_over.png'); // 初期tab
		
	// _over状態ものを元に戻す
	function over_remove(){
		$('#item-list ul.tab li').each(function(){
			var imgs = $('img',this);
			var src = imgs.attr('src');
			var src = src.replace(/_over./, ".");
			imgs.attr('src', src);
		});
	}
	
	
	var items = $('#sort-boxs > li');
	var itemsLink = $('#sort-boxs > li > a');
	
	// レストラン カフェ	
	$('#sort-cafe').click(function(){
		items.each(function(){
			if($(this).hasClass('cafe')){
				if(! $(this).is(':visible')){
					$(this).delay(1000).fadeIn(1000);
				}
			}else {
				$(this).fadeOut(1000);			
			}
		});
		listItemOver(itemsLink);
	});
	
	// 本館	
	$('#sort-main').click(function(){
		items.each(function(){
			if($(this).hasClass('main')){
				if(! $(this).is(':visible')){
					$(this).delay(1000).fadeIn(1000);		
				}
			}else {
				$(this).fadeOut(1000);			
			}
		});
		listItemOver(itemsLink);
	});
	
	// プラザ館
	$('#sort-plaza').click(function(){
		items.each(function(){
			if($(this).hasClass('plaza')){
				if(! $(this).is(':visible')){
					$(this).delay(1000).fadeIn(1000);	
				}
			}else {
				$(this).fadeOut(1000);			
			}
		});
		listItemOver(itemsLink);
	});
	
	// すべて表示
	$('#sort-all').click(function(){
		items.each(function(){
			if(! $(this).is(':visible')){
				$(this).delay(100).fadeIn(1000);	
			}
		});
		listItemOver(itemsLink);
	});
	
	
	// 要素を10秒ごとにシャッフル
	var sortBox = $('#sort-boxs');
	setInterval(function(){
		sortBox.fadeTo(500,0.0,function(){
			sortBox.shuffle().fadeTo(500,1);
			listItemOver(itemsLink);
		});
	},10000);
	function listItemOver(target){
		target.hover(function(){
			$(this).fadeTo(0,0.6);
		},function(){
			$(this).fadeTo(0,1);
		});
	}
	listItemOver(itemsLink);
	listItemOver($('#campaign_bana li'));
	listItemOver($('#event li a'));
	
	


    //
    // ミオからのお知らせ
    //
	$('#topics ul li:odd').addClass('odd');
	
	

	// 新着商品の高さ合わせ
	function equalHeight(group) {  
        tallest = 0;  
        group.each(function() {  
            thisHeight = $(this).height();  
            if(thisHeight > tallest){  
                tallest = thisHeight;  
            }
        });  
        group.height(tallest);
        
        // 親ボックスの大きさを調整
        parentheight = tallest * 2 + 20;
       	sortBox.height(parentheight);
    }
    equalHeight(items);

	// イベントの高さ合わせ
    function equalHeight2(group) {  
        tallest = 0;  
        group.each(function() {  
            thisHeight = $(this).height();  
            if(thisHeight > tallest){  
                tallest = thisHeight;  
            }
        });  
        group.height(tallest);
    }
    
	var eventImg = $("#event > ul > li");
    $("img",eventImg).imagesLoaded(function(){
    	equalHeight2(eventImg);
    });
   
    
    // 新着商品の画像をリサイズ
    $("img",items).each(function(){
    	$(this).imagesLoaded(function(){
    		imgResize($(this));
    	});
 	});
  
	
});
