profile_multibox = null;
guest_multibox = null;

// TODO: Clean all alerts from this file
function init_management_multibox () {
	// multiBox = new MultiBox('mb', {useOverlay: true, openFromLink: false, descClassName: "multiBoxDesc", onClose: test_stuff});
			//         guest_multibox = new MultiBox('mb', {
			//             useOverlay: true,
			//             openFromLink: false,
			// showControls: false,
			// whenComplete: init_guest_multibox_form_elements
			//         });
    // new MultiBox('mb', {useOverlay: true});
		//     	profile_multibox = new MultiBox('profile-mb', {
		// 	useOverlay: true,
		// 	openFromLink: true,
		// 	showControls: false,
		//             overlayClass: 'lightbox-small',
		// 	whenComplete: init_profile_multibox_form_elements
		// });
		giftgirlLightbox = new GiftgirlLightbox();
	    
		init_delete_guests();
		init_delete_profiles();
		init_edit_guest_links();
		init_edit_profile_links();
}

function init_edit_profile_links() {
	edit_profile_links = $ES('.edit-profile-link');
	edit_profile_links.each(function(element) {
		element.removeEvents('click');
		element.addEvent('click', function(ev) {
			giftgirlLightbox.open(element,'dashboard-edit-profile');
			return false;
		});
	})
	
}
function init_edit_guest_links() {
	$ES('.edit-guest-link').each(function(el) {
		el.removeEvents('click');
		el.addEvent('click', function(ev) {
			// alert("clicked me! I'm a edit guest link!");
			giftgirlLightbox.open(el,'dashboard-edit-guest');
			return false;
		});
	});
}
function init_profile_multibox_form_elements() {
	// alert("inside init_profile_multibox_form_elements");
	$('edit_profile_cancel_button').addEvent('click',function(ev) {
		giftgirlLightbox.close();
	});
	$('edit_profile_submit_button').addEvent('click',function(ev) {
		// alert("submit button clicked!");
		     
		giftgirlLightbox.close();
        $('edit_profile_form').set('send', {
            // onRequest: showIndicator,
            onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				// alert("inside function of onComplete");
				$('profiles_column').innerHTML = responseTree;
				init_management_multibox();
			}
		});    

		$('edit_profile_form').send();
	});
}

function close_guest_multibox(){	
	// alert('close_guest_multibox called');
	giftgirlLightbox.close();
}

function reattach_remove_events() {
	$ES('.remove-profile-link','MultiBoxContentContainer').each(function(element) {
		element.addEvent('click', function(ev) {
			remove_profile_from_guest(element);
		});
	});
}

function init_guest_multibox_form_elements() {
	// alert("inside guest init_multibox_form_elements");
	// TODO: determine which form has been loaded; assume edit guest for now
	// Setup remove links
	var remove_links = $ES('.remove-profile-link','MultiBoxContentContainer');
		// alert("remove_links is " + remove_links);
		remove_links.each(function(element) {
			element.addEvent('click', function(ev) {
				remove_profile_from_guest(element)
			});
		});
	// setup add profile to guest link
	var add_profile_link = $E('#add_profile_to_guest_button','MultiBoxContentContainer');
	// alert("add_profile link is " + add_profile_link);
	if (add_profile_link) {
		add_profile_link.addEvent('click', function(ev){
			add_profile_to_guest(add_profile_link);
		});
	}

	$('edit_guest_cancel_button').addEvent('click',function(ev) {
		giftgirlLightbox.close();
	});
	$('edit_guest_submit_button').addEvent('click',function(ev) {
		var all_relationships_chosen = true;
		// alert("submit button clicked!");
		// var valid_fields = true;
		
		// if ($('guest[email]').value == "") {
		// 	$('guest_form_errors').innerHTML = "Email Cannot be blank.";
		// 	valid_fields = false;
		// }
		$ES('.edit-guest-individual-profile').each(function(element) {
			var profile_checkbox = element.getElement('input');
			var relationship_dropdown = element.getElement('select');
			
			if (true == profile_checkbox.checked) {
				if ("" == relationship_dropdown.value) {
					all_relationships_chosen = false;
				}
			}
		});
		
		if (false == all_relationships_chosen) {
			alert("Please choose a relationship for each selected profile.");
			return false;
		}
        $('edit_guest_form').set('send', {
            // onRequest: showIndicator,
			evalScripts: true,
            onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				// alert("inside function of onComplete");
				$('guest_list').innerHTML = responseTree;
				// $('MultiBoxContentContainer').innerHTML = responseTree;
				// init_guest_multibox_form_elements();
				init_management_multibox();
			}
		});    
		// if (valid_fields) {
		$('edit_guest_form').send();
		// guest_multibox.close();
		close_guest_multibox();
		// }
	});
	
}

