function $id(id){ return document.getElementById(id); }
function unClass(obj, className){
	var classList = obj.className.split(' ');
	obj.className = each(classList, function(a){ if(a.length && a != className) return a; }).join(' ');
	return obj;
};
function univent(e, upTag){
	e = e || window.event;
	e.obj = e.srcElement || e.target;
	if(upTag) while(e.obj.tagName != upTag){
		e.obj = e.obj.parentNode;
		if(e.obj == document) return null;
	}
	return e;
}
if(!Array.prototype.indexOf){
	Array.prototype.indexOf = function(needle){
		for(var i = 0; i < this.length; i++)
			if(this[i] === needle) return i;
		return -1;
	};
}
function each(set, fn){
	fn = fn || function(a){ if(a.nodeType == 1) return a; };
	var ret = [];
	for(var i = 0, len = set.length; i < len; i++){
		var val = fn(set[i], i, set);
		if(val !== undefined) ret.push(val);
	}
	return ret;
}
function Ready(){
	var gen_listings = $id('chk_gl'), ml = $id('ml_exclusions');
	if(gen_listings && ml){
		gen_listings.onchange = function(){
			var ml = $id('ml_exclusions');
			ml.className = this.checked ? '' : 'dim';
		};
	}

	document.v = view_size();
	var hslider = $id('blockset'), vslider = $id('subblockset'), fs = $id('browse_fullscreen');
	if(fs){
		$id('browse_fullscreen').onclick = function(){
			var bsr = $id('browser');
			bsr.style.zIndex = '999';
			bsr.setAttribute('width', '978');
			bsr.setAttribute('height', '600');		
		};
		$id('browse_fullscreen').style.zIndex = '2';
	}
	if($id('browse_pop')) $id('browse_pop').onclick = make_fullscreen;
	$id('browser').onload = function(){
		each($id('browse').childNodes, function(a){
			if(a.nodeType == 1 && a.tagName != 'IFRAME')
				a.style.display = 'block';
		});
		//* // This section is for if "Listings" is not the default selected tab.
		if(!$id('browse_pop')){
			try{
				var b = a.contentWindow.document || a.contentDocument || a.document;
			} catch(e){	// if iFrame has foreign document open
				return;
			}
			a.onload = function(){
				each(this.parentNode.childNodes, function(a){
					if(a.nodeType == 1 && a.tagName == 'DIV')
						a.style.display = "block";
				});
				this.onload = function(){};
			};
			a.setAttribute('src', 'http://link.flexmls.com/' + a.getAttribute('title'));
		}
		/**/
		this.onload = function(){};
	};
	$id('vid').onfocus = function(){
		each(this.childNodes, function(a){ if(a.tagName=='EMBED' || a.tagName == 'IFRAME'){
			a.setAttribute('height', '293');
			a.setAttribute('width', '655');
		}});
	};
	$id('vid').onblur = function(){
		each(this.childNodes, function(a){ if(a.tagName=='EMBED' || a.tagName == 'IFRAME'){
			a.setAttribute('height', '1');
			a.setAttribute('width', '1');
		}});
	};
			
	var slide_x = function(hslider, old_left, goal, target_index){
		var new_left = Math.round(((old_left * 5 + goal) / 6) + (goal < old_left ? -2 : 2));
		if(new_left == goal + 1 || new_left == goal - 1)
			new_left = goal;
		hslider.style.left = new_left + 'px';
		if(new_left != goal)
			setTimeout(function(){ slide_x(hslider, new_left, goal, target_index); }, 30);
		else {
			var a = each(hslider.childNodes)[target_index];
			if(a.onfocus) a.onfocus();
		}
	}, slide_y = function(vslider, old_top, goal){
		var new_top = Math.round(((old_top * 5 + goal) / 6) + (goal < old_top ? -2 : 2));
		if(new_top == goal + 1 || new_top == goal - 1)
			new_top = goal;
		vslider.style.top = new_top + 'px';
		if(new_top != goal)
			setTimeout(function(){ slide_y(vslider, new_top, goal); }, 30);
	};
	$id('tabset').onclick = function(e){
		e = univent(e, 'LI');
		if(!e) return;
		e.list = each(e.obj.parentNode.childNodes);
		e.index = e.list.indexOf(e.obj);
		e.old_index = e.list.indexOf(each(e.list, function(a){ if(a.className.indexOf('selected') > -1) return a; })[0]);
		if(e.old_index > -1){
			var a = each(hslider.childNodes)[e.old_index];
			if(a.onblur)
				a.onblur();
		}
		each(e.list, function(a){ unClass(a, 'selected'); });
		e.obj.className = 'selected';
		var sliders = each(hslider.childNodes);
		unClass(sliders[e.old_index], 'selected');
		sliders[e.index].className += ' selected';
		if(InstantSlide)
			hslider.style.left = e.index * -672 + 'px';
		else
			slide_x(hslider, (parseInt(hslider.style.left) || 0), (e.index * -672), e.index);
	};
	$id('subtabset').onclick = function(e){
		e = univent(e, 'LI');
		if(!e) return;
		e.list = each(e.obj.parentNode.childNodes);
		e.index = e.list.indexOf(e.obj);
		e.old_index = e.list.indexOf(each(e.list, function(a){ if(a.className.indexOf('selected') > -1) return a; })[0]);
		if(e.old_index > -1){
			var a = each(vslider.childNodes)[e.old_index];
			if(a.onblur)
				a.onblur();
		}
		each(e.list, function(a){ unClass(a, 'selected'); });
		e.obj.className = 'selected';
		var sliders = each(vslider.childNodes);
		unClass(sliders[e.old_index], 'selected');
		sliders[e.index].className += ' selected';
		if(InstantSlide)
			vslider.style.top = e.index * -295 + 'px';
		else
			slide_y(vslider, (parseInt(vslider.style.top) || 1), (e.index * -295), e.index);
	};
	$id('list_options').onclick = function(ev){
		ev = univent(ev, 'P');
		var that = this;
		// delay onclick until after checkbox is changed.
		setTimeout(function(){list_options_onclick.call(that, ev)}, 0);
	};
	list_options_onclick({obj:$id('list_options')});
	each(document.querySelectorAll('.flash_tabs'), function(flash_tab){
		flash_tab.onclick = flash_tab_handler;
	});
	make_fullscreen.check = setTimeout(make_fullscreen.checker, 1500);
};
InstantSlide = false;
flash_tab_handler = function(ev){
	ev = univent(ev, 'LI');
	var tabs = each(this.childNodes), index = tabs.indexOf(ev.obj);
	each(tabs, function(tab){ unClass(tab, 'selected'); });
	ev.obj.className += ' selected';
	each(each($id(this.getAttribute('rel')).childNodes), function(block, i){
		if(block.nodeType == 1)
			block.style.display = (i == index) ? 'block' : 'none';
	});
};
list_options_onclick = function(ev){
	var gl = $id('general_listings');
	if(!gl) return;
	var arr = each(document.querySelectorAll('#foreclose fieldset input'), function(a){ return a.checked; });
	arr.push(gl.selectedIndex);
	if(arr[0] || arr[1] || arr[2] || arr[3])
		unClass($id('listing_updates'), 'dim')
	else
		$id('listing_updates').className += ' dim';
};
function check_for_dom(){
	if(document.body && $id('browse')){
		Ready();
		Ready = false;
	} else
		setTimeout(check_for_dom, 10);
}
check_for_dom();
window.onload = function(){ if(Ready) Ready(); };

