$(document).ready(function()
{

	$('#btnfaq').click(function(evt)
	{	
		evt.preventDefault();
		$.fn.colorbox({href:'/info/faq/', width:"888", height:"80%"}); 
	}
	);

		// Preload all rollovers
		$("a.rollover img").each(function() {
			$("<img>").attr("src", $(this).attr("src").replace(/.png$/ig,"_over.png"));
		});
		
		// Navigation rollovers
		$("a.rollover img").mouseover(function(){
			$(this).attr("src", $(this).attr("src").replace(/.png$/ig,"_over.png"));
		});
		$("a.rollover img").mouseout(function(){
			$(this).attr("src", $(this).attr("src").replace(/_over.png$/ig,".png"));
		//	$(this).attr("src", $(this).attr("src").replace(/.png$/ig,"_over.png"));
		});
}
); 

$.namespace("lgp_register", 
{
	assignEMailLink : function()
	{
		$('input#email').focus();
		$('form').submit(function(evt)
		{
			evt.preventDefault();
			$.post('/ajax/save_register_email', $('form').serialize(), function(data)
			{
				$('<div id="thanks">Thank you - we will be in contact soon.</div>').insertAfter('form').hide().slideDown('slow');
				$('input#email').val("");
				$('input#email').focus();
			});
		});
	}
	
}); 

$.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});

		}
		);
	}

	
});
 

$.namespace("lgp_basket", 
{
	/*
	// Assigns all links in the basket page - delete and edit etc
	*/
	assignBasketLinks : function()
	{
		$('div.basketproducts table span.btn_amend').click(function(evt)
		{
			evt.preventDefault();

			// What image_id does this product refer to?  Get it from the ID of the parent div.basketitem.
			var image_id = $(this).parents("div.basketitem").attr("id");
			
			// it's in the format image_x, so chop
			image_id = image_id.substr(6);
		
			$.fn.colorbox({href:'/matrix/' + image_id, width:"800px"}); 

			$().unbind("cbox_complete");
			$().unbind("cbox_closed");

			$().bind('cbox_complete', function()
			{
					$().lgp_browse.assignProductMatrix(image_id);
					$('.productbox').corner();
			});	
			$().bind('cbox_closed', function()
			{
					window.location.reload();
			});	
				
		}
		);
		$('div.basketproducts table span.btn_delete').click(function(evt)
		{
			evt.preventDefault();

			// What image_id does this product refer to?  Get it from the ID of the parent div.basketitem.
			var image_id = $(this).parents("div.basketitem").attr("id");
			
			// it's in the format image_x, so chop
			image_id = image_id.substr(6);
	
			// Remove this item on the server
			$.post('/ajax/basket_remove_item/', {"image_id": image_id});
				
			// find the parent called div.basketitem, and hide it
			$(this).parents("div.basketitem").hide(400, function()
			{
				// once complete, remove it from the window and update total cost
				$(this).remove();
		
				// Update Cost
				$().lgp_basket.updateTotalCost();
			});					
		}
		);
	},

	/* Iterate through all span.totalmoney - sum them up and populate span#grandtotal with the value.  Returns
	value also.*/
	updateTotalCost: function()
	{
		var iTotalCost = 0;
		$('span.totalmoney').each(function()
		{
			// trash the pound sign and add it to the totalcost.
			iTotalCost += parseFloat( $(this).html().substr(1) );
		});

		$('span#totalmoney').html('&pound;' + iTotalCost.toFixed(2));
		
		// Show or hide the continue option - obviously cant continue if nothing in basket.
		if (iTotalCost > 0)
			$('div#paypalcontinue').show();
		else
			$('div#paypalcontinue').hide();
		return iTotalCost;
	}
});
 

