var barilla_STT = {
	"setup_popup": function(dropdown_menu, dropdown_link, close_link, open_class) {
		var my_menu  = $(dropdown_menu).hide();
		var my_link  = $(dropdown_link).click(function() {
			if ( my_link.hasClass(open_class) ) {
				close_it();
			} else {
				open_it();
			}
			return false;
		});
		var my_close = my_menu.find(close_link).click(function() {
			close_it();
			return false;
		});

		var close_it = function() {
			my_menu.hide();
			my_link.removeClass(open_class);
		};

		var open_it = function() {
			my_menu.show();
			my_link.addClass(open_class);
		};

		if ( "#category_dropdown" == dropdown_menu ) {
			// This code cleans up the item separators in dropdown
			// when hovering over a menu item
			my_menu.find("li").each(function() {
				var thisLI = $(this);
				var prevLI = thisLI.prev("li");
				thisLI.hover(
					function () {
						thisLI.addClass("no_bottom_border");
						prevLI.addClass("no_bottom_border");
					},
					function () {
						thisLI.removeClass("no_bottom_border");
						prevLI.removeClass("no_bottom_border");
					}
				);
			});
		}
	},
	"setup_popup_cal_psc": function(dropdown_menu, dropdown_link, close_link, open_class) {
		var my_menu  = $(dropdown_menu);
		var my_close = my_menu.find(close_link).click(function() {
			close_it();
			return false;
		});

		var close_it = function() {
			my_menu.hide();
		};

	},

	"setup_expanding_items": function(expandable_items, trigger) {
		$(expandable_items).each(function(index) {

			var li = $(this);
			if ( index > 0 ) {
				li.addClass("closed");
			}

			li.find(trigger).click(function() {
				li.toggleClass("closed");
			});

		});
	},

	"add_flash_to_page": function() {

		if ( $("body#homepage").length > 0 ) {
			// set up and use swfobject to emebed flash in the homepage
			var flashvars = {
			};
			var params = {
				allowScriptAccess: "sameDomain",
				play: "true",
				type: "application/x-shockwave-flash",
				wmode: "opaque",
				quality: "high",
				bgcolor: "#FFFFFF"
			};
			var attributes = {
				align: "middle",
				id: "homepage_flash",
				name: "hero"
			};

			swfobject.embedSWF("../../flash/demo.swf", "homepage_flash", "960", "362", "9.0.28", "../../flash/expressInstall.swf", flashvars, params, attributes);
		}
	},

	"setup_tabs": function(selector, hide_class, active_class) {
		var tab_bars = $(selector);
		tab_bars.each(function() {
			var tab_bar      = $(this);
			var tab_links    = tab_bar.find("li a");

			var tab_indexes  = $.map(tab_links, function(n) {
				return find_anchor(n);
			});

			var tab_sections = $(tab_indexes.join(", ")).addClass(hide_class);
			$(tab_sections[0]).removeClass(hide_class);
			$(tab_links[0]).addClass(active_class)

			tab_links.each(function() {
				var my_tab_section = $(find_anchor(this));
				var this_link = $(this);
				this_link.click(function() {
					tab_sections.addClass(hide_class);
					my_tab_section.removeClass(hide_class);
					tab_links.removeClass(active_class);
					this_link.addClass(active_class);
					return false;
				});
			});

		});

		function find_anchor(a) {
			return a.href.slice(a.href.indexOf("#"));
		}
	},

	"setup_print_receipe_page": function(menu_id, preview_id, print_button_id, recipe_id, theme_map) {
		if ( $("body#print_recipe_page").length == 0 ) { return }

		// configure the theme select menu
		var theme_menu = $(menu_id);
		var preview_area = $(preview_id);

		theme_menu.change(function(theme_hash) {
			var theme = this.value;
			preview_area.css("backgroundImage", "url(" + theme_map[theme] + ")");
		}).change();


		//preload the theme preview images
		for (var i = 0, len = theme_map.length; i < len; i++) {
			$("<img>").attr("src", theme_map[i]);
		}

	},

	"shopping_list_button": function(selector, shopping_list_url) {
		$(selector).click(function() {
			window.open(shopping_list_url, "ShoppingList", "width=550, height=550, scrollbars=1, location=0, status=0");
			return false;
		});
	},

	"weekly_calendar_button": function(selector, calendar_url) {
		$(selector).click(function() {
			window.print();
			return false;
		});
	},

	"confirm_cancel": function(selector, cancel_url) {
		$(selector).click(function() {
			window.open(cancel_url, "Cancel", "width=219, height=100, scrollbars=0, location=0, status=0");
			return false;
		});
	},

	"confirm_delete": function(selector, cancel_url) {
		$(selector).click(function() {
			window.open(cancel_url, "Cancel", "width=245, height=171, scrollbars=0, location=0, status=0");
			return false;
		});
	},

	// Sorry, didn't have much time to test this out.
	"setup_save_to_recipe_book": function(selector,
										  success_image,
										  pop_up,
										  pop_up_close,
										  ajax_url,
										  is_logged_in,
										  ajax_error_message
										  ) {
		var login_pop = $(pop_up);
			login_pop.find(pop_up_close).click(function(){login_pop.hide()});
		
		var login_pop_cal = $('#please_login_cal');
			login_pop_cal.find(pop_up_close).click(function(){login_pop_cal.hide()});

		var login_pop_review = $('#please_login_review');
			login_pop_review.find(pop_up_close).click(function(){login_pop_review.hide()});

		var recipe_name = $("h2:first").text().replace(/[^\w\s]/g,"").replace(/\s/g, "_").toLowerCase(); // This is really crude and I imagine that you will be handling this better

		var recipe_button = $(selector);
		//recipe_button.click(function() {
		//	if ( is_logged_in() ) {
		//	//for more documention on this function see http://docs.jquery.com/Ajax/jQuery.ajax
		//		$.ajax({
		//			type: "GET", // This can also be changed to "POST"
		//			 url: ajax_url,
		//			data: "recipename=" + recipe_name,
		//		dataType: "json",
		//		 success: ajax_success,
		//		   error: ajax_fail
		//		});
		//	} else {
		//		popup_login_window();
		//	}
		//	return false;
		//});

		function popup_login_window() {
			login_pop.show();
		}

		function ajax_success(data) {
			// This function can be expanded to add in additional error checking
			update_recipe_button();
		}

		function ajax_fail() {
			if ( ajax_error_message ) {
				alert(ajax_error_message);
			}
			// else fail silently
		}

		function update_recipe_button() {
			recipe_button.css({"background-image":success_image}).unbind("click").click(function(){return false});
		}
	}

};

