jQuery(function($) {
	$('.sidebar .block-login form, #ssearch_mini_form').hint();
	
    shortenBreadcrumb({
        breadcrumb : jQuery('.breadcrumbs > ul'),
        maxWidth : 435
    });
    
    if(jQuery('.featured-products table tr').size() > 0)
    {
	    jQuery('.featured-products table tr').each
	    (
		    function()
		    {
		       var rowHeight = 0;
		        jQuery(this).find('h2').each
		        (
		            function()
		            {
		                if(jQuery(this).height() > rowHeight)
		                {
		                    rowHeight = jQuery(this).height();
		                }
		                console.log(rowHeight);
		            }
		        );
		        jQuery(this).find('h2').each
		        (
		            function()
		            {
		                jQuery(this).css('padding',(Math.ceil(rowHeight - jQuery(this).height()) /2) + 'px 0');
		            }
		        );
		    }
	    );
    }
});

function shortenBreadcrumb(options){
    /*
        Shortens the breadcrumb to a specified width by removing the text from one list item
        at a time and replacing it with "..." - accepts an options object as an optional
        parameter with two options:
            shortenBreadcrumb({
                breadcrumb : $('div#breadcrumb > ul'),
                maxWidth : 725
            });

        The first is the selector for the breadcrumb ul, the second is the maximum width you
        want it to be.
    */

    var breadcrumb = jQuery('div#breadcrumb > ul');
    var maxWidth = jQuery('div#breadcrumb').innerWidth();

    if (options != undefined){
        if (options.breadcrumb != null) { breadcrumb = options.breadcrumb; }
        if (options.maxWidth != null) { maxWidth = options.maxWidth; }
    }

    var levelCount = breadcrumb.find('li').size();
    var shortEnough = false;
    var totalWidth;
    while (shortEnough == false) {
        totalWidth = 0;
        breadcrumb.children('li').each (function(){
            totalWidth += jQuery(this).outerWidth(true);
        });
        if ( (totalWidth > maxWidth) && (breadcrumb.children('li').not('.short').eq(1).length != 0) )
        {
            var li = breadcrumb.children('li').not('.short').eq(1);
            li.addClass('short');
            li.children('a').attr('title', li.children('a').html());
            li.children('a').html('...');
        }
        else if (totalWidth > maxWidth) {
    		var lastString = jQuery.trim(breadcrumb.children('li:last').html());
    		var lastStringLength = lastString.length;
    		var  q = 0;
        	while (shortEnough == false) {
        		var totalWidth = 0;
        		lastString = lastString.slice(0, lastStringLength-=1);
        		breadcrumb.children('li:last').html(lastString + '...')
        		jQuery('.breadcrumbs > ul').children('li').each (function(){
    				totalWidth += jQuery(this).outerWidth(true);
    			});
        		if(totalWidth <= maxWidth) {
        			shortEnough = true;
        		}
        	}
        }
        else
        {
        	shortEnough = true;
        }
    }
}
