/* Top menu */
jQuery(document).ready(function() {
    jQuery(".nav-container ul li ul li a").hover ( function() {
	
	var img = jQuery(this).attr("rel");
	if (img) {
	    jQuery('.nav-container ul li ul li.img img').attr("src", img); 
	}
    });

     jQuery(".nav-container ul li ul li a").each ( function () {
	var img = new Image();
	img.src = jQuery(this).attr("rel");
    });

});

/* pagination for products */
    var cells = 6;

    function paginate_products() {
	var grid = jQuery('.category-products-all .products-grid');
        var size = jQuery('li:visible', grid).size();

        var current = cells;
        var width = jQuery('li', grid).innerWidth()+2;
        var ul_width = size * width;
        jQuery(grid).css("width", ul_width + 20  + "px");
	jQuery('.toolbar-bottom .prev, .toolbar-bottom .next').show();
	jQuery('.toolbar-bottom .prev, .toolbar-bottom .next').removeClass('b_active');
	jQuery(grid).css('left', '0px');

	if (size <= cells)  return false;

        jQuery('.toolbar-bottom .next').addClass('b_active');
	
        jQuery('.toolbar-bottom .next').unbind('click');
	jQuery('.toolbar-bottom .prev').unbind('click');

        jQuery('.toolbar-bottom .next').click ( function (e) {
            e.preventDefault();

            if (! jQuery(this).hasClass('b_active')) return;

            jQuery(grid).animate({
                left: '-='+width
            }, 200);
            current++;
            if (current > cells) {
                jQuery('.toolbar-bottom .prev').addClass('b_active');
            }
            if (current >= size) {
                jQuery(this).removeClass('b_active');
            }
        });
        jQuery('.toolbar-bottom .prev').click ( function (e) {
            e.preventDefault();

            if (! jQuery(this).hasClass('b_active')) return;

            jQuery(grid).animate({
                left: '+='+width
            }, 200);
            current--;
            if (current < size) {
                jQuery('.toolbar-bottom .next').addClass('b_active');
            }
            if (current == cells) {
                jQuery(this).removeClass('b_active');
            }

        });
    }



/* Tabs */
jQuery(document).ready(function() {
    jQuery(".coffee_solution .coffee_tabs ul li").click (function() {
	jQuery(".coffee_solution .coffee_tabs ul li").each ( function () {
	    var div_content = "#d" + jQuery(this).attr("id");
	    jQuery(this).removeClass('active'); 
	    if (jQuery(div_content)) {
		jQuery(div_content).removeClass('active');
	    }
	});

	jQuery(this).addClass('active');
	var div_content = "#d" + jQuery(this).attr("id");
	if (jQuery(div_content)) {
	    jQuery(div_content).addClass('active');
	}
    });

    jQuery('.top_cats a').click(function() {
	var enable = false;
	if (jQuery(this).hasClass('cats_enabled')) {enable = true;}
        jQuery('.top_cats a').removeClass('cats_enabled');
        if (! enable) jQuery(this).addClass('cats_enabled');
    });

    paginate_products();

    jQuery('.popup_hover a').click(function(e) {e.preventDefault();});


    jQuery('.popup_hover').hover(function(){
        jQuery('.product_popup').css("left", "-10000px").removeClass('active');
	jQuery('.product_popup').css("right", "auto");
        var id = jQuery(this).attr('id');
	var popid = "p" + String(id);
	jQuery('#' +  popid).css("left", "auto").addClass('active');
	jQuery('#' + popid).css("right", "15px");
    });

    jQuery('.product_popup:first').css("left", "auto");
    jQuery('.product_popup:first').css("right", "15px");

    jQuery('.product_popup_content button').attr('onclick','').unbind('click');

    jQuery('.product_popup_content button').click(function() {
//	jQuery(".contact-product").show();
        var arrayPageSize = l_box.getPageSize();
        $('overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });

        new Effect.Appear(l_box.overlay, { duration: l_box.overlayDuration, from: 0.0, to: LightboxOptions.overlayOpacity });


        $('contact_product').setStyle({ top: '100px', left: '200px' }).show();

	var val = jQuery('.product_popup.active .product_popup_content h2').html();

	if (! val) val = jQuery('.product_popup .product_popup_content h2').html();

	jQuery('#contact_product #subject').val('[Enquiry] ' + val);

	$('overlay').observe('click', (function() {$('contact_product').setStyle({left: '-10000px' })}));

//	jQuery(this).unbind('click');
    });

    jQuery('#contact_product .close').click(function() {l_box.end(); $('contact_product').setStyle({left: '-10000px' }); })

});

