var activeItem = 1;
var lastActiveItem = activeItem;
var t = null;
var auto = 5000;
var slide_auto = true;
var speed = 1000;
var sliderProcess = false;
var mousePressed = false;
var sliderScrollSpeed = 800;
var requeriedAttributes = [];
var evacarouselObj;



jQuery(document).ready(function() {
	
			jQuery(".subcategories_menu").hide();
			
			jQuery(".root_category").mouseenter(function() { 
				jQuery(this).find("a.root_cat_link").addClass('active_root_cat_link');   
				var catId = jQuery(this).attr('id');
				jQuery("#subcat_"+catId).show();
			}).mouseleave(function() {
				jQuery(this).find("a.root_cat_link").removeClass('active_root_cat_link');
				var catId = jQuery(this).attr('id');
				jQuery("#subcat_"+catId).hide();
			});
			
			jQuery(".subcategory").mouseenter(function() { 				  
				jQuery(this).addClass('active_sub_cat_link');				
			}).mouseleave(function() {
				jQuery(this).removeClass('active_sub_cat_link');
			});
			
            jQuery(".search > span").click(function(){
                if (jQuery('.search-form').css('display') == 'none'){
                    jQuery(".search-form").css('display','block');
                    jQuery(".search-form #search").focus();
                }
                else{
                    jQuery(".search-form").css('display','none');
                }
            });
            /*nagorskiy*/
            jQuery(".product-details .measuring-link").click(function(){
                jQuery(".product-details .product-detail-main").hide();
                jQuery(".product-details .measuring").show();
            });
            jQuery(".product-details .measuring .close").click(function(){
                jQuery(".product-details .measuring").hide();
                jQuery(".product-details .product-detail-main").show();
            });
            jQuery(".product-details #reviews-tab").click(function(){
                jQuery(".product-details .reviews-abs").css('display','block');
                jQuery(".product-details .product-detail-main").hide();
            });
            jQuery(".product-details .reviews-abs .close").click(function(){
                jQuery(".product-details .reviews-abs").css('display','none');
                jQuery(".product-details .product-detail-main").show();
            });
            jQuery(".product-details #ask_expert form > a").click(function(){
                jQuery(".product-details .product-detail-main").hide();
                jQuery(".product-details .ask").css('display','block');
            });
            jQuery(".product-details .ask .close").click(function(){
                jQuery(".product-details .ask").css('display','none');
                jQuery(".product-details .product-detail-main").show();
            });

            /*nagorskiy*/
            
            jQuery(".product-details #askexpert-tab").click(function(){
                jQuery(".product-details .askexpert-abs").css('display','block');
                jQuery(".product-details .product-detail-main").hide();
            });
            
            jQuery(".product-details .askexpert-abs .close").click(function(){
                jQuery(".product-details .askexpert-abs").css('display','none');
                jQuery(".product-details .product-detail-main").show();
            });
        });





