﻿function LandingGallery(data, snippet, div) {
    this.lg_intervalID;
    this.current_item = 0;
    this.lg_snp = snippet;
    this.lg_data = data;
    this.contentDIV = div;
    this.lg_data[0].isNew = false;
    this.nrOfApts = this.lg_data.length;
    this.isPaused = false;
    this.animationRunning = false;
    jQuery("#modLandingGallery").mouseenter(function() {
        GalleryObject.stopAnimation();
    });
    jQuery("#modLandingGallery").mouseleave(function() {
        if (!GalleryObject.isPaused && GalleryObject.lg_intervalID == null) {
            GalleryObject.startAnimation();
        }
    });
    this.next = function() {
        if (this.animationRunning)
            return;
        if (jQuery('#PW').css('display') != 'none') {
            return;
        }
        this.animationRunning = true;
        this.current_item += 1;
        if (this.current_item >= this.lg_data.length) {
            this.current_item = 0;
            var content = document.getElementById(this.contentDIV);
            content.innerHTML += content.innerHTML;
            jQuery('#' + this.contentDIV).animate(
                { marginLeft: '-=513' }, 1000, function() {

                    var tmpapts = jQuery('#' + GalleryObject.contentDIV + '>.snippet');
                    for (var i = 0; i < GalleryObject.nrOfApts; i++) {
                        jQuery(tmpapts[i]).remove();
                    }
                    jQuery('#' + GalleryObject.contentDIV).css('margin-left', '0px');
                    GalleryObject.animationRunning = false;
                });


        }
        else {
            if (this.lg_data[this.current_item].isNew == true) {
                this.AppendItem(this.current_item);
                this.lg_data[this.current_item].isNew = false;
            }
            jQuery('#' + this.contentDIV).animate(
                { marginLeft: '-=513' }, 1000, function() { GalleryObject.animationRunning = false; });
        }
    };

    this.prev = function() {
        if (this.animationRunning)
            return;
        this.animationRunning = true;
        this.current_item -= 1;
        if (this.current_item < 0) {
            this.current_item = this.lg_data.length - 1;
            for (var i = 0; i < this.lg_data.length; i++) {
                if (this.lg_data[i].isNew == true) {
                    this.AppendItem(i);
                }
            }
            var content = document.getElementById(this.contentDIV);
            content.innerHTML += content.innerHTML;
            var tmp = 513 * (this.current_item + 1);
            jQuery('#' + this.contentDIV).css('margin-left', '-' + tmp.toString() + 'px');
            jQuery('#' + this.contentDIV).animate(
            { marginLeft: '+=513' }, 1000, function() {
                var tmpapts = jQuery('#' + GalleryObject.contentDIV + '>.snippet');
                for (var i = GalleryObject.nrOfApts; i < 2 * GalleryObject.nrOfApts; i++) {
                    jQuery(tmpapts[i]).remove();
                }
                var tmp = 513 * GalleryObject.current_item;
                jQuery('#' + GalleryObject.contentDIV).css('margin-left', '-' + tmp.toString() + 'px');
                GalleryObject.animationRunning = false;
            });
        }
        else {
            jQuery('#' + this.contentDIV).animate(
                { marginLeft: '+=513' }, 1000, function() { GalleryObject.animationRunning = false; });
        }
    };

    this.Animate = function() {
        if (this.lg_intervalID == null) {
            this.startAnimation();
            this.isPaused = false;
        } else {
            this.stopAnimation();
            this.isPaused = true;
        }
    };

    this.startAnimation = function() {
        if (this.lg_intervalID != null) {
            this.next();
        }
        this.lg_intervalID = setInterval("GalleryObject.next()", 8000);
        jQuery('#lg_pause').css('backgroundPosition', '-49px 0%');

    };
    this.stopAnimation = function() {
        if (this.lg_intervalID != null) {
            clearInterval(this.lg_intervalID);
        }
        this.lg_intervalID = null;
        jQuery('#lg_pause').css('backgroundPosition', '-84px 0%');
    };
    this.AppendItem = function(newItemIndex) {
        this.lg_data[newItemIndex].isNew = false;
        var apt = this.lg_data[newItemIndex];
        var tmp_snp = this.lg_snp;
        tmp_snp = tmp_snp.replace("#img#", apt.IMG);
        tmp_snp = tmp_snp.replace("#imgaptdeturl#", apt.DetailsUrl);
        tmp_snp = tmp_snp.replace("#aptname#", apt.AptName);
        tmp_snp = tmp_snp.replace("#ls#", apt.LivingSpace);
        tmp_snp = tmp_snp.replace("#csm#", apt.CanSleepMax);
        tmp_snp = tmp_snp.replace("#price#", apt.NightFrom);
        tmp_snp = tmp_snp.replace("#apttype#", apt.AptType);
        tmp_snp = tmp_snp.replace("#aptdeturl#", apt.DetailsUrl);
        if (apt.Rating != 0) {
            tmp_snp = tmp_snp.replace("#rating#", apt.Rating);
            var rat = Math.round(parseFloat(apt.Rating.replace(',', '.')));
            tmp_snp = tmp_snp.replace("#rl#", rat * 10);
            tmp_snp = tmp_snp.replace("#rr#", 100 - (rat * 10));
            tmp_snp = tmp_snp.replace("#ratstyle#", "");
            tmp_snp = tmp_snp.replace("#ratstyle2#", "");
            tmp_snp = tmp_snp.replace("#surveyurl#", apt.CommentsUrl);
        }
        else {
            tmp_snp = tmp_snp.replace("#rating#", "");
            tmp_snp = tmp_snp.replace("#rl#", "");
            tmp_snp = tmp_snp.replace("#rr#", "");
            tmp_snp = tmp_snp.replace("#ratstyle#", "display:none;");
            tmp_snp = tmp_snp.replace("#ratstyle2#", "display:none;");
            tmp_snp = tmp_snp.replace("#surveyurl#", "");
        }
        var content = document.getElementById(this.contentDIV);
        content.innerHTML += tmp_snp;
    };
}
