jQuery.fn.CloudZoom.defaults.showTitle = false;
jQuery.fn.CloudZoom.defaults.smoothMove = 1;
jQuery.fn.CloudZoom.defaults.zoomWidth = 378;
jQuery.fn.CloudZoom.defaults.zoomHeight = 378;
jQuery.fn.CloudZoom.defaults.adjustX = 40;
jQuery.fn.CloudZoom.defaults.adjustY = -1;

function bannerRotator(selector, options){
    options = options || {};
    var initCallback = function(carousel){
        if (options.stopOnClick){
            // Disable autoscrolling if the user clicks the prev or next button.
            carousel.buttonNext.bind('click', function(){
                carousel.startAuto(0);
            });
            carousel.buttonPrev.bind('click', function(){
                carousel.startAuto(0);
            });
        }

        // Pause autoscrolling if the user moves with the cursor over the clip.
        carousel.clip.hover(function(){
            carousel.stopAuto();
        }, function(){
            carousel.startAuto();
        });

        if (options.initCallback)
            options.initCallback.apply(this, arguments);
    };

    if (jQuery(selector + ' > li').size() > 1){
        return jQuery(selector).jcarousel({
            auto: options.auto || 5,
            scroll: options.scroll || 1,
            offset: 1,
            start: 1,
            wrap: options.wrap || 'circular',
            buttonNextHTML: options.buttonNextHTML !== undefined ? options.buttonNextHTML : '<a href="#" onclick="return false;">Next</a>',
            buttonPrevHTML: options.buttonPrevHTML !== undefined ? options.buttonPrevHTML : '<a href="#" onclick="return false;">Previous</a>',
            initCallback: initCallback,
            itemVisibleInCallback:options.itemVisibleInCallback || null,
            animation:options.animation || 'slow',
			effect:'fade'
        });
    }
    return null;
}

jQuery(function($){
    $('body').addClass('js');
    $('.image-rotator li').preloadImages();
    $('#main-banners li.no-display').removeClass('no-display');

    $("a.extrn, a[rel*=external]").attr({'target': "_blank"});

    // main banners
    var main_banners = $('#main-banners'),
        pager = main_banners.parent().find('.rotator-pagination'),
        count = main_banners.children().size();
    pager.children().first().addClass('current');
	$('#main-banners').cycle({ 
		fx:     'fade', 
		timeout: 20000,
		pager:  '.rotator-pagination',
		activePagerClass:'current',
		pagerAnchorBuilder: function(idx, slide)
		{
			return '.rotator-pagination a.page'+idx+'';
		}
	});
});


jQuery(function($){
    $('.cms-index-index .new-arrivals li').each(function(index) {
		var $this=$(this),
			maxheight=0;
		if($this.height()>maxheight){
			maxheight=$this.height();
		}
		$('.cms-index-index .new-arrivals li').height(maxheight);
	});
});

jQuery(function($){
    $('.product-essential .tabs h3').click(function(index) {
		var $this=$(this);
		if($this.next().hasClass('no-display')){
			$('.product-essential .tabs h3').removeClass('active');
			$('.product-essential .tabs .cont').addClass('no-display');
			$this.next().removeClass('no-display');
			$this.addClass('active');
			$this.parent().next().addClass('no-display');
		} else {
			$this.next().addClass('no-display');
			$this.removeClass('active');
			$this.parent().next().removeClass('no-display');
		}
	});
});

jQuery(function($){
	$('.cms-index-index .new-arrivals ul').jcarousel({ scroll: 3 });
});


/* product view */
jQuery(function($){
    if ($('body').hasClass('catalog-product-view') && !$('body').hasClass('ajaxcatalog-product-view')){
        $('.product-view .more-views .thumbnails li:first-child').addClass('thumbnail-active');

        $('.product-view .more-views .thumbnails').delegate('a.full-size', 'click', function(){
            var $this = $(this),
                wrapper = $('.product-view .product-img-box'),
                imgbox = wrapper.find('.product-image'),
				enlargeImgBox = wrapper.find('a.enlarge-image'),
				enlargeImg = $this.parent().find('a.enlarge-size'),
                imgwrap = imgbox.parent(),
                med_size = $this.parent().find('a.medium-size');

            if ($this.parent().hasClass('thumbnail-active'))
                return false;

            if (imgwrap.data('productvideo')){
                imgwrap.data('productvideo').remove();
                imgwrap.data('productvideo', null);
                imgwrap.find('.mousetrap').show();
            }
            imgbox.find('img').attr('src', med_size.attr('href'));
            imgbox.attr('href', $this.attr('href')).preloadImages();
			imgbox.find('enlarge-image-box').attr('src', enlargeImg).preloadImages();
			enlargeImgBox.attr('href', enlargeImg).preloadImages();
            $this.parent().addClass('thumbnail-active');
            $this.parent().siblings().removeClass('thumbnail-active');

            // update jqzoom
            var zoom = wrapper.find('a.cloud-zoom');
            zoom.data('zoom').destroy();
            zoom.CloudZoom();
            return false;
        });
        $('.product-view .product-img-box').delegate('a.enlarge-image', 'click', function(){
			$('.product-view .product-img-box .enlarge-image-box').show();
			return false;
        });
        $('.product-view .product-img-box').delegate('span.enlarge-close', 'click', function(){
			$('.product-view .product-img-box .enlarge-image-box').hide();
			return false;
        });
		$('.product-view .product-img-box .enlarge-image-box').hide();
        if (typeof RonisCatalog != 'undefined'){
            RonisCatalog.callOnGalleryChange(function(media, element, config){
                var thumbs = $('.product-view .more-views .thumbnails'),
                    carousel = thumbs.data('jcarousel'),
                    videos = [];

                $('.product-view .more-views .thumbnails .product-video').each(function(){
                    videos.push($(this).parent().html());
                });
                carousel.reset();
                carousel.size(media.length + videos.length);
                jQuery.each(media, function(idx, image){
                    carousel.add(idx, $('<span/>').append(
                        $('<a/>').addClass('full-size').attr({
                            'href': image.fullsize,
                            'title': image.label
                        }).append($('<img/>').attr('src', image.thumbnail))
                    ).append(
                        $('<a/>').addClass('medium-size')
                            .attr('href', image.image).html('Medium size')
                            .hide()
                    ).html());
                });
                jQuery.each(videos, function(idx, video){
                    carousel.add(media.length + idx, video);
                });
                carousel.reload();
                thumbs.find('li:first-child a.full-size').click();
            });
        }

        // "see more" currency changer
        $('.product-view a.currency-switch-link').click(function(){
            var see_more = $('.header-panel .switcher-currency'),
                rnd = Math.ceil(Math.random()*100000),
                html = see_more.html(),
                id = see_more.find('.switch-wrapper').attr('id');
            html = html.replace(new RegExp(id, 'g'), id+rnd);
            $(this).after(html).remove();
            return false;
        });
    }

});

