/**
 * @author tparent
 */

$(document).ready(function() {
    
    var settings = {
             tagCloudGradient: { topColor: '0B4433', bottomColor: '108143' },
			 tagCloudCorners: {radius: '12px'}
    };
    
    $.extend(settings, customSettings);
	
	/* Truncate the snippet titles, if they run over a single line */
	$("#results-list .item .main h4 a").each(function(){
		var myTitle = $(this);
		if(myTitle.height() > 20){
			myTitle.attr("title", '|'+myTitle.text()).truncate(70, {
		    chars: / /,
		    leave: false,
		    trail: [true, '...','']
		  })/*.cluetip({splitTitle:'|', showTitle:false, cluetipClass: 'jtip',positionBy: 'fixed'})*/;
		}
	});
	
	/* $("p[@title]").cluetip({
		splitTitle: "|",
		showTitle:  false,
		cluetipClass: 'jtip',
		positionBy: 'fixed'
	});*/
	
	/* Splash pages shouldn't be hit by the tagCloud dressing - it should be done
	 *    in its own splash-page.js
	 */
	$("body:not(#splash-page) #tagcloud-wrapper").corner(settings.tagCloudCorners.radius).gradient({from: settings.tagCloudGradient.topColor, to: settings.tagCloudGradient.bottomColor, direction: "horizontal"});
	
	/* Truncate the rss-description chunks */
	var RCB = $("#related-content-block");
	$("ul li .rss-description", RCB).truncate(120, {
		chars: /\s/,
		leave: false,
		trail: [true, '...','']
	});
	$("ul li", RCB).slice(10).hide();
    
});

jQuery.fn.truncate = function(max,settings) {
	var d = "";
	var v = "";
	var c = "";
    settings = jQuery.extend({
        chars: /\s/,
        leave: false,
        trail: [false, "...", ""]
    }, settings);
    return this.each(function() {
        if (!settings.leave || (settings.leave && jQuery(this).children().length == 0)) {
            v = jQuery.trim(jQuery(this).text());
			if(v.length > 0) {
	            while (max < v.length && max >= 0) {
	                c = v.charAt(max);
	                if (c.match(settings.chars)) {
	                    d = v.substring(0,max) + settings.trail[1];
	                    jQuery(this).html(d);
	                    break;
	                }
	                max--;
	            }
	            if (settings.trail[0]) {
	                jQuery(this).html("<span>" + d + "</span>").append("<span>" + v + "</span>").find("span:eq(1)").append(settings.trail[2]).css("display","none");
	                jQuery("a:eq(0)",this).click(function() {
	                    jQuery(this).parent().css("display","none").parent().find("span:eq(1)").css("display","inline");
	                    return false;
	                });
	                jQuery("a:eq(1)",this).click(function() {
	                    jQuery(this).parent().css("display","none").parent().find("span:eq(0)").css("display","inline");
	                    return false;
	                });
	            }
			};
        }
    });
};