﻿window.INTER = window.INTER || {};

INTER = {
    globals: {
        labels: {}
    },
    pages: {
        all: {},
        culture: {},
        home: {},
        generic: {}
    },
    widgets: {},
    util: {}
}

INTER.pages.home.init = function() {
    INTER.globals.slideShow = new INTER.widgets.Slideshow("slides", "slide-controls", { timer: 8000 });
};

INTER.widgets.Slideshow = function(slidesId, controlbarId, userConfig) {
    this.slidesEl = YAHOO.util.Dom.get(slidesId);
    this.controlBarEl = YAHOO.util.Dom.get(controlbarId);

    if (!this.slidesEl || !this.controlBarEl) return;

    //this.refreshSlidesElDims();
    this.slidesElDims = {
        xy: YAHOO.util.Dom.getXY(this.slidesEl),
        width: this.slidesEl.offsetWidth,
        height: this.slidesEl.offsetHeight
    };

    this.uc = userConfig || {};
    //HIDE this if you want stop the slider and show all promotions.
    this.slides = YAHOO.util.Dom.getElementsByClassName("slide", this.uc.slidesElType || "li", this.slidesEl);
    this.numSlides = this.slides.length;

    this.slidesPtr = 0;
    this.currentSlide = function() { return this.slidesPtr + 1 }

    // Only create previous and next buttons if there is more than one slide
    if (this.numSlides > 1) {


        // Hyperlink Events Tracking for all banners
        for (var i = 0; i < this.numSlides; i++) {

            //Find all "A" link element            
            if (this.slidesEl) {
                this.aLinkContent = this.slidesEl.getElementsByTagName('a');
                this.numALinkContent = this.aLinkContent.length;
            }
            for (var i = 0; i < this.numALinkContent; i++) {
                for (var i = 0; i < this.numALinkContent; i++) {
                    if ((this.aLinkContent[i].title).toLowerCase() != "" || (this.aLinkContent[i].title).toLowerCase() != "undefined") {
                        //Call the Action
                        var params = INTER.globals.cultureId + "_Home_Rotation_Banners_Links|" + this.aLinkContent[i].title + "|" + (this.aLinkContent[i].href);
                        //eventTracker._trackEvent('[culture]_Home_Rotation_Banners_Links', '[nom du site produit]', '[URL du lien sortant]');
                        YAHOO.util.Event.on(this.aLinkContent[i], 'click', INTER.HyperlinkEventsTracking.TrackPageEvent, params);
                    }
                }
            }
        } //Hyperlink Events Tracking


        // On Mouse Over on "LI" element stop de slider
        // and clear the Timer until the onmouseout reset the Timer.
        for (var i = 0; i < this.numSlides; i++) {

            this.slidesContent = YAHOO.util.Dom.getElementsByClassName("slide-content", "div", this.slides[i]);
            this.numSlidesContent = this.slidesContent.length;

            for (var x = 0; x < this.numSlidesContent; x++) {
                //stopTimer
                this.slidesContent[x].onmouseover = function(that) {
                    return function() {
                        that.stopTimer();
                        return false;
                    }
                } (this);
                //setTimer
                this.slidesContent[x].onmouseout = function(that) {
                    return function() {
                        //that.resetTimer();
                        that.setTimer();
                        return false;
                    }
                } (this);
            }
        }

        // Next button
        this.nextBtn = document.createElement(this.uc.buttonNodeName || "div");
        this.nextBtn.innerHTML = "&rarr;";
        this.nextBtn.className = this.uc.nextBtnClass || "next";
        this.nextBtn.onclick = function(that) {
            return function() {
                that.goTo(1);
                that.resetTimer();
                return false;
            }
        } (this);
        this.controlBarEl.appendChild(this.nextBtn);

        // Indicator
        this.indicator = document.createElement(this.uc.buttonNodeName || "div");
        this.indicator.className = this.uc.indicatorClass || "indicator";
        //Add no click for the control Bar
        this.indicator.onclick = function() {
            return false;
        }
        this.pips = [];

        for (var i = 0; i < this.numSlides; i++) {
            var pip = document.createElement("div");
            //Add number of Slide
            pip.innerHTML = i + 1;

            if ((i + 1) === this.currentSlide()) {
                pip.className = "current";
            }

            pip.onclick = function(that, num) {
                return function() {
                    //If the current slide click == to same click do nothing
                    if (that.slidesPtr != num) {
                        var dir = (that.slidesPtr > num) ? 1 : -1;
                        that.goTo(dir, num);
                        that.resetTimer();
                    }
                }
            } (this, i);

            this.indicator.appendChild(pip);
            this.pips.push(pip);
        }
        this.controlBarEl.appendChild(this.indicator);

        // Previous button
        this.prevBtn = document.createElement(this.uc.buttonNodeName || "div");
        this.prevBtn.innerHTML = "&larr;";
        this.prevBtn.className = this.uc.prevBtnClass || "prev";
        this.prevBtn.onclick = function(that) {
            return function() {
                that.goTo(-1);
                that.resetTimer();
                return false;
            }
        } (this);
        this.controlBarEl.appendChild(this.prevBtn);

        // Get slides out of the way for Safari 2.0.x because they block page links
        for (var i = 1; this.slides[i]; i++) {
            this.slides[i].style.top = "-3000px";
            this.slides[i].style.left = "-3000px";
        }

        this.goTo(0); // to initialize the first slide if it has a popup
        this.setTimer(); // set automated forward timer

        //Add Width size for the controler element to fix background in IE6.
        var controlBarElWidth = Math.floor(this.nextBtn.offsetWidth + this.indicator.offsetWidth + this.prevBtn.offsetWidth + 6);
        this.controlBarEl.style.width = controlBarElWidth + "px";
    } else {
        //Remove all the controlBarEl to hide the background image
        this.controlBarEl.style.display = "none";
    }
}

