﻿(function($) {

    $.fn.extend({

        //pass the options variable to the function
        tpslider: function(options) {
            return this.each(function() {


                //Set the default values:
                var defaults = {
                    rotationSpeed: 10000,
                    transitionSpeed: 1000,
                    autoPlay: true,
                    nav: "",
                    navType: "none",
                    onTransitionStart: function() { },
                    onTransitionEnd: function() { }
                }

                var o = $.extend(defaults, options);
                var obj = $(this);
                var rt;
                var slideid = 0;
                var slidecount = 0;
                var currentslide = 0;

                function rotateSlides(newslide) {
                    var tmpslideid = slideid;
                    o.onTransitionStart(slideid, newslide);
                    obj.children().not(o.nav).fadeOut(o.transitionSpeed);
                    obj.children().filter(":nth-child(" + (newslide + 1) + ")").fadeIn(o.transitionSpeed, function() {
                        o.onTransitionEnd(tmpslideid, newslide);
                    });
                    $(o.nav).children().removeClass("active");
                    $(o.nav).children().filter(":nth-child(" + (newslide + 1) + ")").addClass("active");
                    currentslide = 0;
                    slideid = newslide;
                    if (newslide < slidecount - 1) {
                        currentslide = newslide + 1;
                    }
                    if (o.autoPlay) {
                        rt = setTimeout(function() { rotateSlides(currentslide) }, o.rotationSpeed);
                    }
                }

                function initializeNav() {

                    $(o.nav).css("display", "block");
                    $(o.nav).children().first().addClass("active");
                    $(o.nav).children().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 initialize() {
                    var i = 0;
                    if (o.navType === "dynamic") {
                        o.nav = $("<ul/>").attr("class", "tpslider-nav").css("display", "none").appendTo(obj);
                    }
                    obj.children().not(o.nav).each(function() {
                        if (i > 0) {
                            $(this).css("display", "none");
                        }
                        $(this).css("position", "absolute");
                        if (o.navType === "dynamic") {
                            $("<li/>").text(i + 1).appendTo(o.nav);
                        }
                        i++;
                    });
                    slidecount = i;
                    slideid = 0;
                    if (slidecount > 1) {
                        if (o.navType !== "none") {
                            initializeNav();
                        }
                        if (o.autoPlay) {
                            rt = setTimeout(function() { rotateSlides(1) }, o.rotationSpeed);
                        }
                    }

                }

                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();
        }
    });
});

