$(document).ready(function()
{

	//call the jQuery Hover Over and Out
	$('.hoverable').hover(over, out);
	//Tell the browser to change the background when hovered over
	function over(event) {
		$(this).css("background", "#f2f4f8");
		$(this).css("cursor", "pointer");
	}
	//tell the browser to change the background to nothing 
	//when going outside the object area
	function out(event) {
		$(this).css("background", "");
	}

	//This adds the click event to your object and tells it where to go
	$('.clickable').click(function(e) {
		window.location=$(this).find("a").attr("href");
	});

	// initalise popups
	init_popups();
	function init_popups () {

		// close search panel if clicked outside the search panel
		$(document.body).click(function(event){
			var target = $(event.target);
			if (!target.is("#search_panel")){
				$("#search_panel").hide();
			}
		});
	}

	// reveal more popular products
	$("#more_pop_link").click(function (e) {
		$("#popular_hidden").slideToggle("normal");

		// Prevent page jump on click
		e.preventDefault();
	});

	$("a#apps").click(function (e) {
		$("#apps_list").toggle();

		// Prevent page jump on click
		e.preventDefault();
	})


	// saved changes to list
	// status message fade out
	jQuery.fn.delay = function(time,func){
		return this.each(function(){
			setTimeout(func,time);
		});
	};

	$('div#status').delay(2000, function(){$('div#status').fadeOut(1000)});

	if ($("#changes_saved:visible")) {
		$('span#changes_saved').delay(2000, function(){
			$('span#changes_saved').fadeOut(1000)
		});

		$('span#changes_saved').delay(3000, function(){
			$("span#save_changes").fadeIn(1000);
			$("button#save_changes_btn").fadeIn(1000);
		});
	};

	if ($("#changes_saved_bottom:visible")) {
		$('span#changes_saved_bottom').delay(2000, function(){
			$('span#changes_saved_bottom').fadeOut(1000)
		});

		$('span#changes_saved_bottom').delay(3000, function(){
			$("span#save_changes_bottom").fadeIn(1000);
			$("button#save_changes_btn_bottom").fadeIn(1000);
		});
	};

	// product forms

	$("#specForm_link").click(function (e) {
		$("#specForm").slideToggle();
		$("#specForm_link").toggleClass('clicked');

		// Prevent page jump on click
		e.preventDefault();
	})

	$("#expertForm_link").click(function (e) {
		$("#expertForm").slideToggle();
		$("#expertForm_link").toggleClass('clicked');

		// Prevent page jump on click
		e.preventDefault();
	})
	
	// open forms if the form has been submitted
	if ($('#form_submitted').val() == 'expertForm') {
		$("#expertForm").show();
		$("#expertForm_link").addClass('clicked');
	};

	if ($('#form_submitted').val() == 'specForm') {
		$("#specForm").show();
		$("#specForm_link").addClass('clicked');
	};

	if ($('#form_submitted').val() == 'specForm') {
		$("#reviewForm").show();
		$("#reviewForm_link").addClass('clicked');
	};

	// reminder popup
	question_popup();

	function question_popup () {
		// reminder dialog box when a reminder is clicked
		$(".question_link").mouseover(function (eventObj) {
			// open the dialog

			var x = $("#prod_summary").offset().left;
			var y = $("#prod_summary").offset().top+30;

			//getter
			$("#question_panel").css("top",y);
			$("#question_panel").css("left",x);

			$("#question_panel").show();
		})

		// close the reminder
		$(".question_link").mouseout(function () {
			// open the dialog
			$("#question_panel").hide();
		})
	}

	// add to cart
	$('#product_listing button').click(function(e) {
		// Prevent page jump on click
		e.preventDefault();

		var product_options = [];

		var product_id = $('#product_listing form #product_id').val()
		var product_name = $('#product_listing form #product_name').val()
		var short_name = $('#product_listing form #short').val()

		$('#product_listing form .product_qty').each(function(index, Element) {
			product_options[index] = {};
			product_options[index] = {'product_code' : $('#product_listing form #product_code_'+index).val(), 'qty' : $('#product_listing form #qty_'+index).val(), 'price' : $('#product_listing form #price_'+index).val(), 'size' : $('#product_listing form #size_'+index).val()}
			$(this).val('');
		})


		$.ajax({
			type: "POST",
			dataType: "html",
			url: "/products/addtocart/",
			data: ({
				product_id : product_id,
				product_name : product_name,
				short_name : short_name,
				product_options : product_options
				}),
				success: function(return_data)
				{
					var return_data = $.parseJSON(return_data)
					if (return_data.status == 'done') {
						show_cart_update(return_data)
					} else {
						show_cart_error(return_data)
					}

				}
			});
		})

		function show_cart_update (return_data) {		
			$('div#order_summary table').html(return_data.cart);
			$('button#add_cart_button').hide()
			$('p#added_message').show();

			if ($("#added_message:visible")) {
				$('#added_message').delay(2000, function(){
					$('#added_message').fadeOut(1000)
				});

				$('#add_cart_button').delay(3000, function(){
					$("#add_cart_button").fadeIn(1000);
				});
			};
		}

		function show_cart_error (return_data) {
			$('button#add_cart_button').hide()
			$('p#error_message').show();

			if ($("#error_message:visible")) {
				$('#error_message').delay(2000, function(){
					$('#error_message').fadeOut(1000)
				});

				$('#add_cart_button').delay(3000, function(){
					$("#add_cart_button").fadeIn(1000);
				});
			};
		}


		// search stuff
		function init_search_click() {
			$("#view_all_list").click(function (e) {
				$("#search_form").submit();

				// Prevent page jump on click
				e.preventDefault();
			})
		}

		$("input#search_input").keyup(function () {
			if ($(this).val().length>2) {
				var data = get_search($(this).val());

				// show search popup
				search_panel();
			} else {
				$('div#search_panel').html('');
				$("#search_panel").dialog('close');
			};

		})

		$("input#search_input").focusin(function() {
			if ($(this).val() == 'Search products') {
				$(this).val('');
			};
		})


		function get_search(search_string) {
			var data;

			$.ajax({
				type: "GET",
				dataType: "html",
				url: "/search/ajax_search/"+search_string,
				success: function(data)
				{
					$('div#search_panel').html(data);
					init_search_click()
				}
			});
		}

		function search_panel () {
			// reminder dialog box when a reminder is clicked
			// open the dialog


			var search_x = $("#search_input").offset().left;
			var search_y = $("#search_input").offset().top+30;

			//getter
			//setter
			$("#search_panel").css("top",search_y);
			$("#search_panel").css("left",search_x);

			$("#search_panel").show();
		}

		$("div#zipwall_form").hide();

		$("a#request_zipwall").click(function (e) {
			$("div#zipwall_form").toggle();
			e.preventDefault();
		})
	})
