jQuery(document).ready(fdc_init);

function fdc_init(){
	advanced_options_init();
	registration_form_init();
	toggle_zipcode_field();
}
function advanced_options_init(){
	jQuery('#fdc_more_options').click(toggle_advanced_options);
	jQuery('#fdc_state_selector').change(toggle_zipcode_field);
}

function registration_form_init(){
	jQuery('.fdc_details_link').click(fdc_get_details);
	jQuery('#fdc_reg_reset').click(fdc_registration_cancel);
	jQuery('#fdc_reg_submit').click(fdc_registration_submit);
}

function toggle_advanced_options(){
	jQuery('#fdc_advanced_options')
		.slideToggle(200, function(){
			jQuery('#fdc_more_options').toggleClass('expanded');
		});
}

function toggle_zipcode_field(){
	if(jQuery('#fdc_state_selector').val() == '-1'){
		jQuery('#state_required_options').slideUp(200, function(){
			jQuery('#fdc_zip_input').attr('disabled', true);
			jQuery('#fdc_city_input').attr('disabled', true);
			jQuery('#fdc_county_selector').attr('disabled', true);
		});
	} else {
		jQuery('#state_required_options').slideDown(200, function(){
			jQuery('#fdc_zip_input').attr('disabled', false);
			jQuery('#fdc_city_input').attr('disabled', false);
			jQuery('#fdc_county_selector').attr('disabled', false);
		});
	}
}

function resetCounties(elem){
	while((op = (elem.length - 1)) > 0){
		elem.remove(op);
	} 
}

function getCounties(baseurl){
	state = jQuery('#fdc_state_selector').val();
	countyElem = document.getElementById('fdc_county_selector');
	
	resetCounties(countyElem);
	
	if(state == '-1'){
		countyElem.options[0] = new Option('', '');
		return;
	} else {
		countyElem.options[0] = new Option('Getting Counties...', '');
		countyElem.setAttribute('disabled', true);
	}
	
	var url = baseurl + '/wp-content/plugins/foreclosure/ajax/get_counties.php';
	var parms = 'state=' + state;
	
	jQuery.get(url, parms, function(xml){ handleCountyResponse(xml); }, 'xml');
}

function handleCountyResponse(countyXml){
	countyList = countyXml.getElementsByTagName('county');
	var countyElem = document.getElementById('fdc_county_selector');
	resetCounties(countyElem);
	jQuery(countyElem).attr('disabled', false);
	
	countyElem.options[0] = new Option('Select a County', '000');
	for(var i=0; i < countyList.length; i++){
		fips = countyList[i].getAttribute('fips');
		county = countyList[i].firstChild.nodeValue;
		countyElem.options[i+1] = new Option(county, fips);
	}
}

function fdc_search_validate(formElem){
	var countyStr = jQuery('#fdc_county_selector').val();
	var stateStr = jQuery('#fdc_state_selector').val();
	var cityStr = jQuery('#fdc_city_input').val();
	var zipStr = jQuery('#fdc_zip_input').val();
	
	var hasError = false;
	if(countyStr == '000'){
		if((jQuery.trim(zipStr) == '') && ( jQuery.trim(cityStr) == '') ){
			jQuery('#fdc_search_error').html('Please enter a county, city or zipcode.').slideDown(200);
			hasError = true;
		}	
	}
	if(stateStr == '-1'){
		jQuery('#fdc_search_error').html('Please select a state.').slideDown(200);
		hasError = true;	
	}
	return !hasError;
}

/* registration capturing functions */
function fdc_get_details(e){
	var element = jQuery(this);
			
	if(Get_Cookie('fdc_reg') == null){
		fdc_show_registration_form(element);
		e.preventDefault();
	} else {
		// submit link href to details link tracker
		var url = siteurl + '/wp-content/plugins/foreclosure/ajax/track_details_link.php';
		
		var registrationOnly = jQuery('#registration_page_only').val();

		var linkHref = ''; 
		if(registrationOnly){
			var cobrandSubdomain = jQuery('#cobrand_subdomain').val();
			linkHref = 'https://' + cobrandSubdomain + '.foreclosure.com/registration.html';
		} else {
			linkHref = element.attr('href');
		}
		var parms = 'dest=' + escape(linkHref);
		jQuery.get(url, parms, function(response){
			document.location.href = linkHref;
		}, 'json');

		e.preventDefault();
	}
}

