//#imageViewスライドショー
function slideSwitch(){

	$active = $("#imageView img.active");
	$current = $(".mainVisual ol li.current");
	if($active.length == 0){
		$active = $("#imageView img:last");
	}
	if($current.length == 0){
		$current = $(".mainVisual ol li:last");
	}
	
	$next = $active.next().length ? $active.next() : $("#imageView img:first");
	$nextIcon = $current.next().length ? $current.next() : $(".mainVisual ol li:first");
	
	$active.addClass("lastActive");
	
	$next.css({opacity: 0}).addClass("active").animate({opacity: 1}, 4000, function(){
		$active.removeClass("active lastActive");
	});
	$nextIcon.addClass("current");
	$current.removeClass("current");
}

// #brandlist

function detailSlide() {
	
	var detailHeight = [];
    var brandHeight = [];
    var openFlag = false;
	var closeFlag = false;
    var loadingImage = null;
    
    init();
    
    // 初期設定
    function init() {
        
        $("#brandlist .brandsColumn .brand .detail").each(function(i){
            detailHeight[i] = $(this).height();
            $(this).css("visibility", "visible").hide();
        });

        $("#brandlist .brandsColumn .brand").each(function(i){
            brandHeight[i] = $(this).height();
        });

        // 一覧 margin調整、マウスオーバー、カレント
        $("#brandlist .brandsColumn .brand:last-child").css("margin-right", "0");
        $("#brandlist .brandsColumn .brand a.thumb img").before("<span></span>");
        $("#brandlist .brandsColumn .brand a.thumb span").css("opacity", "0.4");
        $("#brandlist .brandsColumn .brand.current a.thumb span").css("opacity", "0");
        if($("div:animated").length == 0 ){
            $("#brandlist .brandsColumn .brand a.thumb").hover(function(){
                if(!openFlag && !closeFlag){
                    $("span", $(this)).css("opacity", "0");
                }
            },
            function(){
                if(!openFlag && !closeFlag){
                    if($(this).siblings(".detail").css("display") == "block"){
                        if(closeFlag){
                            $("span", $(this)).css("opacity", "0.4");
                        } else{
                            $("span", $(this)).css("opacity", "0");
                        }
                    } else{
                        if(openFlag){
                            $("span", $(this)).css("opacity", "0");
                        } else{
                            $("span", $(this)).css("opacity", "0.4");
                        }
                    }
                }
            });
        }
        
        // 詳細 開閉処理（ブランドサムネイルクリック処理）
        $("#brandlist .brandsColumn .brand a.thumb").each(function(i){
            $(this).click(function(){
                if($("div:animated, html:animated, body:animated").length == 0){
                    if($("#brandlist .brandsColumn .brand .detail:visible").length == 0){
                        //開いてる.detailがない場合
                        prepareDetailContent(i, function(){
                           openDetail(true, i); 
                        });
                    } else{
                        if($(this).siblings(".detail").css("display") == "block"){
                            //自分の.detailが開いている場合
                            closeDetail(false, false, i);
                        } else{
                            $("#brandlist .brandsColumn .brand .detail").each(function(index){
                                if($(this).css("display") == "block"){
                                    closeDetail(true, i, index);
                                }
                            });
                        }
                    }
                    return false;
                }
            });
        });
    }
	
	// 詳細 開く
	function openDetail(scrollFlag, openNum){
		openFlag = true;
		if(scrollFlag){
			targetScroll(openNum, function(){
				$("#brandlist .brandsColumn .brand .detail").eq(openNum).slideDown(800).parent(".brand").addClass("current").animate({height : detailHeight[openNum] + brandHeight[openNum]}, 800, function(){
					openFlag = false;
				});
			});
		}
	}
	
	// 詳細 閉じる
	function closeDetail(scrollFlag, openNum, closeNum){
		closeFlag = true;
		resetFront();
		$("#brandlist .brandsColumn .brand .detail").eq(closeNum).slideUp(1200).parent(".brand").stop().animate({height : brandHeight[closeNum]}, 1200, function(){
			$("#brandlist .brandsColumn .brand").removeClass("current");
			$(this).find("a.thumb span").css("opacity", "0.4");
            if(scrollFlag){
				targetScroll(openNum, function(){
                    $("#brandlist .brandsColumn .brand .detail").eq(openNum).slideDown(800).parent(".brand").addClass("current").animate({height : detailHeight[openNum] + brandHeight[openNum]}, 800);
				});
                if(openNum !== false){
                    prepareDetailContent(openNum, function(){
                        openDetail(true, openNum); 
                    });
                }
			}
            
			closeFlag = false;
		});
	}
	
	// スクロール
	function targetScroll(openNum, callback){
		// openNumの場所までスクロール
		var openNumTop = $("#brandlist .brandsColumn .brand a.thumb").eq(openNum).offset().top;
		$("html, body").animate({scrollTop : openNumTop}, 1200, function(){
			//スクロールが終わったら、callback実行
			callback();
		});
	}
	
    // リセット
	function resetFront(){
		$("ul.largeView").each(function(){
			$(this).find("li:not(:first)").removeClass("front");
			$(this).find("li:first").addClass("front");
			$(this).parents(".detail").find("a.prev").hide();
			if($(this).find("li").length != 1){
				$(this).parents(".detail").find("a.next").show();
			}
		});
		$(".photo p").each(function(){
			$(this).replaceWith($(this).parents(".detail").find("ul.thumb li:eq(0) p").clone());
		});
	}
    
	// 詳細 拡大イメージ切り替え操作
	function productView() {
        // 詳細 拡大イメージデフォルト
        $("ul.largeView").each(function(){
            if($(this).find("li").length == 1){
                $(this).parents(".detail").find("a.next").hide();
            }
        });
        var visibleDetail = $("#brandlist .detail:visible");

		$("#brandlist .detail ul.thumb li a").hover(function(){
			$(this).css("opacity", "0.7");
		},
		function(){
			$(this).css("opacity", "1");
		});
		
		$("#brandlist .detail").each(function(){
			// サムネクリック
			$("ul.thumb li", $(this)).each(function(n){
				$(this).click(function(){
					if($("li:animated").length == 0){
					
						var visibleDetail = $("#brandlist .detail:visible");
						
						if($("ul.largeView li", visibleDetail).length != 0){
							var frontIndex = $("ul.largeView li.front", visibleDetail).index();
							if(n != frontIndex){
								$("ul.largeView li", visibleDetail).eq(n).addClass("acc");
								$("ul.largeView li.front", visibleDetail).fadeOut(800, function(){
									$(this).removeClass("front").show();
									$(this).siblings("li.acc").removeClass("acc").addClass("front");
								});
								$(".photo p", visibleDetail).replaceWith($("ul.thumb li", visibleDetail).eq(n).find("p").clone());
							}
						
							if(n == 0){
								$("a.prev", visibleDetail).hide();
							} else{
								$("a.prev", visibleDetail).show();
							}
							
							if(n == $("ul.thumb li", visibleDetail).length-1){
								$("a.next", visibleDetail).hide();
							} else{
								$("a.next", visibleDetail).show();
							}
						}
						
					}
				});
			});
			
			// Prevボタン
			$("a.prev", $(this)).click(function(){
				if($("li:animated").length == 0){
				
					var visibleDetail = $("#brandlist .detail:visible");
					var frontIndex = $("ul.largeView li.front", visibleDetail).index();
					$("ul.largeView li", visibleDetail).eq(frontIndex-1).addClass("acc");
					$("ul.largeView li.front", visibleDetail).fadeOut(800, function(){
						$(this).removeClass("front").show();
						$(this).siblings("li.acc").removeClass("acc").addClass("front");
					});
					$(".photo p", visibleDetail).replaceWith($("ul.thumb li", visibleDetail).eq(frontIndex-1).find("p").clone());
					
					$(this).siblings("a.next").show();
					if(frontIndex == 1){
						$(this).hide();
					} else{
						$(this).show();
					}
				
				}
			});
			
			// Nextボタン
			$("a.next", $(this)).click(function(){
				if($("li:animated").length == 0){
				
					var visibleDetail = $("#brandlist .detail:visible");
					var frontIndex = $("ul.largeView li.front", visibleDetail).index();
					$("ul.largeView li", visibleDetail).eq(frontIndex+1).addClass("acc");
					$("ul.largeView li.front", visibleDetail).fadeOut(800, function(){
						$(this).removeClass("front").show();
						$(this).siblings("li.acc").removeClass("acc").addClass("front");
					});
					$(".photo p", visibleDetail).replaceWith($("ul.thumb li", visibleDetail).eq(frontIndex+1).find("p").clone());
	
					$(this).siblings("a.prev").show();
					if(frontIndex == $("ul.thumb li", visibleDetail).length - 2){
						$(this).hide();
					} else{
						$(this).show();
					}
				
				}
			});
		});
	}
	
    // 詳細 閉じるボタンアクション
    function bindCloseButtonAction(target, index) {
        $(target).bind('click', function(){
            if($("div:animated").length == 0){
                closeDetail(false, false, index);
            }
        });
    }
    
    // 詳細HTML表示の前処理
    function prepareDetailContent(index, callback) {
        
        var brandContent = $("#brandlist .brandsColumn .brand").eq(index);
        var detailContent = $(brandContent).find(".detail");
        var post_name = $(brandContent).find("a").attr("post_name");
        if ($(detailContent).html() == "") {
            $.ajax({
                type: "post",
                url: location.href,
                data: "brandlist=" + post_name,
                success: function(result){

                    // 詳細HTMLの生成
                    $(detailContent).html(result);

                    // 初期設定
                    resetFront();
                    productView();
                    
                    // 閉じるボタンの処理
                    var button = $(detailContent).find(".close a");
                    bindCloseButtonAction(button, index);

                    // 高さのセット
                    detailHeight[index] = $(detailContent).height();
                    $(detailContent).css("visibility", "visible").hide();
                    brandHeight[index] = $(brandContent).height();

                    // コールバック
                    callback();
                },
                error: function(e){
                    console.log(e);
                }
            });
        } else {
            // コールバック
            callback();
        }
    }
}
