var MyWarburg = {
	active:false,
	activate: function() { this.active = true; },
	num_compared: 0,
	max_compared: 4,
	compare: function(a) {
		var req = this.parseRequest(a);
		if (!this.active) return this.prompt(req);
		if (this.num_compared >= this.max_compared) {
			alert('You can only compare ' + this.max_compared + ' at once.\n\nPlease deselect one or more compared properties,\nor view all properties selected for comparison.');
			return false;
		} else if (req) {
			if (this.num_compared + 1 == this.max_compared) {
				window.location = req.base + 'my_warburg/' + req.action + '/' + req.objType + '/' + req.objId + '?r=' + encodeURI(req.base + 'my_warburg/compare');
			} else {
				new Ajax.Request(a.href, { method: 'post' });
				this.num_compared++;
				this.turnOn(a);
			}
			return false;
		}
	},
	save: function(a) {
		var req = this.parseRequest(a);
		if (!this.active) return this.prompt(req);
		if (req) {
			new Ajax.Request(a.href, { method: 'post' });
			this.turnOn(a);
			return false;
		}
	},
	uncompare: function(a, afterUncompare) {
		var req = this.parseRequest(a);
		if (!this.active) return this.prompt(req);
		if (req) {
			new Ajax.Request(a.href, { method: 'post' });
			if (req.objType == 'all') {
				this.num_compared = 0;
			} else {
				this.num_compared--;
			}
			if (afterUncompare) eval(afterUncompare);
			return false;
		}
	},
	remove: function(a, afterRemove) {
		var req = this.parseRequest(a);
		if (!this.active) return this.prompt(req);
		if (req) {
			new Ajax.Request(a.href, { method: 'post' });
			if (afterRemove) eval(afterRemove);
			return false;
		}
	},
	ohUpdate: function(a) {
		var req = this.parseRequest(a);
		if (req) {
			var oldClassName = this.getPrimaryClassName(a);
			if (oldClassName) {
				if (this.isOn(oldClassName)) {
					this.remove(a, 'this.turnOff(a);');
					a.href = req.base + 'my_warburg/add/open_house/' + req.objId;
				} else {
					this.save(a);
					a.href = req.base + 'my_warburg/remove/open_house/' + req.objId;
				}
			}
		}
		return false;
	},
	note: function(a) {
		var req = this.parseRequest(a);
		if (!this.active) return this.prompt(req);
		if (req) {
			if (($F('note_'+req.objId+'_old') != $F('note_'+req.objId)) || (($F('note_'+req.objId+'_share_old')==1) != $('note_'+req.objId+'_share').checked)) {
				new Ajax.Request(a.href, { method: 'post', parameters: $(a).up().down('form').serialize(true) });
			}
			if ($F('note_'+req.objId)) this.turnOn(a);
			else this.turnOff(a);
			$('note_'+req.objId+'_old').value = $F('note_'+req.objId);
			$('note_'+req.objId+'_share_old').value = $('note_'+req.objId+'_share').checked ? '1' : '0';
			this.closeNote(a);
			return false;
		}
	},
	openNote: function(a) {
		var req = this.parseRequest(a);
		if (!this.active) return this.prompt(req);
		if (req) {
			$('note_'+req.objId).value = $F('note_'+req.objId+'_old');
			$('note_'+req.objId+'_share').checked = ($F('note_'+req.objId+'_share_old') == 1);
			$(a).up().down('.myw_note_wrapper').show();
			return false;
		}
	},
	closeNote: function(a) {
		$(a).up().down('.myw_note_wrapper').hide();
	},
	savedItemShowOptions: function(a_wrap, divName) {
		if (!this.active) return;
		var a = $(a_wrap).down();
		if (!this.isOn(this.getPrimaryClassName(a))) return;
		var req = this.parseRequest(a);
		var optionsDiv = $(a_wrap).down('div.' + divName);
		optionsDiv.show();
	},
	savedItemHideOptions: function(a_wrap, divName) {
		var a = $(a_wrap).down();
		var req = this.parseRequest(a);
		var optionsDiv = $(a_wrap).down('div.' + divName);
		optionsDiv.hide();
	},
	savedPropertyShowOptions: function(a_wrap) {
		return this.savedItemShowOptions(a_wrap, 'myw_propsearch_results_options');
	},
	savedPropertyHideOptions: function(a_wrap) {
		return this.savedItemHideOptions(a_wrap, 'myw_propsearch_results_options');
	},
	savedBuildingShowOptions: function(a_wrap) {
		return this.savedItemShowOptions(a_wrap, 'myw_buildsearch_results_options');
	},
	savedBuildingHideOptions: function(a_wrap) {
		return this.savedItemHideOptions(a_wrap, 'myw_buildsearch_results_options');
	},
	savedBrokerShowOptions: function(a_wrap) {
		return this.savedItemShowOptions(a_wrap, 'myw_brokersearch_results_options');
	},
	savedBrokerHideOptions: function(a_wrap) {
		return this.savedItemHideOptions(a_wrap, 'myw_brokersearch_results_options');
	},
	parseRequest: function(a) {
		var pieces = a.href.split('/my_warburg/');
		var base = pieces[0] + '/';
		var pieces = pieces[1].split('/');
		if (pieces.length < 3) return false;
		else return {
			base: base,
			action: pieces[0],
			objType: pieces[1],
			objId: pieces[2],
			anchor: a
		}
	},
	prompt: function(req) {
		if (!req) return true;
		var contents = '' +
			'<div id="my_warburg_teaser">' +
				'<h2>In order to use this feature, you must be a My Warburg member.</h2>' +
				'<p>My Warburg is designed to help buyers and sellers personalize and organize their experience at warburgrealty.com. Through My Warburg...</p>' +
				'<table>' +
					'<thead>' +
						'<tr>' +
							'<th id="buyers_col">' +
								'Buyers may:' +
							'</th>' +
							'<th id="sellers_col">' +
								'Sellers may:' +
							'</th>' +
						'</tr>' +
					'</thead>' +
					'<tbody>' +
						'<tr>' +
							'<td>' +
								'<ul>' +
									'<li>' +
										'Create and save searches and favorite properties' +
									'</li>' +
									'<li>' +
										'Search, save and organize our complete Open House schedule' +
									'</li>' +
									'<li>' +
										'Sign up for our email notification program and receive emails about open houses, and new properties of interest' +
									'</li>' +
									'<li>' +
										'Search for and save your favorite buildings and learn about their amenities' +
									'</li>' +
									'<li>' +
										'Save contact information on individual Warburg agents' +
									'</li>' +
									'<li>' +
										'View comparable sales' +
									'</li>' +
								'</ul>' +
							'</td>' +
							'<td>' +
								'<ul>' +
									'<li>' +
										'See how Warburg will market your home through our exclusive marketing program' +
									'</li>' +
									'<li>' +
										'Request a market evaluation of your home' +
									'</li>' +
									'<li>' +
										'Browse recent Warburg sales and rentals' +
									'</li>' +
									'<li>' +
										'View Sample Board Package Documents' +
									'</li>' +
								'</ul>' +
							'</td>' +
						'</tr>' +
					'</tbody>' +
				'</table>' +
				'<div id="teaser_actions">' +
					'<a href="' + req.base + 'my_warburg/register" title="Register Now"><img src="' + req.base + 'img/buttons/register_now.gif" alt="Register Now" width="129" height="29" /></a>' +
				'</div>' +
				'<div id="teaser_close">' +
					'<a href="' + document.location + '" onclick="Flash.close();return false;" title="Close"><img src="' + req.base + 'img/buttons/sm-g-close.gif" alt="x" width="9" height="9" /></a>' +
				'</div>' +
			'</div>' +
			'<div id="teaser_login">' +
				'<div id="teaser_new_user" class="myw_tabs">' +
					'<ul>' +
						'<li id="teaser_nu_sign_in" class="selected"><a name="">Sign in to My Warburg</a></li>' +
						'<li id="teaser_nu_register"><a href="' + req.base + 'my_warburg/register">Register</a></li>' +
					'</ul>' +
				'</div>' +
				'<div id="teaser_sign_in_tab">' +
					'<div id="teaser_sign_in_tab_inner">' +
						'<form method="post" action="' + req.base + 'my_warburg/login" id="teaser_sign_in_form">' +
							'<h5><a name="myw_sign_in"></a>Sign in to My Warburg</h5>' +
							'<div class="text_field">' +
								'<label for="myw_email" id="teaser_email_label" class="label_text">' +
									'Email:' +
								'</label>' +
								'<input id="teaser_email" name="email" class="input_text" maxlength="64" value="" type="text">' +
							'</div>' +
							'<div class="text_field">' +
								'<label for="myw_password" id="teaser_password_label" class="label_text">' +
									'Password:' +
								'</label>' +
								'<input id="teaser_password" name="password" class="input_password" maxlength="128" type="password">' +
							'</div>' +
							'<div class="checkbox_field">' +
								'<input id="teaser_remember" name="remember" class="input_checkbox" value="1" type="checkbox">' +
								'<label for="teaser_remember" id="teaser_remember_label" class="label_checkbox"> ' +
									'Remember me' +
								'</label>' +
							'</div>' +
							'<div id="teaser_submit_wrap">' +
								'<input id="teaser_submit" src="' + req.base + 'img/buttons/myw_sign_in.gif" class="input_image" alt="Sign In" type="image">' +
							'</div>' +
							'<div id="teaser_options">' +
								'<ul class="option_bar">' +
									'<li class="first"><a href="' + req.base + 'my_warburg/register">Register Now</a></li>' +
									'<li><a href="#" onclick="return false;">Learn More</a></li>' +
									'<li><a href="#" onclick="return false;">Help</a></li>' +
								'</ul>' +
							'</div>' +
						'</form>' +
					'</div>' +
				'</div>' +
			'</div>';
		Flash.show(contents, 'my_warburg');
		return false;
	},
	confirm: function(a) {
		var req = this.parseRequest(a);
		if (!this.active) return;
		if (req) {
			var contents = '' + 
				'<p>You are now registered to receive email notifications. We will notify you of any future Open Houses for this ' + req.objType + '.</p>' + 
				'<div align="center"><p><a href="' + document.location + '" onclick="Flash.close();return false;" title="Close"><img src="' + req.base + 'img/buttons/ok_flash.gif" alt="OK" width="45" height="29" /></a></p></div>';
			Flash.show(contents, 'my_warburg_confirm');
			$(a).up().hide();
		}
	},
	help: function(base) {
		var contents = '' +
			'<div id="my_warburg_help">' +
				'<p>My Warburg is a powerful functionality that allows you to create your own personalized log-in. As a Warburg buyer, saving your search criteria means you receive real-time updates as new properties come on the market. Multiple lists help you can manage your searches quickly and efficiently. As a Warburg seller, My Warburg allows you to access real-time detailed statistics on your own property, links to advertising and internet hits.</p>' +
				'<p>Simply follow instructions to create your individual log-in and take advantage of My Warburg\'s features.</p>' +
				'<div id="help_actions">' +
					'<a href="' + base + 'my_warburg/register" title="Register Now"><img src="' + base + 'img/buttons/register_now.gif" alt="Register Now" width="129" height="29" /></a>' +
				'</div>' +
				'<div id="help_close">' +
					'<a href="' + document.location + '" onclick="Flash.close();return false;" title="Close"><img src="' + base + 'img/buttons/sm-g-close.gif" alt="x" width="9" height="9" /></a>' +
				'</div>' +
			'</div>' +
			'<div id="teaser_login">' +
				'<div id="teaser_new_user" class="myw_tabs">' +
					'<ul>' +
						'<li id="teaser_nu_sign_in" class="selected"><a name="">Sign in to My Warburg</a></li>' +
						'<li id="teaser_nu_register"><a href="' + base + 'my_warburg/register">Register</a></li>' +
					'</ul>' +
				'</div>' +
				'<div id="teaser_sign_in_tab">' +
					'<div id="teaser_sign_in_tab_inner">' +
						'<form method="post" action="' + base + 'my_warburg/login" id="teaser_sign_in_form">' +
							'<h5><a name="myw_sign_in"></a>Sign in to My Warburg</h5>' +
							'<div class="text_field">' +
								'<label for="myw_email" id="teaser_email_label" class="label_text">' +
									'Email:' +
								'</label>' +
								'<input id="teaser_email" name="email" class="input_text" maxlength="64" value="" type="text">' +
							'</div>' +
							'<div class="text_field">' +
								'<label for="myw_password" id="teaser_password_label" class="label_text">' +
									'Password:' +
								'</label>' +
								'<input id="teaser_password" name="password" class="input_password" maxlength="128" type="password">' +
							'</div>' +
							'<div class="checkbox_field">' +
								'<input id="teaser_remember" name="remember" class="input_checkbox" value="1" type="checkbox">' +
								'<label for="teaser_remember" id="teaser_remember_label" class="label_checkbox"> ' +
									'Remember me' +
								'</label>' +
							'</div>' +
							'<div id="teaser_submit_wrap">' +
								'<input id="teaser_submit" src="' + base + 'img/buttons/myw_sign_in.gif" class="input_image" alt="Sign In" type="image">' +
							'</div>' +
							'<div id="teaser_options">' +
								'<ul class="option_bar">' +
									'<li class="first"><a href="' + base + 'my_warburg/register">Register Now</a></li>' +
									'<li><a href="#" onclick="return false;">Learn More</a></li>' +
									'<li><a href="#" onclick="return false;">Help</a></li>' +
								'</ul>' +
							'</div>' +
						'</form>' +
					'</div>' +
				'</div>' +
			'</div>';
		Flash.show(contents, 'my_warburg');
		return false;
	},
	getPrimaryClassName: function(a) {
		return $w($(a).className).find(function(c){return c.indexOf('myw')==0});
	},
	getClassRoot: function(cn) {
		return (this.isOn(cn)) ? cn.substring(0, cn.length - 3) : cn;
	},
	isOn: function(cn) {
		return (cn.substring(cn.length - 3) == '_on');
	},
	turnOn: function(a) {
		var oldClassName = this.getPrimaryClassName(a);
		if (oldClassName && !this.isOn(oldClassName)) {
			$(a).removeClassName(oldClassName);
			$(a).addClassName(oldClassName + '_on');
		}
	},
	turnOff: function(a) {
		var oldClassName = this.getPrimaryClassName(a);
		if (oldClassName && this.isOn(oldClassName)) {
			$(a).removeClassName(oldClassName);
			$(a).addClassName(this.getClassRoot(oldClassName));
		}
	},
	getUser: function() {
		if (!this.active) return false;
		var user = { fname:'', lname:'', email:'' };
		var prop = false;
		unescape(document.cookie).split(';').each(function(b) {
			if (prop) {
				eval('user.' + prop + ' = "' + b.replace(/^s:[0-9]+:"([^"]+)"$/, '$1').replace(/"/g, '\"') + '"');
				prop = false;
			} else if (b=='s:5:"fname"' || b=='s:5:"lname"' || b=='s:5:"email"') {
				prop = b.replace(/^s:[0-9]+:"([^"]+)"$/, '$1');
			}
		});
		return user;
	}
}