function ps_valid(obj){
	while(obj.tagName != 'FORM' && obj.tagName != 'BODY'){
		obj = obj.parentNode;
	}
	if(obj.tagName == 'BODY') return false;
	if(ps_valid.by_id[obj.id]){
		if(ps_valid.by_id[obj.id](obj)){
			var hid = document.createElement('input');
			hid.setAttribute('type', 'hidden');
			hid.setAttribute('name', 'form_id');
			hid.setAttribute('value', obj.id);
			obj.appendChild(hid);
			return true;		// allow form submission
		} else return false;	// prevent form submission
	} else
		return true;
}
ps_valid.by_id = {
	'personal_info' : function(form){
		var pass = true, list = each(form.childNodes);
		list = list[list.length - 1].childNodes;
		each(list, function(a){
			if(a.nodeType == 1){
				var b = a.childNodes[1];
				if(!b){
					b = a.nextSibling;
					if(b.nodeType != 1)
						b = b.nextSibling;
				}
				if(!b || b.nodeType != 1 || b.tagName != 'INPUT' || b.type != 'text')
					return;
				if(b.value == ""){
					pass = false;
					ps_valid.mark(b);
				} else if(b){
					ps_valid.unmark.call(b);
				}
			}
		});
		return pass;
	},
	'cma' : function(form){
		var pass = true;
		if(!ps_valid.by_id['personal_info'](form))
			pass = false;
		if(!pass)
			alert("Required information is missing.\nI can't send you information if I can't contact you.");
		return pass;
	},
	'sss_form' : function(form){
		var pass = true;
		if(!ps_valid.by_id['personal_info'](form))
			pass = false;
		if(!pass)
			alert("Required information is missing.\nI can't send you information if I can't contact you.");
		return pass;
	},
	'ml_form' : function(form){
		var pass = true, list = each(form.childNodes);
		// must pick a mailing list
		if(!(list[2].firstChild.firstChild.checked || list[3].firstChild.firstChild.checked))
			pass = false;
		// must complete personal info
		if(!ps_valid.by_id['personal_info'](form))
			pass = false;
		if(!pass)
			alert("Some required information is missing.\nI can't send you information if I can't contact you.");
		return pass;
	},
	'form1' : function(form){
		var pass = true;
		each(['frm_name', 'frm_email', 'frm_phone'], function(id){
			if($id(id).value == ""){
				pass = false;
				ps_valid.mark(id);
			}
		});
		var anyML = false;
		each(document.querySelectorAll('#form1 input[type=checkbox]'), function(chbox, i){
			if(i < 7 && chbox.checked == true){ anyML = true; }
		});
		if(anyML) ps_valid.unmark.call($id('list_options'));
		if(!pass)
			alert("Some required fields are empty.\nI've marked them in red.  Please fill them in.");
		else if(!anyML){
			pass = false;
			ps_valid.mark('list_options');
			alert("Please select the instant email update you want before continuing.");
		}
		return pass;
	}
};
ps_valid.mark = function(id){
	if(typeof(id) == 'string')
		id = $id(id);
	id.className += ' marked';
	id.onclick = ps_valid.unmark;
	id.onfocus = ps_valid.unmark;
};
ps_valid.unmark = function(){
	unClass(this, 'marked');
	this.onclick = function(){};
	this.onfocus = function(){};
};
function view_size(){
	var win = {'x' : 625, 'y' : 450};
	if (document.body && document.body.offsetWidth){
		win.x = document.body.offsetWidth;
		win.y = document.body.offsetHeight;
	}
	if (document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth ){
		win.x = document.documentElement.offsetWidth;
		win.y = document.documentElement.offsetHeight;
	}
	if (window.innerWidth && window.innerHeight){
		win.x = window.innerWidth;
		win.y = window.innerHeight;
	}
	return win;
}
function make_fullscreen(){
	clearTimeout(make_fullscreen.check);
	document.body.parentNode.style.overflow = 'hidden';
	var ind = document.location.href.indexOf('#');
	if(ind == -1)
		document.location.href += '#fs';
	else
		document.location.href = document.location.href.slice(0, ind + 1) + 'fs';
	$id('home_top').style.left = '-312px';
	$id('home_bottom').style.top = 522 + document.v.y + 'px';
	$id('askbrian_wrapper').style.top = 324 + document.v.y + 'px';
	$id('sliderbox').style.left = '0';
	$id('page').style.width = document.v.x + 'px';
	each(['sliderbox', 'blockset', 'browse', 'browser'], function(id){
		$id(id).style.width = document.v.x + 'px';
		$id(id).style.height = document.v.y + 'px';
	});
	$id('browse_pop_container').style.width = document.v.x * .6 + 'px';
	$id('tabset').style.display = 'none';
	scroll(5, 203);
	document.v.name = this.firstChild.data;
	this.firstChild.data = 'Restore';
	this.onclick = end_fullscreen;
	make_fullscreen.check = setTimeout(make_fullscreen.checker, 30);
}
make_fullscreen.checker = function(){
	var ind = document.location.href.indexOf('#');
	if(ind > -1){
		var hash = document.location.href.slice(ind);
		if('#fs' == hash){
			if($id('browse_pop').firstChild.data == 'Full Screen')
				make_fullscreen.call($id('browse_pop'));
		} else {
			if($id('browse_pop').firstChild.data != 'Full Screen')
				end_fullscreen.call($id('browse_pop'));
		}
	} else {
		if($id('browse_pop').firstChild.data != 'Full Screen')
			end_fullscreen.call($id('browse_pop'));
	}
	make_fullscreen.check = setTimeout(make_fullscreen.checker, 30);
};
function end_fullscreen(){
	clearTimeout(make_fullscreen.checker);
	var ind = document.location.href.indexOf('#');
	if(ind > -1){
		var hash = document.location.href.slice(ind);
		if(hash != '#')
			document.location.href = document.location.href.slice(0, ind + 1);
	}
	document.body.parentNode.style.overflow = '';
	$id('home_top').style.left = '';
	$id('home_bottom').style.top = '';
	$id('askbrian_wrapper').style.top = '';
	$id('page').style.width = '';
	$id('sliderbox').style.left = ''
	each(['sliderbox', 'blockset', 'browse', 'browser'], function(id){
		$id(id).style.width = '';
		$id(id).style.height = '';
	});
	$id('browse_pop_container').style.width = '';
	$id('tabset').style.display = 'block';
	scroll(0, 0);
	this.firstChild.data = document.v.name || 'Full Screen';
	this.onclick = make_fullscreen;
}