INTER.widgets.Slideshow.prototype.setTimer = function() {
    if (this.uc.timer) {
        this.slideShowInterval = window.setInterval(
			function(that) {
			    return function() {
			        that.goTo(1);
			    }
			} (this),
			this.uc.timer
		);
    }
}

INTER.widgets.Slideshow.prototype.resetTimer = function() {
    if (this.slideShowInterval) {
        window.clearInterval(this.slideShowInterval);
        this.setTimer();
    }
}

INTER.widgets.Slideshow.prototype.stopTimer = function() {
    if (this.slideShowInterval) {
        window.clearInterval(this.slideShowInterval);
    }
}

INTER.widgets.Slideshow.prototype.refreshSlidesElDims = function() {
    this.slidesElDims = {
        xy: YAHOO.util.Dom.getXY(this.slidesEl),
        width: this.slidesEl.offsetWidth,
        height: this.slidesEl.offsetHeight
    };
}

INTER.widgets.Slideshow.prototype.goTo = function(dir, skipTo) {
    this.slidesElDims = {
        xy: YAHOO.util.Dom.getXY(this.slidesEl),
        width: this.slidesEl.offsetWidth,
        height: this.slidesEl.offsetHeight
    };

    var outSlide = this.slides[this.slidesPtr];
    var outSlideXY = YAHOO.util.Dom.getXY(outSlide);
    var outSlideWidth = outSlide.offsetWidth;

    if (dir == -1) {
        var outSlideAnim = new YAHOO.util.Motion(outSlide, {
            opacity: {
                from: 1,
                to: 0
            },
            points: {
                from: [
                        this.slidesElDims.xy[0],
                        this.slidesElDims.xy[1]
                    ],
                to: [
                        this.slidesElDims.xy[0] + this.slidesElDims.width,
                        this.slidesElDims.xy[1]
                    ]
            }
        }
        );
        outSlideAnim.onComplete.subscribe(
           function(el) {
               return function() {
                   el.style.top = "-3000px";
                   el.style.left = "-3000px";
               }
           } (outSlide)
        );
        outSlideAnim.duration = 0.5;
        outSlideAnim.method = YAHOO.util.Easing.easeOutStrong;

        this.slidesPtr = (this.slides[skipTo]) ? skipTo : this.slides[this.slidesPtr - 1] ? this.slidesPtr - 1 : this.numSlides - 1;
        var inSlide = this.slides[this.slidesPtr];
        var inSlideXY = YAHOO.util.Dom.getXY(inSlide);
        var inSlideAnim = new YAHOO.util.Motion(inSlide, {
            opacity: {
                from: 0,
                to: 1
            },
            points: {
                from: [
                        this.slidesElDims.xy[0] - this.slidesElDims.width,
                        this.slidesElDims.xy[1]
                    ],
                to: [
                        this.slidesElDims.xy[0],
                        this.slidesElDims.xy[1]
                    ]
            }
        }
        );
        inSlideAnim.duration = 0.5;
        inSlideAnim.method = YAHOO.util.Easing.easeOutStrong;

        outSlideAnim.animate();
        inSlideAnim.animate();


    } else if (dir == 1) {
        var outSlideAnim = new YAHOO.util.Motion(outSlide, {
            opacity: {
                from: 1,
                to: 0
            },
            points: {
                from: [
                        this.slidesElDims.xy[0],
                        this.slidesElDims.xy[1]
                    ],
                to: [
                        this.slidesElDims.xy[0] - this.slidesElDims.width,
                        this.slidesElDims.xy[1]
                    ]
            }
        }
        );
        outSlideAnim.onComplete.subscribe(
           function(el) {
               return function() {
                   el.style.top = "-3000px";
                   el.style.left = "-3000px";
               }
           } (outSlide)
        );
        outSlideAnim.duration = 0.5;
        outSlideAnim.method = YAHOO.util.Easing.easeOutStrong;

        this.slidesPtr = (this.slides[skipTo]) ? skipTo : this.slides[this.slidesPtr + 1] ? this.slidesPtr + 1 : 0;
        var inSlide = this.slides[this.slidesPtr];
        var inSlideXY = YAHOO.util.Dom.getXY(inSlide);
        var inSlideAnim = new YAHOO.util.Motion(inSlide, {
            opacity: {
                from: 0,
                to: 1
            },
            points: {
                from: [
                        this.slidesElDims.xy[0] + this.slidesElDims.width,
                        this.slidesElDims.xy[1]
                    ],
                to: [
                        this.slidesElDims.xy[0],
                        this.slidesElDims.xy[1]
                    ]
            }
        }
        );
        inSlideAnim.duration = 0.5;
        inSlideAnim.method = YAHOO.util.Easing.easeOutStrong;

        outSlideAnim.animate();
        inSlideAnim.animate();
    }

    if (this.indicator) {
        for (var i = 0; this.pips[i]; i++) {
            if ((i + 1) === this.currentSlide()) {
                this.pips[i].className = "current";
            } else {
                this.pips[i].className = "";
            }
        }
    } 
}