function fdc_registration_cancel(){
	tb_remove();
	jQuery('#fdc_registration_div').hide();
	jQuery('#fdc_registration_error').hide();
}

function fdc_show_registration_form(element){
	jQuery('#caller_link_destination').val(element.attr('href'));
	tb_show("Register", "#TB_inline?height=200&width=300&inlineId=fdc_registration_div&modal=true", false);
	return false;
}

function fdc_registration_submit(e){
	var regForm = document.getElementById('fdc_registration_form');
	if( !fdc_registration_validate(regForm)){
		e.preventDefault();
		return false;
	}
	jQuery('#fdc_registration_form').hide();
	
	var formAction = jQuery(regForm).attr('action');
	var formData = jQuery(regForm).serialize();
	
	jQuery.ajax({
		url: formAction,
		async: false, 
		data: formData, 
		dataType: 'json', 
		success: function(responseJson){
			if(responseJson.result){
				Set_Cookie('fdc_reg', "true", 30, '/');
			}
		}
	});
	
	var cobrandSubdomain = jQuery('#cobrand_subdomain').val();
	var registrationPage = 'https://' + cobrandSubdomain + '.foreclosure.com/registration.html';
	regForm.action = registrationPage;
}

function fdc_registration_validate(formElem){
	var fname = formElem.firstName.value;
	var lname = formElem.lastName.value;
	var email = formElem.email.value;
	var phone = formElem.phone.value;
	
	jQuery(formElem.firstName).removeClass('hasError');
	jQuery(formElem.lastName).removeClass('hasError');
	jQuery(formElem.email).removeClass('hasError');
	jQuery(formElem.phone).removeClass('hasError');
	
	var error = false;
	if(jQuery.trim(fname) == ''){
		error = true;
		jQuery(formElem.firstName).addClass('hasError');
	}
	if(jQuery.trim(lname) == ''){
		error = true;
		jQuery(formElem.lastName).addClass('hasError');
	}
	if(jQuery.trim(email) == ''){
		error = true;
		jQuery(formElem.email).addClass('hasError');
	}
	if(jQuery.trim(phone) == ''){
		error = true;
		jQuery(formElem.phone).addClass('hasError');
	}
	
	if(error){
		return false;
	}
	
	return true;
}

function fdc_toggle_preview(previewId, mapSrc)
{
	if (document.getElementById("preview_"+previewId).style.display == 'none'){	
		//document.getElementById("preview_"+previewId).style.display = 'block';
		jQuery("#preview_" + previewId).slideDown(200);
		
		if(mapSrc == 'google'){
			var preview_map_img = jQuery("#map_"+previewId);
			// load the google map image if its there
			if(preview_map_img.attr('src') == ''){
				var newsrc = preview_map_img.attr('tmpsrc');
				preview_map_img.attr('src', newsrc);
			}
		} else {
			if(document.getElementById('fdc_ve_map_'+previewId)){
				var mapEl = jQuery('#fdc_ve_map_' +previewId);
				var lat = mapEl.attr('lat');
				var lng = mapEl.attr('lng');
				var LA = new VELatLong(lat, lng);
				var map = new VEMap('fdc_ve_map_' +previewId);
				var pushpinimg = siteurl + '/wp-content/plugins/foreclosure/css/img/icon_point_orange.gif';
				var pin = new VEPushpin(previewId, LA, pushpinimg);
				map.LoadMap(LA, 15, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);
				map.AddPushpin(pin);
				map.HideDashboard();
				map.HideScalebar();
			}			
		}
		
		
		document.getElementById("arrow_"+previewId).className = 'details_arrow_down';		
	} else {
		//document.getElementById("preview_"+previewId).style.display = 'none';
		jQuery("#preview_" + previewId).slideUp(200);
		document.getElementById("arrow_"+previewId).className = 'details_arrow_right';
	}
}
