﻿(function($) {

    $.fn.extend({

        //pass the options variable to the function
        tprotator: function(options) {


            //Set the default values, use comma to separate the settings, example:
            var defaults = {
                rotationSpeed: 10000,
                transitionSpeed: 1000,
                height: 240,
                width: 640,
                nav: ".tp-nav",
                navitem: ".tp-nav li",
                captions: ".tp-captions"
            }
            var options = $.extend(defaults, options);
            var o = options;
            var obj = $(this);
            var rt;
            var ids = new Array();

            obj.css("height", o.height + "px");
            obj.css("width", o.width + "px");

            function rotateSlides(newslide) {
                if ($(o.captions + " p#c" + ids[slideid]).length) {
                    $(o.captions).animate({ bottom: ((captionsHeight(slideid) + 30) * -1) + "px" }, (o.transitionSpeed / 2));
                }
                $(".tpslide").fadeOut(o.transitionSpeed);
                $("#i" + ids[newslide]).fadeIn(o.transitionSpeed, function() {
                    if ($(o.captions + " p#c" + ids[newslide]).length) {
                        $(o.captions).css("bottom", ((captionsHeight(newslide) + 30) * -1) + "px");
                        $(o.captions).css("height", captionsHeight(newslide) + "px");
                        $(o.captions + " p").hide();
                        $(o.captions + " p#c" + ids[newslide]).show();
                        $(o.captions).animate({ bottom: "0px" }, o.transitionSpeed);
                    }
                });
                $(o.navitem).css("opacity", "0.5");
                $(o.navitem + "#t" + ids[newslide]).fadeTo(o.transitionSpeed, 1);
                currentslide = 0;
                slideid = newslide;
                if (newslide < slidecount - 1) {
                    currentslide = newslide + 1;
                }
                rt = setTimeout(function() { rotateSlides(currentslide) }, o.rotationSpeed);
            }

            function initializeNav() {
                $(o.nav).css("display", "block");
                //$(o.navitem + ":first-child").addClass("active");
                $(o.navitem).click(function() {
                    clearTimeout(rt);
                    var id = $(this).index();
                    if (parseInt(slideid) !== parseInt(id)) {
                        rotateSlides(parseInt(id));
                    } else {
                        if (slideid < slidecount - 1) {
                            currentslide = slideid + 1;
                        }
                        rt = setTimeout(function() { rotateSlides(currentslide) }, o.rotationSpeed);
                    }
                });
            }

            function captionsHeight(index) {
                return $(o.captions + " p#c" + ids[index]).height();
            }

            function initialize() {
                var i = 0;
                $(o.navitem).css("opacity", "0.5");
                $(o.navitem).first().css("opacity", "1");
                obj.children().not(o.nav).each(function() {
                    var id = this.id.replace("i", "");
                    ids[i] = id;
                    $(this).addClass("tpslide");
                    if (i > 0) {
                        $(this).css("display", "none");
                    }
                    $(this).css("position", "absolute").css("left", "0px").css("top", "0px");
                    i++;
                });
                slidecount = i;
                slideid = 0;
                if (slidecount > 1) {
                    initializeNav();
                    rt = setTimeout(function() { rotateSlides(1) }, o.rotationSpeed);
                }
                $(o.captions).css("opacity", "0.5");
                //$(o.captions + " p").css("width", ((o.width - 30) - $(o.nav).width()) + "px");
                $(o.captions).show();
                $(o.captions).css("bottom", "-500px");
                if ($(o.captions + " p#c" + ids[0]).length) {
                    $(o.captions).css("bottom", ((captionsHeight(0) + 30) * -1) + "px");
                    $(o.captions).css("height", captionsHeight(0) + "px");
                    $(o.captions + " p#c" + ids[0]).show();
                    $(o.captions).animate({ bottom: "0px" }, o.transitionSpeed);
                }

            }

            initialize();

        }
    });

})(jQuery);


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 ($("#sortable").length) {
        $("#sortable").sortable({
            update: function(event, ui) {
                saveOrder();
            },
            placeholder: 'highlight'
        });
    }
    if ($(".link-type input:checked").val() === "P") {
        $("#selectpage").show();
        $("#newwindow").show();
    } else if ($(".link-type input:checked").val() === "U") {
        $("#enterurl").show();
        $("#newwindow").show();
    }
    $(".link-type input").change(function() {
        $(".link-option").hide();
        if (this.value === "P") {
            $("#selectpage").show();
            $("#newwindow").show();
        } else if (this.value === "U") {
            $("#enterurl").show();
            $("#newwindow").show();
        }
    });
});

