﻿jQuery(document).ready(function($) {
    $(".specific-box-slide,top-specific-box").find("li:first-child").addClass("current");
    function calcHeight() {
        var currentTallest = 0,
        currentRowStart = 0,
        rowDivs = new Array(),
        $el,
        topPosition = 0;
        $(".specific-box").each(function() {
            $el = $(this);
            topPosition = $el.position().top;

            if (currentRowStart != topPosition) {
                for (currentDiv = 0; currentDiv < rowDivs.length; currentDiv++) {
                    rowDivs[currentDiv].find(".specific-box-slide").height(currentTallest);
                }
                rowDivs.length = 0;
                currentRowStart = topPosition;
                currentTallest = $el.find(".specific-box-slide li.current").height();
                rowDivs.push($el);
            } else {
                rowDivs.push($el);
                currentTallest = (currentTallest < $el.find(".specific-box-slide li.current").height()) ? ($el.find(".specific-box-slide li.current").height()) : (currentTallest);
            }
            for (currentDiv = 0; currentDiv < rowDivs.length; currentDiv++) {
                rowDivs[currentDiv].find(".specific-box-slide").height(currentTallest);
            }
        });
    }

    function mycarousel_initCallback(carousel, item, idx, state) {
        if (state == 'init') return false;
        $(item).parent().find('.jcarousel-item').removeClass("current");
        $(item).addClass("current");
        calcHeight();

    };
    $('.specific-box-slide').each(function() {
        $(this).jcarousel({
            scroll: 1,
            itemVisibleInCallback: { onAfterAnimation: mycarousel_initCallback }
        });
    });
    calcHeight();

    function calcHeight_productList(item) {
        var arr = $(item);
        for (i = arr.length; i--; ) {
            var ml = 0;
            $(arr[i]).find('h2').each(function() {
                if ($(this).height() > ml) {
                    ml = $(this).height();
                }
            }).height(ml); ;
        }
    }
    calcHeight_productList('.products-grid');

    $('.product_information').find('li').each(function() {
        if ($.trim($(this).html()).length == 0) {
            $(this).css('font-size', '0');
        }

    });

    $("#nav li .sub-menu").each(function() {
        var sub_menu_height = $(this).find(".submenu-table").height();
        var cmsblock_height = $(this).find(".cmsblock").height();
        var currentTallest = sub_menu_height > cmsblock_height ? sub_menu_height : cmsblock_height;
        if (sub_menu_height > cmsblock_height) {
            $(this).find(".cmsblock").height(sub_menu_height);
        } else if (sub_menu_height < cmsblock_height) {
            $(this).find(".submenu-table").height(cmsblock_height);
        }
    });
	
		
	/*mac menu
	if ( navigator.userAgent.toLowerCase().indexOf( 'mac' ) > -1 ) { 
		$('body').addClass('mac');
		if ( navigator.userAgent.toLowerCase().indexOf('safari') > -1 ) { 
			$('body').addClass('mac-safari');
		}
	}
*/
	function table_width(max_width, nav_word, nav_li) {
	    var nav_n = $(nav_li).size();
	    var min_width = 0;
	    $(nav_word).each(function() {
	        min_width = min_width + $(this).width();
	    });
	    //console.log('max_width:'+max_width+'\t\t'+'min_width:'+min_width+'\t\t'+'nav_n:'+nav_n);	
	    var nav_left,
			nav_right,
			nav_first_left,
			nav_last_right;

	    //1.
	    var remainder = (max_width - min_width) % nav_n;
	    var nav_av = (max_width - min_width - remainder) / nav_n;
	    //console.log('remainder:'+remainder+'\t\t'+'nav_av:'+nav_av);	

	    //2.
	    if (nav_av % 2) {
	        nav_left = (nav_av + 1) / 2;
	        nav_right = (nav_av - 1) / 2;
	    } else {
	        nav_left = nav_right = nav_av / 2;
	    }
	    $('#nav>li>a').css({
	        'padding-left': nav_left,
	        'padding-right': nav_right
	    });
	    //console.log('nav_left:'+nav_left+'\t\t\t'+'nav_right:'+nav_right);	
	    //3.
	    if (remainder) {
	        if (remainder % 2) {
	            nav_first_left = (remainder - 1) / 2;
	            nav_last_right = (remainder + 1) / 2;
	        } else {
	            nav_first_left = nav_last_right = remainder / 2;
	        }
	        $('#nav>li:first>a').css('padding-left', nav_left + nav_first_left);
	        $('#nav>li:last>a').css('padding-right', nav_right + nav_last_right);
	    }
	    //console.log('nav_first_left:'+nav_first_left+'\t\t'+'nav_last_right:'+nav_last_right);	
	};
	table_width(999, '#nav>li>a', '#nav>li');
})
