$.namespace("lgp_browse", 
{
	/*
	Go through the browse page, and assign the password function to any <a>s classed as 'pw_needed'
	*/
	assignGetPassword : function()
	{
		$('input#pw').focus();
		$('form').submit(function(evt)
		{
			evt.preventDefault();
			$('div#error').remove();
			$.post('/ajax/check_password/', $('form').serialize(), function(data)
			{
				if (data == "OK")
				{
					window.location.reload();
				}
				else
				{
					$('<div id="error"><br />Incorrect Password</div>').insertAfter('form').hide().slideDown('slow');
				}
			});
		});
	},

	assignImageNavigation : function(options)
	{
		var folder_id = options.folder_id;
		var folder_path = options.folder_path;
		var initial_page = options.initial_page;
		var iMaxPages = ($('div#pagelinks ul li span').size());

		$('div#pagelinks ul li span').click(function(e)
		{
			// get requested page by looking inside link
			var iPage = Number ($(this).text() );

			// add the 'selected' class to this page number.
			$('span.selected').removeClass("selected");
			$(this).addClass("selected");

			// Page one? Then remove the prev link
			if (iPage == 1)
			{
				$('span#btn_prev').hide();
			}
			else
			{
				$('span#btn_prev').show();
			}

			// last page? then remove the next link
			if (iPage == iMaxPages)
			{
				$('span#btn_next').hide();
			}
			else
			{
				$('span#btn_next').show();
			}



			$('div#photos').load('/ajax/photo_show_page/', { 'page': iPage, 
															 'folder_id' : folder_id,
															 'folder_path' : folder_path
															});

			
		});

		/* Previous page */
		$('span#btn_prev').click(function(e)
		{
			// get current page
			var iCurrent = Number( $('span.selected').text() );
			iCurrent --;
			$('div#pagelinks ul li span:eq(' + (iCurrent - 1) + ')').trigger('click');
		});

		/* Next Page */
		$('span#btn_next').click(function(e)
		{
			// get current page
			var iCurrent = Number( $('span.selected').text() );
			iCurrent ++;
			$('div#pagelinks ul li span:eq(' + (iCurrent - 1) + ')').trigger('click');
			
		});

		// Set up a click event for checkboxes.  Use delegation - set up click event on holding div#photos only
		$('div#photos').click(function(e)
		{
			if ($(e.target).is(':checkbox'))
			{
				var checked = ($(e.target).attr('checked')) ? 1 : 0;
				var image_id = $(e.target).attr("id").replace(/checkbox_/, "");
				$.post('/ajax/photo_select_one/', {'checked' : checked, 'folder_id' : folder_id, 'image_id': image_id});
			}
		}
		);

		/* Select all in set */
		/* Current page number needs to be set, so get that from the contents of span#page */
		$('#btn_select_all_set').click(function(e)
		{
			$('div#photos').load('/ajax/photo_select_all_in_set/', 
															 { 'page': $('span.selected').text(), 
															   'folder_id' : folder_id,
															   'folder_path' : folder_path
															});
		}
		);

		/* Select all on page  */
		/* Current page number needs to be set, so get that from the contents of span#page */
		$('#btn_select_all_page').click(function(e)
		{
			$('div#photos').load('/ajax/photo_select_all_on_page/', { 'page': $('span.selected').text(), 
																	  'folder_id' : folder_id,
															  		  'folder_path' : folder_path
																	});
		}
		);

		/* Select none  */
		/* Current page number needs to be set, so get that from the contents of span#page */
		$('#btn_select_none').click(function(e)
		{
			$('div#photos').load('/ajax/photo_select_none/', { 'page': $('span.selected').text(), 
															   'folder_id' : folder_id,
															   'folder_path' : folder_path
															});
		}
		);

		/* Add to basket  */
		$('#btn_add_to_basket').click(function(e)
		{	

			if ($(":checkbox:checked").length == 0)
			{
				// no image selected, as no checkboxes selected.
				$.fn.colorbox({html:"<div class=\"errormodal\"><strong>Error</strong><br /><br />Please select an image</div>", transition:"none", initialWidth:300, initialHeight:10, preloading:false});

			} else if ($('#productoption_id').val() > 0)
			{
				$('div#photos').load('/ajax/photo_add_to_basket/', { 'page': $('span.selected').text(), 
															   		'folder_id' : folder_id,
															   		'folder_path' : folder_path,
															   		'productoption_id' : $('#productoption_id').val()
																	});
			}
			else
			{
				// no product selected 
				$.fn.colorbox({html:"<div class=\"errormodal\"><strong>Error</strong><br /><br />Please select a product</div>", transition:"none", initialWidth:300, initialHeight:10, preloading:false});
				
			}
		}
		);

		// 'dispatch' click event to first pagination item.
		$('div#pagelinks ul li span:eq(' + (initial_page - 1) + ')').trigger('click');



	},

	assignProductMatrix:function(image_id)
	{
		$('div.productbox > a').click(function(event)
		{
			if ($(this).html().indexOf("expand") > 0)
			{
				$(this).html("Click to hide options");
			}
			else
			{
				$(this).html("Click to expand options");
			}

			$(this).parent().find('div.wrap').toggle("normal");
		});
		$('div.productbox input').keyup(function(event)
		{
			var option_id = $(this).attr("id").substr(4);
			var val = $(this).attr("value");
			$.post('/ajax/modify_quantity/', {'image_id': image_id, 'val': val, 'option_id': option_id});

		});

		// Reopen any products where we have options selected.
		$('div.productbox input').each(function()
		{
			if ($(this).attr("value") > 0)
			{
				$(this).parent().parent().show();
			}
		}
		);

		// Assign  + and -
		$('a:contains(+)').click(function(event)
		{
			event.preventDefault();
			var option_id = $(this).prev('input').attr("id").substr(4);
			var val = Number($(this).prev('input').val()) + 1;
			$(this).prev('input').val( Number($(this).prev('input').val()) + 1 );
			$.post('/ajax/basket_modify_quantity/', {'image_id': image_id, 'val': val, 'option_id': option_id});

		}
		);
		$('a:contains(-)').click(function(event)
		{
			event.preventDefault();
			var val = Number($(this).next('input').val()) - 1;
			var option_id = $(this).next('input').attr("id").substr(4);
			if (val < 0)
				val = 0;
			$(this).next('input').val(val);
			$.post('/ajax/basket_modify_quantity/', {'image_id': image_id, 'val': val, 'option_id': option_id});

		}
		);
	}

	
});
