
/**
 * Set height in row (parentSelector, childSelector, itemsPerRow)
 */
function setEqualHeightInRow(parentSelector, childSelector, itemsPerRow){
           
    $(parentSelector).each(function(){

        var items = $(this).find(childSelector);

        var itemCount = $(items).size();

        var j;

        // Set equeal height for items in row

        for (var i = 0; i < itemCount; i += itemsPerRow) {

            // Get max height in row

            var actualMaxHeight = 0;

            for (j = 0; j < itemsPerRow; j++) {

                var height = $(items).eq(i+j).height();

                if(height > actualMaxHeight) actualMaxHeight = height;

            }

            // Set height in row

            for (j = 0; j < itemsPerRow; j++) {

                $(items).eq(i+j).height(actualMaxHeight);

            }
        }
    });
}

/**
 * If element exist return true   
 */
function isElement(element){     
          
    var result;

    if($(element).length > 0){ result = true; }else{ result = false }  

    return result;   

}

$(document).ready(function() {

    $("a.fancybox").fancybox({
        'transitionIn'	:	'elastic',
        'transitionOut'	:	'elastic',
        'speedIn'		:	600,
        'speedOut'		:	200,
        'overlayShow'	:	false
    });    

    /**********************************************************************/
    /* Basket                                                             */
    /**********************************************************************/
    
    $('#basket ul.steps li:last').addClass('last');

    $('#basket ul.steps li a').each(function() {
        if($(this).find('br').length == 1){
            $(this).addClass('twoRows');
        }
    });

    /**********************************************************************/
    /* Table tr event                                                     */
    /**********************************************************************/
    
    if(isElement('table')){

        $('table tr:even').addClass('even');

    }

    /**********************************************************************/
    /* Form elements focus                                                */
    /**********************************************************************/

    if(isElement('input.focus')){

        $('input.focus').focus(function() {if (this.value == this.defaultValue){ this.value = ''; }});

    }

    if (isElement('.productsList')) {

        $('.productsList li').click(function() {
            window.location.href = $(this).find('h2 a').attr('href');
        });

    }

    if (isElement('.jQuerySlider')) {

        $('.jQuerySlider').slider({

            range: true,
            min: 0,
            max: 99999,
            step: 100,
            values: [ 0, 60000 ],
            slide: function( event, ui ) {
                    $('.priceFrom strong span').html(ui.values[ 0 ]);
                    $('.priceTo strong span').html(ui.values[ 1 ]);
            }

        });

        $('.priceFrom strong span').html($('.jQuerySlider').slider('values', 0));

        $('.priceTo strong span').html($('.jQuerySlider').slider('values', 1));

    }


    /**********************************************************************/
    /* jCarousel                                                          */
    /**********************************************************************/

    if (isElement('#slider .jcarousel')) {

        $('#slider .jcarousel').jcarousel({
            scroll: 1,
            visible: 1,
            auto: 5,
            wrap: "both"
        });
    }

    if(isElement('#productsSlider .jcarousel')){
        $('#productsSlider .jcarousel').jcarousel({
            scroll: 1,
            visible: 4,
            auto: 5,
            wrap: "both"
        });
    }

    /**********************************************************************/
    /* Show and hide content                                              */
    /**********************************************************************/

    if (isElement('.showOrHideContent')) {

        $('.showOrHideContent').click(function(a) {

            a.preventDefault();

            $($(this).attr('href')).toggle(200);

        });
    }
});

$(window).load(function() {
    
    //Set height in row (parentSelector, childSelector, itemsPerRow)                                                  *
    

        setEqualHeightInRow('.productsList', 'h2', 4);

        setEqualHeightInRow('.productsList', '.image', 4);

        setEqualHeightInRow('.productsList', 'li', 4);
        
        setEqualHeightInRow('.columns', '.col', 2);
    
    
});
