(function(b){var a={cities:{},id:null,language:{},loading:'<div class="altos_loading"></div>',retries:0,sizes:[],url:window.location.href,zips:{},initialize:function(){if(b(".altos_container").length>0){a.getSettings()}},getSettings:function(){var f={query:"altos_get_settings",small:0,large:0};var c={zips:[],cities:[]};var d,e;b(".altos_form").each(function(){d=b(this).find("input[name='altos_zips']").val();e=b(this).find("input[name='altos_cities']").val();if(d){d=d.split(",");b(d).each(function(g,h){if(b.inArray(h,c.zips)<0){c.zips.push(h)}});f.zips=c.zips.join(",")}if(e){e=e.split(",");b(e).each(function(g,h){if(b.inArray(h,c.cities)<0){c.cities.push(h)}});f.cities=c.cities.join(",")}});if(b(".altos_widget_small").length>0){f.small=1}if(b(".altos_widget_large").length>0){f.large=1}b.post(a.url,f,function(g){a.id=g.id;a.language=g.language;a.cities=g.cities;a.zips=g.zips;a.sizes=g.sizes;a.hook();b(".altos_container").each(function(){if(b(this).hasClass("altos_widget_small")){a.updateSmallWidget(b(this).find("form").attr("name"))}else{if(b(this).hasClass("altos_widget_large")){a.updateLargeWidget(b(this).find("form").attr("name"))}}})},"json")},hook:function(){if(b(".altos_widget_small").length>0){if(b("select[name='altos_areas_select']").length>0){b("select[name='altos_areas_select']").each(function(){b(this).bind("change",function(){a.updateSmallWidget(b(this).parents("form").attr("name"))})})}}if(b(".altos_widget_large").length>0){b("input.altos_control:not([name=altos_areas])").each(function(){b(this).bind("click",function(){a.updateLargeWidget(b(this).parents("form").attr("name"))})});b("input[name=altos_areas]").each(function(){b(this).bind("click",function(){var c="form#"+b(this).parents("form").attr("name");if(b(c+" input[name=altos_areas]:checked").length<1){b(c+" .altos_areas_all").attr("checked","checked")}else{if(b(this).hasClass("altos_areas_all")){b(c+" input[name=altos_areas]:checked:not('.altos_areas_all')").each(function(){if(!b(this).hasClass("altos_areas_all")){b(this).attr("checked","")}else{b(this).attr("checked","checked")}})}else{b(c+" .altos_areas_all").attr("checked","")}}a.updateLargeWidget(b(this).parents("form").attr("name"))})});b(".altos_widget_large").each(function(){b(this).find(".altos_statistic:first").addClass("altos_statistic_selected")});b(".altos_statistic").each(function(){b(this).bind("click",function(){var c="form#"+b(this).parents("form").attr("name");b(c+" .altos_statistic").removeClass("altos_statistic_selected");b(this).addClass("altos_statistic_selected");a.updateLargeWidget(b(this).parents("form").attr("name"))})})}},unhook:function(c){if(typeof(c)=="undefined"){c=""}else{c="form#"+c+" "}if(b(c+".altos_widget_small").length>0){b(c+"select[name='altos_areas_select']").each(function(){b(this).unbind("change")})}if(b(c+".altos_widget_large").length>0){b(c+".altos_control").each(function(d){b(this).unbind("click")});b(c+"input[name=altos_areas]").each(function(d){b(this).unbind("mousedown")});b(c+".altos_areas_all").each(function(){b(this).unbind("click")});b(c+".altos_statistic").each(function(){b(this).unbind("click")})}},parseID:function(c){if(typeof c=="undefined"){return false}return c.replace(/altos_widget_/g,"")},loadChart:function(e,d){if(typeof(e)=="undefined"){return false}b("form#"+e+" .altos_chart").html("").html(a.loading);var c=new Image();c.name=e;c.onload=function(){var g=b("form#"+e+" .altos_statistic_selected").attr("stat");var f="<h2>"+a.language[g]+"</h2>";if(a.language[g+"_desc"]){f+="<p>"+a.language[g+"_desc"]+"</p>"}f+='<img src="'+d+'" />';b("form#"+e+" .altos_chart").html("").html(f)};c.onerror=function(){a.unhook();b("form#"+c.name+".altos_container").html("<h1>Altos Research Plugin Error</h1>"+c.src+'<p style="text-align:center">Altos Research is currently in maintenance mode and should be back online shortly.  Thank you for your patience!</p>')};c.src=d},getArea:function(c,d){if(typeof(d)=="undefined"){return{}}var e={};if(c=="zips"){if(typeof(a.zips[d])!="undefined"){e=a.zips[d]}}else{if(typeof(a.cities[d])!="undefined"){e=a.cities[d]}}return e},imageURL:function(d){d=d||{};if(typeof d.state=="undefined"){d.state=""}if(typeof d.city=="undefined"){d.city=""}if(typeof d.zip=="undefined"){d.zip="a"}d.state=d.state.toUpperCase();d.city=d.city.toUpperCase();if(typeof d.stat=="undefined"){d.stat=b(".altos_statistic").attr("stat")}if(typeof d.rollavg=="undefined"){d.rollavg="a"}if(typeof d.quartile=="undefined"){d.quartile="a"}if(typeof d.size=="undefined"){d.size="t"}if(typeof d.timespan=="undefined"){d.timespan="e"}if(typeof d.type=="undefined"){d.type="sf"}var c="http://charts.altosresearch.com/altos/app";c+="?st="+d.state+"&c="+d.city+"&z="+d.zip;c+="&s="+d.stat+"&ra="+d.rollavg+"&q="+d.quartile;c+="&sz="+d.size+"&ts="+d.timespan+"&rt="+d.type;c+="&service=chart&pai="+a.id;return c},getChartConfig:function(d){var h=d;if(typeof(d)=="undefined"){d=""}else{d="form#"+d+" "}var c={state:"",city:"",zip:"",stat:"",quartile:"",timespan:"",type:""};var f,e,k,p,n,k;var m=false;var j=b(d+"input[name='altos_selector']").val();var g={zips:[],cities:[],cities_url:[],states:[]};var l=0;c.stat=b(d+" .altos_statistic_selected").attr("stat");if(b(d+"input[name='altos_timeframes']").attr("type")=="hidden"){c.timespan=b(d+"input[name='altos_timeframes']").val()}else{c.timespan=b(d+"input[name='altos_timeframes']:checked").val()}if(b(d+"input[name='altos_prices']").attr("type")=="hidden"){c.quartile=b(d+"input[name='altos_prices']").val()}else{c.quartile=b(d+"input[name='altos_prices']:checked").val()}if(b(d+"input[name='altos_types']").attr("type")=="hidden"){c.type=b(d+"input[name='altos_types']").val()}else{c.type=b(d+"input[name='altos_types']:checked").val()}if(j=="zips"){if(b(d+"input[name='altos_zips']").val()==""){return false}}else{if(b(d+"input[name='altos_cities']").val()==""){return false}}var o=false;if(b(d+".altos_areas_all").attr("type")=="checkbox"){o=b(d+".altos_areas_all").attr("checked")}else{if(b(d+".altos_areas_all").attr("type")=="hidden"){o=true}}if(o){c.zip="a"}if(b(d+".altos_areas_all").attr("type")=="checkbox"&&!o){b(d+"input[name='altos_areas']:checked").each(function(){var i=b(this).val();e=a.getArea(j,i);if(e){if(j=="zips"){g.zips.push(e.zip)}else{g.zips.push("a")}g.cities.push(e.city);g.cities_url.push(e.city_url);g.states.push(e.state);l++}})}if(o){g.zips=[];g.cities=[];g.cities_url=[];g.states=[];if(j=="zips"){p=b(d+"input[name='altos_zips']").val();p=p.split(",");k=[];b(p).each(function(q,r){e=a.getArea(j,r);if(b.inArray(e.city_url,k)<0){g.cities.push(e.city);g.cities_url.push(e.city_url);g.states.push(e.state);g.zips.push(e.zip)}});k=null}else{n=b(d+"input[name='altos_cities']").val();n=n.split(",");b(n).each(function(q,r){e=a.getArea(j,r);g.cities.push(e.city);g.cities_url.push(e.city_url);g.states.push(e.state);g.zips.push("a")})}}c.state=g.states.join(",");c.city=g.cities.join(",");c.zip=g.zips.join(",");return c},updateStatImages:function(d){var c=a.getChartConfig(d);if(typeof(d)=="undefined"){d=""}else{d="form#"+d+" "}b(".altos_statistic").each(function(){var e=b(this).attr("stat");if(typeof e!="undefined"){c.stat=e;c.size=a.sizes.statistics;b(this).attr("src",a.imageURL(c))}})},updateSmallWidget:function(f){if(typeof(f)=="undefined"){f=""}else{f="form#"+f+" "}if(b(f+"select[name='altos_areas_select']").length<1){return false}var c=b(f+"input[name='altos_selector']").val();var d=b(f+"select[name='altos_areas_select']").val();var e=a.getArea(c,d);if(e.length<1){return false}b(f+".altos_median_price").text(e.median_price);b(f+".altos_mean").text(e.mean);b(f+".altos_inventory").text(e.inventory);b(f+".altos_median_price_per_sqft").text(e.median_price_per_sqft);b(f+".altos_median_sqft").text(e.median_sqft);b(f+".altos_median_lot").text(e.median_lot);b(f+".altos_mean_beds").text(e.mean_beds);b(f+".altos_median_baths").text(e.median_baths);b(f+".altos_median_listings_sold").text(e.median_listings_sold);b(f+".altos_median_new_listings").text(e.median_new_listings);b(f+".altos_mean_dom").text(e.mean_dom);b(f+".altos_median_age").text(e.median_age);b(f+".altos_area_city").text(e.city+", "+e.state);b(f+".altos_area_zip").text(e.zip)},updateLargeWidget:function(f){if(typeof(a.id)=="undefined"&&a.retries<10){a.retries++;a.getSettings();return false}else{if(typeof(a.id)=="undefined"&&a.retries>=10){a.unhook();b(".altos_container").html('<h1>Altos Research Plugin Error</h1><p style="text-align:center">The settings for this widget could not be retrieved!  Please notify the site owner.</p>')}}var e=a.getChartConfig(f);if(e==false){b(f+".altos_container").html('<h1>Altos Research Plugin Error</h1><p style="text-align:center">No market areas could be found for this widget. Please notify the site owner.</p>');return}var d=f;if(typeof(f)=="undefined"){f=""}else{f="form#"+f+" "}var c=b(f+".altos_chart img").css("height");b(f+".altos_chart").css("min-height",c);if(b.browser.msie){b(f+".altos_chart").css("height",c)}if(e.zip.length<1&&areas_all){a.unhook(d);b(f+".altos_container").html('<h1>Altos Research Plugin Error</h1><p style="text-align:center">No market areas could be found for this widget. Please notify the site owner.</p>')}else{if(e.zip.length<1&&!areas_all){if(b(f+".altos_areas_all").attr("type")=="checkbox"){b(f+".altos_areas_all").attr("checked","checked")}a.updateLargeWidget(d)}else{a.updateStatImages(d);e.size=a.sizes.large;a.loadChart(d,a.imageURL(e))}}}};b(document).ready(function(){a.initialize()})})(jQuery);