﻿// JScript File

//2D array of divs in sliders
var slidernodes = new Object();
//Array of curent pages on slider
var curentslide = new Object();
//Array of old pages on slider
var oldslide = new Object();
//Array of fadeIn/Out effects
var fadeFx = new Object();
//State of slider (0=fadeout, 1=fadein, 2=break)
var state = new Object();

var ContentSlider = new Class({
    // inicialization of slider
    initialize: function(id, fadeInTime, fadeOutTime, breakTime) {
        this.id = id;
        this.fadeInTime = fadeInTime;
        this.fadeOutTime = fadeOutTime;
        this.breakTime = breakTime;
        slidernodes[this.id] = [];
        fadeFx[this.id] = [];
        curentslide[this.id] = 0;
        oldslide[this.id] = 0;
        var slider = $(this.id);
        var alldivs = $$("div.ContentPage");
        for (var i = 0; i < alldivs.length; i++) {
            if (alldivs[i].parentNode.id == this.id) {
                slidernodes[this.id].push(alldivs[i])
                fadeFx[this.id].push(new Fx.Morph(alldivs[i], { duration: this.fadeInTime, wait: false }));
            }
        }
        if (slidernodes[this.id].length != 0) {
            this.show(curentslide[this.id]);
        }
    },
    //show start page and hide others
    show: function(page) {

        slidernodes[this.id].each(function(item, index) {
            item.setOpacity(0);
            item.setStyle('z-index', '-10');
        });
        slidernodes[this.id][page].setOpacity(1);
        slidernodes[this.id][page].setStyle('z-index', '10');
    },
    //turn page (fadeIn-fadeOut effect)
    turnPage: function(page, isAuto) {
        if (isAuto) {
            var tmp = this.id;
            state[tmp] = 0;
            fadeFx[this.id][curentslide[this.id]].start({ 'opacity': 0.0, 'z-index': -10 }).chain(function() {
                state[tmp] = 1;
                fadeFx[tmp][page].start({ 'opacity': 1.0, 'z-index': 10 }).chain(function() {
                    state[tmp] = 2;
                });
            });
            oldslide[this.id] = curentslide[this.id];
            curentslide[this.id] = page;

        }
        else {   //disable autoTurnPage
            if (typeof window[this.id + "timer"] != "undefined") {
                clearTimeout(window[this.id + "timer"])
            }

            var tmp = this.id;
            if (state[tmp] == 0) {  //if fadeOut
                //Clear old chain
                fadeFx[this.id][oldslide[this.id]].clearChain();

                state[tmp] = 0;
                fadeFx[this.id][oldslide[this.id]].start({ 'opacity': 0.0, 'z-index': -10 }).chain(function() {
                    state[tmp] = 1;
                    fadeFx[tmp][page].start({ 'opacity': 1.0, 'z-index': 10 }).chain(function() {
                        state[tmp] = 2;
                    });
                });

                oldslide[this.id] = curentslide[this.id];
                curentslide[this.id] = page;
            }
            else {  //if fadeIn or break
                //Clear old chain
                fadeFx[this.id][curentslide[this.id]].clearChain();

                state[tmp] = 0;
                fadeFx[this.id][curentslide[this.id]].start({ 'opacity': 0.0, 'z-index': -10 }).chain(function() {
                    state[tmp] = 1;
                    fadeFx[tmp][page].start({ 'opacity': 1.0, 'z-index': 10 }).chain(function() {
                        state[tmp] = 2;
                    });
                });

                oldslide[this.id] = curentslide[this.id];

                curentslide[this.id] = page;
                try {
                    document.getElementById("ctl00_plcMain_ContentSlider_page_" + (oldslide[this.id])).style.background = "url(" + "\'" + "/Images-(1)/greydot.aspx" + "\')";
                    document.getElementById("ctl00_plcMain_ContentSlider_page_" + (page)).style.background = "url(" + "\'" + "/Images-(1)/reddot.aspx" + "\')";
                    document.getElementById("runSlider").style.background = "\'none\'";
                    document.getElementById("ctl00_plcMain_ContentSlider_runSlider").style.background = "\'none\'";
                    
                }
                catch (ex) {
                }

                try {
                    document.getElementById("ctl00_plcMain_CMSEditableRegion1_Slideshow1_ContentSlider_page_" + (oldslide[this.id])).style.background = "url(" + "\'" + "/Images-(1)/greydot.aspx" + "\')";
                    document.getElementById("ctl00_plcMain_CMSEditableRegion1_Slideshow1_ContentSlider_page_" + (page)).style.background = "url(" + "\'" + "/Images-(1)/reddot.aspx" + "\')";
                    document.getElementById("runSlider").style.background = "\'none\'";
                }
                catch (ex) { 
                }

            }
        }
    }
});

// Auto turn page
function autoTurnPage(slider, page, time, turn) {

    
    try
    {
        if (page >= slidernodes[slider.id].length) {
            document.getElementById("ctl00_plcMain_ContentSlider_page_" + (page-1)).style.background = "url(" + "\'" + "/Images-(1)/greydot.aspx" + "\')";
            //
            try {
                document.getElementById("ctl00_plcMain_CMSEditableRegion1_Slideshow1_ContentSlider_page_" + (page - 1)).style.background = "url(" + "\'" + "/Images-(1)/greydot.aspx" + "\')";
            }
            catch (ex) {
            }

            page = 0;
        }

        if(turn)
        {
            slider.turnPage(page,true);
        }

        window[slider.id + "timer"] = setTimeout(function() { autoTurnPage(slider, page + 1, time, true) }, time)
        //pageno = document.getElementById("ctl00_plcMain_ContentSlider_page_0").getElementsByTagName("a");
        //pageno = document.getElementById('ctl00_plcMain_ContentSlider_page_' + page-1);
        try {
            document.getElementById("ctl00_plcMain_ContentSlider_page_" + page).style.background = "url(" + "\'" + "/Images-(1)/reddot.aspx" + "\')";
            document.getElementById("ctl00_plcMain_ContentSlider_page_" + (page - 1)).style.background = "url(" + "\'" + "/Images-(1)/greydot.aspx" + "\')";
        }
        catch (ex) {
        }
        try {
            document.getElementById("ctl00_plcMain_CMSEditableRegion1_Slideshow1_ContentSlider_page_" + page).style.background = "url(" + "\'" + "/Images-(1)/reddot.aspx" + "\')";
            document.getElementById("ctl00_plcMain_CMSEditableRegion1_Slideshow1_ContentSlider_page_" + (page - 1)).style.background = "url(" + "\'" + "/Images-(1)/greydot.aspx" + "\')";
        }
        catch (ex) {
        }
        //document.getElementById("ctl00_plcMain_ContentSlider_page_" + page).setAttribute("class","active");
        //document.getElementById("ctl00_plcMain_ContentSlider_page_" + (page - 1)).style.background = "url(" + "\'" + "/Images-(1)/greydot.aspx" + "\')";
        //document.getElementById("idElement").setAttribute("class", "className");

        
        //alert("The content of the first DIV element is " + "\"" + pageno.style.background +  "\".");
        
    }
    catch (ex)
    {
    }
}
