﻿function SlideShow(_Container, _showingImageCount, _ChangeItemsCount, _gallaryMoveSpeed, _speed, _DelayTime, _padding) {
    this.showingImageCount = _showingImageCount;
    this.ChangeItemsCount = _ChangeItemsCount;
    this.gallaryMoveSpeed = _gallaryMoveSpeed;
    this.speed = _speed;
    this.padding = _padding;
    this.tempLeft;
    this.tempTop;
    this.movingSpeed = 800;
    this.current;
    this.allItems = new Array();
    this.arrShowImages = new Array();
    this.previousSelectDiv;
    this.lf = 0; this.gallaryImageWidth = 0; this.gallaryImageHeight = 0; this.maxLeft = 0;
    this.current = 0;
    this.Container = _Container;
    this.IsBeastDeals = false;
    this.IsPlay = false;
    this.TimeOutID = 0;
    this.DelayTime = _DelayTime;
    this.DivSlideShow;
    this.AnimateGallery = true;
}
SlideShow.prototype.Run = function(isPlay) {
    this.allItems = this.GetAllItems();
    if (this.allItems.length > 0)
        this.showInfo(this.allItems[0]);
    this.SetGallaryAnimation();
    if (isPlay) {
        this.IsPlay = true;
        this.play();
    }
}
SlideShow.prototype.RunSlideShow = function(isPlay, divSlideShow, animateGallery) {
    this.IsBeastDeals = true;
    this.AnimateGallery = animateGallery;
    this.DivSlideShow = divSlideShow;
    this.Run(isPlay);
}
SlideShow.prototype.GetAllItems = function() {
    var obj = this;
    var s = this.getElementsByClassName("list")
    for (var k = 0; k < s.length; k++) {
        s[k].onclick = function() { obj.showInfo(this); }
        if (k < this.showingImageCount) {
            this.arrShowImages.push(k);
        }
    }
    return s;
}
//Show------------------------------------------------------------------------------------------------
SlideShow.prototype.showInfo = function(currenDiv) {
    if (this.IsBeastDeals) {
        if (this.previousSelectDiv != null) this.previousSelectDiv.className = "Deals_bg_On";
        var s = currenDiv.getElementsByTagName("div");
        for (var k = 0; k < s.length; k++) {
            if (s[k].className == "showInfo") {
                $(document.getElementById(this.DivSlideShow)).css({ opacity: 0.0 });
                document.getElementById(this.DivSlideShow).innerHTML = s[k].innerHTML;
                $(document.getElementById(this.DivSlideShow)).animate({ opacity: 1.0 }, this.speed)
            }
            else
                if (s[k].className == "Deals_bg_On" || s[k].className == "Deals_bg_Off") {
                s[k].className = "Deals_bg_Off";
                this.previousSelectDiv = s[k];

            }
        }
    }
}

//Get Items Count-------------------------------------------------------------------
SlideShow.prototype.GetPageCount = function() {
    return this.allItems.length;
}

//Setting---------------------------------------------------------------------------
SlideShow.prototype.SetGallaryAnimation = function() {
    var cc = this.GetPageCount();
    if (cc > 0) {
        var imgTemp;
        imgTemp = this.allItems[0];
        //        if (children.length > 0)
        //            imgTemp = children[0];
        if (imgTemp != null) {
            this.gallaryImageWidth = parseInt(getStyle(imgTemp, "width")) + this.padding;
            this.gallaryImageHeight = parseInt(getStyle(imgTemp, "height")) + this.padding;
            if (!this.gallaryImageWidth) this.gallaryImageWidth = 0;
            if (!this.gallaryImageHeight) this.gallaryImageHeight = 0;
        }
    }
    this.maxLeft = parseInt((cc - this.showingImageCount) * this.gallaryImageWidth) * -1;
    var gal = document.getElementById(this.Container);
    if (gal != null && this.AnimateGallery == true) {

        gal.style.display = 'block';
        gal.style.width = ((this.gallaryImageWidth * this.showingImageCount)) + "px";
        gal.style.position = "relative";
        gal.style.overflow = "hidden";
        var al = document.getElementById(this.Container + "Items");
        al.style.position = "relative";
        
        al.style.width=(this.gallaryImageWidth * this.GetPageCount())+"px";
    }
}

//Move All Gallary Images previous--------------------------------------------------------
SlideShow.prototype.prv = function() {
    if (this.lf != 0) {
        this.current--;
        if (this.current >= 0)
            this.showInfo(this.allItems[(this.current) * (this.ChangeItemsCount)]);
        for (var j = 0; j < this.showingImageCount; j++)
            this.arrShowImages[j] = parseInt(this.arrShowImages[j]) - 1;
        this.lf = parseInt(this.lf) + this.ChangeItemsCount * this.gallaryImageWidth;
        var MyDiv = document.getElementById(this.Container + "Items");
        if (this.AnimateGallery == true) $(MyDiv).animate({ left: +this.lf }, this.gallaryMoveSpeed);
        return true;
    }
    return false;
}

//Move All Gallary Images Next-------------------------------------------------------------------------
SlideShow.prototype.nxt = function() {
    if (this.lf > this.maxLeft) {
        this.current++;
        if (this.current < this.allItems.length)
            this.showInfo(this.allItems[(this.current) * (this.ChangeItemsCount)]);
        for (var j = 0; j < this.showingImageCount; j++)
            this.arrShowImages[j] = parseInt(this.arrShowImages[j]) + 1;
        this.lf = parseInt(this.lf) - this.ChangeItemsCount * this.gallaryImageWidth;
        var MyDiv = document.getElementById(this.Container + "Items");
        if (this.AnimateGallery == true) $(MyDiv).animate({ left: this.lf }, this.gallaryMoveSpeed); // { queue: false, duration: 1000 })
        return true;
    }
    return false;
}
//-----------------------------
SlideShow.prototype.First = function() {
    while (this.prv());
}
//------------------------------------------
SlideShow.prototype.Last = function() {
    while (this.nxt());

}
//Move All Play/Pause---------------------------------------------------------------------
SlideShow.prototype.PlayPause = function(img) {
    this.IsPlay = !this.IsPlay;
    if (this.IsPlay) {
        img.src = "../../../Images/btnPause.png"
        this.play();
    }
    else {
        img.src = "../../../Images/btnPlay.png";
        clearTimeout(this.TimeOutID);
    }
}
//Move Play Function---------------------------------------------------------------------
SlideShow.prototype.play = function() {
    var obj = this;
    if (!this.nxt())
    { while (this.prv()); }
    this.TimeOutID = setTimeout(function() { obj.play(); }, this.DelayTime);
}
//Get Current Page-----------------------------------------------------------------------
SlideShow.prototype.GetCurrentPage = function() {
    return this.current;
}
//Get Style of Element-----------------------------------------------------------------------------------
function getStyle(oElm, strCssRule) {
    var strValue = "";
    if (document.defaultView && document.defaultView.getComputedStyle) {
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    }
    else if (oElm.currentStyle) {
        strCssRule = strCssRule.replace(/\-(\w)/g, function(strMatch, p1) {
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }

    return strValue.split("px")[0];
}
//Get Elmenet  By Class Name---------------------------------------------------------
SlideShow.prototype.getElementsByClassName = function(cl) {
    var retnode = [];
    var myclass = new RegExp('\\b' + cl + '\\b');
    var elem = document.getElementById(this.Container).getElementsByTagName('*');
    for (var i = 0; i < elem.length; i++) {
        var classes = elem[i].className;
        if (myclass.test(classes)) retnode.push(elem[i]);
    }
    return retnode;
}; 



