jQuery.noConflict();

jQuery(document).ready(function($)
{
    jQuery(".show_more").click(function()
    {
        var intro = jQuery(this).prev();
        var outro = jQuery(this).prev().find('span');

        jQuery(this).css('display', 'none');
        jQuery(this).prev().css('display', 'none');

        if(outro.css('display') == 'inline')
        {
            outro.css('display', 'none');
        }
        else
        {
            outro.css('display', 'inline');
        }

        intro.slideToggle('slow', function () {
            var anch = $(this).next();

            if(anch.html() == '&lt;&lt; zwiń')
            {
                anch.html('czytaj więcej &gt;&gt;');
                anch.show('slow');
            }
            else
            {   
                anch.html('&lt;&lt; zwiń');
                anch.show('slow');
            }
        });
    });

    jQuery('.imgInfoZoom').mouseenter(function() {
        var offset = $(this).offset();
        
        var offsetTop = (offset.top - $(document).scrollTop());
        var windowHeight = $(window).height();
        
        var fixedTop = windowHeight - offsetTop;

        var popup = jQuery(this).prev();
        var pointer = jQuery(this).prev().find(".imgPopupPointer");
        var popupHeight = popup.height();

        var popupTopTop = 10 - offsetTop + 'px';
        var popupTopBottom = -10 - (popupHeight - fixedTop) + 'px';

        if(offsetTop < 120 && offsetTop > -22)
        {
            popup.css('top', popupTopTop);
            pointer.css('top', offsetTop + 'px');
        }
        else if(offsetTop < -22)
        {
            popup.css('top', popupTopTop);
            pointer.css('display', 'none');
        }
        else if(fixedTop < 260 && fixedTop > 45)
        {
            var fix = popupHeight - fixedTop;
            popup.css('top', popupTopBottom);
            pointer.css('top', fix + 'px');
        }
        else if(fixedTop < 45)
        {
            popup.css('top', popupTopBottom);
            pointer.css('display', 'none');
        }
        else
        {
            popup.css('top', -110 + 'px');
        }

        popup.show('fast');

        popup.mouseenter(function()
        {
            var offsetTop = (offset.top - jQuery(document).scrollTop());
            var windowHeight = jQuery(window).height();
            var fixedTop = windowHeight - offsetTop;
            var fix = popupHeight - fixedTop;

            jQuery(this).css('display', 'block');

            var pointer = jQuery('div:first', this);
            
            if(offsetTop < 120 && offsetTop > -22)
            {
                pointer.css('top', offsetTop + 'px');
            }
            else if(offsetTop < -22)
            {
                pointer.css('display', 'none');
            }
            else if(fixedTop < 260 && fixedTop > 45)
            {
                jQuery('div:first', this).css('top', fix + 'px');
            }
            else if(fixedTop < 45)
            {
                pointer.css('display', 'none');
            }
            
            jQuery('img:first', this).click(function(){
                jQuery(this).parent().parent().parent().css('display', 'none');
            });
            
        }).mouseleave(function() {
            jQuery(this).hide();
            jQuery(".imagesThumbs img").removeClass('thumbActive');
            jQuery(".imagesThumbs img:first").addClass('thumbActive');
            var imgwrap = jQuery(".imagesThumbs img:first").parent().parent();
            imgwrap = jQuery('img:first', imgwrap);
            var src = jQuery(".imagesThumbs img:first").attr('src');
            src = src.replace('_', '');
            imgwrap.attr('src', src);
            jQuery(this).prev().css('top', '0');
            jQuery('.imgPopupPointer', this).css('top', '110px');
        });

    }).mouseleave(function() {
        jQuery(this).prev().hide();
        jQuery(this).prev().find(".imgPopupPointer").css({'top':'110px', 'display' : 'block'});
    });

    jQuery("#plus").click(function() {
        var dup = jQuery('#duplicate');

        if(jQuery("#produkt-foto p").size() > 3)
        {
            return false;
        }
        else
        {
            dup.parent().append('<p>' + dup.html() + '</p>');
        }
    });

    jQuery("#minus").click(function() {
        if(jQuery("#produkt-foto p").size() == 1)
        {
            return false;
        }
        else
        {
            jQuery('#produkt-foto p:last-child').remove();
        }
    });

    jQuery(".imagesThumbs img:first").addClass('thumbActive');

    jQuery(".imagesThumbs img").click(function() {
        jQuery(".imagesThumbs img").removeClass('thumbActive');
        jQuery(this).addClass('thumbActive');

        var tsrc = jQuery(this).attr('src');
        
        var imgwrap = jQuery(this).parent().parent();
        imgwrap = jQuery('img:first', imgwrap);
        
        var awrap = jQuery(this).parent().parent();
        awrap = jQuery('a:first', awrap);
        
        var wsrc = tsrc.replace('_', '');
        imgwrap.attr('src', wsrc);
        awrap.attr('href', wsrc);
    });

    jQuery(".ovThumbs img:first").addClass('thumbActive');
    
    jQuery(".ovThumbs img").click(function() {
        jQuery(".ovThumbs img").removeClass('thumbActive');
        jQuery(this).addClass('thumbActive');

        var tsrc = jQuery(this).attr('src');
        var imgwrap = jQuery(this).parent().prev().find('img');
        var wsrc = tsrc.replace('_', '');
        imgwrap.attr('src', wsrc);
    });

   jQuery(".imgWrap[rel]").overlay({mask: '#444'});
   jQuery(".imgInfoZoom img[rel]").overlay({mask: '#444'});
   jQuery(".overlay .close").click(function(){
       var ovThumbs = jQuery(this).next().next();
       ovThumbs.find("img").removeClass('thumbActive');
       jQuery("img:first", ovThumbs).addClass('thumbActive');
       jQuery(this).next().find('img').attr('src', jQuery("img:first", ovThumbs).attr('src'));
   });

$("#chained").scrollable({circular: true, mousewheel: true}).navigator().autoscroll({
	interval: 4000		
})
});
