
var App = (function ($) {
    var App = {};
    App.BASE_URL = '';
    App.LANG = 'pl';
    App.checkSize = function () {
        var t, f, w, r;
        w = $(window).height();
        t = $('#topbox').height();
        f = $('#footbox').height();
        r = w - (t + f);
        if (r > 0) {
            $('#footbox').css('margin-top', r + 'px');
        } else {
            $('#footbox').css('margin-top', '0px');
        }
    };
    $(document).ready(function () {
        App.checkSize();
        setInterval(App.checkSize, 1);
    });
    
    
    (App.AjaxPager = function () {
        this.init.apply(this, arguments);
    }).prototype = {
        index: 0,
        newIndex: 0,
        timer: 0,
        loading: false,
        init : function (options) {
            var that = this;
            this.opts = {
                scaleBox: true
            };
            $.extend(this.opts, options);
            this.ui = {
                'box': $('#' + this.opts.boxId),
                'content': $('#' + this.opts.contentId)
            };
            this.ui.pager = this.ui.box.find('.pager a');
            this.maxIndex = this.ui.pager.length;
            this.ui.content.css({
                'overflow': 'hidden',
                'height': this.ui.content.height() + 'px'
            });
            this.ui.pager.each(function (index, element) {
               var item = $(element);
               item.click(function (e) {
                   e.stopPropagation();
                   e.preventDefault();
                   that.load(index);
               });
               if (item.hasClass('selected')) {
                   that.index = index;
               }
            });
            
            var hh, ch, html;
            html = that.ui.content.find('.content');
            hh = html.outerHeight();
            ch = that.ui.content.height();
            if (hh < ch) {
                html.css({
                    paddingTop: '+' + Math.round((ch - hh) / 2) + 'px'
                });
            }
        },
        load : function (index) {
            if (this.loading || this.index == index) {
                return;
            }
            this.loading = true;
            var that = this;
            this.newIndex = index;
            this.startLoader();
            this.ui.content.css({
                'height': this.ui.content.height() + 'px'
            });
            this.ui.content.find('.content').animate({
                'opacity' : 0.7
            }, 100);
            $.get(this.opts.url.replace('{page}', (index + 1) + ''), function (html) {
                that.ui.content.find('.content').stop().animate({
                    'opacity': 0
                }, 200, function () {
                    that.ui.content.empty();
                    html = $(html);
                    html.css({
                        'opacity': 0
                    });
                    that.ui.content.html(html);
                    if (that.opts.scaleBox) {
                        that.ui.content.animate({
                            'height': that.ui.content.find('.content').outerHeight() + 'px'
                        }, 200, function () {
                            that.ui.content.find('.content').animate({
                                'opacity': 1
                            }, 200, function () {
                                that.stopLoader();
                            });
                        });
                    } else {
                        var hh, ch;
                        hh = html.outerHeight();
                        ch = that.ui.content.height();
                        if (hh < ch) {
                            html.css({
                                paddingTop: '+' + Math.round((ch - hh) / 2) + 'px'
                            });
                        }
                        that.ui.content.find('.content').animate({
                            'opacity': 1
                        }, 200, function () {
                            that.stopLoader();
                        });
                    }
                });
            });
        },
        
        startLoader : function () {
            var that = this;
            this._stopLoading = false;
            $(this.ui.pager[this.index]).css({
                'opacity' : 0.5
            });
            this.timer = setInterval(function () {
                that.loader();
            }, 150);
        }, 
        
        stopLoader : function () {
            this._stopLoading = true;
        },
        
        loader : function () {
            var button = $(this.ui.pager[this.newIndex]);
            if (button.hasClass('selected') && !this._stopLoading) {
                button.removeClass('selected');
            } else {
                button.addClass('selected');
            }
            if (this._stopLoading) {
                $(this.ui.pager[this.index]).removeClass('selected').css({
                    'opacity' : 1
                });
                clearInterval(this.timer);
                this.index = this.newIndex;
                this.loading = false;
            }
        }
    };
    
    
    return App;
}(jQuery));


(function ($) {
    
    $.fn.tabify = function (options) {
        var selector = this, tabs, buttons, opts;
        opts = {
            'tabSelector' : '.tab',
            'contentSelector' : '.tab-content'
        };
        $.extend(opts, options);
        tabs = selector.find(opts.contentSelector);
        buttons = selector.find(opts.tabSelector);
        if (selector.data('tabified')) {
            buttons.unbind('click.tabify');
        }
        buttons.each(function (index, element) {
            var button, tab;
            if (tabs[index]) {
                button = $(this);
                tab    = $(tabs[index]);
                button.bind('click.tabify', function (e) {
                    e.preventDefault();
                    e.stopPropagation();
                    if (!button.hasClass('selected')) {
                        buttons.removeClass('selected');
                        tabs.hide();
                        button.addClass('selected');
                        tab.show();
                    }
                });
            }
        });
        selector.data('tabified', true);
    };
    
}(jQuery));