function add_profile_to_guest(element) {
	// alert("inside add_profile_to_guest")
	var profile_drop_down = $E('#profile_to_add','MultiBoxContentContainer');
	var profile_id = profile_drop_down.value;
	var guest_id = $E('#guest_id','MultiBoxContentContainer').value;
	// alert("profile_id is "+profile_id);
	// var j = new Ajax('/account/add_profile_to_guest/', {
	// 	update: $('guest_form_profiles'),
	// 	method: 'post',
	// 	data: { "profile_id": profile_id, "guest_id": guest_id},
	// 	onComplete: reattach_remove_events
	// }).request();
	id_to_add = 'profile_'+profile_id;
	if (!$(id_to_add)) {
		var profile_name = profile_drop_down[profile_drop_down.selectedIndex].innerHTML;
	
		var hidden_tag = new Element('input', {
			'type': 'hidden',
			'name': 'profiles[]',
			'value': profile_id
		});
		// alert(hidden_tag);
		// alert("hidden_tag has "+hidden_tag.get('html'));
		var remove_link = new Element('a', {
			'class': 'remove-profile-link',
			'id': 'remove_profile_'+profile_id,
			'href': 'javascript: void(0)',
			'html': 'remove'
		});
	
		remove_link.addEvent('click', function(ev) {
			remove_profile_from_guest(remove_link);
		});
	
		var profile_div = new Element('div', {
			'id': 'profile_'+profile_id
		});
		
		var paragraph = new Element('p', {
			'html': profile_name + ' '
		});
		
		hidden_tag.inject(paragraph);
		remove_link.inject(paragraph);
		
		paragraph.inject(profile_div);
	
		profile_div.inject('guest_form_profiles');
}
}
function remove_profile_from_guest(element) {
	// alert("inside remove_profile_from_guest");
	var split_string = element.id.split("remove_profile_");
	split_string = split_string[1].split("_");
	profile_id = split_string[0];
	guest_id = split_string[1];
	// alert("profile_id is "+profile_id);
	// alert("guest_id is "+guest_id);
	
	id_to_remove = "profile_"+profile_id;
	// alert("id_to_remove is "+id_to_remove);
	// $('guest_form_profiles').erase(id_to_remove);
	$(id_to_remove).dispose();
	
	// var j = new Ajax('/account/remove_profile_from_guest/', {
	// 		update: $('guest_form_profiles'),
	// 		method: 'post',
	// 		data: { "profile_id": profile_id, "guest_id": guest_id},
	// 		onComplete: reattach_remove_events
	// 	}).request();
	// 	
}
function init_multibox_form_elements(){
	
}

function init_delete_guests(){
	
	$ES('.delete_guest_class').each( function(node){
		node.removeEvents('click');
	    node.addEvent('click', function(ev){
			if (confirm('Are you sure you want to delete this guest?')){
				guest_id = node.id.split("guest_id_");
				
				new Ajax('/account/delete_guest/'+guest_id[1],{
					method:'post',
					update:$('guest_list'),
					onComplete: function(){
						// init_delete_guests();
						// init_edit_guest_links();
						init_management_multibox();
					} 
				}).request();
				
			}
			return false;
	    });
	});
	
}

