$(document).bind("mousedown", function() {
    ResetPopups();
});

function ResetPopups() {
    $('#UserMenu').removeClass('usernav-open');
}


function UserClick() {
    $('#UserMenu').toggleClass('usernav-open');
    $("#UserMenu").mousedown(function () { return false; });
}

//if ($) $ (function () {$ ('a').hover (
// function () {
//   if ($ (this).attr('href') != '') {
//     $ ('a[href=' + $(this).attr('href') + ']').addClass ('hover')
//   }
// },
// function () {
//   $ ('a').removeClass ('hover')
// }
//)})
$(function () {
    InitScrollablePanel();
});

var api;
function InitScrollablePanel() {
    $('.scrollable').each(function () {
        $(this).jScrollPane(
                {
                    showArrows: $(this).is('.arrow')
                }
			);
        api = $(this).data('jsp');
        var throttleTimeout;
        $(window).bind('resize',
                function () {
                    if ($.browser.msie) {
                        // IE fires multiple resize events while you are dragging the browser window which
                        // causes it to crash if you try to update the scrollpane on every one. So we need
                        // to throttle it to fire a maximum of once every 50 milliseconds...
                        if (!throttleTimeout) {
                            throttleTimeout = setTimeout(
                                function () {
                                    api.reinitialise();
                                    throttleTimeout = null;
                                },
                                50
							);
                        }
                    } else {
                        api.reinitialise();
                    }
                }
			);
        }
	);
}

$(document).ready(function () {

    $("div.post-body div.post-text img").each(function () {

        //Get the width of the image
        var width = $(this).width();

        //Max-width substitution (works for all browsers)
        if (width > 440) {
            $(this).css("width", "100%");
        }

    });


    $('#userbar .searchfield input').focusin(function () {
        $('#userbar .searchfield').addClass('focus');
        $('#userbar .searchfield .sf').addClass('focus')
    });
    $('#userbar .searchfield input').focusout(function () {
        $('#userbar .searchfield').removeClass('focus');
        $('#userbar .searchfield .sf').removeClass('focus')
    });






    $("a.modal").fancybox({

        'padding': 0,
        'autoScale': true,
        'transitionIn': 'fade',
        'transitionOut': 'fade',
        'centerOnScroll': true
    });

    $("a[rel=prodimages]").fancybox({
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'titlePosition': 'inside',
        'titleFormat': function (title, currentArray, currentIndex, currentOpts) {
            return '<span id="fancybox-title-over">Фото ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
        }
    });


    $(".search-submit").unbind("click").click(function () {
        $('#cse-search-box').submit();
    });

});

function shop_title_items() {
    var min_item_w = 200;
    var stretch = 1;

    var s_teaser            = $('.shop-teaser');
    var s_teasers_holder    = $('.shop-teasers');
    var _window             = $('#shop-title-page');
    
    var window_w = _window.width();
    var window_w_tmp = 0;
    
    var items = 0;
    var items_tmp = 0;
    
    
    
    function _init() {
        $(window).resize(function(){
            window_w = _window.width();
            items_tmp = Math.floor(window_w / min_item_w);
            
            if (items_tmp != items) {               
                if (items_tmp > 0) items = items_tmp;
                new_w = 100 / items / stretch; 
                new_w = new_w.toFixed(3);
            
                s_teaser.css('width', new_w + '%');             

            }
        });
        
        $(window).resize();
    }

    return {
        init: _init
    };
}

$(function(){
    var ruberTeasers = new shop_title_items();
    ruberTeasers.init();
    $(window).resize();
});
