/**
 * =================================================================
 * Quick Links widget
 * =================================================================
 * -----------------------------------------------------------------
 *
 * Prepared by Alex Marquez
 * 
 * Display and layout for Quick Links widget.
 * 
 **/  
;(function($) {
  $.fn.quickLinks = function(options) {
    var defaults = {
      classes : {
                  "containerPrefix"       : "quickLinks_"
				  ,"containerSuffix"      : "Cols"
                  ,"list_rowEnd"          : "quickLinks_rowEnd"
				  ,"linkPrefix"			  : "quicklink_"
                }
      ,maxLinks : 8
      ,maxCols : 4
	  ,minCols : 3
	  ,numCols : 4
	  ,instanceId : null
    };
    
	if (options.maxLinks == 0) { options.maxLinks = defaults.maxLinks; };
	if (options.maxCols == 0) { options.maxCols = defaults.maxCols; };
	if (options.minCols == 0) { options.minCols = defaults.minCols; };
    var options = $.extend(defaults, options);
        
    function paddingFix(container) {
      container.find("li a").each(
        function(index, item) {
          var my_span = $(item).find('span');
          
          var item_height = $(item).height();
          var span_height = $(my_span).height();
          var height_split = Math.floor( ( item_height - span_height ) / 2);
          
          if (height_split > 0) {
            $(item).css( {
              'height' : (item_height - height_split * 2) + 'px',
              'padding-top' : height_split + 'px',
              'padding-bottom' : height_split + 'px'
            });
          }
        }
      );
    };
    
    function addFormatClasses(container) {
		var quick_links = container.find("li");
		var num_links = quick_links.length;
		
		// Get number of columns
		var numRows = options.maxLinks/options.maxCols;
		var cols = Math.ceil(num_links/numRows);
		if (cols > options.maxCols) { cols = options.maxCols; }
		if (cols < options.minCols) { cols = options.minCols; }
		options.numCols = cols;
		  
		var containerClass = options.classes.containerPrefix + options.numCols + options.classes.containerSuffix;
		container.find("div.quickLinksWidgetContainer").addClass(containerClass);
		
		// Now we need to add the rowEnd class to the last <li> on each row
		var i = options.numCols;
		while (i <= num_links) {
			container.find("li").eq(i - 1).addClass(options.classes.list_rowEnd);
			i += options.numCols;
		}
    }
    
	/**
	*	Our list items are returned out of order for some reason. Re-order the <li> elements.
	*/
	function sortList(container) {
		var ul = container.find("ul");
		var listitems = ul.children('li').get();
		
		listitems.sort(function(a, b) {
			// ids are in this format : quicklink_1282240633_0 where the last digit is the index
			var idPrefix = "quicklink_" + options.instanceId + "_";
			// get the ID for each
			var compA = $(a).attr("id").replace(idPrefix, "");
			var compB = $(b).attr("id").replace(idPrefix, "");
			return (compA - compB);
		});
		
		$.each(listitems, function(idx, itm) { ul.append(itm); });
	}
	
    function log(message) { /* debug function */
      if (console && console.log) {
        console.log(message);
      }
    }
    
    return this.each(function(){
		var container = $(this);
		//showGhosts(container);
		sortList(container);
		addFormatClasses(container);
		paddingFix(container);
    });
    
  };

})(jQuery);

