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

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

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

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

jQuery.fn.run = function() {
    
    var context = this;
    
    $.ifixpng('/img/blank.gif');
    $('*:not(.noifixpng)', context).ifixpng();
    
    $('.popup', context).click(function(e) {
        var url = $(this).attr('popup');
        if (!url) {
            url = $(this).attr('href');
        }
        if (!url) {
            return false;
        }
        $.get(url, {'template':'96'}, function(data) {
            popupShow({'data':data,'event':e});
        });
        return false;
    });

    $('#popup-c', context).click(function(e) {
        popupHide();
        return false;
    });
    
    $('.focus-blur', context).each(function() {
        $(this).attr('default', $(this).val());
        $(this).bind('focus',function() {
            if ($(this).val() == $(this).attr('default')) {
                $(this).val('');
            }
        });
        $(this).bind('blur',function() {
            if ($(this).val() == '') {
                $(this).val($(this).attr('default'));
            }
        });
    });
    
    $('.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;
    });
    
    /*$('#popup-m-i form', context).ajaxForm({
        'target':'#popup-m-i',
        'data':{'template':'96'},
        'success':function() {
            $(context).run();
        }
    });*/
    
    $('.recalc-cart', context).click(
        function() {
            $(this).parents('tr').hide().find('input').val(0);
            $(this).parents('form').submit();
            return false;
        }
    );
    
    $('.add-to-cart', context).click(function(e) {
        $(this).parents('form').ajaxForm({
            'success':function(data) {
                tooltipShow({'data':data,'event':e});
                $('#head-cart-txt').load('/netcat/modules/default/netshop.php', {'action':'cart'}, function() {
                    ;
                });
            }
        }).submit();
        return false;
    });

    /*$('#region-select').change(function() {
        $(this).parents('form').ajaxForm({
            'success':function(data) {
                $('#region-total').html(data);
            }
        }).submit();
        return false;
    });*/

    $('.image-change', context).click(
        function() {
            var href = $(this).attr('href');
            var name = $(this).attr('name');
            var popup = $(this).attr('popup');
            //$('.model-zoom a').attr('href', popup);
            if (href == null || name == null) {
                return false;
            }
            $(this).parent().trigger('click');
            $(name).attr('src', href).load(function() {
                $(name).fadeOut('fast', function() {
                    //var p = parseInt(($(this).parent().height() - $(this).height()) / 2);
                    //if (p > 0) {
                    //    $(this).css({'paddingTop':p,'paddingBottom':p});
                    //}
                    $(name).fadeIn('fast');
                });
            });
            return false;
        }
    );
    
    $('#search select, #head-search select', context).each(function() {
        $d = $('<span></span>').addClass('select').append(
            $('<sup></sup>')
        ).append(
            $('<ins></ins>').append($(this).clone())
        ).append(
            $('<sub></sub>')
        );
        $(this).replaceWith($d);
        $('select', $d).selectmenu({'style':'dropdown','maxHeight':150});
    });
    
    /*$('#head-tabs ul li', context).click(function() {
        $(this).addClass('active').siblings().removeClass('active');
        return false;
    });*/
    
    $('ul.sub > li > div', context).click(function() {
        if ($(this).parent().children('ul').size()) {
            $(this).parent().toggleClass('active');
            return false;
        }
    });
    
    $('#head-menu ul li', context).each(function() {
        var active = null;
        if ($(this).hasClass('active')) {
            active = this;
        }
        //alert(active);
        $(this).hover(function() {
            if (this != active) $(this).addClass('active');
            $('> .tooltip', this).show().run();
        }, function() {
            if (this != active) $(this).removeClass('active');
            $('> .tooltip', this).hide().run();
        });
    });
    
    /*$('#search', context).each(function() {
        $.get('/netcat/modules/default/index.php', {'action':'auto','id':2816}, function(data) {
            $('#search-auto-p').show();
            $('#search-auto-p select').selectmenu('destroy').html(data).selectmenu({'style':'dropdown','maxHeight':150});
            $('#search-auto-s').show();
        });
    });*/
    
    $('#search-auto-p select', context).change(function(e) {
        $('#search-auto-m').hide();
        $('#search-auto-y').hide();
        //$('#search-auto-s').hide();
        $('#search-auto-s a').attr('href', $('option:selected', this).attr('url'));
        $.get('/netcat/modules/default/index.php', {'action':'auto','id':$(this).val()}, function(data) {
            $('#search-auto-m').show();
            $('#search-auto-m select').selectmenu('destroy').html(data).selectmenu({'style':'dropdown','maxHeight':150});
            //$('#search-auto-s').show();
        });
        return false;
    });
    
    $('#search-auto-m select', context).change(function(e) {
        $('#search-auto-y').hide();
        //$('#search-auto-s').hide();
        $('#search-auto-s a').attr('href', $('option:selected', this).attr('url'));
        $.get('/netcat/modules/default/index.php', {'action':'auto','id':$(this).val()}, function(data) {
            $('#search-auto-y').show();
            $('#search-auto-y select').selectmenu('destroy').html(data).selectmenu({'style':'dropdown','maxHeight':150});
            //$('#search-auto-s').show();
        });
        return false;
    });
    
    $('#search-auto-y select', context).change(function(e) {
        $('#search-auto-s a').attr('href', $('option:selected', this).attr('url'));
        //$('#search-auto-s').show();
        return false;
    });
    
    $('#head-search .button', context).click(function(e) {
        $('#head-search form').attr('action', $('#head-search select').val()).submit();
    });
    
    $('.text:not(.active) .text-hidden', context).hide();
    
    $('.text .text-toggle a', context).click(
        function() {
            $(this).parents('.text').toggleClass('active').children('.text-hidden').slideToggle('slow');
            return false;
        }
    );
    
    $('.scrollable', context).scrollable({'size':3,'next':'.scroll-next','prev':'.scroll-prev','speed':'slow'});
    
    $(window, context).scroll(function(e) {
        $('#popup').dequeue().animate({'top':$(window).scrollTop() + 25,'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() + 25,'left':parseInt(($(window).width() - $('#popup').width()) / 2)}, 'slow');
    });
    
    if (window.location.hash != null && window.location.hash.length > 1) {
        $("a[name='" + window.location.hash.substring(1) + "']", context).trigger('click');
    }
    
    return this;
    
};
