/*
 * Horizontal Bar Graph for jQuery
 * version 0.1a
 *
 * http://www.dumpsterdoggy.com/plugins/horiz-bar-graph
 *
 * Copyright (c) 2009 Chris Missal
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 */
(function($) {
  $.fn.horizontalBarGraph = function(options) {

    var opts = $.extend({}, $.fn.horizontalBarGraph.defaults, options);

    this.children(".name_candidate").addClass("hbg-label");
    this.children(".votes_candidate").each(	function() {
      $(this).children(".percentage").createBar(opts)
      $(this).children(".votesLabel").createTitle(opts)
    }).css({'width': opts.interval+"em"});
		return this;
	};

      
	
	$.fn.createBar = function(opts) {
		var val = this.text();
    var _width = val * opts.interval +"em"
    this.css({'width': _width}).addClass("hbg-bar");
    /*applyOptions(this, opts);*/
	}
	
	$.fn.createTitle = function(opts) {
		var votes = this.text();
    this.prev().html($("<span/>").html(votes).addClass("hbg-value"));
    var _td = this.parent()
    var barHtml = _td.html();

    var _div = $('<div/>').css({'width': '120px', 'border': '1px dashed black'});
    votes=="0 Votes" ? _div.html(votes) : _div.html(barHtml).children(".votesLabel").remove();
	  _td.html(_div)
  }
	
	function isTitleDD(e) {
		return (e.hasClass(".votesLabel"));
	}
	
	/*	
    
    //this is the cruft that was part of the original function for animation

    setBarHover($(this), opts);
		tryShowTitle(this);
		
		if(opts.animated) {
			createShowButton(opts, this).insertBefore(this);
		}
		if(opts.colors.length) {
			setColors(this.children("dd"), opts);
		}
		if(opts.hoverColors.length) {
			setHoverColors(this.children("dd"), opts);
		}
  scaleGraph(this); 
  
  //end crufrt of original function
  */
	
  /*function scaleGraph(graph) {*/
  /*var maxWidth = 0;*/
  /*graph.children("dt").each(function() {*/
  /*maxWidth = Math.max($(this).width(), maxWidth);*/
  /*}).css({width: maxWidth+"px"});*/
  /*}*/

  /* 
   
  //stuff dealing with pretty colors and animation

	function applyOptions(e, opts) {
		if(opts.animated) { e.hide(); }
	}
	
	function setColors(bars, opts) {
		var i = 0;
		bars.each(function() { 
			var c = i++ % opts.colors.length;
			$(this).css({backgroundColor: opts.colors[c]});
		});
	}
	
	function setBarHover(bar, opts) {
		bar.hover(function() {
			bar.addClass("hbg-bar-hover");
		}, function() {
			bar.removeClass("hbg-bar-hover");
		});
	}
	
	function setHoverColors(bars, opts) {
		var i = 0;
		bars.each(function(i) {
			var bar = $(this);
			var c = bar.css("background-color");
			var hc = opts.hoverColors[i++ % opts.hoverColors.length];
			bar.hover(function() {
				$(this).css({backgroundColor: hc});
			}, function() {
				$(this).css({backgroundColor: c});
			});
		});
	}
	
	function createShowButton(opts, graph) {
		var button = $("<span />").text(opts.button).addClass("hbg-show-button");
		button.click(function() {
			graph.children("dd").show('slow', function() { button.fadeOut('normal'); });
		});
		return button;
	}
	
	function tryShowTitle(graph) {
		var title = graph.attr("title");
		if(title) {
			$("<div/>").text(title).addClass("hbg-title").insertBefore(graph);
			graph.css({overflow: "hidden"});
		}
	}
  //end animation
*/	

	$.fn.horizontalBarGraph.defaults = {
		interval: 1,
		hasTitles: true,
		animated: false,
		button: 'Show Values',
		colors: [],
		hoverColors: []
	};
	
})(jQuery);
