$(document).ready(
	function() {
		// multiple resource print selection
		$('#print_select_form').submit(function() {
				var selected_resources = readCookie('print_prod_ids');
				var resources_to_print = [];
				
				// check the cookie of all selected resources first, then the form
				if (selected_resources) {
					resources_to_print = selected_resources.split(',');
				} else {
					// see if one or more resources have been checked
					for (e = 0; e < this.elements.length; e++) {
						if (this.elements[e].type == 'checkbox') {
							if (this.elements[e].checked) {
								resources_to_print.push(this.elements[e].value);
							}
						}
					}
				}
				
				if (resources_to_print.length > 0) {
					if (resources_to_print.length == 1) {
						popupWindow('/popup_content.php?print=true&id=' + resources_to_print.toString(), 'Content', 600, 520);
					} else {
						popupWindow('/popup_content.php?multi=true&print=true&id=' + resources_to_print.join(','), 'Content', 600, 520);
					}
				} else {
					alert('Please select one or more resources for printing.');
				}
				
				return false;
		});
		
		// attribute filter select box
		$("form[name='content_type_limit'] select").change(function() {
				$(this).parent('form').submit();
		});
		
		// monitor save to print checkboxes
		$("#print_select_form input[type='checkbox']").click(function() {
				var current = readCookie('print_prod_ids') || '';
				var current_a = current ? current.split(',') : [];
				var val_inarray = jQuery.inArray(this.value, current_a);
				
				if (this.checked && val_inarray == -1) {
					// add it to the list of selected prods to print
					current_a.push(this.value);
				} else if (!this.checked && val_inarray > -1) {
					// remove it from the list
					current_a.splice(val_inarray, 1);
				}
				createCookie('print_prod_ids', current_a.join(','));
		});
		
		// allow removal of resources from the print articles popup
		$(".article_print_text").prepend('<p>Click <em>Remove</em> to remove an item from the print list.</p><!-- see enhance.js -->');
		$("div.article").each(function() {
				var article_div = $(this);
				var product_id = article_div.attr('id').substring(12);
				var remove_link = $('<span class="article_remove">Remove</span> ');
				
				// add the remove link to the item
				article_div.prepend(remove_link);
				
				remove_link.click(function() {
						// remove the resource div from the DOM
						article_div.remove();
						
						// remove the resource id from the selected products cookie
						var selected_resources = readCookie('print_prod_ids') || '';
						var current_a = selected_resources ? selected_resources.split(',') : [];
						var val_inarray = jQuery.inArray(product_id, current_a);
						if (val_inarray > -1) {
							// remove it from the list
							current_a.splice(val_inarray, 1);
						}
						createCookie('print_prod_ids', current_a.join(','));
						
						// uncheck the resource in the parent window
						window.opener.$('#products_id-'+product_id).removeAttr('checked');
				});
		});
		
		$('#news_signup_dd').submit(function(ev) {
				//return true;
				ev.preventDefault();
				
				$.post('news_signup_dd_process.php', { email_address: $('#news_signup_dd input:eq(0)').val(), state: $('#news_signup_dd select:eq(0)').val(), newsletter_dd_print_type: $('#news_signup_dd select:eq(1)').val() }, function(data) {
						
						$('#news_signup_dd .error').remove();
						
						if (data == 'success\n') {
							$('#news_signup_dd table:first').before(
								$('<p></p>')
									.addClass('success')
									.text('E-notice signup successful!')
							);
							$('#news_signup_dd table').hide(500);
						} else {
							$('#news_signup_dd table:first').after(
								$('<p></p>')
									.addClass('error')
									.text(data)
							);
						}
				});
		});
		
	}
);

// from http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