$(document).ready(function() {
	barilla_STT.setup_popup("#category_dropdown", "#category_link", ".close_box", "menu_open");
	barilla_STT.setup_popup_cal_psc("#popup_calendar_psc", "#add_to_calendar_button", ".close_box", "calendar_open");
	barilla_STT.setup_expanding_items("ul.expandable li", ".open_close");
	//barilla_STT.add_flash_to_page();
	//barilla_STT.shopping_list_button(".print_shopping_list", "./shopping_list.html");
	//barilla_STT.weekly_calendar_button(".print_weekly");
	//barilla_STT.confirm_cancel("#edit_event_page .cancel", "./confirm_cancel.html");
	//barilla_STT.confirm_delete("#recipe_book .delete", "./confirm_delete.html");
	barilla_STT.setup_tabs("ul.tab_list", "tab_hidden", "active");
	barilla_STT.setup_print_receipe_page("#theme", "#preview_area", "#print_recipe_button", "#the_recipe",
										 {"plain":"../img/print_themes/plain_preview.png",
										  "theme_001":"../img/print_themes/theme_001_preview.jpg",
										  "theme_002":"../img/print_themes/theme_002_preview.jpg",
										  "theme_003":"../img/print_themes/theme_003_preview.jpg"
										  //
										  //format:
										  //
										  //"theme_name":"preview_image"
										  //
										  //theme_name needs to be a unique value and it should be the
										  //value in the option tag of select#theme on the print_recipe_page.
										  //
										  });
	barilla_STT.setup_save_to_recipe_book("#save_to_recipe_book_button",
		"url(../../img/ui/recipe_saved_button.png)",
		"#please_login",
		".close_box",
		"/barilla/your_url_here", //This needs to be changed to whatever url you will be using
		function() {
		// TODO insert function to determine if user is logged in
		//
		// Currently this function always returns false so that it works as a stub,
		// it needs to be updated to actually do something, perhaps look at cookie
		return false;
		},
		"There was an error connecting to the server, please try again later." // remove this parameter to have requests fail silently
	);
});