jQuery(document).ready(function($) {
    function homepageOnLoad()
    {

                    jQuery('.previous').click(function() {
                        $('.image-rotator').cycle('pause');
                    });
                    jQuery('.next').click(function() {
                        $('.image-rotator').cycle('pause');
                    });
                    jQuery('.rotator-paginaton a').click(function() {
                        $('.image-rotator').cycle('pause');
                    });

                    jQuery('.image-rotator').cycle({
                        fx: 'scrollLeft',
                        timeout: 6000,
                        next:'.rotator .next',
                        prev:'.rotator .previous',
                        pager:  '.rotator-paginaton',
                        activePagerClass:'current',

                        pagerAnchorBuilder: function(idx, slide)
                        {
                            return '.rotator-paginaton li.page'+idx+' a';
                        },
                        prevNextClick: function(isNext, zeroBasedSlideIndex, slideElement)
                        {
                            this.fx = isNext?'scrollLeft':'scrollRight';
                            var init = jQuery.fn.cycle.transitions[this.fx];
                            if (jQuery.isFunction(init))
                                init(jQuery('.image-rotator'), jQuery('.image-rotator').children(), this);
                        },
                        pagerClick: function()
                        {
                            this.fx = this.currSlide<this.nextSlide?'scrollLeft':'scrollRight';
                            var init = jQuery.fn.cycle.transitions[this.fx];
                            if (jQuery.isFunction(init))
                                init(jQuery('.image-rotator'), jQuery('.image-rotator').children(), this);
                        }
                    });

/*

                $('.rot-panels .panels').jcarousel({
                scroll: 4,
                animation:1500,
                initCallback: function mycarousel_initCallback(carousel) {
                    jQuery('.rotator-panels #next').bind('click', function() {
                        if (carousel.last!=$(carousel.list).children().length)
                            carousel.next();
                        return false;
                    });

                    jQuery('.rotator-panels #previous').bind('click', function() {
                        if (carousel.first!=1)
                            carousel.prev();
                        return false;
                    });
                },
                itemLastInCallback: {
                    onAfterAnimation: function(carousel,item,index){
                        if (carousel.last==$(carousel.list).children().length)
                            jQuery('.rotator-panels #next').addClass('disabled');
                        else
                            jQuery('.rotator-panels #next').removeClass('disabled');
                    }
                },
                itemLastOutCallback: {
                    onAfterAnimation: function(carousel,item,index){
                        if (carousel.last==$(carousel.list).children().length)
                            jQuery('.rotator-panels #next').addClass('disabled');
                        else
                            jQuery('.rotator-panels #next').removeClass('disabled');
                    }
                },
                itemFirstOutCallback: {
                    onAfterAnimation: function(carousel,item,index){
                        if (carousel.first==1)
                            jQuery('.rotator-panels #previous').addClass('disabled');
                        else
                            jQuery('.rotator-panels #previous').removeClass('disabled');
                    }
                },
                itemFirstInCallback: {
                    onAfterAnimation: function(carousel,item,index){
                        if (carousel.first==1)
                            jQuery('.rotator-panels #previous').addClass('disabled');
                        else
                            jQuery('.rotator-panels #previous').removeClass('disabled');
                    }
                },
                buttonNextHTML: null,
                buttonPrevHTML: null
            });

*/



                $('.rot-panels .panels').jcarousel({
                scroll: 4,
                animation:1500,
                initCallback: function mycarousel_initCallback(carousel) {
                    jQuery('.rotator-panels .next').bind('click', function() {
                        if (carousel.last!=$(carousel.list).children().length)
                            carousel.next();
                        return false;
                    });

                    jQuery('.rotator-panels .previous').bind('click', function() {
                        if (carousel.first!=1)
                            carousel.prev();
                        return false;
                    });
                },
                itemLastInCallback: {
                    onAfterAnimation: function(carousel,item,index){
                        if (carousel.last==$(carousel.list).children().length)
                            jQuery('.rotator-panels .next').css('display','none');
                        else
                            jQuery('.rotator-panels .next').css('display','block');
                    }
                },
                itemLastOutCallback: {
                    onAfterAnimation: function(carousel,item,index){
                        if (carousel.last==$(carousel.list).children().length)
                            jQuery('.rotator-panels .next').css('display','none');
                        else
                            jQuery('.rotator-panels .next').css('display','block');
                    }
                },
                itemFirstOutCallback: {
                    onAfterAnimation: function(carousel,item,index){
                        if (carousel.first==1)
                            jQuery('.rotator-panels .previous').css('display','none');
                        else
                            jQuery('.rotator-panels .previous').css('display','block');
                    }
                },
                itemFirstInCallback: {
                    onAfterAnimation: function(carousel,item,index){
                        if (carousel.first==1)
                            jQuery('.rotator-panels .previous').css('display','none');
                        else
                            jQuery('.rotator-panels .previous').css('display','block');
                    }
                },
                buttonNextHTML: null,
                buttonPrevHTML: null
            });
            jQuery('.rot-panels .panels li:eq(0) a.image').click();

            //featured products and bestsellers
            isBestSellersPanels = false;
            jQuery('.bestsellers-panels').hide();

            jQuery('#featured_products').click(function() {
                jQuery('li:has(a#bestsellers)').removeClass('current');
                jQuery('li:has(a#featured_products)').addClass('current');
                jQuery('.bestsellers-panels').hide();
                jQuery('.featured-products-panels').show();
            });
            jQuery('#bestsellers').click(function() {
                jQuery('li:has(a#bestsellers)').addClass('current');
                jQuery('li:has(a#featured_products)').removeClass('current');
                jQuery('.featured-products-panels').hide();
                jQuery('.bestsellers-panels').show();
                if (!isBestSellersPanels) {
                    initDefCarousel('.best-rot-panels .panels', '.bestsellers-panels', 6, 1500);
                    isBestSellersPanels = true;
                }
            });

            initDefCarousel('.feat-rot-panels .panels', '.featured-products-panels', 6, 1500);
    };



    if ($('body').hasClass('cms-index-index')) homepageOnLoad();


});

