$(document).ready(function() {
	
	/*	$("#product-links ul li a").overlay({
		
		expose: '#333333',
		left: "center",
		onBeforeLoad: function() {
		
		// grab wrapper element inside content
		var wrap = this.getOverlay().find("#load_content");
		
		// load the page specified in the trigger
		wrap.load(this.getTrigger().attr("href"));
		},
		
		load: true
		
	});*/

	//More/Less
	
	var adjustheight = $("#landing-header-text .expand").height();
	//More Text
	var moreText = "+ More";
	var lessText = "- Less";
	
	
	$("#landing-header-text .expand").css('overflow', 'hidden');
	$("#landing-header-text").append('[...]');
	$("a.adjust").html(moreText);
	
	$(".adjust").toggle(function() {
		$(this).parents("div:first").find(".expand").css('height', 'auto').css('overflow', 'visible');
		// Hide the [...] when expanded
		$(this).text(lessText);
	}, function() {
		$(this).parents("div:first").find(".expand").css('height', adjustheight).css('overflow', 'hidden');
		$(this).text(moreText);
	});
	
/* Product Browsing Page Expand Collapse */
function expand_text(){
$("div.product-table .material_description").bind("expand_text", function(){
	
	$(this).find(".more_button").hide().remove();
	
	var less_button = $("<a/>",{
	"class": "less_button",
	text: "- Less",
	click: function(){
	$(this).closest("div.product-table .material_description").trigger("collapse_text");
	}
	});
	
	$(this).css('height', 'auto').css("overflow", "visible").append(less_button);
});

$("div.product-table .material_description").bind("collapse_text", function(){
	var original_height = $(this).data('original_height');
	$(this).css('height', original_height).css("overflow", "hidden");
	$(this).find(".less_button").hide().remove();
	
	var more_button = $("<a/>",{
		"class": "more_button",
		text: "+ More",
		click: function(){
		$(this).closest("div.product-table .material_description").trigger("expand_text");
	}
	});

	$(this).find(".intro").append(more_button);
});

$("div.product-table .intro").each(function(i){
		
	var more_button = $("<a/>",{
	"class": "more_button",
	text: "+ More",
	click: function(){
		$(this).closest("div.product-table .material_description").trigger("expand_text");
	}
	});
	
	$(this).append(more_button);
	var start_height = $(this).height() + $(this).closest(".product-table").find("h5").first().height() - 5;
	$(this).closest(".material_description").height(start_height).css("overflow","hidden").data("original_height",start_height);


});
}//End expand_text();
expand_text();

/*
$("div.product-table table").bind("show_more_prices", function(){
	var filtered_state = $(this).data("price_filter");
	if (filtered_state === "active"){
		$(this).find("td.pricing").toggle("fast").toggleClass("filtered");
		$(this).find("td.price").toggle("fast").toggleClass("filtered");
		var $table_header = $(this).find("th.price_header");
		$table_header.attr("colspan");
	};
});
*/

//Event to Shift Quanities 
$("#popular-products div.product-table table tr.quantity-break").bind("show_more_quantites", function(){
	var quanity_data = $(this).data("quantities");
	var quanity_row = $(this);
	var quanity_data_split = quanity_data.slice();
	quanity_data_split = quanity_data_split.splice(3, quanity_data.length)
	var quanity_data_count = -1; 
	
	$.each(quanity_data_split,function(key,value){
		quanity_data_count += 1;
		$(quanity_row).find("td.price").eq(quanity_data_count).html("").text(value);
	});	
});

$("#popular-products div.product-table table tr.pricing-row").bind("show_more_prices", function(){
	var pricing_data = $(this).data("pricing");
	var pricing_row = $(this);
	var pricing_data_split = pricing_data.slice();
	pricing_data_split = pricing_data_split.splice(3, pricing_data.length);
	var pricing_data_count = -1; 
	
	$.each(pricing_data_split,function(key,value){
		pricing_data_count += 1;
		$(pricing_row).find("td.pricing").eq(pricing_data_count).html("").text(value);
	});	
});


//Event to Shift Quanities 
$("#popular-products div.product-table table tr.quantity-break").bind("show_less_quantites", function(){
	var quanity_data = $(this).data("quantities");
	var quanity_row = $(this);
	var quanity_data_split = quanity_data.slice();
	quanity_data_split = quanity_data_split.splice(0,3);
	var quanity_data_count = -1; 
	
	$.each(quanity_data_split,function(key,value){
		quanity_data_count += 1;
		$(quanity_row).find("td.price").eq(quanity_data_count).html("").text(value);
	});	
});

$("#popular-products div.product-table table tr.pricing-row").bind("show_less_prices", function(){
	var pricing_data = $(this).data("pricing");
	var pricing_row = $(this);
	var pricing_data_split = pricing_data.slice();
	pricing_data_split = pricing_data_split.splice(0,3);
	var pricing_data_count = -1; 
	
	$.each(pricing_data_split,function(key,value){
		pricing_data_count += 1;
		$(pricing_row).find("td.pricing").eq(pricing_data_count).html("").text(value);
	});	
});

//Quantity Price Filter
/* Count the pricing cells, filter out all after three*/
$("#popular-products div.product-table table").each(function(i){
	var $pricing_table = $(this);
	var $total_pricing_rows = $(this).find("tr.pricing-row");
	var $total_quantity_rows = $(this).find("tr.quantity-break");
	var filter_state = "";
	
	/* Check if the there are more then three quantity breaks, if so run the filter function */
	
	$total_quantity_rows.each(function(i){
	var $quanity_cells = $(this).find("td.price");
	var quanity_data = [];
	$quanity_cells.each(function(i){
		var quantity = $(this).text();
		 quanity_data.push(quantity);
	});
	
	if ( $quanity_cells.length > 3){
		$pricing_table.data("price_filter","active");
		$(this).find("td.price:gt(2)").hide().addClass("filtered");
	};
	
	$(this).data("quantities",quanity_data)
	});
	
	filter_state = $pricing_table.data("price_filter");
	if (filter_state === "active"){
	$total_pricing_rows.each(function(i){
		var $pricing_cells = $(this).find("td.pricing");
		var pricing_data = [];
		
		$pricing_cells.each(function(i){
			var price = $(this).text();
			pricing_data.push(price);
		});
		
		$(this).data("pricing",pricing_data);
		$(this).find("td.pricing:gt(2)").hide().addClass("filtered");
		});
	};	

	
	if (filter_state === "active"){ 
		var $table_header = $pricing_table.find("th.price_header");
		$table_header.attr("colspan","3");
		
	var more_button = $("<a/>", {
			"class": "more_price_button",
			text: "C",
			href: "#show_more_prices",
			click: function(event){
				event.preventDefault();
	$(this).closest("table").find("tr.quantity-break").trigger("show_more_quantites");
	$(this).closest("table").find("tr.pricing-row").each(function(i){
		$(this).trigger("show_more_prices");	
	});
			}
		});
		
		var less_button = $("<a/>", {
			"class": "less_price_button",
			text: "B",
			href: "#show_less_prices",
			click: function(event){
				event.preventDefault();
	$(this).closest("table").find("tr.quantity-break").trigger("show_less_quantites");
	$(this).closest("table").find("tr.pricing-row").each(function(i){
		$(this).trigger("show_less_prices");	
	});

			}
		});
	$table_header.prepend(less_button).append(more_button);
		
	};
		
});



});