INTER.HyperlinkEventsTracking = {

    AddOnClickEvent: function(containerId, culture) {

        this.Culture = culture;

        var allLinks = YAHOO.util.Dom.getElementsBy(function() { return true; }, 'A');
        var allLinksLength = allLinks.length;
        var params = "";

        for (var i = 0; i < allLinksLength; i++) {

        }
    },

    TrackPageEvent: function(e, params) {
        var aParams = params.split('|');
        if (aParams.length == 3)
            eventTracker._trackEvent(aParams[0], aParams[1], aParams[2]);
    },
    TrackPageView: function(e, params) {
        pageTracker._trackPageview(params);
    }
};

function popWin(url, w, h, scroll, tools, name, center, baseUrl) {
    var str = "height=" + h + ",innerHeight=" + h;
    str += ",width=" + w + ",innerWidth=" + w;
    if (baseUrl) url = webSiteCMSUrl + url;
    if (!center) var center = false;
    if (!scroll) scroll = 0;
    if (!tools) tools = 0;
    if (!name) name = "pop";

    if ((window.screen) && (center)) {
        var ah = screen.availHeight - 30;
        var aw = screen.availWidth - 10;

        var xc = (aw - w) / 2;
        var yc = (ah - h) / 2;

        str += ",left=" + xc + ",screenX=" + xc;
        str += ",top=" + yc + ",screenY=" + yc;
    }

    pop = window.open(url, name, 'toolbar=' + tools + ',location=0,directories=0,status=0,menubar=0,scrollbars=' + scroll + ',resizable=1,' + str).focus();
}
