﻿var slideshow = {

    rotationSpeed: 7000,
    transitionSpeed: 1000,

    preLoadSlide: function(index, loaddirectly) {
        if ($("#i" + lrgIds[index]).length) {
        } else {
            var img = $("<img/>").attr("src", lrgImgs[index]).attr("id", "i" + lrgIds[index]).css("display", "none").appendTo(".tp-imgs");
            if (loaddirectly) {
                $("#i" + lrgIds[index]).load(function() {
                    slideshow.rotateSlides(index);
                });
            }
        }
    },

    rotateSlides: function(index) {
        if ($("#i" + lrgIds[index]).length) {
            $(".tp-imgs img:not(#i" + lrgIds[index] + ")").fadeOut(slideshow.transitionSpeed);
            $(".tp-captions p").css("display", "none");
            $(".tp-captions p#c" + lrgIds[index]).css("display", "block");
            $(".tp-nav img").removeClass("active");
            $(".tp-nav img#t" + lrgIds[index]).addClass("active");

            if (index % 6 === 0) {
                currentpage = Math.round(((index + 1) / 6) + 0.499999) - 1;
                slideshow.goToPage(currentpage);
            }

            $("#i" + lrgIds[index]).fadeIn();
            currentslide = 0;
            slideindex = index;
            if (index < slidecount - 1) {
                currentslide = index + 1;
            }
            slideshow.preLoadSlide(currentslide);
            rt = setTimeout("slideshow.rotateSlides(" + currentslide + ")", slideshow.rotationSpeed);
        } else {
            slideshow.preLoadSlide(index, true);
        }
    },

    goToPage: function(page) {
        $(".tp-nav").animate({ left: -(page * 336) + "px" });
    },

    initializeNav: function() {
        if (slidecount > 6) {
            var prev = $("<span/>").addClass("prev").append("Previous").appendTo(".tp-nav-wrapper");
            var next = $("<span/>").addClass("next").append("Next").appendTo(".tp-nav-wrapper");
            prev.click(function() {
                if (currentpage > 0) {
                    currentpage = currentpage - 1;
                    slideshow.goToPage(currentpage);
                }
            });
            next.click(function() {
                if (currentpage < totalpages) {
                    currentpage = currentpage + 1;
                    slideshow.goToPage(currentpage);
                }
            });
        }
        $(".tp-nav img").click(function() {
            clearTimeout(rt);
            slideshow.rotateSlides($(".tp-nav img").index(this));

        });
    },

    initialize: function() {
        var i = 0;
        var imgs;
        var ids;
		
		// Start replace image src code
		// Added as a temporary solution to remove the call to http://cdnmc.taylorpond.com
		/*$(".tp-img-rotator img").each(function() {
			this.src = this.src.replace("http://cdnmc.taylorpond.com", "");
		});*/
		// End replace image src code
		
        $(".tp-nav img").each(function() {
            if (i === 0) {
                $(this).addClass("active");
                imgs = this.src.replace("thumbs", "large");
                ids = this.id.replace("t", "");
            } else {
                imgs += "~" + this.src.replace("thumbs", "large");
                ids += "~" + this.id.replace("t", "");
            }
            i++;
        });
        lrgImgs = imgs.split("~");
        lrgIds = ids.split("~");
        $(".tp-captions p#c" + lrgIds[0]).css("display", "block");
        slidecount = i;
        slideindex = 0;
        currentpage = 0;
        totalpages = (Math.round((slidecount / 6) + 0.499999)) - 1;
        $(".tp-nav").css("width", (56 * slidecount) + "px");
        if (slidecount > 1) {
            slideshow.initializeNav();
            slideshow.preLoadSlide(1);
            rt = setTimeout("slideshow.rotateSlides(1)", slideshow.rotationSpeed);
        }
    }

}

function saveOrder() {
    //var sto = setTimeout("$('#ctl00_ctl00_loading').css('display', 'block')", 1000);
    $.ajax({
        type: "GET",
        url: document.URL,
        data: "saveorder=" + $("#sortable").sortable('toArray'),
        success: function(msg) {
            if (msg !== "success") {
                $("#images-error").fadeIn();
            } else {
                $("#images-error").fadeOut();
            }
        }
    });
}
$(document).ready(function() {
    if ($(".tp-img-rotator").length) {
        slideshow.initialize();
    }
    if ($("#sortable").length) {
        $("#sortable").sortable({
            update: function(event, ui) {
                saveOrder();
            },
            placeholder: 'highlight'
        });
    }
});