function init_delete_profiles(){
	
	$ES('.delete_profile_class').each( function(node){
		node.removeEvents('click');
	    node.addEvent('click', function(ev){
			if (confirm('Delete this profile?')){
				profile_id = node.id.split("profile_id");
				
				new Ajax('/account/delete_profile/'+profile_id[1],{
					method:'post',
					update:$('owned_profiles'),
					onComplete: function(){
						// init_delete_profiles();
						// init_edit_profile_links();
						init_management_multibox();
					} 
				}).request();
				
			}
			return false;
	    });
	});
	
}

window.onDomReady(init_management_multibox);

function submit_update_user_form() {
    $('change_contact_info_form').set('send', { onComplete: function(rT,rE,rH,rJS) {
													$('change_contact_info').innerHTML = rT;
													$('change_contact_info_submit').addEvent('click', submit_update_user_form);
													createAccordion(1);
													}
											  });
	$('change_contact_info_form').send();
}

function submit_billing_info_form() {
	$('billing_info_form').set('send', 
		{onComplete: function(rT,rE,rH,rJS) {
			$('change_billing_info').innerHTML = rT;
			$('billing_info_form_submit').addEvent('click', submit_update_user_form);
			createAccordion(2);
		} });
	$('billing_info_form').send();
}

function submit_password_form() {
	$('change_password_form').set('send', {onComplete: function(rT,rE,rH,rJS) {
		$('change_password').innerHTML = rT;
		$('password_form_submit').addEvent('click',submit_password_form);
		createAccordion(0);
	}});
	$('change_password_form').send();
}

function init_submit_form_buttons() {
	if ($('change_contact_info_submit')) {
		$('change_contact_info_submit').addEvent('click', submit_update_user_form);
	}
	if ($('billing_info_form_submit')) {
		$('billing_info_form_submit').addEvent('click', submit_billing_info_form);		
	}
	if ($('password_form_submit')) {		
		$('password_form_submit').addEvent('click',submit_password_form);
	}
	
	login_password_submit = $('confirm_login_password_submit');
	if (login_password_submit) {
		login_password_submit.addEvent('click', function() {
			password_form = $('confirm_login_password_form');
			submit_confirm_login_form(password_form,0);
		});
		// login_password_submit.addEvent('click', submit_confirm_login_form(password_form));
	}

	login_contact_submit = $('confirm_login_contact_info_submit');
	if (login_contact_submit) {
		login_contact_submit.addEvent('click', function() {
			contact_form = $('confirm_login_contact_info');
			submit_confirm_login_form(contact_form,1);		
		});
	}
	login_billing_submit = $('confirm_login_billing_submit');
	if (login_billing_submit) {
		login_billing_submit.addEvent('click', function() {
			billing_form = $('confirm_login_billing_form');
			submit_confirm_login_form(billing_form,2);
		});
	}
}

function submit_confirm_login_form(form_element,index_to_show) {
	form_element.set('send', {onComplete: function(rT,rE,rH,rJS) {
		$('management_forms').innerHTML = rT;
		init_submit_form_buttons();
		createAccordion(index_to_show);
	}});
	form_element.send();
	
}

window.onDomReady(init_submit_form_buttons);

function init_profiles_navigation_dropdown() {
	// alert("init_go_to_profile_button fired!");
    if (  ! $('profiles_navigation') ){
        return;
    }
	$('profiles_navigation').addEvent('change', function() {
		profile = $('profiles_navigation').value
		if (profile == "") {
			return false;
		} else {
			var j = new Ajax('/profiles/' + profile + '/load_profile', {
					method: 'get',
					onComplete: function() {
						location.href = "/";
					} 
					
				}).request();			
		}
	});
}
function createAccordion(index_to_show){ 
 	// tabsCtl = $('right-col').getElements('div.tab');
	var accordion = new Accordion('h5.accordion2', 'div.accordion2', { 
		show: index_to_show,
		alwaysHide: true, 
		duration: 200
		// onActive: function(item){  item.getParent().addClass("opened"); },
		// onBackground:  function(item){  item.getParent().removeClass("opened"); }
	});
	// }, $('right-scroll') );
    
}
window.addEvent('domready', init_profiles_navigation_dropdown);
window.addEvent('domready', function(){createAccordion(100);});

