﻿function fixImgList() {
    $(this).data("curImg", -1);
 var mw = $(this).parent().parent().prev().width();
                //alert("this:"+$(this).parent().parent().prev().html());
                var w = 20;
                    $(this).children().each( function() {
                        w+=$(this).width()+4;
                    });
                if (w<mw || $(this).height()>45) {
                    $(this).parent().parent().css("margin-left",((mw-w)/2)+18);
                    $(this).parent().prev().prev().hide();
                    $(this).parent().next().next().hide();
                    $(this).parent().width(w-18);
                } else {
                    $(this).parent().width(mw-24);
                    
                }
                $(this).width(w - 18);
                $(this).parent().parent().animate({ opacity: 1 }, 2000);
}
function switchImages() {

    var imgNo = $(this).data("curImg");
    imgNo++;
    if (imgNo >= $(this).children().length-1) {
        imgNo = 0;
    }
    $(this).children().eq(imgNo).each(switchImage);

    $(this).data("curImg", imgNo);
    $(this).animate({ opacity: 1 }, 4000, "linear", switchImages);
}

function fixImageLists() {
    $(".thumbsPanelList").each(
            fixImgList
        );

    if (slideShow) {
            $(".thumbsPanelList:first").each(function() {

                $(this).animate({ opacity: 1 }, 4000, "linear", switchImages);
            }
            );
    }
        $(".RArrow").mouseout(
            function() {
                $(this).prev().prev().children().stop();
            }
        );
        $(".LArrow").mouseout(
            function() {
                $(this).next().next().children().stop();
            }
        );
        $(".RArrow").mouseover(
            function() {
                $(this).prev().prev().children().animate(
                {
                marginLeft:$(this).prev().prev().width()-$(this).prev().prev().children().width()
                },1500 );
            }
        );
        $(".LArrow").mouseover(
            function() {
                $(this).next().next().children().animate(
                {
                marginLeft:0
                },1500 );
            }
        );

}
function switchImage() {
    var mySrc = $(this).attr("src");
    var myAlt = $(this).attr("alt");
    $(this).attr("src", $(this).parent().parent().parent().prev().prev().children().attr("src"));
    $(this).parent().parent().parent().prev().prev().children().attr("src", mySrc);
    $(this).attr("alt", $(this).parent().parent().parent().prev().prev().children().attr("alt"));
    $(this).parent().parent().parent().prev().prev().children().attr("alt", myAlt);
    $(this).parent().parent().parent().prev().text(myAlt);
    $(this).parent().each(
                fixImgList
            );
}
$(window).load(

    function() {
        $(".thumbImage").click(function() {
            $(this).each(switchImage);
            $(this).parent().stop(true);

        });
        $(".MainImage").click(
            function() {
                jQuery.facebox("<img src=\"" + $(this).attr("src") + "\" style=\"max-width:" + ($("body").width() - 40) + "px\" /><br /><br />" + $(this).attr("alt"));
                // jQuery.facebox({ image: $(this).attr("src") });
            }
            );

        fixImageLists();

    }
);