/*
	jQuery Snb-Slider v1.0
	Copyright (c) 2011 Hirotaka Otake
	This plugin available for use in all personal or commercial projects under both MIT and GPL licenses.
*/

$(document).ready(function() {
    var interval_id;
    var interval_ms = 5000;
    var slide_count = $("#slide_show ul.slide_content").length;
    var current_index = 0;
    var current_z_index = 1000;
    var ua = navigator.userAgent;
    
    $("#slide_show ul").hide();
        
    $.address.init(function(event) {
        // Initializes the plugin
        $('#slide_navi a').address();
    }).change(function(event) {
        var path = event.pathNames[0];
        if (typeof path == 'undefined' ) {
            selected = $("#slide_show ul.slide_content:eq(0)").attr("id");
        } else {
            selected = path.toLowerCase(); // force to Lower Case
        }
        
        $("#"+selected).hide();
        $('#slide_navi').show();
        auto_slide_show(selected);
    });
    
    function callback_animate() {
        //console.log('callback_animate');
        $("#slide_navi a").unbind("click");
    }
    
    function auto_slide_show(selected) {
        // setting
        var img_opacity = 0.4;          	// 画像の透明度を指定（この透明度から徐々に不透明にしながら表示する）
        var delay_ms = 0;               		// スライドを開始する（duration）の前に間をミリ秒で設定出来る
        var duration_ms = 1200;          // スライドの非表示から表示するのにかかるミリ秒
        var fadeout_ms = duration_ms - 100;
        var easing = "easeInOutCubic";   // animationで使うeasingの設定（easeInExpo、easeInOutExpo、easeInOutCubic）
        var motion_type = "animation";  // 動きの種類 "animation" or "slide" (animationはeasingが使える)
        var direction = ["up","down","left","right"];   // motionがslideの場合にどこからスライドさせるかの方向を決める配列
        
        // 現在のスライドのindexを取得
        $("#slide_show ul.slide_content").each(function(i) {
            if (selected == $(this).attr("id")) {
                current_index = i;
            } else {
                var old_z_index = current_z_index - (i+1);
                $(this).css("z-index", old_z_index);
            }
        });
        
        // 古いスライドの重なり順を後ろにして、フェードアウトしながら消す
        if ($("#slide_show ul.slide_content:visible").length) {

            // スライドナビのaタグを一時的に無効にする
            $("#slide_navi a").bind("click", function(){
                return false;
            });

            var old_id = $("#slide_show ul.slide_content:visible").attr("id");
            if (ua.indexOf("MSIE")>=0) {
                //$("#"+old_id).css({"position":"absolute", "z-index":0}).hide();
                //$("#"+old_id).css({"position":"absolute"}).hide();
                $("#"+old_id).hide();
            } else {
                //$("#"+old_id).css({"position":"absolute", "z-index":0}).fadeOut(fadeout_ms);
                $("#"+old_id).css({"position":"absolute"}).fadeOut(fadeout_ms, callback_animate);
            }
            var mouse_off = $("#navi_"+old_id+" img").attr('src').replace("_o.", "_n.");
            $("#navi_"+old_id+" img").attr('src', mouse_off);
        }
        // 新しいスライドの重なり順を前にして、表示しておく（overflow領域にいるので表示しても見えない）
        $("#"+selected).css({"position":"relative", "z-index":current_z_index}).show();
        var mouse_on = $("#navi_"+selected+" img").attr('src').replace("_n.", "_o.");
        $("#navi_"+selected+" img").attr('src', mouse_on);
        
        // スライドさせる画像をループして処理する
        $("#"+selected+" li img").each(function(i) {
            
            // animation系の動き
            if (motion_type == "animation") {
                var img_width = $(this).width();
                var img_height = $(this).height();
                
                $(this).parent().css({"width": img_width, "height": img_height});
                switch (parseInt(Math.random()*4)+1){
                  case 1:
                    //console.log("up");
                    $(this).css({"left":0, "top":img_height, "opacity":img_opacity});
                    $(this).delay(delay_ms).animate({top: '0', opacity: 1}, duration_ms, easing);
                    break;
                  case 2:
                    //console.log("down");
                    $(this).css({"left":0, "top":img_height*(-1), "opacity":img_opacity});
                    $(this).delay(delay_ms).animate({top: '0', opacity: 1}, duration_ms, easing);
                    break;
                  case 3:
                    //console.log("left");
                    $(this).css({"left":img_width*(-1), "top":0, "opacity":img_opacity});
                    $(this).delay(delay_ms).animate({left: '0', opacity: 1}, duration_ms, easing);
                    break;
                  case 4:
                    //console.log("right");
                    $(this).css({"left":img_width, "top":0, "opacity":img_opacity});
                    $(this).delay(delay_ms).animate({left: '0', opacity: 1}, duration_ms, easing);
                    break;
                }
                
            // ui.slide系の動き
            } else if (motion_type == "slide") {
                $(this).css({"left":0, "top":0}).hide();
                var rand_direction = direction[parseInt(Math.random()*4)];
                //console.log(rand_direction);
                $(this).delay(delay_ms).show("slide", { direction: rand_direction }, duration_ms);
            }
        });
        
        // indexをインクリメントする
        current_index++;
        // indexがスライドの数よりも多い場合は先頭に戻す
        if ((current_index) == slide_count) {
            current_index = 0;
        }
        
        // URLを動的に変更
        //$.address.pathNames();
        
        // indexからスライドのIDを取得する
        selected = $("#slide_show ul.slide_content:eq("+current_index+")").attr("id");
        
        // インターバルを設定
        if(interval_id) clearInterval(interval_id);
        interval_id = setInterval(function(){
            auto_slide_show(selected);
        }, interval_ms);
    }
});
