(function($) {

    /*
    ###########################################
    Rounded White Corner Wrap
    ###########################################
    */

    $.fn.boxWrap = function(options) {
        options = $.extend({
            width: '',
            height: '',
            boxType: 'whiteBoxShadow', // choices: whiteBoxShadow,blueBox,
            isModal: false,
            modalTriggerClass: "triggerModal"
        }, options);
        var counter = 0;
        $(this).each(function() {
            if (options.isModal) {
                $(this).css({ display: "block" });
                $(this).wrap('<div id="modal_' + options.modalTriggerClass + '" class="modalWindow"><div class="boxWrapModalContent"></div></div>');
                $("#modal_" + options.modalTriggerClass).prepend('<div class="boxWrapModalBk ' + options.modalTriggerClass + '"></div>');
                $("#modal_" + options.modalTriggerClass + " .boxWrapModalContent").append('<div class="closeModal ' + options.modalTriggerClass + '"></div>');
            }
            $(this).wrap('<div class="' + options.boxType + '_wrap" style="width:' + options.width + 'px;"><div class="bd"><div class="c"><div class="s"></div></div></div></div>');
            $(this).parent().parent().parent().parent().prepend('<div class="hd"><div class="c"></div></div>').append('<div class="ft"><div class="c"></div></div>');
            counter++;
        });
        if (options.isModal) {
            $("." + options.modalTriggerClass).click(function() {
                $("#modal_" + options.modalTriggerClass).toggle();
            });
            $("#modal_" + options.modalTriggerClass + " .boxWrapModalContent").css({ width: options.width + "px" });
            $(window).resize(function() {
                $('.boxWrapModalBk').css({
                    width: $(window).width(),
                    height: $(window).height()
                });
            });
        }
    }

    /*
    ###########################################
    Get the Greatest Height
    ###########################################
    */

    $.fn.greatestHeight = function() {
        $(this).each(function() {
            var Height = 0;
            $(this).children().each(function() {
                Height = $(this).height() > Height ? $(this).height() : Height;
            });
            $(this).height(Height);
        });
    }


    /*
    ###########################################
    SlideShow
    ###########################################
    */

    $.fn.slideShow = function(options) {
        options = $.extend({
            width: 400,
            height: 300,
            speed: 5, //in seconds
            leftMargin: 0
        }, options);

        $(this).each(function() {
            var slideshow_name_noHash = "slideshowWrapper_" + $(this).attr("id");
            var slideshow_interval;

            $(this).wrap("<div id='" + slideshow_name_noHash + "'><div class='slidesWindow'></div></div>");
            $(this).parent().after("<div class='slideshowControls' align='center'></div>");

            slideshow_name = "#" + slideshow_name_noHash;

            $(slideshow_name).css({
                float: "left",
                marginLeft: options.leftMargin,
                width: options.width + "px",
                height: (options.height + 20) + "px"
            });

            $(slideshow_name + " div.slidesWindow").css({
                position: "relative",
                overflow: "hidden",
                float: "left",
                width: options.width + "px",
                height: options.height + "px"
            });

            $(this).css({
                position: "relative",
                listStyle: "none",
                margin: "0px",
                padding: "0px",
                width: (options.width * $(this).children().size()) + "px",
                height: options.height
            });

            $(this).find("li").css({
                display: "block",
                position: "relative",
                listStyle: "none",
                float: "left",
                margin: "0px",
                padding: "0px",
                width: options.width + "px",
                height: options.height + "px"
            });

            $(slideshow_name + " div.slideshowControls").css({
                position: "relative",
                float: "left",
                clear: "both",
                width: options.width + "px",
                height: "10px",
                marginTop: "10px",
                zIndex: "1000"
            });

            $(this).find("li").each(function(index) {
                $(this).attr("id", index);
                $(slideshow_name + " div.slideshowControls").append("<a href='" + window.location + "#" + index + "' class='slideshowControls_" + index + "'><img src='/Includes/Media/Images/Template/circle.png' border='0'/></a>&nbsp;");
            });

            $.localScroll.defaults.axis = 'xy';
            $(slideshow_name + " div.slideshowControls").localScroll({
                target: slideshow_name + " div.slidesWindow", // could be a selector or a $ object too.
                duration: 400,
                hash: false
            });

            $(slideshow_name + " div.slideshowControls a").click(function(event, a) {
                if (a != "continue_to_play") {
                    clearInterval(slideshow_interval);
                }
                $(slideshow_name + " div.slideshowControls a img").attr("src", "/Includes/Media/Images/Template/circle.png");
                $("img", this).attr("src", "/Includes/Media/Images/Template/circle_active.png");
                $(this).blur();
            });

            if ($(slideshow_name + " div.slidesWindow li.activeslide").length == 0) {
                $(slideshow_name + " div.slidesWindow li:first").addClass('activeSlide');
                $(slideshow_name + " div.slideshowControls a:first img").attr("src", "/Includes/Media/Images/Template/circle_active.png");
            }

            slideshow_interval = setInterval(function() {
                $(slideshow_name).nextSlide()
            }, (options.speed * 1000));
        });
    }

    /*
    ###########################################
    NextSlide (used in slideshow)
    ###########################################
    */

    $.fn.nextSlide = function() {
        var currentSlide = $(this).find("li.activeSlide").removeClass('activeSlide');
        if (currentSlide.length == 0) {
            currentSlide = $(this).find("ul li:last");
        }
        var nextSlide = currentSlide.next().length ? currentSlide.next() : $(this).find("ul li:first");
        nextSlide.addClass('activeSlide');

        var nextSlide_control = $(this).find("li.activeSlide").attr("id");
        $(this).find("a.slideshowControls_" + nextSlide_control).trigger("click", ["continue_to_play"]);
    }

    /*
    ###########################################
    SlideShow
    ###########################################
    */

    $.fn.panelShow = function(options) {
        options = $.extend({
            width: 400,
            height: 300,
            active: 1,
            boxWrap: '',
            imgWidth: 400,
            innerFrame: false
        }, options);

        $(this).each(function() {
            $(this).width(options.width).height(options.height);

            if (options.boxWrap != '') {
                $(this).boxWrap({ boxType: options.boxWrap });
            }

            $(this).children("li").find("img").width(options.imgWidth);

            $(this).children(":nth-child(" + options.active + ")").addClass("active");

            var inactiveWidth = (options.width - $(this).children(".active").width()) / ($(this).children("li").size() - 1);

            $(this).children("li").each(function() {
                $(this).width(inactiveWidth);

                if (options.innerFrame) {
                    $(this).children().wrap("<div class='innerWrap'></div>");
                    $(this).find("div.innerWrap").append("<div class='innerBorder'></div><div class='innerBorder2'></div>");
                }
            });

            $(this).children("li.active").width($(this).children("li.active").children().width());
            $(this).find("div.innerBorder").width(($(this).find("li.active").width() - 45) + "px").height(($(this).find("li.active").height() - 45) + "px");
            $(this).find("div.innerBorder2").width(($(this).find("li.active").width() - 39) + "px").height(($(this).find("li.active").height() - 39) + "px");

            $(this).children("li").stop().hover(function() {
                if (!$(this).hasClass("active")) {
                    $(this).parent().children("li.active").removeClass("active").width(inactiveWidth);
                    $(this).addClass("active");
                    $(this).width($(this).children().width());
                }
            }, function() { });
        });
    }

})(jQuery);



