$(document).ready(
    function() {
        bind(this);
    }
);

var href = window.location.href;
var anchor = href.indexOf('#');
anchor = anchor != -1 ? href.substring(anchor + 1, href.length) : null;

var popupShow = function(options) {
    
    var popup = $('#popup').get(0);
    var shadow = $('#shadow').get(0);
    
    $('#popup-content', popup).html(options.data);
    
    $(shadow).height($(document).height());
    $(popup).css({'top':$(window).scrollTop()+50,'left':parseInt(($(window).width()-$(popup).width())/2)});
    
    if ($.browser.msie == true) {
        $(shadow).show();
        $(popup).show();
    }
    else {
        $(shadow).animate({'opacity':'show'},'slow');
        $(popup).animate({'opacity':'show'},'slow');
    }
    
    bind(popup);
    
}

var popupHide = function() {
    
    var popup = $('#popup').get(0);
    var shadow = $('#shadow').get(0);
    
    if ($.browser.msie == true) {
        $(shadow).hide();
        $(popup).hide();
    }
    else {
        $(shadow).animate({'opacity':'hide'}, 'slow');
        $(popup).animate({'opacity':'hide'}, 'slow');
    }
    
}

var tooltipShow = function(options) {
    
    var tooltip = $('#tooltip').clone().removeAttr('id').insertAfter('#tooltip').get(0);
    
    $('.tooltip-content-m', tooltip).html(options.data);
    $('.tooltip-shadow-m', tooltip).html(options.data);
    
    var h = $(tooltip).height();
    var x = options.event.pageX - 50;
    var y = options.event.pageY - h - 15;
    var timeout = 3000;
    
    if ($.browser.msie == true) {
        $(tooltip).css({'top':y,'left':x}).show(10, function() {
            setTimeout(function() {
                $(tooltip).hide(0, function() {
                    $(tooltip).remove();
                });
            }, timeout);
        });
    }
    else {
        $(tooltip).css({'top':y,'left':x}).animate({'opacity':'show'}, 'slow', function() {
            setTimeout(function() {
                $(tooltip).animate({'opacity':'hide'}, 'slow', function() {
                    $(tooltip).remove();
                });
            }, timeout);
        });
    }
    
}

var bind = function(context) {
    
    $.ifixpng('/images/emp.gif');
    $('*', context).ifixpng(); 
    
    $('.text:not(.toggle) .hidden', context).hide();
    
    if (anchor != null) {
        $("a[name='" + anchor + "']", context).parents('.text').toggleClass('toggle').children('.hidden').slideToggle('slow');
    }
    
    $('#menu div.item', context).hover(
        function() {
            $(this).addClass('selected').children('div.submenu').slideDown();
        },
        function() {
            $(this).removeClass('selected').children('div.submenu').slideUp();
        }
    );
    
    $('.popup', context).click(
        function(e) {
            $.get($(this).attr('popup'), {}, function(data) {
                popupShow({'data':data,'event':e});
            });
            return false;
        }
    );
    
    $('.scrollable', context).scrollable({'size':3,'next':'.scroll-next','prev':'.scroll-prev','speed':500});
    
    $('.image-change', context).click(
        function() {
            var href = $(this).attr('href');
            var name = $(this).attr('name');
            var popup = $(this).attr('popup');
            $('.model-loupe-plus a').attr('href', popup);
            if (href == null || name == null) {
                return false;
            }
            $(this).parent().trigger('click');
            $(name).fadeOut('normal', function() {
                $(this).attr('src', href).load(function() {
                    var p = parseInt(($(this).parent().height() - $(this).height()) / 2);
                    if (p > 0) {
                        $(this).css({'paddingTop':p,'paddingBottom':p});
                    }
                    $(this).fadeIn('normal');
                });
            });
            return false;
        }
    );
    
    $('#popup-close', context).click(
        function(e) {
            popupHide();
        }
    );
    
    $(window, context).scroll(
        function(e) {
            $('#popup').dequeue().animate({'top':$(window).scrollTop() + 50,'left':parseInt(($(window).width() - $('#popup').width()) / 2)}, 'slow');
        }
    );
    
    $(window, context).resize(
        function(e) {
            $('#shadow').height($(document).height())/*.width($(window).width())*/;
            $('#popup').dequeue().animate({'top':$(window).scrollTop() + 50,'left':parseInt(($(window).width() - $('#popup').width()) / 2)}, 'slow');
        }
    );
    
    $('.form-submit', context).click(
        function() {
            $(this).parents('form').submit();
            return false;
        }
    );
    
    $('.ajax-form-submit', context).click(
        function(e) {
            $(this).parents('form').ajaxForm({
                'success': function(data) {
                    tooltipShow({'data':data,'event':e});
                }
            }).submit();
            return false;
        }
    );
    
    /*$('.reload-cart').click(
        function() {
            $('#header-goods').load('/ #header-goods', {}, function() {
                $(this).replaceWith($(this).html());
            });
            return false;
        }
    );*/
    
    $('.recalc-cart').click(
        function() {
            $(this).parents('tr').find('input').val(0);
            $(this).parents('form').submit();
            return false;
        }
    );
    
    $('.add-to-cart').click(
        function(e) {
            $(this).parents('form').ajaxForm({
                'success': function(data) {
                    tooltipShow({'data':data,'event':e});
                    $('#header-goods').load('/ #header-goods', {}, function() {
                        $(this).replaceWith($(this).html());
                    });
                }
            }).submit();
            return false;
        }
    );
    
    $('.text .toggle a', context).click(
        function() {
            $(this).parents('.text').toggleClass('toggle').children('.hidden').slideToggle('slow');
            return false;
        }
    );
    
    $('input', context).change(
        function() {
            if ($(this).parents('form').size()) {
                return;
            }
            var current = this;
            $('form input').each(
                function() {
                    if ($(this).attr('name') == $(current).attr('name')) {
                        $(this).val($(current).val());
                    }
                }
            );
        }
    );
    
    /*$('.ul.catalogue > .li:not(.current)').next('.ul').hide();
    
    $('.ul.catalogue > .li > a').click(
        function() {
            $(this).parent().toggleClass('current').next('.ul').toggle();
            return false;
        }
    );*/
    
    /*$('#header-r .form-input input').each(
        function() {
            $(this).attr('default', $(this).val());
        }
    );
    
    $('#header-r .form-input input').focus(
        function() {
            if ($(this).val() == $(this).attr('default')) {
                $(this).val('');
            }
        }
    );

    $('#header-r .form-input input').blur(
        function() {
            if ($(this).val() == '') {
                $(this).val($(this).attr('default'));
            }
        }
    );*/
    
}
