Kino = {};

Kino.Statistics = {

    generateSeed: function() {

        return (Math.random()*100000);
    },

    setCountPixel: function() {

        Kino.Statistics.Emv.refreshPixel();
        Kino.Statistics.Ivw.refreshPixel();
        Kino.Statistics.Rtt.refreshPixel();
    }
};

Kino.Statistics.Emv = {

    baseUrl: '',
    id: 'kinoPixel',

    setBaseUrl: function(url) {

        this.baseUrl = url;
    },

    refreshPixel: function() {

        var elem = document.getElementById(this.id);
        var seed = Kino.Statistics.generateSeed();

        emvUrl = this.baseUrl + (this.baseUrl.indexOf('?') != -1 ? '&' : '?') + 'seed=' + seed;

        if (elem !== null) {

            $(elem).attr({ src: emvUrl });
        }
    },

    init: function() {

        this.initTabs();
        this.initCarousel();
        this.initPaginator();
        this.initSelector();
        this.initZoom();
    },

    initCarousel: function() {

        var _this = this;
        $('.jcarousel-prev:not(.jcarousel-prev-disabled), .jcarousel-next:not(.jcarousel-next-disabled)').live(
                'click', function(event, ui) {

            _this.clickedActive(ui);
        });
    },

    initTabs: function() {

        var _this = this;
        $('#page').bind('tabsselect', function(event, ui) {

              _this.clickedActive(ui);
        });
    },

    initPaginator: function() {

        var _this = this;
        $('.paginatorLink:not(.active, .disabled)').live('click', function(event, ui) {

            _this.clickedActive(ui);
        });
    },

    initSelector: function() {

        var _this = this;
        $('#selector').bind('change', function(event, ui) {

            _this.clickedActive(ui);
        });
    },

    initZoom: function() {

        var _this = this;
        $('.zoom').live('click', function(event, ui) {

            _this.clickedActive(ui);
        });
    },

    clickedActive: function(element) {

        this.refreshPixel();
    }
};

/**
 * Statistic JS helper for Rtt tracking
 * automaticaly adds events to refresh pixel on yui tab change and caroussel changes.
 */
Kino.Statistics.Rtt = {

    baseUrl: '',
    id: 'rttPixel',

    setBaseUrl: function(url) {

        this.baseUrl = url;
    },

    refreshPixel: function() {

        var elem = document.getElementById(this.id);
        var seed = Kino.Statistics.generateSeed();

        rttUrl = this.baseUrl + (this.baseUrl.indexOf('?') != -1 ? '&' : '?') + 'seed=' + seed;

        if (elem !== null) {

            $(elem).attr({ src: rttUrl });
        }
    },

    init: function() {

        this.initTabs();
        // Below inits should not be needed here; see rtt-subelement action for usage
    },

    initTabs: function() {

        var _this = this;
        $('#page').bind('tabsselect', function(event, ui) {

              _this.clickedActive(ui);
        });
    },

    clickedActive: function(element) {

        this.refreshPixel();
    }
};

Kino.Statistics.Rtt.Subelement = function(baseUrl, containerId) {

    this.baseUrl     = baseUrl;
    this.id          = 'rttPixel_' + containerId;
    this.containerId = containerId;

    this.getId = function() {

        return this.id;
    };

    this.getContainerId = function() {

        return this.containerId;
    };

    this.refreshPixel = function() {

        elem   = document.getElementById(this.getId());
        seed   = Kino.Statistics.generateSeed();
        rttUrl = this.baseUrl + (this.baseUrl.indexOf('?') != -1 ? '&' : '?') + 'seed=' + seed;

        if (elem !== null) {

            $(elem).attr({ src: rttUrl });
        } else if ($(Kino.Statistics.Rtt.id) !== null) {

            $(document.createElement('img'))
                .attr({ src: rttUrl, id: this.getId(), width: 1, height: 1, alt: '' })
                .appendTo($(Kino.Statistics.Rtt.id));
        }
    };

    this.addEventListeners = function() {

        elem = $('#' + this.containerId);
        this.findChildrenByClassName(elem);
    };

    this.findChildrenByClassName = function(elem) {

        var _this = this;
        $(elem).find('.paginatorLink:not(.active, .disabled)').live('click', function(event, ui) {

            _this.refreshPixel();
        });

        $(elem).find('.zoom').live('click', function(event, ui) {

            _this.refreshPixel();
        });
    };
};

