product_index = 0;

$(document).ready(function(){
		
    $(".navigation_menu_item_0").mouseover(function(){		
		$(this).stop();
    	$(this).animate({top:"-4px"});		
    });
    
    $(".navigation_menu_item_0").mouseout(function(){
		$(this).stop();
    	$(this).animate({top:"0px"});
    });
		
	$(".dont_move").unbind();
		
	/*$('#add_product').submit( function() {
		submit_form($(this).serialize());
		$('#overlay').hide("slow");
		
		return false;
	});
	
	$('.order_details').blur(function() {
		$.post("includes/session.php5?update=true&" + $('#order_details_form').serialize());
	});*/
	
	$('#add_product').submit( function() {
		//alert($(this).serialize());
		var row = "<tr id='row_" + product_index + "' >";
		row += "<input type='hidden' name='product_" + product_index + "' value='" + $(this).serialize() + "' />";
		row += "<td>" + $("#add_product_product").val() + "</td>";
		row += "<td>" + $("#add_product_quantity").val() + "</td>";
		row += "<td>" + $("#add_product_length").val() + "</td>";
		row += "<td>" + $("#add_product_colour").val() + "</td>";
		row += "<td>" + $("#add_product_bracket").val() + "</td>";
		row += "<td>" + $("#add_product_feed").val() + "</td>";
		row += "<td>" + $("#add_product_other_reqs").val() + "</td>";
		row += "<td><a href='#' onclick='javascript:remove_line(" + product_index + ")'>Remove Line</a></td>";
		row += "</tr>";
		$('#products > tbody').append(row);		
		product_index++;
		$('#max_index').val(product_index);
		
		$("#add_product_product").val("");
		$("#add_product_quantity").val("");
		$("#add_product_length").val("");
		$("#add_product_colour").val("");
		$("#add_product_bracket").val("");
		$("#add_product_feed").val("");
		$("#add_product_other_reqs").val("");
		//$('#send_order').unbind();
		return false;
	});
	
	$('#send_order').click( function () {
		send_order_form();
	});
	
	$('#see_our_gallery').click( function() {
		$('#overlay_inner').load("Library/index.html");
		$('#overlay').show("slow");
		$('#overlay_close').click( function () {
		  $(this).parent().hide("slow");
		});
	});
});

function remove_home_move()
{
	$(".nav_home").unbind();
}

function show_brochure()
{
	$('#overlay_inner').load("brochure_frame.php5");
	$('#overlay').show("slow");
	$('#overlay_close').click( function () {
	  $(this).parent().hide("slow");
	});
}
/*
function show_add_product()
{
	$('#overlay_inner').load("add_product.php5");
	$('#overlay').show("slow", function () {
		$('#add_product').submit( function() {
			
			data = $('#add_product').serialize();
			$.post("add_product_submit.php5?" + data, function() {
				data=1;
				$('#overlay').hide("slow");
				update_order_form();
			});
			return false;
		});
	});
	$('#overlay_close').click( function () {
	  $(this).parent().hide("slow");
	});
}*/

function update_order_form()
{
	$.getJSON("includes/session.php5","JSON=true",function (data) {
		$('#products > tbody').children().remove();
		$('#invoice').html(data.invoice_address);
		$('#delivery').html(data.delivery_address);
		$('#contact_name').val(data.contact_name);
		$('#job_reference').val(data.job_reference);
		$('#telephone_no').val(data.telephone_number);
		$('#required_date').val(data.required_date);
		$('#email').val(data.email_address);
		for(y = 0; y < data.product_id; y++)
		{
			var row = "<tr>";
			row += '<td>' + data.products[y].product + '</td>';
			row += '<td>' + data.products[y].quantity + '</td>';
			row += '<td>' + data.products[y].prod_length + '</td>';
			row += '<td>' + data.products[y].colour + '</td>';
			row += '<td>' + data.products[y].bracket_size + '</td>';
			row += '<td>' + data.products[y].feed + '</td>';
			row += '<td>' + data.products[y].other_requirements + '</td>';
			row += "<td><a href='#' onclick='javascript:remove_line(" + data.products[y].id + ")'>Remove Line</a></td>";
			row += "</tr>";
			$('#products > tbody').append(row);
		}
	});
	
}

function remove_line(id)
{
	$.post("includes/session.php5?remove=" + id,function() {
		update_order_form();
	});	
}

function send_order_form()
{
	$.post("includes/session.php5?send=true");
}

function remove_line(index)
{
	$('#row_' + index).remove();
}