function initDefCarousel(mainClass, navigationClass, scroll, animation, isClick)
{
    if (isClick == undefined) isClick = true;

    var next     = navigationClass + ' .next';
    var previous = navigationClass + ' .previous';

    jQuery(mainClass).jcarousel({
        scroll: scroll,
        animation: animation,
        initCallback: function mycarousel_initCallback(carousel) {
            jQuery(next).bind('click', function() {
                if (carousel.last!=jQuery(carousel.list).children().length)
                    carousel.next();
                return false;
            });

            jQuery(previous).bind('click', function() {
                if (carousel.first!=1)
                    carousel.prev();
                return false;
            });
        },
        itemLastInCallback: {
            onAfterAnimation: function(carousel,item,index){
                if (carousel.last==jQuery(carousel.list).children().length)
                    jQuery(next).css('display','none');
                else
                    jQuery(next).css('display','block');
            }
        },
        itemLastOutCallback: {
            onAfterAnimation: function(carousel,item,index){
                if (carousel.last==jQuery(carousel.list).children().length)
                    jQuery(next).css('display','none');
                else
                    jQuery(next).css('display','block');
            }
        },
        itemFirstOutCallback: {
            onAfterAnimation: function(carousel,item,index){
                if (carousel.first==1)
                    jQuery(previous).css('display','none');
                else
                    jQuery(previous).css('display','block');
            }
        },
        itemFirstInCallback: {
            onAfterAnimation: function(carousel,item,index){
                if (carousel.first==1)
                    jQuery(previous).css('display','none');
                else
                    jQuery(previous).css('display','block');
            }
        },
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

    if (isClick) {
        jQuery(mainClass + ' li:eq(0) a.image').click();
    }
}

jQuery(document).ready(function($) {
// Thumbnail mage loader
    function productDetailOnLoad()
    {
        jQuery('.thumbs .items li:eq(0) a.image').addClass('active');
        
        jQuery('#reviews .review').hide();
        jQuery('#reviews .review:first').show();

        //related and also like products panels
        isAlsoLikePanels = false;

        jQuery('.also-like-panels-details').hide();
        jQuery('li:has(a#related_tab)').addClass('current');
        
        jQuery('#related_tab').click(function() {
            jQuery('#tabs_bottom_nav li').removeClass('current');
            jQuery('li:has(a#related_tab)').addClass('current');
            jQuery('.also-like-panels-details').hide();
            jQuery('.related-products-panels-details').show();
        });
        
         jQuery('#also_like_tab').click(function() {
            jQuery('#tabs_bottom_nav li').removeClass('current');
            jQuery('li:has(a#also_like_tab)').addClass('current');
            jQuery('.related-products-panels-details').hide();
            jQuery('.also-like-panels-details').show();
/*
        jQuery('#related_tab').click(function() {
            jQuery('#tabs_bottom_nav li').removeClass('current');
            jQuery('li:has(a#related_tab)').addClass('current');
            jQuery('.also-like-panels-details').css({visibility:'hidden',height:'0'});
            jQuery('.related-products-panels-details').css({visibility:'visible',height:'125px'});
        });
        jQuery('#also_like_tab').click(function() {
            jQuery('#tabs_bottom_nav li').removeClass('current');
            jQuery('li:has(a#also_like_tab)').addClass('current');
            jQuery('.also-like-panels-details').show();
            jQuery('.related-products-panels-details').css({visibility:'hidden',height:'0'});
            jQuery('.also-like-panels-details').css({visibility:'visible',height:'125px'});
*/
            if (!isAlsoLikePanels) {
                initDefCarousel('.also-like-rot-panels-details .panels', '.also-like-panels-details', 6, 1500);
                isAlsoLikePanels = true;
            }
        });

        initDefCarousel('.related-rot-panels-details .panels', '.related-products-panels-details', 6, 1500);

        reloadGalleryThumbs();

        if ($('.jqzoom')) {
                    $(".jqzoom").jqueryzoom({
                                xzoom: 397,
                                yzoom: 425,
                                preload:1,
                                lens:1
                            });
        }
        //thumbs
        initDefCarousel('.product-view .gallery .thumbs .items-source', '.product-view .gallery .thumbs', 3, 1500, false);
        //IE fix carousel
        var thumbs = jQuery('.product-view .gallery .thumbs');
        if (!thumbs.data('width')) {
            thumbs.data('width', thumbs.width());
        }

        var ulWidth = jQuery('.product-view .gallery .thumbs ul').width();
        if (ulWidth < thumbs.data('width')) {
            thumbs.width(ulWidth);
        } else {
            thumbs.width(thumbs.data('width'));
        }
        //------------------
    }

    if ($('body').hasClass('catalog-product-view')) productDetailOnLoad();
    if ($('body').hasClass('catalog-category-view')) categoryViewOnLoad();

});

function categoryViewOnLoad(isOpen)
{
    jQuery('.toolbar-bottom form.sort-by').hide();

    jQuery(".product").hover(function(){
        jQuery(this).addClass('active');
    },function(){
        jQuery(this).removeClass('active');
    });

    jQuery('.adj-nav-filter-box-cat').hide();
    jQuery('.adj-nav-filter-box-price').hide();
    //jQuery('#filter-nav-button').click(function(){jQuery('#catalog-adjnav-box').css('display')=='none'?jQuery('#catalog-adjnav-box').css('display',''):jQuery('#catalog-adjnav-box').css('display','none');});

    var box    = jQuery('#catalog-adjnav-box');
    var button = jQuery('#filter-nav-button');

    if (!isOpen){
        box.data('isDisplayed', false);

        var boxHeight = 142;

        button.click(function(){
            if (box.data('isDisplayed')){
                box.stop();
                box.css('overflow', 'hidden');
                if (parseInt(box.css('height'), 10) < 5){
                    box.css('height', 0);
                }

                box.animate({height: 0}, 'fast', function(){
                    box.data('isDisplayed', false);
                    box.css({
                        'visibility': 'hidden',
                        'overflow': 'hidden',
                        'display': 'block',
                        'height': 0
                    });
                });
            } else {
                box.css({
                    'visibility' : 'visible',
                    'overflow'   : 'visible'
                });

                box.stop().css('display', 'block').animate({height : boxHeight}, 'fast', function(){
                    box.data('isDisplayed', true);
                });
            }
        });
    } else {
        //init sortby select
        Custom.init();
    }

    jQuery('.narrow-by a.close').click(function(){
        if (box.data('isDisplayed')){
            box.stop().animate({height : 0}, 'fast', function(){
                box.data('isDisplayed', false);
                box.css({
                    'visibility': 'hidden',
                    'overflow': 'hidden',
                    'display': 'block',
                    'height': 0
                });
            });
        }
    });
};

// Thumbnail mage loader

function reloadGalleryThumbs()
            {
                jQuery('.thumbs .items a.image').click(function(e){
                    e.preventDefault();
                    if(!jQuery(this).is('.active')){
                    var thumb = jQuery(this);
                    thumb.parent().parent().children('li').children('.active').removeClass('active');
                    thumb.addClass('active');
                    var large_image = jQuery(this).attr("href");
                    var xlarge_image = jQuery(this).attr("rel");
                    var image = jQuery('.jqzoom').children('a').children('img');
                    image.parent().attr("href",''+ xlarge_image +'');
                    jQuery('.view_full_size').attr("href",''+ xlarge_image +'');
                    image.fadeOut('slow', function(){
                    jQuery(this).attr("src",''+ large_image +'');

                    });
                    image.load(function() {
                        image.fadeIn('slow', function(){

                        });
                        var zoom_lock = 0;
                        });
                    }

                });
                jQuery('a.image').click(function(e) {

                    if (jQuery(this).parent().hasClass('li_video') == false) {
                        jQuery('.inner').detach();
                    }
                });

            };
function initializeScroller(){
    if (!jQuery('#catalog-adjnav-box').data('initialized')){
        jQuery('#catalog-adjnav-box').data('initialized', true);
        jQuery('#catalog-adjnav-box').css({
            'visibility': 'hidden',
            'overflow': 'hidden',
            'display': 'block',
            'height': 0
        });
    }

    jQuery('.nav-filter .scroller:not(.scrollerInit)').each(function(){
        jQuery(this).addClass('scrollerInit').customScroller({horizontal: 0});
    });
}

