// Events
window.onload = function() {
	// todo: check if search form isn't on all pages
	updateSearchForm();
	
	// Google analytics

if (typeof(_gat)=='object')
        setTimeout(function(){
            _gat._getTracker("UA-10063683-14")._trackPageview()}, 1500);

/*var pageTracker = _gat._getTracker("UA-10063683-14");
	pageTracker._trackPageview();*/
}

// Functions
function sidebarMapClickedOnState(state) {
	document.getElementById('sidebar_map').src = 'images/new/home-map-'+state+'.jpg';
	document.getElementById('sidebar_search_state').value = state;
}

function highlight_search_form_state(state) {
	for (var i = 0; i < $('search_state').options.length; i++) {
		if ($('search_state').options[i].value == state) {
			$('search_state').selectedIndex = i;
			$('search_state').style.backgroundColor = 'yellow';
			setTimeout(function() {
				$('search_state').style.backgroundColor = '';
			}, 400);
			setTimeout(function() {
				$('search_state').style.backgroundColor = 'yellow';
			}, 600);
			setTimeout(function() {
				$('search_state').style.backgroundColor = '';
			}, 1000);
			return;
		}
	}
}

function indexChooseSectionClick(linkEl) {
	// if there is no sidebar search state value, then allow the link to proceed normally
	if (!document.getElementById('sidebar_search_state') || !document.getElementById('sidebar_search_state').value) {
		return true;
	}
	
	// build the href to go to
	var href = 'search.results?cat=' + linkEl.href.toString().match(/cat=([^&]*)/)[1];
	href += '&state=' + document.getElementById('sidebar_search_state').value;
	
	// go to the href
	document.location = href;
	return false;
}

function playRazorstreamVideo(html) {
	// prepare element
	var fElem = document.createElement('div');
	fElem.id = 'razorstreamcontent';
	fElem.style.width='100%';
	fElem.style.height='100%';
	fElem.style.top=0;
	fElem.style.left=0;
	fElem.style.position='fixed';
	document.body.appendChild(fElem);  
	
	// insert html
	containerEl.innerHTML = html;
	
	// handle IE
	if ((navigator.userAgent.match(/MSIE 6\./))) {
		fElem.style.position='absolute';
		razorstreamIE6Scroll();
		window.onscroll = razorstreamIE6Scroll;
	}
	
	return false;
}

function razorstreamIE6Scroll() {
	scrollElem = document.getElementById('flashboxcontent');
	if (!scrollElem) {
		return;
	}
	scrollElem.style.top = document.documentElement.scrollTop;
	scrollElem.style.left = document.documentElement.scrollLeft;
	document.recalc();
}

var _validate_form_field_nodes;
function validate_form(form_el, required_fields) {
	// find labels and fields
	var formLabelEls = Element.select(form_el, 'label');
	var formFields = Form.getElements(form_el);
	
	// find and mark invalid fields
	var invalidFieldLabels = [];
	formFields.each(function(field_el) {
		field_el.removeClassName('invalid_value');
		
		required_fields.each(function(field_name) {
			if (field_el.getAttribute('name') != field_name) {
				return;
			}
      
			if ((field_el.type == 'checkbox' && !field_el.checked) || (field_el.type != 'checkbox' && field_el.value == '')) {
				field_el.addClassName('invalid_value');
				formLabelEls.each(function(label) {
					var label_for_value = label.readAttribute('for'); // readAttribute works in IE, setAttribuet returns null for "for" on a label in IE
					
						if ((navigator.userAgent.match(/MSIE 8\./))) {
							invalidFieldLabels.push("Blank Field");
						}
					if (field_el.id == label_for_value) {
						var labelValue = label.innerHTML.toString();
						if (labelValue.substr(-1) == ':') {
							labelValue = labelValue.substr(0, labelValue.length - 1);
						}
						
						var label_is_alread_in_use = false;
						invalidFieldLabels.each(function(is_already_in_use_check_label) {
							if (is_already_in_use_check_label == labelValue) {
								label_is_alread_in_use = true;
							}
						});
						
						if (!label_is_alread_in_use) {
							invalidFieldLabels.push(labelValue);
						}
					}
				});
			}
		});
	});
  
	if (invalidFieldLabels.length == 0) {
		return true;
	}
	
	var alertMsg = 'There are errors in the form, please ensure that these fields have been filled in:\n';
	invalidFieldLabels.each(function(label) {
		alertMsg += "\n  • " + label;
	});
	
	alert(alertMsg); 
	return false;
}

function search_form_section_did_change() {
	updateSearchForm();
}

function updateSearchForm() {
	if ($('search_section_sales')) {
		var section = ($('search_section_sales').getValue() == '1') ? '1' : '2';
	} else if ($('search_section')) {
		var section = $('search_section').getValue();
	} else {
		section = '2';
	}
	
	var min_select = $('search_price_min');
	if (min_select) {
		min_select.options.length = 0;
		min_select.options[min_select.options.length] = new Option('Min. Price', '');
		min_select.options[min_select.options.length] = new Option('No Min.', '');
		if (section == '1') {
			min_select.options[min_select.options.length] = new Option('$50,000', '50000');
			min_select.options[min_select.options.length] = new Option('$100,000', '100000');
			min_select.options[min_select.options.length] = new Option('$250,000', '250000');
			min_select.options[min_select.options.length] = new Option('$500,000', '500000');
			min_select.options[min_select.options.length] = new Option('$1,000,000', '1000000');
		} else {
			min_select.options[min_select.options.length] = new Option('$100/wk', '100');
			min_select.options[min_select.options.length] = new Option('$250/wk', '250');
			min_select.options[min_select.options.length] = new Option('$500/wk', '500');
			min_select.options[min_select.options.length] = new Option('$750/wk', '750');
		}
	}
	
	var max_select = $('search_price_max');
	if (max_select) {
		max_select.options.length = 0;
		max_select.options[max_select.options.length] = new Option('Max. Price', '');
		max_select.options[max_select.options.length] = new Option('No Max.', '');
		if (section == '1') {
			max_select.options[max_select.options.length] = new Option('$100,000', '100000');
			max_select.options[max_select.options.length] = new Option('$250,000', '250000');
			max_select.options[max_select.options.length] = new Option('$500,000', '500000');
			max_select.options[max_select.options.length] = new Option('$1,000,000', '1000000');
			max_select.options[max_select.options.length] = new Option('$1,500,000', '1500000');
		} else {
			max_select.options[max_select.options.length] = new Option('$250/wk', '250');
			max_select.options[max_select.options.length] = new Option('$500/wk', '500');
			max_select.options[max_select.options.length] = new Option('$750/wk', '750');
			max_select.options[max_select.options.length] = new Option('$1,000/wk', '1000');
		}
	}
}