//jQuery

$(document).ready(function() {
    //slider code for photo tours
    var totalwidth = parseInt($('#slider_content').width());
    var slideposition = parseInt($('#slider').css('left'));
    var slidewidth = parseInt($('#slide_viewer').width());
    var totalpages = totalwidth / slidewidth;
    var pageindex = 1;
    show_slide_data();
    setButtons();


    $('#slide_next_button').click(function() {
        if (!($('#slider').is(':animated'))) {
            if (pageindex == totalpages) {
                move_to_slide(1);
            } else {
                move_slider(pageindex + 1);
            }
            clear_slide_interval();
        }
        return false;
    });


    $('#slide_prev_button').click(function() {
        if (!($('#slider').is(':animated'))) {
            if (pageindex == 1) {
                move_to_slide(totalpages);
            } else {
                move_slider(pageindex - 1);
            }
            clear_slide_interval();
        }
        return false;
    });

    $('.slider_nav_item').click(function() {
        if (!($('#slider').is(':animated'))) {
            var index = $('.slider_nav_item').index(this);
            move_slider(index + 1);
            clear_slide_interval();
        }
        return false;
    });

    function move_slider(to_index) {
        var movepages = to_index - pageindex;
        var movewidth = movepages * slidewidth;
        var movedirection = "-=";
        if (movepages < 0) {
            movewidth = movewidth * -1;
            movedirection = "+=";
        }
        pageindex = to_index;
        setButtons();

        $('#slider').animate({ left: movedirection + movewidth + "px" }, "slow", function() {
            slideposition = parseInt($("#slider").css("left"));
            show_slide_data();
        });
    }

    function move_to_slide(to_index) {
        slideposition = (to_index - 1) * -slidewidth;
        pageindex = to_index;
        setButtons();
        $("#slider").hide(function() {
            $("#slider").css('left', slideposition + 'px');
            $("#slider").fadeIn(500, function() {
                show_slide_data();
            });
        });
    }

    function show_slide_data() {
        $(".viewer_data").hide();
        $(".viewer_data").eq(pageindex - 1).delay(300).fadeIn(500);

    }

    function setButtons2() {

    }

    function setButtons() {
        //$("#slider_stats").text(slideposition + " " + totalwidth + " " + pageindex + " of " + totalpages);

        for (var i = 0; i < $('.slider_nav_item').length; i++) {
            if (i == pageindex - 1) {
                //$(".slider_nav_item").eq(i).css({ "font-weight": "bold", "text-decoration": "none" });
                $(".slider_nav_item").eq(i).fadeOut("fast", function() {
                    $(this).addClass("slider_nav_on").fadeIn("slow");
                });
                //the above code does not look good in IE. So far, I can find no good solution except positioning another element over the top and fading it in and out after moving it.
            } else {
                //$(".slider_nav_item").eq(i).css({ "font-weight": "normal", "text-decoration": "underline" });
                $(".slider_nav_item").eq(i).removeClass("slider_nav_on");
            }
        }

    }

    function clear_slide_interval() {
        clearInterval(slideset);
    }

    var slideset
    $(window).load(function() {
        //could use this to wait on interval until after images have loaded.

        slideset = setInterval(function() {
            if (pageindex == totalpages) {
                move_to_slide(1);
            } else {
                move_slider(pageindex + 1);
            }

        }, 5000);

    });


});                    // end ready
