function on_every_load() {

    gapi.plusone.go();


    $('[rel=group1]').fancybox({
        type: 'image'
    });

    if ($.cookie("big_screen") == 'True')
    {
        if ( ! $('html').hasClass('wide-screen'))
            $('html').addClass('wide-screen');
    }
    else 
    {
        if ($('html').hasClass('wide-screen'))
            $('html').removeClass('wide-screen');
    }

    $(document).ready(function(){
                          
        var big_screen = (screen.width >= 1280) ? 'True' : 'False';
        var rand = Math.random();

        $('#unsubscribe_email_button').live('click', function() {
            $.get('/newsletter/subscribed/?type=unsubscribe&email=' + $('#unsubscribe_email').val(), function(res) { 
                $('#newsletter_unsubscribed_text').html('<h2>Sėkmingai atsisakyta</h2>');
            });
        });

        $.getJSON('/personal_data/?_=' + rand + '&big_screen=' + big_screen, function(data) {
            if (data.logged_in) {
                if (data.facebook) {
                    $('.li-logout-facebook').show();
                } else {
                    $('.li-logout').show();
                }
                $('.li-login').hide();
            } else {
                $('.li-logout-facebook, .li-logout').hide();
                $('.li-login').show();
            }
            $('.cart-item-count').html(data.cart_quantity);
        });

        $('.few-comments .comments-list>div:gt(2)[id^="comment"]').hide();
        $('.few-comments .comments-list>div:last').show();

        var comments = $('#comments-block .comments-list>div');
        var comment_holder = $('#comments-block .comments-list');

        if (comments.length > 3) {
            $('#comments-block .comments-list>div:gt(2)').hide();
            $('.tiny-filter').hide();
            comment_holder.parent().append('<div class="fr">' +
                                                '<a style="margin-right: 10px;" class="fr block green-button all-comments">Rodyti Visus</a>' +
                                            '</div>' +
                                            '<div class="clr">&nbsp;</div>'
                                           );
            $('#comments-block .all-comments').die().live('click', function() {
                $('.comments-block-all .all-comments').show();
                switchProductTab('comm'); 
     
                //comments.slideDown('slow');
                //$('.tiny-filter').show();
                //$('#comments-block .all-comments').parent().hide();
            });

        }

        $('.few-comments .all-comments').die().live('click', function() {
            $('#comments-block .comments-list>div').show();
            $('#tiny-filter').show();
            $('#comments-block .all-comments').parent().hide();
            switchProductTab('comm');
            setTimeout(function() {
                $('html, body').scrollTop(ajaxed_jQuery('#tab-1').offset().top);
            }, 500);
        });

        $('.post-comment').die().live('click', function() {
            $('.comment-form').fadeIn('slow');
            setTimeout(function() {
                $('html, body').scrollTop(ajaxed_jQuery('.comment-form').offset().top);
                $('.comment-form textarea').focus();
            }, 500);
        });

        $('.few-comments .post-comment').die().live('click', function() {
            switchProductTab('comm');
            $('.few-comments').hide();
            $('.comment-form').show();
        });

        if (comments.length == 0) {
            $('.comment-form').show();
        }
        
        var active = $('div .delivery .city .title').parent().find('.address_info:visible');
        var coord = active.parent().find('.coord').val();
        if (coord) {
            if (GBrowserIsCompatible()) {
                map = new GMap2(active.parent().find('.map')[0]);
                map.setCenter(new GLatLng(coord.split(',')[0], coord.split(',')[1]), 15); 
                map.addControl(new GSmallZoomControl());

                var marker = new GMarker(new GLatLng(coord.split(',')[0], coord.split(',')[1]));
                map.addOverlay(marker);
            }
        }

        $('.value').css('cursor', 'pointer');
        $('.value').click(function() {
        switchProductTab('comm');
            window.location.hash = '#komentarai';
        });
        $('.stars').css('cursor', 'pointer');
        $('.stars').click(function() {
            window.location = $('a', $(this).parent()).attr('href') + '#komentarai';
        });

        if (window.myInterval) { clearInterval(window.myInterval); }

        window.myInterval = setInterval("changeTopOffers()", 30000);

        $('.clear_form').click(function() {
            $('#send_request')[0].reset();
        });

        /* if (window.location.toString().split("#")[1] == 'komentarai') { */
        if (window.location.href.indexOf('komentarai') != -1) {
            switchProductTab('comm');
        } else {
            switchProductTab('desc');
        };

        $(".editable").editable("/client/setup/", {
            'onblur': 'submit',
            submitdata : {'inline': '1'}
        });

        $('.address').click(function(){
            data = ''
            $.get("/client/setup/", { address_id: $(this).attr('id') },
                function(data){
                    $('.address_form .title').html('Redaguoti');
                    $('.address_form #address_id').attr('value', data[0]['pk']);
                    $('.address_form #id_city').attr('value', data[0]['fields']['city']);
                    $('.address_form #id_region option[value='+ data[0]['fields']['region'] + ']').attr('selected', 'selected');
                    $('.address_form #id_street').attr('value', data[0]['fields']['street']);
                    $('.address_form #id_house').attr('value', data[0]['fields']['house']);
                    $('.address_form #id_flat').attr('value', data[0]['fields']['flat']);
                }, "json");
            return false;
        });

        
        /*	
        hideSubmenu();

        $('#super-menu').bind("mouseout", close);
        $('#super-menu').bind("mouseover", function() { clearTimeout(hideTimer); });// stop drop down menu from being closed

        var hideTimer = setTimeout(function() {  }, 1);//initialise so not undefined

        function close() {
            clearTimeout(hideTimer);
            hideTimer = setTimeout(function() { $('.subnav').fadeOut('fast'); }, 80);//close drop down menu  
        };
        */

        /*
        var popupStatus = 0;
        function popup_block($popup)
        {
            var windowWidth = document.documentElement.clientWidth;
            var windowHeight = document.documentElement.clientHeight;
            var popupHeight = $popup.height();
            var popupWidth = $popup.width();
                $popup.css({
                    "position": "absolute",
                    "top": windowHeight/2-popupHeight/2,
                    "left": windowWidth/2-popupWidth/2
                });

            if(popupStatus==0){
                $("#backgroundPopup").fadeIn("slow");
                $("#backgroundPopup").css('opacity', 1.0);
                $popup.fadeIn("slow");
                popupStatus = 1;
            }

        };
        */

        $('.quantity').change(function () {
            $(this).parents('form').submit();
        });

        $('.delivery input').change(function () {
            //$(this).parents('form').submit();
            var data = $(this).closest('form').serialize();
            $.post('/cart/payment/', data);
        });


        $('.thumb-list li').click(function(e){
            
            e.preventDefault();
            $.fancybox.cancel();
            
            $(this).parents().find('li').removeClass('act');
            
            if ($(this).hasClass('youtube')) {
                $(this).parents('.item').find('table img').hide();
                $object = $(this).find('.select_video').clone();
                //console.log($object);
                if (!$(this).parents('.item').find('.tac object').size())
                    $(this).parents('.item').find('.tac').append($object);
                $(this).parents('.item').find('table object').show().css("display", "inline");
            } else {
                $src = $(this).find('.select_image').attr('src');
                $full_url = $(this).find('.thumb').attr('data-full-img');
                $(this).parents('.item').find('table object').remove();
                $(this).parents('.item').find('table img.pr_main_img').show();
                $(this).parents('.item').find('table img.pr_main_img').attr('src', $src);
                $(this).parents('.item').find('table a').attr('href', $full_url);
            }
            
            $(this).addClass('act');
        });
        $('.form_login .popup_remind').click(function(){
            $popup = $('#popup_remind');
            popup_block($popup);
            $('#popup_remind').show();

            return false;
        });
        $('li .popup_login').click(function(){
            $popup = $('#popup_login');
            popup_block($popup);
            $('#popup_login').show();

            return false;
        });
        $('#offer_price a').click(function(){
            $popup = $('#popup_offer');
            popup_block($popup);
            $('#popup_offer').show();

            return false;
        });

        
        /*
        $('li .product_added').click(function(){
            //$('body').animate({opacity:0.5},300);
            $popup = $('#product_added');
            popup_block($popup);
            $('#product_added').show();

            return false;
        });
        */

        $('.popup-1 .close').click(function(e){
            //$('body').animate({opacity:1},300);
            $(this).parents('.popup-1').hide();
            popup_close($(this).parents('.popup-1'));
            e.preventDefault();
            return false;
        });

        function submit_form()
        {
            $(this).parents('form').submit();
        }

        $('.submit').unbind('click', submit_form).click(submit_form);
        $('.discount').click(function () {
            $(this).closest('form').submit();
            return true;
            //return false;
        });

        $('.star').click(function() {
            $(this).parents('.your-value').find('.fr').html("Įvertinote");
        });

        $('.title-slider li').click(function() {
            clearTimeout(window.myInterval);
            id = $(this).attr('id').split('-')[2];
            changeClickableItem(id);
        });

        $('input[type=radio].star').rating();

        $('.star_value.only-rating a').unbind('click').bind('click', function() {
            var self = this;
            $.ajax({ 
                url: "/product/rate_product/", 
                data: ({rate: $(this).html(), pid: $(this).closest('.star_value.only-rating').attr('data-product-id') }), 
                success: function() {
                    $('.star-rating-control div').unbind();
                    $(self).parent().parent().find('a').unbind();
                }
            });
        });
        
        // Newsletter
        $('.toggle_newsletter_box').toggle(function(e) {
            $('.subscribe_newsletter').slideUp();
            $('.unsubscribe_newsletter').slideDown();
            $('.toggle_newsletter_box').html('Užsisakyti naujienlaiškį');
        }, function(e) {
            $('.unsubscribe_newsletter').slideUp();
            $('.subscribe_newsletter').slideDown();
            $('.toggle_newsletter_box').html('Atsisakyti naujienlaiškio');
        });

        $('#newsletter_subscribe').click(function(e) {
            $.get('/newsletter/subscribed/?type=subscribe&email=' + $('#newsletter_email_subscribe').val(), function(res) {
                if (res == 'ok') {
                    $('.subscribe_newsletter span.info').html('<div style="color: green;">Naujienlaiškis sėkmingai užsakytas</div>');
                } else {
                    $('.subscribe_newsletter span.info').html('<div style="color: red;">Naujienlaiškio užsakyti nepavyko</div>');
                }
            });
        });
        $('#newsletter_unsubscribe').click(function(e) {
            $.get('/newsletter/subscribed/?type=unsubscribe&email=' + $('#newsletter_email_unsubscribe').val(), function(res) {
                if (res == 'ok') {
                    $('.unsubscribe_newsletter span.info').html('<div style="color: green;">Naujienlaiškio sėkmingai atsisakyta</div>');
                } else {
                    $('.unsubscribe_newsletter span.info').html('<div style="color: red;">Naujienlaiškio atsisakyti nepavyko</div>');
                }
            });
        });                  
    });

    $(function() {

        var inner = $('.shops-slider-inner'),
        container = inner.parent(),
        next_btn = container.find('.shops-slider-next'),
        prev_btn = container.find('.shops-slider-prev'),
        auto_speed = 800,
        manual_speed = 200,
        width = 198,
        position = 0;

        function animating()
        {
            return container.hasClass('animating');
        }

        function pos(back, speed)
        {
            container.addClass('animating');
            if ( ! inner.length) {
                return;
            }
            var left = inner.css('left').replace('px', '') * 1;
            if ( ! back)
            {
                // Forward
                left -= width;
                position += 1;
            }
            else
            {
                // Backward
                left += width;
                position -= 1;
            }

            if (position == -10)
            {
                position = -1;
                inner.css('left', -1782);
                left = -1584;
            }

            if (position == 10)
            {
                position = 1;
                inner.css('left', -1782);
                left = -1980;
            }

        if (speed == undefined)
            {
            speed = container.hasClass('has-mouse') ? manual_speed : auto_speed;
        } 

            inner.animate({'left': left + 'px'}, speed, function() {
                container.removeClass('animating');
            });
        }

        function next()
        {
            if (animating()) return;
            pos();
        }

        function prev()
        {
            if (animating()) return;
            pos(true);
        }

        container.hover(function() {
            $(this).addClass('has-mouse');
        }, function() {
            $(this).removeClass('has-mouse');
        });

        next_btn.click(next);
        prev_btn.click(prev);

        var random_number = Math.floor(Math.random()*10);

        for (var i = 0; i <= random_number; i++) {
        pos(false, 0);
        }

        setInterval(function() {
            if (animating() || container.hasClass('has-mouse')) return;
            pos();
        }, 30000);

    }); 

}


