﻿var total;
var objImage;
var allObjects;
var delay = 10000;
var HomeScrollTimer;

$(document).ready(function () {
    HomeSlider.init();
});



var HomeSlider = {

    init: function () {
        allObjects = $('#HOME_SLIDING_CONTENT_PANELS_CONTENT').children();
        if (allObjects.length > 1) {
            total = allObjects.length;
            objImage = 1;
            $.each(allObjects, function () {
                $(this).css({ 'position': 'absolute', 'z-index': ((total + 1) - objImage), 'height': '320px' });

                if (objImage != 1) {
                    $(this).css({ 'opacity':'0.00' });

                }
                $(this).attr('id', 'sc-' + objImage);
                objImage += 1;
            });
            objImage = 1;
            HomeScrollTimer = setTimeout("HomeSlider.scrollMe('#sc-" + objImage + "','Left')", delay);
        }
    },



    scrollMe: function (objName, direction) {
        clearTimeout(HomeScrollTimer);
        //alert(allObjects.length);
        //alert(direction);

        //objImage is current on leaving

        if (objName == 0) {
            objName = "#sc-" + objImage;
        }

        var newPanel = objImage >= total ? newPanel = 1 : newPanel = objImage + 1;


        //fadeIn the new panel
        var nP = $('#sc-' + newPanel);
        //np.animate({ opacity: 1 }, 300,'linear', function () { });

        nP.animate({ opacity: 1,
            left: '0'
        }, 1000, 'linear', function () { });
        //fadeOut the old Panel

        var leftPanelVal;
        if (direction == "Left") {
            leftPanelVal = "-900";
        }
        else {
            leftPanelVal = "900";
        }

        var newDirection = "";
        if (direction == "Left") {
            newDirection = "Right";
        } else {
            newDirection = "Left";
        }

        var e = $(objName);
        e.animate({ opacity: 0.00,
            left: leftPanelVal
        }, 1000, 'linear', function () {
            objImage >= total ? objImage = 1 : objImage += 1;
            $.each(allObjects, function () { var zi = $(this).css('z-index'); zi += 1; $(this).css({ 'z-index': zi }); });
            e.css({ 'left': '0px', 'z-index': '1' });
            HomeScrollTimer=setTimeout("HomeSlider.scrollMe('#sc-" + objImage + "','" + newDirection + "')", delay);
        });



    }

};



