$Y.use("zillow-region-selection");(function(){this.WidgetCodeUtils={cacheWrappedCodeContainers:function(){var a=$$(".pre-container","widget-pres");
this.codeContainer=[];a.forEach(function(b){var c=$$("textarea",b);if(c&&c.length&&c.length>0){c=c[0];
c.id=b.getAttribute("value");this.codeContainer.push(c);}},this);},cacheCodeContainers:function(){this.codeContainer=$$("textarea","widget-pres");
},cobrandScid:function(){if(this.screenname&&this.cobrandEnabled){this.widgetCode=this.widgetCode.replace(/(scid=[\w\-]+)/g,"$1&scrnnm="+this.screenname);
}},cobrandExisting:function(){if(this.screenname&&!(this.cobrandEnabled)){this.widgetCode=this.widgetCode.replace(/(\?|&)(scrnnm|cobrand)=[\w\-]*/g,"");
}},hookRegionUpdate:function(a){log("WidgetCodeUtils.hookRegionUpdate",a,a.instance,this);a.instance.onChange.subscribe(WidgetCodeUtils.onRegionUpdate,this);
},onRegionUpdate:function(c,j){log("WidgetCodeUtils.onRegionUpdate",j,this);var g=/(\/local-info\/?[^\?#]*)/;
var i=(c==="secondary");if(!i){var a=j[0];var e=j[2];var f=j[3].replace(/\/$/,"");var b=j[4].match(g)[0].replace(/\/$/,"");
this.regionId=j[1];var h=new RegExp("(\\/)?\\-home\\-value");switch(e){case 4:a+=" County";case 1:if(a=="US"){a="National";
}case 2:b=b.replace(h,"");break;}log("homesURL: ",f);log("localURL: ",b);this.regionName=j[5].replace(/,[ ]*/g,"+").replace(/[ ]+/g,"+");
}var d={};d[(i?"sid":"rid")]=j[1];this.codeContainer.forEach(function(l){var k=l.value;if(!i){k=(l.value).replace(/(<span class=['"]region['"]>)[^<]+(<\/span>)/g,"$1"+a+"$2").replace(/\/homes\/(for_sale\/?[^/]*)/g,f).replace(g,b);
}l.value=this.updateWidgetParams(k,d);this.onRegionUpdate.fireEvent(l,j);},this);this.selectWidget(true);
},updateWidgetPres:function(a){this.codeContainer.forEach(function(b){this.onAllPresUpdate.fireEvent(b);
},this);if(a!==undefined&&a!==null&&a===false){this.selectWidget();}else{this.selectWidget(true);}}};
})();WidgetCodeUtils.set={mortgage:{onGenerate:"cobrandExisting"},wisdomofcrowd:{onGenerate:"cobrandScid"},regional:{onInit:"cacheCodeContainers",onGenerate:"cobrandScid"}};
(function(){var a=null;var f="http://"+window.location.host+"/";var c={screenNameId:"screen-name",codeTargetId:"generated-code",widgetFormatSelectId:"widget-format-select",widgetPreviewBlockId:"widget-block"};
$Y.on("domready",function(){a=$("cobrand-checkbox");if(a){$E.on(a,"click",function(g){Subscriber.fireEvent("Cobrand.toggle",$E.getTarget(g).checked);
});}});function d(h){var g=$E.getTarget(h);log(this+" selectWidgetHandler",g);if(this.widgetFormatSelect&&g.id==this.widgetFormatSelect.id){this.selectWidget();
}else{this.selectWidget(true);}}var e=function(h){log("[WidgetCodeGenerator "+this.id+"] initializing");
this.widgetDisplayBlock=$(h.widgetPreviewBlockId);this.codeTarget=$(h.codeTargetId);$E.on(this.codeTarget,"click",function(i){this.select();
});if($(h.screenNameId)){this.screenname=$(h.screenNameId).value;}if(a){this.cobrandEnabled=a.checked;
Subscriber.on("Cobrand.toggle",this.setCobrandState,this);}this.selects=[];var g=$$("select[prefixorder]","configuration-section");
if(g){g.forEach(function(i){var j=i.getAttribute("prefixorder");this.selects[j-1]=i;$E.on(i,"change",d,this,true);
if(i.id==h.widgetFormatSelectId){this.widgetFormatSelect=i;}},this);}this.onInit.fireEvent(h);this.selectWidget(true);
};function b(k,h,g){var j=Object.extend({onInit:null,onShow:null,onGenerate:null,onUpdate:this.updateCodeBlock,onSelect:null,onAllPresUpdate:null,onRegionUpdate:null},h||{});
this.id=k;for(var i in j){this[i]=new Subscriber("[WidgetCodeGenerator "+this.id+"]."+i);if(j[i]){this[i].subscribe((typeof j[i]=="string")?WidgetCodeUtils[j[i]]:j[i],this);
}}$Y.on("domready",e,this,$Y.merge(c,g));}b.prototype={constructor:b.prototype.constructor,toString:function(){return"[WidgetCodeGenerator "+this.id+"]";
},setCobrandState:function(g){this.cobrandEnabled=g;this.updateCodeArea();},regexClean:function(g){this.widgetCode=(g||this.widgetCode).replace(/\s+/g," ").replace(/((?:href|src)=")\/([^"]*")/g,"$1"+f+"$2").replace(/(url\()\s*\/([^\)]*\))/g,"$1"+f+"$2").trim()||"";
},generateCode:function(){this.widgetCode="";if(this.selectedWidget){this.widgetCode=this.selectedWidget.value;
this.regexClean();this.onGenerate.fireEvent();}return this.widgetCode;},selectWidget:function(m){var g=this.onSelect.fireEvent();
var l="";if(g){var i=this.id;l=i;for(var k=0;k<this.selects.length;k++){var h=this.selects[k];var j=h.options[h.selectedIndex].value;
if(this.widgetFormatSelect&&this.widgetFormatSelect.id==h.id){l+="-iframe";}else{l+="-"+j;}i+="-"+j;}this.selectedWidget=$(i+"-pre");
}this.updateCodeArea();if(m){if(this.widgetFormatSelect){this.onUpdate.fireEvent($(l+"-pre"));}else{this.onUpdate.fireEvent();
}}},updateCodeArea:function(){this.codeTarget.value=this.generateCode();this.onShow.fireEvent();},updateCodeBlock:function(g){Element.setHTML(this.widgetDisplayBlock,(g||this.codeTarget).value);
},updateWidgetParams:function(g,k){var h=/src="([^"]+)"/;if(!h.test(g)){return g;}var j=h.exec(g)[1].split("?");
var i=Object.fromURLString(j[1]);if(i){i=Object.extend(i,k);j[1]=Object.toURLString(i,null,true);}return g.replace(h,'src="'+j.join("?")+'"');
}};window.WidgetCodeGenerator=b;})();
