﻿/*******************************************************************************
* Product: Core
* 
* (c) 2002-2010 Vitec Fastighetssystem AB
* 
* Change History:
* 100519   HE      Initial version
******************************************************************************/

/*
 * Define our coreslider object and its methods
 *
 * ctrlId           The id of the slider-container.
 * liquid           If height should be liquid. Optional, default value is true.
 * speed            The slide speed. Optional, default value is slow
 * balign           The vertical alignment of the prev/next buttons (top/middle)
 * selfslidingtime  Timer to self slide
 */
function coresliderObj(ctrlId, liquid, speed, balign, selfslidingtime) {
    //
    // Parameter members
    //
    this.ctrlId = ctrlId;
    this.liquid = (liquid != null ? liquid : true);
    this.speed = (speed != null ? speed : "slow");
    this.balign = (balign != null ? balign : "middle");
    this.selfslidingtime = (selfslidingtime != null ? selfslidingtime : 0);
    

    //
    // Logic members
    //
    this.changing = false;
    this.slides = null;
    this.maxheight = 0;
    this.current = 0;
    this.interval;

    /*
     * Changes the current slide either by sliding of fading.
     */
    this.changeslide = function (newIndex, slide, forward) {
        if (newIndex != this.current) {
            if (!this.changing) {
                this.changing = true;

                // Get the involved slides
                var curr = $(this.slides[this.current]);
                var next = $(this.slides[newIndex]);

                // Prepare the next slide
                if (slide) {
                    if (forward)
                        next.css({ left: curr.width() });
                    else
                        next.css({ left: -curr.width() });
                    next.show();
                } else {
                    next.css({ left: 0 });
                }

                // Now animate height
                if (this.liquid) {
                    $("#" + this.ctrlId + " .slider-inner").animate({ height: next.height() }, this.speed, "linear");
                }

                // Now change slides
                if (slide) {
                    next.animate({ left: 0 }, this.speed, "linear");
                    if (forward) {
                        curr.animate({ left: -curr.width() }, this.speed, "linear", function () {
                            curr.hide();
                            coresliders[$(this).parent().parent().parent().attr("id")].changing = false;
                        });
                    } else {
                        curr.animate({ left: curr.width() }, this.speed, "linear", function () {
                            curr.hide();
                            coresliders[$(this).parent().parent().parent().attr("id")].changing = false;
                        });
                    }
                } else {
                    curr.fadeOut("medium", function () {
                        next.fadeIn("medium", function () {
                            coresliders[$(this).parent().parent().parent().attr("id")].changing = false;
                        });
                    });
                }
                this.current = newIndex;
                // Set pager at current index to selected
                $("#" + this.ctrlId + " .pager").children("li").each(function (index) {
                    $(this).removeClass("selected");
                    if (index == coresliders[$(this).parent().parent().parent().attr("id")].current)
                        $(this).addClass("selected");
                });
            }
        }
        return false;
    };

    /*
     * Initializes the slider
     */
    this.init = function () {
        // Store slides
        this.slides = $("#" + this.ctrlId + " .slides").children("div");
        this.maxheight = 0;

        // Get maxheight
        for (var p = 0; p < this.slides.length; p++) {
            $(this.slides[p]).addClass("slider");

            if (!this.liquid || p == 0) {
                this.maxheight = $(this.slides[p]).height() > this.maxheight ? $(this.slides[p]).height() : this.maxheight;
            }
            if (p > 0)
                $(this.slides[p]).hide();
        }


        // Set maxheight
        $("#" + this.ctrlId + " .slider-inner").css({ height: this.maxheight });

        // Position prev/next buttons
        var btn_prev = $("#" + this.ctrlId + " #slider_prev:first");
        var btn_next = $("#" + this.ctrlId + " #slider_next:first");

        if (this.slides.length > 1) {
            if (this.balign == "top") {
                btn_prev.css({ top: 0, left: 0 });
                btn_next.css({ top: 0, left: $("#" + this.ctrlId + " .slider-inner").width() - btn_next.width() });
            } else {
                btn_prev.css({ top: (this.maxheight / 3) - (btn_prev.height() / 2), left: 0 });
                btn_next.css({ top: (this.maxheight / 3) - (btn_next.height() / 2), left: $("#" + this.ctrlId + " .slider-inner").width() - btn_next.width() });
            }
        } else {
            btn_prev.hide();
            btn_next.hide();
        }

        // Set first pager item to selected
        if (this.slides.length > 1) {
            $("#" + this.ctrlId + " .pager").children("li:first").addClass("selected");
        } else $("#" + this.ctrlId + " .pager").hide();
    };
}

/*
 * Creates the global slider object
 */
var coresliders = [] ;

/*
 * Creates and initializes a new coreslider object
 */
function coreslider(ctrlId, liquid, speed, buttonalignment, selfslidingtime) {
    coresliders[ctrlId] = new coresliderObj(ctrlId, liquid, speed, buttonalignment, selfslidingtime);
    
    

    // Bind init event
    $(document).bind('ready', { ctrl: ctrlId }, function (event) {
        coresliders[event.data.ctrl].init();

        // Bind next click
        $("#" + event.data.ctrl + " #slider_next").bind("click", { ctrl: event.data.ctrl }, function (event) {
            resetTimer(coresliders[event.data.ctrl]);
            return coresliders[event.data.ctrl].changeslide((coresliders[event.data.ctrl].current + 1) % coresliders[event.data.ctrl].slides.length, true, true);
        });

        // Bind prev click
        $("#" + event.data.ctrl + " #slider_prev").bind("click", { ctrl: event.data.ctrl }, function (event) {
            resetTimer(coresliders[event.data.ctrl]);
            return coresliders[event.data.ctrl].changeslide(coresliders[event.data.ctrl].current - 1 < 0 ? coresliders[event.data.ctrl].slides.length - 1 : coresliders[event.data.ctrl].current - 1, true, false);
        });

        // Bind pager click
        $("#" + event.data.ctrl + " .pager li").bind("click", { ctrl: event.data.ctrl }, function (event) {
            resetTimer(coresliders[event.data.ctrl]);
            return coresliders[event.data.ctrl].changeslide($("#" + event.data.ctrl + " .pager li").index($(this)), false);
        });
        resetTimer(coresliders[event.data.ctrl]);
    });
}

function resetTimer(obj) {
    if (obj.selfslidingtime != 0) {
        clearInterval(obj.interval);
        obj.interval = setInterval(function () {
            obj.changeslide((obj.current + 1) % obj.slides.length, false);
        }, obj.selfslidingtime);
    }
 }
