﻿var Promo = function(elem, baseId, imgIdTag, renderFn, trackFn) {
    this.linkedElem = null;
    this.baseId = null;
    this.imgIdTag = null;
    this.renderFn = null;
    this.trackFn = null;

    this.butTrail = '';
    this.postUrl = null;
    this.animWidth = 616;
    this.itemWidth = 4;
    this.statsRegisterCount = 0;    // 0 - no stats registered


    this.objects = new Array();
    this.moving = false;
    this.curPage = 1;
    this.numPages = 0;

    // required params
    this.linkedElem = elem;
    this.baseId = baseId;
    this.imgIdTag = imgIdTag;
    this.renderFn = renderFn;
    this.trackFn = trackFn;

    // additional settings
    this.setButtonTrailer = function(butVal) {
        this.butTrail = ' ' + butVal;
    }
    this.setAnimWidth = function(width) {
        this.animWidth = width;
    }
    this.setPostUrl = function(url) {
        var wib_prefix = /(\/funda\/|\/fundainbusiness\/)/.exec(window.location.href);
        if (wib_prefix) {
            this.postUrl = wib_prefix[0] + url;
        } else {
            this.postUrl = "/" + url;
        }
    }
    this.setItemWidth = function(width) {
        this.itemWidth = width;
    }
    this.setStatsRegisterCount = function(num) {
        this.statsRegisterCount = num;
    }
    var dizz = this;

    // link buttons
    $(document).ready(function() {
        $('#' + dizz.linkedElem + ' a.promo-arr-l').click(function() {
            if (!$(this).hasClass('disabled')) {
                dizz.click(-1);
            }
        });
        $('#' + dizz.linkedElem + ' a.promo-arr-r').click(function() {
            if (!$(this).hasClass('disabled')) {
                dizz.click(1);
            }
        });
    });


    // other functions
    this.addObjectData = function(obj) {
        var nr = this.objects.length;
        var newObj = $.extend(obj, { num: nr });
        this.objects[nr] = new this.PromoObject(this, obj);
    };


    this.display = function() {
        // this function will re-display existing items
        this.numPages = Math.ceil(this.objects.length / this.itemWidth);

        // set initial display
        var html = '<ul id="' + this.baseId + '">';

        for (var i = 0; i < this.objects.length; i++) {
            html += this.renderFn(this.objects[i].data);
        }
        html += '</ul>';

        $('#' + this.linkedElem + ' div.hp-promo-container').html(html);

        this.setButtons();
        this.trackFn();
        this.loadSomeMore(5, 5);
    };

    this.setButtons = function() {
        if (this.objects.length > this.itemWidth) {
            $('#' + this.linkedElem + ' a.promo-arr-l').show();
            $('#' + this.linkedElem + ' a.promo-arr-r').show();
            if (this.curPage == 1) {
                $('#' + this.linkedElem + ' a.promo-arr-l').addClass('disabled');
                $('#' + this.linkedElem + ' a.promo-arr-l').attr('title', '');
            }
            else {
                $('#' + this.linkedElem + ' a.promo-arr-l').removeClass('disabled');
                $('#' + this.linkedElem + ' a.promo-arr-l').attr('title', FTranslate.Get('vorige5') + this.butTrail);
            }
            if (this.curPage < this.numPages) {
                $('#' + this.linkedElem + ' a.promo-arr-r').removeClass('disabled');
                $('#' + this.linkedElem + ' a.promo-arr-r').attr('title', FTranslate.Get('volgende5') + this.butTrail);
            }
            else {
                $('#' + this.linkedElem + ' a.promo-arr-r').addClass('disabled');
                $('#' + this.linkedElem + ' a.promo-arr-r').attr('title', '');
            }
        } else {
            $('#' + this.linkedElem + ' a.promo-arr-l').hide();
            $('#' + this.linkedElem + ' a.promo-arr-r').hide();
        }
    };

    this.click = function(dx) {
        if (this.moving === false) {

            if (this.curPage + dx > this.numPages
             || this.curPage + dx < 1) return;

            this.statsRegisterCount = (this.curPage * 5) + 5;

            this.curPage += dx;
            this.moving = true;


            // the loading of additional images / registering stats (click)
            for (var i = 0; i < 10; i++) {
                statNum = (this.curPage * 5) + i - 5;
                if (statNum < this.objects.length) {
                    this.objects[statNum].LoadImage();

                    if (this.statsRegisterCount > 0             /* registering stats requested */
                     && this.postUrl != null                    /* we have valid post Url */
                     && !this.objects[statNum].statReg          /* no stats registered for this object */
                     && statNum < this.statsRegisterCount) {    /* objectNumber in register range */
                        $.post(this.postUrl, { id: this.objects[statNum].data.objectId });
                        this.objects[statNum].statReg = true;
                    }
                }
            }
            // jQuery animation
            var dizz = this;
            $('#' + this.baseId).animate({ left: '+=' + (this.animWidth * dx * -1) }, 850, 'swing', function() {
                dizz.moving = false;
            });

            this.setButtons();
        }
    };

    this.loadSomeMore = function(start, num) {
        // the loading of additional images / registering stats (click)
        for (var i = start; i < start + num; i++) {
            if (i < this.objects.length) {
                this.objects[i].LoadImage();

                if (this.statsRegisterCount > 0            /* registering stats requested */
                 && this.postUrl != null                   /* we have valid post Url */
                 && !this.objects[i].statReg               /* no stats registered for this object */
                 && i < this.statsRegisterCount) {   /* objectNumber in register range */
                    $.post(this.postUrl, { id: this.objects[i].data.objectId });
                    this.objects[i].statReg = true;
                }
            }
        }
    };

    this.PromoObject = function(parent, data) {
        this.parent = parent;
        this.data = $.extend(data, { imgLoaded: false });

        // register stats (gevonden)
        if (parent.postUrl != null && data.num < parent.statsRegisterCount) {
            $.post(parent.postUrl, { id: data.objectId });
        }
        this.statReg = (data.num < parent.statsRegisterCount);

        this.LoadImage = function() {
            if (!this.data.imgLoaded) {
                $('#' + this.parent.imgIdTag + this.data.num + ' img')[0].src = this.data.Foto;
                this.data.imgLoaded = true;
            }
        };

    };
};

