var enhance = {
	reset_tabs: function() {
		$$('div.gift_option_section').invoke('hide');
		$$('div.gift_option_section:first-of-type').invoke('show');
		$$('div.tabbox').invoke('setStyle', { backgroundImage: 'url(common/themes/tnc_guide/tab1.gif)' });
		return true;
	},
	
	init: function() {
		// attach tool tips to need links on the choose.php page
		$$('a.need').each(function(a) {
				// .hash contains the part of the href that starts with #
				var need_detail_id = a.hash.substring(1, a.hash.length);
				new Tip(a, $(need_detail_id), {
					hook:{target: 'rightMiddle', tip: 'topLeft'}, 
					offset:{x:5, y:-52},
					className: 'needs'
				});
				Event.observe(a, 'click', function(ev) {
					Event.stop(ev);
					var assoc_checkbox = $(this).previous('input');
					assoc_checkbox.checked = assoc_checkbox.checked ? false : true;
				});
		});
		
		// attach modal popups to gift options on the options.php page
		$$('a.option_list').each(function(a) {
				var gift_detail_id = a.hash.substring(1, a.hash.length);
				var gift_detail_title = $(gift_detail_id).down('h1').innerHTML;
				$(gift_detail_id).down('h1').remove();
				Event.observe(a, 'click', function(ev) {
					Event.stop(ev);
					var win = new Window({
						className: 'tnc_guide', 
						title: gift_detail_title, 
						maximizable: false, 
						minimizable: false, 
						resizable: false, 
						minWidth: 10, 
						width: 756, 
						recenterAuto: false,
						draggable: false, 
						destroyOnClose: true
					});
					win.setContent(gift_detail_id);
					win.setCloseCallback(enhance.reset_tabs);
					win.showCenter(true, 125);
				});
		});
		
		// initially, hide all but the first tab content divs
		enhance.reset_tabs();
		
		// modify all gift options to turn them into tabbed boxes
		var total_tab_count = 0;
		$$('div.gift_option').each(function(div) {
				var tabs = [];
				var tabs_html = '';
				var tab_count = 1;
				
				div.immediateDescendants().each(function(child) {
						if (child.className == 'gift_option_section') {
							child.setAttribute('id', 'tab_content_'+total_tab_count);
							tabs.push('<a class="tab tabnum'+tab_count+'" href="#tab_content_'+total_tab_count+'">' + child.down('h2').innerHTML + '</a>');
							child.down('h2').remove();
							tab_count++;
							total_tab_count++;
						}
				});
				
				$A(tabs).each(function(tab) {
						tabs_html += tab;
				});
				
				new Insertion.Top(div, '<div class="tabbox">' + tabs_html + '</div><div style="clear: both;"></div>');
		});
		
		// turn all of the new tabs into clickables
		//$$('a.tab').invoke('observe', 'click', enhance.test);
		$$('a.tab').each(function(a) {
				new TabLink(a);
		});
		
		// require 1-3 checkboxes on the needs form
		if ($('needs_form')) {
			Event.observe($('needs_form'), 'submit', function(ev) {
				var selected_needs = $('needs_form').serialize(true);
				var error_msg = '';
				var error_classname = 'error';
				
				if (!selected_needs['needs[]']) {
					error_msg = 'Please choose at least one want or need.';
				} else if (selected_needs['needs[]'].length > 3) {
					error_msg = 'Please choose no more than three wants or needs.';
				}
				
				if (error_msg) {
					Event.stop(ev);
					//if (!$('directions').hasClassName(error_classname))	$('directions').addClassName(error_classname);
					//
					//if ($('directions').innerHTML != error_msg) {
					//	$('directions').innerHTML = error_msg;
					//} else {
						alert(error_msg);
					//}
				}
			});
		}
		
		// enhance the contact form and other stuff on the contact page
		if ($('contact_form')) {
			// add the hidden js_enabled form field for the dbmailer php stuff
			new Insertion.Top('contact_form', '<input type="hidden" name="js_enabled" value="true" />');
			
			// enhance the contact us link to turn it into a window
			Event.observe('contact_link', 'click', function(ev) {
					Event.stop(ev);
					var win = new Window({
								className: 'tnc_guide', 
								title: 'Contact Us', 
								maximizable: false, 
								minimizable: false, 
								resizable: false, 
								width: 400, 
								recenterAuto: false,
								draggable: false, 
								destroyOnClose: true
							});
							win.setLocation(120);
							win.setAjaxContent('tnc_re_info.html',{method: 'get'} ,true, true);
			});
			
			// set up the contact via stuff
			Event.observe($('contact_via'), 'change', enhance.displayVia);
			enhance.displayVia();
      
      // store the value of the last why visited option
      var last_why_val = 'Other (please specify)';
      
      // add the other box, but hide it
      var otherbox = new Element('input', { name: 'otherbox', type: 'text', id: 'otherbox' } );
      $('why_visited').insert( { after: otherbox } );
      otherbox.hide();
      
      // update the last why_visited option value whenever the otherbox is updated
      Event.observe(otherbox, 'keyup', function(event) {
          last_why_val = otherbox.getValue();
          //$$('#why_visited option:last-child').invoke('update', last_why_val);
          $$('#why_visited option:last-child').invoke('writeAttribute', 'value', last_why_val);
      });

      // show/hide why_visited other option
			$('why_visited').observe('change', function(ev) {
        if ($F('why_visited') == last_why_val) {
          otherbox.show();
        } else {
          otherbox.hide();
        }
      });
			
			$('btn_prev').observe('click', function(ev) {
					Event.stop(ev);
					history.go(-1);
			});
			
			// validation
			Event.observe($('contact_form'), 'submit', function(ev) {
				var formdata = $('contact_form').serialize(true);
				var error_msg = '';
				var error_classname = 'error';
				
				if (!formdata.fullname) {
					error_msg += 'Please enter your Name. ';
				}
				
				if (!formdata.contact_via) {
					error_msg += 'Please tell us how you would like to be contacted.';
				} else {
					var via_text = {email: 'Email Address', phone: 'Phone Number', mail: 'Mailing Address'};
					if (!formdata[formdata.contact_via]) {
						error_msg += 'Please enter your ' + via_text[formdata.contact_via] + '.';
					}
				}
				
				if (error_msg) {
					Event.stop(ev);
					//if (!$('directions').hasClassName(error_classname))	$('directions').addClassName(error_classname);
          //
					//if ($('directions').innerHTML != error_msg) {
					//	$('directions').innerHTML = error_msg;
					//} else {
						alert(error_msg);
					//}
				}
			});
		}
		
		// tell a friend
		if ($('taf_link')) {
			Event.observe($('taf_link'), 'click', function(ev) {
					Event.stop(ev);
					window.open('tell_a_friend.php?link='+escape(document.location),'taf','width=300,height=500,resizable=1,scrollbars=1');
			});
		}
		if ($('taf_close_link')) {
			Event.observe($('taf_close_link'), 'click', function(ev) {
					Event.stop(ev);
					window.close();
			});
		}
		if ($('taf_return_link')) {
			Event.observe($('taf_return_link'), 'click', function(ev) {
					Event.stop(ev);
					window.close();
			});
		}
		
	}, // end init
	
	displayVia: function() {
		$('via_email').hide();
		$('via_mail').hide();
		$('via_phone').hide();
		$('via_'+$F('contact_via')).show();
	}
  
};

var TabLink = Class.create();
TabLink.prototype = {
	
	initialize: function(ctrl) {
		this.ctrl = ctrl;
		Event.observe(this.ctrl, 'click', this.handleClick.bindAsEventListener(this.ctrl), false);
	},
	
	handleClick: function(ev) {
		Event.stop(ev);
		
		var tab_content_id = this.hash.substring(1);
		var newtabbg;
		
		// reset all tabs
		$$('div.gift_option_section').invoke('hide');
		
		if (this.hasClassName('tabnum1')) newtabbg = 'tab1';
		if (this.hasClassName('tabnum2')) newtabbg = 'tab2';
		if (this.hasClassName('tabnum3')) newtabbg = 'tab3';
		if (this.hasClassName('tabnum4')) newtabbg = 'tab4';

		$(this).up('div').setStyle({ backgroundImage: 'url(common/themes/tnc_guide/'+newtabbg+'.gif)' });
		$(tab_content_id).show();
	}

}

function debug(obj) {
	var temp = '';
	for (var i in obj) {
		temp += i + ':' + obj[i] + '\n';
	}
	return temp;
}

Event.observe(window, 'load', enhance.init, false);
