var page = {
    init: function() {
        $('a').click(function() {
            this.blur();
        });

        this.initAutoScroll();
        this.initTabs();
        this.initLightBoxes();
    },
    initLightBoxes: function() {
//        Shadowbox.init();
        $('.lightbox').colorbox({maxWidth: '90%'});
    },
    initAutoScroll: function() {
        var vAutoScrollDist = 23;
        $('.autoscroll').each(function() {
            var vMoveable = $('<div style="width: ' + $(this).width() + 'px; height: ' + $(this).height() + 'px;"></div>'),
                vWrapper = vMoveable.clone();
            var vTop = $(this).position().top;
            vMoveable.css('position', 'absolute');
            vMoveable.css('top', vTop);
            vWrapper.css('margin', $(this).css('margin'));
            $(this).wrap(vWrapper);
            $(this).wrap(vMoveable);
            vMoveable = $(this).parent();
            var fScrollNow = function(){
//                var vAutoScrollDist = $(window).height() / 2 - vMoveable.height() / 2;
                var vScrollTop = $('body').scrollTop();
                if(!vScrollTop)
                    vScrollTop = $(document).scrollTop();
                vMoveable.stop();
                var vNewTop = vScrollTop > vTop - vAutoScrollDist ? vScrollTop + vAutoScrollDist : vTop;
                vMoveable.animate({top: vNewTop}, 500);
            };
            $(window).scroll(fScrollNow);
            fScrollNow();
        });

        setTimeout(function() {
            $.post(
                '/index/socialmedia',
                {},
                function(pReturn) {
                    var vDivs = $('div');
                    for(var n = 0; n < vDivs.length; n++) {
                        var vDiv = $(vDivs.get(n));
                        if(vDiv.attr('id') != 'socialMediaIcons')
                            continue;
                        
                        vDiv.fadeIn(500);
                        return vDiv.html(pReturn);
                    }
                }
            );
        }, 10);
    },
    initTabs: function() {
        /**
         * safari + chrome spacer debug
         **/
        if(!$.browser.webkit)
            $('.eluTabs .inactiveSpacer').css('margin-left', '-3px');

        $('.eluTabs').each(function() {
            var $box = $(this),
                $tabButtons = $(this).find('.tabButtons td'),
                $currentButton = $box.find('.active');

            $tabButtons.each(function() {
                var $button = $(this),
                    vNum = $button.attr('lang'),
                    $content = $box.find('.eluCon' + vNum),
                    $link = $button.find('a'),
                    vImages = {
                        active: {
                            url: 'url(' + $button.find('.icon_active_url').val() + ')',
                            pos: $button.find('.icon_active_pos').val()
                        },
                        inactive: {
                            url: 'url(' + $button.find('.icon_inactive_url').val() + ')',
                            pos: $button.find('.icon_inactive_pos').val()
                        }
                    };

                $button.data('con', $content);
//                $content.find('.formButton').click(function() {
                $content.find('.tmpAction').click(function() {
                    $(this).fadeOut(50, function() {
                        $content.find('.eluForm').fadeIn(400)
                            .jqTransform();
                    });
                    return false;
                });

                $(this).data('deactivate', function() {
                    $button.removeClass('active');
                    $link.css('background-image', vImages.inactive.url);
                    $link.css('background-position', vImages.inactive.pos);
                });

                var fSelect = function() {
                    if($currentButton.html() == $button.html())
                        return;

                    $currentButton.data('deactivate')();
                    $currentButton.data('con')
                        .stop(false, true)
                        .hide(0, function() {
                        $button.data('con').fadeIn(300);
                    });
                    $currentButton = $button;
                    /*
                    $tabButtons.each(function(){
                        $(this).data('deactivate')();
                    })
                    */
                    $button.addClass('active');
                    $link.css('background-image', vImages.active.url);
                    $link.css('background-position', vImages.active.pos);
                };
                
                var vOver = false;
                $button.hover(function() {
                    vOver = true;
                    setTimeout(function() {
                        if(vOver)
                            fSelect();
                    }, 30);
                }, function() {
                    vOver = false;
                });

                $button.click(function() {
                    fSelect();
                    return false;
                });
            });
        });
    }
};

$(window).load(function() {page.init();});
