var Navigation = function() {
    this.ACTIVATION_DELAY = 300;
    this.DEACTIVATION_DELAY = 200;

    this.activeTab;
    this.lastActiveTab;
    this.activationTimer;
    this.deactivationTimer;

    this.showTab = function(tabObj) {
        if (this.activeTab === tabObj.index()) {
            this.lastActiveTab = tabObj.index();

            tabObj.find('.spacer').attr('style', 'display:none;');
            tabObj.prev().find('.spacer').attr('style', 'display:none;');
            tabObj.addClass('active');
        }
    };

    this.hideTab = function(tabObj) {
        if (this.activeTab !== tabObj.index()) {
            tabObj.find('.spacer').attr('style', 'display:block;');
            tabObj.prev().find('.spacer').attr('style', 'display:block;');
            tabObj.removeClass('active');
        }
    }

    this.setup = function() {
        var instance = this;

        $('.navigation2 .menu-item').hover(
            function() {
                instance.activeTab = $(this).index();

                if (instance.lastActiveTab === $(this).index()) {
                    clearTimeout(instance.deactivationTimer);
                }

                var tabObj = $(this);

                var closure = function() {
                    instance.showTab(tabObj);
                };

                instance.activationTimer = setTimeout(closure, instance.ACTIVATION_DELAY);
            },
            function() {
                instance.activeTab = null;
                clearTimeout(instance.activationTimer);

                var tabObj = $(this);

                var closure = function() {
                    instance.hideTab(tabObj);
                };

                instance.deactivationTimer = setTimeout(closure, instance.DEACTIVATION_DELAY);
            }
        );
    }

    this.setup();
};

$(function() {
    var nav = new Navigation();
});

$(document).ready(function() {

        $('input[name="payment"]').click(function() {
            if ($('input[name="payment"]:checked').val() == 15)
            {
                $('#ideal-select').show();
                $('#ideal-description').hide();
            }
            else
            {
                $('#ideal-select').hide();
                $('#ideal-description').show();
            }
        });

            if ($('input[name="payment"]:checked').val() == 15)
            {
                $('#ideal-select').show();
                $('#ideal-description').hide();
            }
            else
            {
                $('#ideal-select').hide();
                $('#ideal-description').show();
            }

        $('.contact-form').fancybox({
            'type' : 'iframe',
            'width' : 676,
            'height' : 605,
            'titleShow' : false,
            'autoScale' : false,
            'showCloseButton' : true,
            'autoDimensions' : false,
            'centerOnScroll' : false
            });
        
        $('.stock-notify').fancybox({
            'type' : 'iframe',
            'width' : 676,
            'height' : 605,
            'titleShow' : false,
            'autoScale' : false,
            'showCloseButton' : true,
            'autoDimensions' : false,
            'centerOnScroll' : false
            });

        $('#navigation>ul>li').hover(function(){
                $(this).addClass('hover');
                if($.browser.msie && $.browser.version == '6.0') {
                $('select').css('visibility', 'hidden');
                }
                }, function(){
                $(this).removeClass('hover');
                if($.browser.msie && $.browser.version == '6.0') {
                $('select').css('visibility', 'visible');
                }
                });
        $('#category-dropdown').change(function(){
                if($(this).val() != 1) {
                window.location.href = $(this).val();
                }
                });
        $('.filter-dropdown').change(function(){
                if($(this).val() != 1) {
                window.location.href = $(this).val();
                }
                });

        $('.reveal').click(function(){
                reveal = $(this).attr('reveal');
                if(reveal != '')
                {
                state = $('#' + reveal).css('display');
                if(state == 'none')
                {
                $('#' + reveal).slideDown(400);
                }
                }
                return false;
                });

        $('.toggle').click(function(){
                reveal = $(this).attr('reveal');
                if(reveal != '')
                {
                state = $('#' + reveal).css('display');
                if(state == 'none')
                {
                $('#' + reveal).slideDown(400);
                }
                else
                {
                $('#' + reveal).slideUp(400);
                }
                }
                return false;
                });

        $('.blog_popup').each(function() {
                $(this).fancybox({
                    'href' : '/ajax/index/content-blog?name=' + encodeURI($(this).attr('blog_article')),
                    'centerOnScroll' : true,
                    'titleShow' : false
                    });
                });

        $('.popup').bind('click',function(e){popwindow(e)});
        $('.filtertoggle').click(function(){
                var obj = $(this);
                var reveal = $(this).attr('reveal');

                if(reveal != '')
                {
                    state = $('#' + reveal).css('display');

                    if(state == 'none')
                    {
                        $(this).html('Less<span class="selected"></span>');
                        $('#' + reveal).slideDown(400);
                        $('.' + reveal + '-hide').css('display', 'none');
                    }
                    else
                    {
                        var anchorObj = $('#' + reveal);
                        var docViewTop = $(window).scrollTop();
                        var docViewBottom = docViewTop + $(window).height();

                        var elemTop = anchorObj.offset().top;
                        var elemBottom = elemTop + $(anchorObj).height();

                        if ((elemTop <= docViewTop) || (elemTop >= docViewBottom))
                        {
                            $('html').scrollTop(elemTop);
                            $('body').scrollTop(elemTop);
                        }

                        $(this).html('More<span></span>');

                        $('#' + reveal).slideUp(400);
                        $('.' + reveal + '-hide').css('display', 'inline');
                    }
                }

        return false;
        });
});


function popupWindow(href, width, height)
{
    window.open(href, "myWindow",  "status=1,height="+height+",width="+width+",resizable=0" );
};


