/*
 * 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("dt,dd").each(function(i) {
		
			var el = $(this);
			if(el.is("dt")) {
				el.css({display: "block", float: "left", clear: "left"}).addClass("hbg-label"); 
				setLabelHover(el, opts); 
				showList(el, opts);
				
				if(el.html() == $.cookie("curRazd")){
					setCurent(el, opts);
				}

				return;
			} else {
				(isTitleDD(el) && opts.hasTitles ? createTitle : createBar)(el, opts);
			}
			
			setBarHover(el, opts);
		});
		
		tryShowTitle(this);
	
		if(opts.colors.length) {
			setColors(this.children("dd"), opts);
		}
		if(opts.hoverColors.length) {
			setHoverColors(this.children("dd"), opts);
		}
		
		scaleGraph(this);
		
		return this;
	};
	
	function scaleGraph(graph) {
		var maxWidth = 0;
		graph.children("dt").each(function() {
			maxWidth = Math.max($(this).width(), maxWidth);
		}).css({width: maxWidth+"px"});
	}
	
	function setCurent(label, opts) {
		label.click();
	}
	
	function showList(label, opts) {
		label.click(function() {
			var lbl = label.html();
			var list = $(".browser-list");
			
			$('.hbg-label-curent').removeClass("hbg-label-curent");
			label.addClass("hbg-label-curent");
			
			$.cookie("curRazd", lbl, {expires: 7});
			
			$.getJSON('includes/ajax.charts_list.php', 'label='+lbl, function(data){
				var items = [];
				var i = 0;

				$.each(data, function(key, val) {
					items.push('<span><a href="goods.php?search=' + val + '" class="g1" title="Поиск: ' + val + '">' + val + '</a></span>');
				});

				list.children("dt").each(function(){
					$(this).children("span").slideUp('slow', function(){
						$(this).html(""+items[i]+"").slideDown('slow');
						i++;
					});
					
				});
			});
		});
	}
	
	function setBarHover(bar, opts) {
		bar.hover(function() {
			bar.addClass("hbg-bar-hover");
		}, function() {
			bar.removeClass("hbg-bar-hover");
		});
	}
	
	function setLabelHover(label, opts) {
		label.hover(function() {
			label.removeClass("hbg-label");
			label.addClass("hbg-label-hover");
		}, function() {
			label.removeClass("hbg-label-hover");
			label.addClass("hbg-label");
		});
	}
	
	function createBar(e, opts) {
		var val = e.text();
		var dl_width = opts.dlWidth;
		var dt_width = opts.dtWidth;
		var dd_width = dl_width - dt_width - 15;
		var per = Math.floor(val/opts.interval);
		var width = dd_width * per / 100;

		e.css({marginLeft: e.prev().is("dt") ? "5px" : "0px", width: "0px"});
		e.html("&nbsp;");
		applyOptions(e, opts);
		
		if(opts.animated) {
			e.animate({width: width+"px"}, 1500 );
		} else {
			e.css({width: width+"px"});
		}
	}
	
	function createTitle(e, opts) {
		var title = e.text();
		e.prev().attr("title", title);
		e.remove();
	}
	
	function tryShowTitle(graph) {
		var title = graph.attr("title");
		if(title) {
			$("<div/>").text(title).addClass("hbg-title").insertBefore(graph);
			graph.css({overflow: "hidden"});
		}
	}
	
	function setColors(bars, opts) {
		var i = 0;
		bars.each(function() { 
			var c = i++ % opts.colors.length;
			$(this).css({backgroundColor: opts.colors[c]});
		});
	}
	
	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 applyOptions(e, opts) {
		if ($.browser.msie) {
			e.css({marginLeft: opts.dtWidth+10+"px"}).addClass("hbg-bar");
		} else {
			e.css({float: "left"}).addClass("hbg-bar");
		}
	}
	
	function isTitleDD(e) {
		return (e.is(":even") && e.prev().is("dd"));
	}
	
	$.fn.horizontalBarGraph.defaults = {
		interval: 1,
		hasTitles: false,
		animated: false,
		button: 'Show Values',
		colors: [],
		hoverColors: []
	};
	
})(jQuery);

