(function(jQuery){
    jQuery.fn.vjachetZoomer = function(options) {
        options = jQuery.extend({
            scale: 1.5,
            speed: 200
        }, options);
        var zind = 100;
        return this.each(function () {
            var obj = jQuery(this);
            obj.css({
                position:'absolute'
            })
            obj.parent().css({
                position:'relative'
            });
            var objWidth = obj.width();
            var objHeight = obj.height();
            var objPadding = obj.css('padding');
            obj.hover(
                function() {
                    //alert(objWidth*(options.scale-1)/2);
                    $(this).css({
                        'z-index' : ++zind
                        }).stop().animate({
                        marginLeft: -objWidth*(options.scale-1),
                        marginTop: -objHeight*(options.scale-1),
                        width: objWidth*options.scale,
                        height: objHeight*options.scale,
                        padding: 20
                    }, options.speed);
                } ,
                function() {
                    $(this).stop().animate({
                        marginLeft: 0,
                        marginTop: 0,
                        width: objWidth,
                        height: objHeight,
                        padding: objPadding
                    }, options.speed)
                    .css({
                        'z-index' : '0'
                    });
                });
        });
    };
})(jQuery);
(function(jQuery){
    jQuery.fn.vjachetGallery = function(options) {
        options = jQuery.extend({
            mainId: 'main',
            bgCertain: '#FADE70',
            opacityCertain: 0.9,
            thumbWidth: 200,
            thumbHeight: 200,
            mainWidth: 350,
            mainHeight: 300
        }, options);
        var mainPos = jQuery('#'+options.mainId).offset();
        var tmpMain = jQuery('<img alt="" />')
        .css({
            'position':'absolute',
            left: mainPos.left,
            top: mainPos.top,
            width: options.mainWidth,
            height: options.mainHeight,
            opacity: 0
        }).hide()
        .appendTo(jQuery('body'))
        .load(function() {
            jQuery(this).animate({
                opacity: 1
            }, 'slow', function () {
                jQuery('#'+options.mainId).attr('src', jQuery(this).attr('src'))
            })
        });
        var certain = jQuery('<span></span>')
        .css({
            'position':'absolute',
            'background-color':options.bgCertain,
            left: mainPos.left - 10,
            top: mainPos.top - 10,
            width: options.mainWidth + 20,
            height: options.mainHeight + 20,
            opacity: 0
        })
        .appendTo(jQuery('body'));
        jQuery('#'+options.mainId).load(function() {
            tmpMain.css({
                opacity: 0
            });
            certain.animate({
                opacity: 0
            }, 'slow').find('img').hide();
        })
        certain.hide();
        var loader = certain.append($('<img src="/images/ajax-loader.gif" alt="" style="position: relative; left:50%; top:50%; opacity: 1 !important" />'))

        return this.each(function () {
            var self = jQuery(this);
            self.click(function() {
                certain.animate({
                    opacity: options.opacityCertain
                    }, 'slow', function() {
                    tmpMain.attr('src', self.attr('src').replace('_'+options.thumbWidth+'_'+options.thumbHeight, '_'+options.mainWidth+'_'+options.mainHeight))
                }).find('img').show()
            });
        });
    };
})(jQuery);

(function(jQuery) {
    jQuery.fn.textDropShadow = function(ShdwClass){//элементы заменяются. Вместо оригинала внутри будет два спана - один содержащий сам текст, а другой тень этого текста.
        jQuery(this).css('position','relative').html('<span class="'+ShdwClass+'">'+jQuery(this).html()+'</span><span style="position:relative;">'+jQuery(this).html()+'</span>');
        return jQuery(this);
    }
})(jQuery);

