﻿/* Namespace for all Creditland js script */
if (!window.Creditland) { window.Creditland = {}; }

/* Hp animation */
Creditland.HpAnimation = {};

Creditland.HpAnimation.Slide = function(animation) {
    // jQuery element
    this.$elt = null;
    // Product type
    this.productType = null;
    // Parent animation
    this.animation = animation;
};

// Init slider
Creditland.HpAnimation.Slide.prototype.init = function(elt, index, zIndex) {
    this.$elt = $(elt);
    // Product type
    this.productType = this.$elt.attr("id");
    // Set slide position
    this.$elt.css("z-index", zIndex);
    this.$elt.css("left", this.animation.offset * index);
    // Add focus click and mouseover element
    this.$elt.append("<div class='navBtn leftNavBtn'></div><div class='navBtn rightNavBtn'></div>");
    // Add behavior
    var slide = this;
    var slideOffset = slide.animation.offset / 2;
    this.$elt.find(".navBtn").css("width", (this.animation.offset + slideOffset) + "px");
    this.$elt.find(".navBtn").click(function() { ALF.Liwio.event('menu-interactif', slide.productType, 'tab'); slide.animation.focusOnSlide(slide, true); });
    this.$elt.find(".leftNavBtn").mouseover(function() { slide.$elt.animate({ marginLeft: "-=" + slideOffset + "px" }, 300); });
    this.$elt.find(".leftNavBtn").mouseout(function() { slide.$elt.animate({ marginLeft: "+=" + slideOffset + "px" }, 300); });
    this.$elt.find(".rightNavBtn").mouseover(function() { slide.$elt.animate({ marginLeft: "+=" + slideOffset + "px" }, 300); });
    this.$elt.find(".rightNavBtn").mouseout(function() { slide.$elt.animate({ marginLeft: "-=" + slideOffset + "px" }, 300); });
    this.$elt.find(".btn").click(function() { ALF.Liwio.event('menu-interactif', slide.productType, 'compare'); })
};
// Update current state
Creditland.HpAnimation.Slide.prototype.update = function(focus, zIndex) {
    if (focus)
        this.$elt.addClass("slide_focus");
    else
        this.$elt.removeClass("slide_focus")
    this.$elt.css("z-index", zIndex);
};

Creditland.HpAnimation.SlideAnimation = function() {
    // jQuery element
    this.$elt = null;
    // Slides collection
    this.slides = new Array();
    // Animation
    this.speed = 1000; // ms
    this.offset = 50; // px
    this.play = false; // Playing in progress
};
Creditland.HpAnimation.SlideAnimation.prototype.init = function(eltId, speed, offset) {
    this.$elt = $("#" + eltId);
    // Animation configuration
    this.speed = speed;
    this.offset = offset;
    // Add slides
    var list = this.$elt.find(".slide");
    for (var i = 0; i < list.length; i++) {
        var z = list.length - i;
        var slide = new Creditland.HpAnimation.Slide(this);
        slide.init(list[i], i, z);
        if (i == 0) slide.update(true, z);
        this.slides.push(slide);
    }
};
// Set focus on slide index
Creditland.HpAnimation.SlideAnimation.prototype.focus = function(i, stop) {
    // Stop animation
    if (stop != null && stop)
        this.stop();
    // Move slide
    var z = 0;
    for (var j = 0; j < this.slides.length; j++) {
        if (j < i)
            z++;
        else if (j == i)
            z = this.slides.length;
        else
            z--;
        this.slides[j].update((j == i ? true : false), z);
    }
};
// Set focus on slide
Creditland.HpAnimation.SlideAnimation.prototype.focusOnSlide = function(slide, stop) {
    for (var i = 0; i < this.slides.length; i++) {
        if (this.slides[i] == slide) {
            this.focus(i, stop);
            break;
        }
    }
};
// Start animation
Creditland.HpAnimation.SlideAnimation.prototype.start = function() {
    this.play = true;
    var anim = this;
    var i = 0;
    var f = function() {
        if (anim.play) {
            // Focus slide
            i++;
            anim.focus(i);
            // Move previous focus slide effect
            if (i > 0)
                anim.slides[i - 1].$elt.animate({ marginLeft: "+=" + anim.offset + "px" }, 0).animate({ marginLeft: "-=" + anim.offset + "px" });
            // Loop
            if (i == anim.slides.length - 1) i = -1;
            // Next
            setTimeout(f, anim.speed);
        }
    }
    setTimeout(f, this.speed);
};
// Stop animation
Creditland.HpAnimation.SlideAnimation.prototype.stop = function() {
    this.play = false;
};
// Load
Creditland.HpAnimation.SlideAnimation.prototype.load = function(imgUrl) {
    var anim = this;
    var img = new Image();
    $(img).addClass("preload");
    $(img).load(function() {
        // Hide image
        $(this).hide();
        // Update slide background
        anim.$elt.find(".spritebg").css("background-image", "url(" + imgUrl + ")");
        // Remove loading
        anim.$elt.removeClass("loading");
        // Display slides
        anim.$elt.find(".slides").fadeIn();
        // Start animation
        anim.start();
    }).error(function() {
        // notify the user that the image could not be loaded
    }).attr("src", imgUrl);
};