Kino.Statistics.Ivw = {

    baseUrl: '',
    id: 'img#ivwPixel',
    ivwCode: '',
    ivwComment: '',

    setBaseUrl: function(url) {

        this.baseUrl = url;
    },
    
    setIvwCode: function(code) {
        
        this.ivwCode = code;
    },
    
    setIvwComment: function(comment) {
        
        this.ivwComment = comment;
    },

    refreshPixel: function() {

        ivwUrl = ivwConfig.baseUrl +
                    '/' + ivwConfig.type +
                    '/' + ('' !== this.ivwCode ? this.ivwCode : ivwConfig.code) +
                    ';' + ivwConfig.comment +
                    '?seed=' + Kino.Statistics.generateSeed();

        if ($(this.id).length > 0) {

            $(this.id).attr({ src: ivwUrl });
        }
    },

    init: function() {

        this.initTabs();
        this.initCarousel();
        this.initPaginator();
        this.initSelector();
        this.initZoom();
    },

    initCarousel: function() {

        var _this = this;
        $('.jcarousel-prev:not(.jcarousel-prev-disabled), .jcarousel-next:not(.jcarousel-next-disabled)').live(
                'click', function(event, ui) {

            _this.clickedActive($(this).parent().parent().data('boxIvwCode'));
        });
    },

    initTabs: function() {

        var _this = this;
        $('div.box.tabs.tabbed').bind('tabsselect', function(event, ui) {

            _this.clickedActive($(this).data('boxIvwCode'));
        });
    },

    initPaginator: function() {

        var _this = this;
        $('.paginatorLink:not(.active, .disabled)').live('click', function(event, ui) {

            _this.clickedActive();
        });
    },

    initSelector: function() {

        var _this = this;
        $('#selector').bind('change', function(event, ui) {

            _this.clickedActive();
        });
    },

    initZoom: function() {

        var _this = this;
        $('.zoom').live('click', function(event, ui) {

            _this.clickedActive();
        });
    },

    clickedActive: function(value) {

        if ('string' === typeof value) {
            
            this.setIvwCode(value);
        }

        this.refreshPixel();
    }
};

Kino.Statistics.Google = {

    init: function() {

        this.initTabs();
        this.initCarousel();
        this.initPaginator();
        this.initSelector();
        this.initZoom();
    },

    initCarousel: function() {

        var _this = this;
        $('.jcarousel-prev:not(.jcarousel-prev-disabled), .jcarousel-next:not(.jcarousel-next-disabled)').live(
                'click', function(event, ui) {

            _this.clickedActive(ui, 'Carousel', 'Click');
        });
    },

    initTabs: function() {

        var _this = this;
        $('#page').bind('tabsselect', function(event, ui) {

              _this.clickedActive(ui, 'Tab', 'Selected');
        });
    },

    initPaginator: function() {

        var _this = this;
        $('.paginatorLink:not(.active, .disabled)').live('click', function(event, ui) {

            _this.clickedActive(ui, 'Paginator', 'Click');
        });
    },

    initSelector: function() {

        var _this = this;
        $('#selector').bind('change', function(event, ui) {

            _this.clickedActive(ui, 'Selector', 'Change');
        });
    },

    initZoom: function() {

        var _this = this;
        $('.zoom').live('click', function(event, ui) {

            _this.clickedActive(ui, 'Zoom', 'Click');
        });
    },

    clickedActive: function(element, category, action, optionalLabel, optionalIntegerValue) {

        _gaq.push(['_trackEvent', category, action, optionalLabel, optionalIntegerValue]);
    }
};
