/**
* @author Ken Hill <http://www.norwood.com>
* @copyright Norwood Promotional Products <http://www.norwood.com>
* @package Norwood.com
* @require prototype.js, livepipe.js, window.js
* 
* Handle upkeep of product selections for Compare, Export, etc. tool use.
*/

if(typeof(Prototype) == "undefined")
	throw "Selection Manager requires Prototype to be loaded.";
if(typeof(Object.Event) == "undefined")
	throw "Selection Manager requires Object.Event to be loaded.";

FavoritesManager = Class.create({
	// Variable Inits
	debug: false,
	
	myFavoritesURL: "/special/myfavorites/My Favorites",
	favoritesBase: "/favorites/",
	
	last_added_hash: 0,
	
	//ie6msg: "This feature requires a newer version of your browser. For your convenience we have provided links to popular browsers at the top of this page.",
	
	initialize: function(skip_init) {
		this.debug = debug;
		
		if (!skip_init) {
			var off_left = 0;
			var off_top = 19;
			
			// Determine proper offset depending on page type
			if ($('page_product_id') != null) {
				off_left = -110;
				off_top = 21;
			}
			
			// Add add-to-favorites menu action to the buttons
			$$('.add_to_fav_menu').each(function(e) {
		    	favorites_menus.push( new Control.Window(e, {
			        position: 'relative',
			        hover: false,
			        offsetLeft: off_left,
			        offsetTop: off_top,
			        width: 210,
			        delayOpen: 0,
			        className: 'favorites_menu',
			        slide: true,
			        iframeshim: false,
			        closeOnClick: e,
			        
			    	parameters: {
			          	requestType: 'favorites',
			          	task: 'getFavoritesMenu',
			          	page: document.location.href
			        },
			        
			        beforeOpen: function() {
			        	// Test if already open, if so, don't try to open again
			        	if ($('show_list_opt') != null)
			        		throw $break;
			        	
			        	// Close any open boxes
			        	favorites_menus.each(function(obj) { 
		        			obj.close();
		        		});
			        	if (typeof(detail_panes) != 'undefined' && detail_panes != null) {
			        		detail_panes.each(function(obj) { 
				        		obj.close();
			        		});
			        	}
			        	if (typeof(more_box_btns) != 'undefined' && more_box_btns != null) {
				        	more_box_btns.each(function(obj) { 
			        			obj.close();
				        	});
			        	}
			        },
			        
			        beforeClose: function() {
						Effect.SlideUp(this.container, {
							duration: 0.5,
							afterFinish: function(e) {
								// Force the menus to reissue AJAX call on the next click
								this.getRemoteContentInsertionTarget().innerHTML = "";
								this.remoteContentLoaded = false; 
							}.bind(this)
						});
			        }
		    	}));
		    });
		}
	},
	
	// Slide up the menu then close it and clear the cache
	//   This will force the menus to reload via ajax on each click
	closeMenu: function() {
		favorites_menus.each(function(menu) {
			menu.close();
		});
	},
	
	goToMyFavorites: function() {
		var url = this.myFavoritesURL;
		setTimeout(function() { window.location = url; }, 0);
	},
	
	goToFavoritesList: function(hash) {
		if (hash == 0 && this.last_added_hash != 0)
			hash = this.last_added_hash;
		else
			return;
		
		var url = this.favoritesBase + hash + '/';
		setTimeout(function() { window.location = url; }, 0);
	},
	
	// Add to Favorites
	//   If list_id = 0, we are creating a new list
	addToList: function(list_id) {
		var params = {
			requestType: 'favorites',
			task: 'addSelectedToList',
			list_id: list_id
		};
		
		if ($('page_product_id') != null)
			params.productid = $('page_product_id').value;
		
		if (list_id == 0) {
			if ($('list_name').value != "")
				params.list_name = $('list_name').value;
			else {
				alert("Please enter a name for the new list");
				return;
			}
		}
		
		new Ajax.Request('/ajax.php',{
			method: 'post',
			parameters: params,
			onSuccess: function(response) {
				var res = response.responseJSON;
				
				if (res.error != null) {
					if (this.debug) alert(res.error);
				}
				else if (res.list_id != null) {
					if (typeof(sel_mgr) != 'undefined' && sel_mgr != null) {
						sel_mgr.selected = new Array();
						sel_mgr.clearHighlights();
					}
					
					this.last_added_hash = res.hash;
					
					$('show_list_opt').hide();
					$('created_list_opt').hide();
					
					if (res.created != null && res.created) {
						Effect.SlideUp($('create_list_inline'), {queue: 'end', duration: 0.5});
						
						var added_msg = $('created_list_opt').remove();
						
						$('create_list_inline').insert({after: added_msg});
						
						Effect.SlideDown($('created_list_opt'), {queue: 'end', duration: 0.5});
					}
					else {
						var added_msg = $('show_list_opt').remove();
						
						// Fix the bottom border, if necessary
						if ($('list_'+list_id).hasClassName('bottom'))
							added_msg.addClassName('bottom');
						else
							added_msg.removeClassName('bottom');
						
						$('list_'+list_id).insert({after: added_msg});
						
						Effect.SlideDown($('show_list_opt'), {queue: 'end', duration: 0.5});
					}
				}
			}.bind(this),
			onFailure: function(response) {
				if (this.debug) alert("AJAX call failed for addToList");
			}.bind(this)
		});
	},
	
	// Create a new Favorites List
	createList: function() {
		$('list_name').value = "";
		
		if ($('show_list_opt').visible())
			Effect.SlideUp($('show_list_opt'), {queue: 'end', duration: 0.5});
		
		if ($('created_list_opt').visible())
			Effect.SlideUp($('created_list_opt'), {queue: 'end', duration: 0.5});
		
		Effect.toggle('create_list_inline', 'slide', { 
			duration: 0.5,
			afterFinish: function(e) {
				$('list_name').focus();
			}
		});
	},
	
	// Show text to verify that user wants to remove selected items
	removeSelectedVerify: function() {
		Effect.SlideDown($('remove_check'), {queue: 'end', duration: 0.25});
	},
	
	// Remove the selected items from this list (when user is list owner)
	removeSelected: function(list_id) {
		var params = {
			requestType: 'favorites',
			task: 'removeSelected',
			list_id: list_id
		};
		
		new Ajax.Request('/ajax.php',{
			method: 'post',
			parameters: params,
			onSuccess: function(response) {
				var res = response.responseJSON;
				
				if (res.error != null) {
					if (this.debug) alert(res.error);
				}
				
				// Reload the page
				setTimeout(function() { window.location = ""; }, 0);
			}.bind(this),
			onFailure: function(response) {
				if (this.debug) alert("AJAX call failed for removeSelected");
			}.bind(this)
		});
	}
});
