﻿
$(function () { init(); });

function init() {



    var float_id = '';

    var showFloatItem = function (triggerObj, targetObj) {
        var left = triggerObj.className == 'photo' ? -45 : -45;
        var top = triggerObj.className == 'photo' ? -90 : -70;

        var offset = $(triggerObj).offset();
        var diff = ($(window).width() + $(window).scrollLeft()) - (offset.left + $(targetObj).width() + left + 30);
        if (diff < 0) { left += diff; }
        diff = offset.left - $(window).scrollLeft() + (left - 5);
        if (diff < 0) { left -= diff; }
        diff = offset.top - $(window).scrollTop() + (top - 10);
        if (diff < 0) { top -= diff; }
        diff = ($(window).height() + $(window).scrollTop()) - (offset.top + $(targetObj).height() + top + 30);
        if (diff < 0) { top += diff; }
        $(targetObj).css({ "left": left + "px", "top": top + "px" });

        $('.float_parent').removeClass("z100");
        $(triggerObj).closest("li").addClass("z100");

        float_id = $(targetObj).get(0).id;

        $('.float_item:not(#' + float_id + ')').hide();
        $("#" + float_id).fadeIn(200, function () {
            $('.float_item:not(#' + float_id + ')').hide();
        });

    };


    //mousestop関数------------------------------------------
    $.mousestopDelay = 50;

    // special event        
    $.event.special.mousestop = {
        setup: function (data) {
            $(this).data('mousestop', { delay: data })
                .bind('mouseenter.mousestop', mouseenterHandler)
                .bind('mouseleave.mousestop', mouseleaveHandler);
        }, teardown: function () {
            $(this).removeData('mousestop')
            .unbind('.mousestop');
        }
    };

    // private methods    
    function mouseenterHandler() {
        if (typeof this.timeout === 'undefined') {
            this.timeout = null;
        }
        var elem = $(this),
data = elem.data('mousestop'),
delay = data.delay || $.mousestopDelay;


        elem.bind('mousemove.mousestop', function () {
            clearTimeout(this.timeout);
            this.timeout = setTimeout(function () {
                elem.trigger('mousestop');
            }, delay);
        });
    };
    function mouseleaveHandler() {
        var elem = $(this); elem.unbind('mousemove.mousestop');
        clearTimeout(this.timeout);
    };

    // shorthand alias   
    $.fn.mousestop = function (data, fn) {
        if (fn == null) {
            fn = data;
            data = null;
        }
        return arguments.length > 0 ? this.bind('mousestop', data, fn) : this.trigger('mousestop');
    };
    //mousestop関数------------------------------------------
    


    if (isSmartPhoneOriPad) {
        //スマホの場合
        $('.photo, .re_photo').each(function (i, sp) {
            $(sp).bind("click", function (e) {
                showFloatItem(e.currentTarget, $(e.currentTarget).next("div").get(0));
                e.preventDefault();
            });
        });
        $('.close_link').bind('click', function () {
            $('.float_item').hide();
        });
    }
    else {

        
        //PCブラウザの場合
        //-------------------------------------------------
        $('.float_item').hover(
            function () {
            },
            function () {
                $(this).hide(); //float_itemを非表示に設定
            }
        );

        //マウスが停止したら処理する
        $('.photo').mousestop(200,function () {
            showFloatItem($(this), $(this).next("div").get(0)); //対象のfloat_itemを表示
        });
        //-------------------------------------------------
    }
}


function loadFileList(approot, shop_id) {
    
    $.ajaxSetup({ cache: false });

    var jogai = ""; //表示除外リストを作る場合はこちら

    offset = $("#hdn_offset").val();
    rowcount = 30; //表示件数

    close_link = "";
    if (isSmartPhoneOriPad) { close_link = '<div class="close_link">閉じる</div>'; }

    //ポップアップのフォーマット
    var fmt_li = '<li class="float_parent" style="display:none;">'
    +'<div class="photo"><img src="{0}"></div>'
    + '<div class="float_item" id="float_item_{6}" style="display: none;">'

    +close_link
    
    
    + '<div class="photo_big"><a href="{1}"><img src="{0}" /></a></div>'
    + '<div class="overbox"></div>'
    + '<div class="title">{2}</div>'
    + '<div class="area">{3}</div>'
    + '<div class="name wordBreak">{4}</div>'
    + '<div class="profile">{5}</div>'
    + '<div class="underbox"></div>'
    +'</div></li>';

    //$.post(approot + '/shop/ajax/diary_list.ashx' + jogai
    $.post('../../shop/ajax/diary_list.ashx' + jogai
    , { shop_id: shop_id, fmt: fmt_li, row: 10, offset: offset, rowcount: rowcount }
    , function (data, status, XHR) {
        //ULに追加
        $('#new_list').append(data);

        //フェードインで表示する   
        $('#new_list > li:hidden').fadeIn(800);


        init(); //イベント再設定
        $("#hdn_offset").val(parseInt(offset) + rowcount);

        //ＭＡＸになったら「もっと見る」ボタンは消す

        max = parseInt($("#hdn_max").val());
        if (parseInt(offset) + rowcount >= max) {
            $('#more').hide();
        }
        $('#tatamu').show();
    }

    );
   
}

//たたむ
function closeFileList() {
    $('#tatamu').hide();
    $('#new_list>li').each(function (index, obj) {
        if (index >= 10) {
            $(obj).remove();
        }
    });
    $("#hdn_offset").val(12);
 }

