$(document).ready(function() {

		$("#pricelistcontainer").append(  $(".pricelist").clone().removeClass("pricelist").addClass("pricelist-prices")  );
		$("#pricelistcontainer").prepend('<div id="pricelist-tabs"><a class="pb-acrylic" href="#" rel="acrylic">Acrylic</a><a href="#" class="pb-canvas" rel="canvas">Canvas</a><a href="#" class="pb-metroblock" rel="metroblock">Metroblock</a><a href="#" class="pb-framed" rel="framed">Frames</a></div>');
		
		$(".pricelist-prices thead tr").each(function() {
			$(this).append("<th>&nbsp;</th>");									
		});
		
		$(".pricelist-prices tbody tr").each(function() {
			$(this).append("<td>&nbsp;</td>");									
		});
		
		$(".pricelist tr").each(function() {
			$("td:gt(1), th:gt(1)", $(this)).remove();
		});
				
		$(".pricelist-prices tr").each(function() {
			$("td:lt(2), th:lt(2)", $(this)).remove();
			$("td", $(this)).each(function(i) {		$(this).addClass("col_" + i);		});
			$("th", $(this)).each(function(i) {		$(this).addClass("col_" + i);		});
		});
		
		$(".pricelist").wrap('<div class="table-wrapper"></div>');
		$(".pricelist-prices").wrap('<div class="prices-table-wrapper"></div>');
						
		$(".pricelist-prices .pricelist-header-top th").each(function() {
			var w=0;
			$(this).prevAll().each(function(i) {
				w += $(this).width() + 20;
			});
			$(this).data("xpos", w);
		});
			
		$(".pricelist-prices .pricelist-header-top th").bind("click", function() {
			$(".pricelist-prices").animate({"margin-left" : (-1*$(this).data("xpos"))}, "fast");
			
			$(".pricelist-prices .pricelist-header-top th").removeClass("current");
			$(this).addClass("current");
			
		});
		
		$(".ph-back").bind("click", function() {
			var gg = $(".pricelist-prices .pricelist-header-top th.current").prev();
			gg.trigger("click");
		});
		
		$('#pricelist-tabs a').live("click", function() {
			var ii = $(this).attr("rel");
			var hh = $(".pricelist-prices .pricelist-header-top th.ph-" + ii);
			hh.trigger("click");
			
			$('#pricelist-tabs a').removeClass("current");
			$(this).addClass("current");
			
			return false;
		});
		
		/*--------------------------------------------------------- Collapsing Rows -------------------*/
		
		$('td:eq(1)', '.pricelist .pricelist-header').html('<a class="hider" href="#">Hide</a>');
		
		$('.pricelist-header a').live("click", function() {
		
			$this = $(this).parent().parent();
		
			var start = $this.index("tbody tr") + 1;			
			var end = ($this.nextAll('.pricelist-header').length) ? $this.nextAll('.pricelist-header').index("tbody tr") : $this.siblings('tr').length + 1;
			
			//$this.append(start + " - " + end);
						
			if($this.hasClass("hidden")) {
				for(i=start; i<end; i++) {
					$('.pricelist tbody tr').eq(i).show();
					$('.pricelist-prices tbody tr').eq(i).show();
				}
				$this.removeClass("hidden");
				$('a', $this).text("Hide").removeClass().addClass("hider");
			}
			else {
				for(i=start; i<end; i++) {
					$('.pricelist tbody tr').eq(i).hide();
					$('.pricelist-prices tbody tr').eq(i).hide();
				}
				$this.addClass("hidden");
				$('a', $this).text("Show").removeClass().addClass("shower");
			}
			
			return false;

		});
		
		/*--------------------------------------------------------- Table Row Hover -------------------*/
		
		$('.pricelist-prices').delegate("td", "mouseover", function(){
			p = $(this).parent();
			p.children("td").addClass("hover");
			
			$('.pricelist tr:eq(' + (p.index()+2) + ') td').addClass("hover");
		});
		
		$('.pricelist-prices').delegate("td", "mouseout", function(){
			p = $(this).parent();
			p.children("td").removeClass("hover");
			
			$('.pricelist tr:eq(' + (p.index()+2) + ') td').removeClass("hover");
		});
		
});
