var $j = jQuery.noConflict();
$j(document).ready(function() { 

    //Fix pngs for IE6
    $j(document).pngFix();  

    // activate fancyBox
    $j("a.lightbox").fancybox(); 
    
    // resize the News Images
    resizeNewsImages('.content .news-list-container .news-list-item .thumbs');
    resizeNewsImages('.content .news-single-item .thumbs');
    
    // hide all News from list expect the first one and make them foldable
    if($j('body#p17').length == 0)
    {
        $j('.news-list-container .news-list-item:gt(0)').find('.hideable').hide();
        $j('.news-list-container .news-list-item:gt(0) h1').css({'margin-bottom':'0'});
        $j('.news-list-container .news-list-item h1').css({'cursor':'pointer'});
        $j('.news-list-container .news-list-item h1').bind('click', function(e) {
            $j('.news-list-container .news-list-item').find('.hideable').hide('fast');

            // correct the margin-bottom for h1, because h2 has negative margin-top 
            $j('.news-list-container .news-list-item h1').css({'margin-bottom':'0'});
            $j(this).css({'margin-bottom':'35px'});
            
            // show the element content
            $j(this).parent().children('.hideable').show('fast');
        });
    }
    // hide all ingredients and make them foldable
    var zutaten = $j('#container-right-product .csc-textpic');
    hideIngredients(zutaten);
    replaceSign($j('#container-right-product .csc-textpic'));
    
    // show further informations for products after click
    var clickForMore = $j('.content .content-product .click-for-more');
    clickForMore.show();
    clickForMore.nextAll().hide();
    clickForMore.bind('click', function() {
        $j(this).hide();
        $j(this).nextAll().show();    
    });
    
    // swap Images for News
    swapImages($j('.news-list-item'));
    swapImages($j('.news-single-item'));
    
    // add a class to the first header in news list
    $j('.news-list-container .news-list-item:eq(0) h1').addClass('first');
    
    // Folding für irFAQ
    initToggleElements(
        'dt.tx-irfaq-dynheader', // clickProxySelector
        '+ dd' // toggledElementSelector)
    );    
    
});

function resizeNewsImages(wrap) {
    $j(wrap).each(function() 
    {
      $j(this).find('li:gt(0) img').each( function() {
          $j(this).css({'width':'38px' , 'height':'28px'});
       });
    });
}

function hideIngredients(zutaten)
{
    $j('#container-right-product .csc-textpic:gt(0):not(.no-collapse .csc-textpic)').hide();
    $j('#container-right-product .csc-textpic:eq(0)').addClass('opened');
    $j('#container-right-product .csc-textpic:gt(0)').addClass('closed');
    $j('#container-right-product .csc-header h1').css({'cursor':'pointer'});
    
    replaceSign(zutaten);
    
    zutaten.prev().bind('click', function() {
        var clicked = $j(this);
        zutaten.not(clicked.next()).hide('normal');
        $j(this).next().toggle('normal');


        if ($j(this).next().hasClass('closed'))
        {
            $j(this).next().removeClass('closed');
            $j(this).next().addClass('opened');
        }
        else
        {
            $j(this).next().addClass('closed');
            $j(this).next().removeClass('opened');
        }
        zutaten.not($j(this).next()).addClass('closed');
        zutaten.not($j(this).next()).removeClass('opened');

        replaceSign(zutaten);
    });
}
function replaceSign(zutaten) {
    zutaten.each(function() {
        if ($j(this).hasClass('closed'))
        {
            var regexpMinus = /(\-),?/g;
            var str = $j(this).prev().html().replace(regexpMinus,"+");
            $j(this).prev().html(str);
        }
        if ($j(this).hasClass('opened'))
        {
            var regexpPlus = /(\+),?/g;
            var str = $j(this).prev().html().replace(regexpPlus,"-");                
            $j(this).prev().html(str);
        }
    });
}   
function swapImages(item)
{
    // counter for adding a rel-tag for each nenws item at the news-list
    var count = 0;
    
    item.each(function() {
        var bigImage = $j(this).find('.thumbs li a img:eq(0)');
        var smallImages = $j(this).find(' .thumbs li a img:gt(0)');
        
        // add rel-tag to the big image
        bigImage.parent().attr('rel', 'imageset[' + count + ']');
        
        smallImages.each(function() {
            var element = $j(this);

            // add rel-tag to the small image
            element.parent().attr('rel', 'imageset[' + count + ']');
            
            // prevent click event
            $j(this).parent().unbind('click').click(function(){ return false; });

            // change mouse cursor
            $j(this).parent().css({ 'cursor':'default' });
            
            // swap images on mouseenter
            $j(this).parent().parent().bind('mouseenter', function() {
                var imgSrc1 = element.attr('src');
                var linkTarget1 = element.parent().attr('href');
                var imgSrc2 = bigImage.attr('src');
                var linkTarget2 = bigImage.parent().attr('href');

                bigImage.attr('src', imgSrc1);
                bigImage.parent().attr('href', linkTarget1);

                element.attr('src', imgSrc2);
                element.parent().attr('href', linkTarget2);   
            });
        });
        count++;
    });
}


function initToggleElements(clickProxySelector, toggledElementSelector)
{
    var $allClickProxies = $j(clickProxySelector);
    
    if ($allClickProxies)
    {
        $allClickProxies
            .css({cursor: 'pointer'})
            .click(
                function()
                {
                    // the clicked h2 is a proxy for another element which will be
                    // hidden or displayed, search for this first because it will
                    // be reused
                    $element = $j(this).find(toggledElementSelector);

                    if ($j(this).hasClass('opened'))
                    {
                        $j(this).removeClass('opened');
                    }
                    else
                    {
                        $j(this).addClass('opened');
                    }
                    
                    // treat clicked element  (use slideDown() instead of
                    // toggle() is user is not allowed to close opened elements
                    // by clicking on them)
                    $element.toggle('fast');
                    
                    // hide all visible elements, but not the clicked element (that's the trick!)
                    $allClickProxies.find(toggledElementSelector).filter(':visible').not($element).slideUp('fast');
                    $allClickProxies.not(this).removeClass('opened');
                }
            )
            // hide all by default
            .find(toggledElementSelector).hide();
    }
}