jQuery(function(){
    $('#inp-search')
    .val('поиск по сайту')
    .css({
        color:'#999'
    })
    .focus(function(){
        if ($(this).val()=='поиск по сайту') {
            $(this).val('')
        }
        $(this).css({
            color:'#000'
        })
    })
    .blur(function(){
        if ($(this).val()=='') {
            $(this).css({
                color:'#999'
            })
            $(this).val('поиск по сайту')
        }

    })
    if ($('table.top_select td div.select_sec').length) {
    	var hov = function () {
	        $(this).css({
	            backgroundPosition:'left top'
	        })
    	}
    	var hovout = function () {
	        $(this).css({
				backgroundPosition:'right top'
	        })
    	}
    	$('table.top_select td div.select_sec').hover(hov,hovout);
    }
    if ($('#top-block div.shop-section').length) {
        $('#top-block div.shop-section').each(function(){
            $(this)
            .css({cursor:'pointer'})
            .hover(
                function(){
                    $(this).css({
                        backgroundPosition:'left top'
                    })
                },
                function(){
                    $(this).css({
                        backgroundPosition:'right top'
                    })
                }
             )
             .click(function() {
                 window.location.href = $(this).children('a:eq(0)').attr('href');
             })
        })
    }
    if ($("input.date_input").length) {
        $.datepicker.setDefaults($.extend({
            dateFormat:'yy-mm-dd',
            yearRange: '-50:+0',
            changeYear: true,
            changeMonth: true
        }, $.datepicker.regional['ru']));
        $("input.date_input").datepicker();
    }

    if ($('table.individ-filtr-col tr td+td').length) {
        $('table.individ-filtr-col tr td+td').css({
            textAlign:'right'
        })
    }
    if ($('table.goods_tbl tr').length) {
        $('table.goods_tbl tr').each(function() {
            $(this).hover(
                function() {
                    $(this).css({
                        backgroundColor:'#F2F2F2'
                    })
                },
                function() {
                    $(this).css({
                        backgroundColor:'transparent'
                    })
                }
                )
        })
    }
    $('.shadowed').textDropShadow('shadow');
    $('.to_basket_link').click(function(){
        add_good($(this).attr('goods_id'))
        return false
    })
    $('a.icqicon').hover(
        function() {
            var pos = $(this).offset()
            $('#icq-popup').css({
                'top':pos.top + $(this).height() - 2,
                'left':pos.left,
                'opacity':1
            }).width(300).height('auto').stop().show('fast')
        },
        function() {
            $('#icq-popup').stop().hide('fast')
        }
        )
    $('#icq-popup').hover(
        function() {
            $('#icq-popup').stop().css({
                'opacity':1
            }).width(300).height('auto').show()
        },
        function() {
            $('#icq-popup').stop().hide('fast')
        }
        )
    var comp_builder = $('#comp_container')
    if (comp_builder.length) {
        $('.goods', comp_builder).change(function() {
            var cat_id = $(this).attr('cat_id')
            var option = $('option:selected', $(this))
            var price = option.attr('price');
            var image =  option.attr('image');
            var img = $('td#img_cat_'+cat_id+' img');
            if (img.length) {
                if (!img.attr('base_src')) {
                    img.attr('base_src',img.attr('src'))
                }
                if (image) {
                    img.attr('src','/resized/'+image+'_100_100')
                } else {
                    img.attr('src',img.attr('base_src'))
                }

            }
            var count = $('#price_' + cat_id).prev().val()
            if (!count) {
                count = 1
                $('#price_' + cat_id).prev().val(count)
            }

            $('#price_' + cat_id).text(price*count)
            $('#price_' + cat_id).attr('price', price)
            calc_itog()
            $('#div_itog_price').show()
        })
        inp_change = function() {
            var price = $(this).next().attr('price')
            var count = $(this).val()
            if (!count) {
                count = 1
                $(this).val(count)
            }
            $(this).next().text(price*count)
            calc_itog()
            $('#div_itog_price').show()
        }
        $('.count', comp_builder).keyup(inp_change).change(inp_change)
        $('.manufs', comp_builder).change(function() {
            var cat_id = $(this).attr('cat_id')
            var man_id = $(this).val()
            $('#goods_of_cat_' + cat_id).html('<option>загрузка...</option>')
            $.post(
                '/module.php',
                {
                    'module': 'Shop_Goods',
                    'task': 'show_select_cat_manuf_goods',
                    'cat_id':cat_id,
                    'man_id':man_id
                },
                function(data){
                    $('#goods_of_cat_' + cat_id).html(data)
                }, 'html');
        })
        //alert(top)
        $('#div_itog_price').remove().clone().appendTo('body').css({
            'position':'absolute',
            left: ($(window).width() - 1080) / 2 + 900 - $('#div_itog_price').width(),
            top: $(window).scrollTop() + $(window).height() - 200
        })
        if (!$('#itog_price').text()) $('#div_itog_price').hide()
        calc_itog()
        $(window)
        .bind('scroll resize', function() {
            if (itog_price_ID) clearTimeout(itog_price_ID)
            itog_price_ID = null
            itog_price_ID = setTimeout('set_itog_div()', 400)
        })
    }
});
var itog_price_ID = 0;
var itog_price_top = 0;
function add_good(id)
{
    $.get("/add_good.php", {
        'id': id,
        kolvo: 1
    },function(data){
        data = data.split('|')
        if (data[0] == 'little') {
            $('#basket_count').text(data[2])
        } else {
            if ($('#basket_count').length) $('#basket_count').text(data[0]);
			if (id>0) {
				var fancyOpts = {
					hideOnOverlayClick: false,
					overlayShow: false,
					autoScale: false,
					opacity: true,
					shadowHide: false
				}
				$.fancybox($('#good_added'),fancyOpts);
			}
        }
    });
}

function set_itog_div()
{
    if (itog_price_ID) clearTimeout(itog_price_ID)
    itog_price_ID = null
    var new_top = $(window).scrollTop() + $(window).height() - 200
    if (new_top < 417) new_top = 417
    if (new_top > $('#cap').height() + $('#body').height()) new_top = $('#cap').height() + $('#body').height() - 200
    //alert(itog_price_top+'::'+new_top)

    if (new_top != itog_price_top) {
        itog_price_top = new_top;
        itog_price_ID = setTimeout('set_itog_div()', 400);
    } else {
        $('#div_itog_price').animate({'top':itog_price_top})
    }

}
function calc_itog()
{
    var itog = 0
    $('.price_val').each(function() {
        if ($(this).text()!=undefined) itog += $(this).text()*1
    })
    $('#itog_price').text(itog)
    $('#price').val(itog)
}



