var utils={};
utils.cookie=(function(){return{get:function(F){if(!F){return F
}var E="",C=document.cookie,D=C.indexOf(F+"="),B,A;
if(D>-1){B=D+F.length;
A=C.indexOf(";",B);
E=(A==-1)?C.substring(B+1):C.substring(B+1,A)
}return E
},set:function(G,H,A){if(!G.length){return false
}var E=new Date(),C,B=G,D=H||"",F=A.domain||"",I=A.path||"/";
E.setTime(E.getTime()+((A.expires||100)*24*60*60*1000));
C=E.toGMTString();
document.cookie=B+"="+D+"; expires="+C+"; domain="+F+"; path="+I
},remove:function(E,B){var A=E,C=B.domain||"",D=B.path||"/";
document.cookie=A+'=""; expires=Thu, 01-Jan-1970 00:00:01 GMT; domain='+C+"; path="+D
}}
}());
utils.Postcoder=(function(){var E=glow.dom.get,A={PST:"postcode",QRY:"result > name",LEA:"lea id",TVR:"bbctv id",WEA:"weather id",CCI:"county_council id",CON:"constituency id",CTY:"county id",DST:"district",EUR:"euro_region id",LAU:"local_authority id",LST:"listings id",NHS:"health_authority id",TWN:"post_town id",RAD:"radio id",WRD:"ward id",WIL:"wil id",MAT:"match_type"};
function B(G){var F=new glow.dom.NodeList();
switch(G.get("result").length){case 0:F=glow.dom.create("<p>No results found, please try again.</p>");
break;
case 1:if(G.get("wil").length>1){F=D.call(this,G,"wil")
}break;
default:F=D.call(this,G,"result")
}return F
}function D(L,H){var G=glow.dom.create("<ul></ul>"),K=this,F,J,I,M;
L.get(H).each(function(){F=E(this).get("name").text();
if(H=="result"){F+=", "+E(this).get("hint").text()
}J=glow.dom.create('<li><a href="#">'+F+"</a></li>");
glow.events.addListener(J,"click",function(){I={};
if(this.nodeName=="wil"){I={wil:E(this).get("id").text()};
M=E(this).parent().get("id").text()
}else{M=E(this).get("id").text()
}utils.Postcoder.call(K,M,I);
return false
},this);
G.append(J)
});
return G
}function C(H,K){var G=K.wil||0,J=this,I,F,L;
K=K||{};
if(K.panel){this.panel=K.panel
}if(K.panelOpts){this.panelOpts=K.panelOpts
}I=this.makeRequest(H);
if(G){I.get("wil").each(function(){if(E(this).get("id").text()!=G){E(this).remove()
}})
}F=B.call(this,I);
if(K.onDisambiguation){glow.events.addListener(J,"disambiguation",K.onDisambiguation)
}if(K.onSuccess){glow.events.addListener(J,"success",K.onSuccess)
}if(F.length){glow.events.fire(J,"disambiguation",F)
}else{glow.events.fire(J,"success");
if(this.panel&&this.panel.length){onConfirm=function(){J.buildCookie(I);
if((typeof K.onConfirm)=="function"){if(!K.onConfirm()){window.location.reload(true)
}}else{window.location.reload(true)
}};
L={location:I.get("result > name").text()};
panelNode=glow.dom.create(glow.lang.interpolate(this.panel,L));
panelNode.appendTo(E(document.body));
this.confirmation=new utils.widgets.Dialog(panelNode,this.panelOpts);
glow.events.addListener(this.confirmation,"confirm",onConfirm);
this.confirmation.show()
}else{this.buildCookie(I)
}}}C.prototype={makeRequest:function(F){var H=new glow.dom.NodeList(),G="/tv/guide/postcoder/"+F;
glow.net.get(G,{onLoad:function(I){H=E(I.xml())
},error:function(I){alert("error: "+I.statusText())
},async:false});
return H
},buildCookie:function(H){var G="",F,I;
for(F in A){I="";
H.get(A[F]).each(function(J){if(J){I+="."
}I+=E(this).text()
});
G+=F+I+":"
}utils.cookie.set("BBCpostcoder",G,{domain:"bbc.co.uk",expires:100})
}};
return C
}());
utils.widgets={};
utils.widgets.Dialog=(function(){var F=glow.dom,D=F.get,B=glow.events,C=glow.widgets,E=glow.lang;
function A(K,J){J=J||{};
var I=false,H=this,G=D(K).append(F.create('<p class="ft"><a class="glow-dialog-cancel button" href="#"><span>Cancel</span></a><a class="glow-dialog-confirm button" href="#"><span>Confirm</span></a></p>'));
if(J.template){I=true
}J=E.apply({modal:false,theme:"dark"},J);
B.addListener(G.get(".glow-dialog-cancel"),"click",function(){H.hide();
return false
});
B.addListener(G.get(".glow-dialog-confirm"),"click",function(){B.fire(H,"confirm");
H.hide();
return false
});
C.Panel.call(this,K,J);
if(!I){this.content.addClass("glow-dialog")
}}E.extend(A,C.Panel);
return A
}());
Localisation=function(){var F,B,E,A='<div class="hpPopup" id="hpPopup_Location"><h2 class="hd hpPopup">Location Change</h2><p>Do you really want to change your location to <strong>{location}</strong>?</p></div>',C='<div><h2 class="hd">Set your location</h2><p><label id="localisation_place_label" for="localistaion_place">Set location : </label><input type="text" id="localistaion_place" size="20" name="place" class="faded" onfocus="glow.dom.get(this).removeClass(\'faded\');glow.dom.get(this).val(\'\');" value="Enter UK Postcode or place name" /><a id="localisation_place_btn" href="#" onclick="Localisation.setLocation(glow.dom.get(\'#localistaion_place\').val());">Save</a></p><div id="location_results"></div></div>',D;
return{init:function(){if(glow.dom.get("#change_region").length<1){return 
}D=new glow.widgets.InfoPanel(glow.dom.create(C),{context:"#change_region",pointerPosition:"r",theme:"dark",modal:false});
glow.dom.get("#change_region").attr("href","javascript:Localisation.showPanel()");
B=glow.dom.get("#location_results");
glow.events.addListener("#localistaion_place","keypress",function(G){if(G.keyCode==13){Localisation.setLocation(glow.dom.get(this).val())
}})
},hidePanel:function(){D.hide()
},setLocation:function(G){if(G===""||glow.dom.get("#localistaion_place").hasClass("faded")){this.writeResults(glow.dom.create("<p>Please enter a location</p>"));
return false
}E=new utils.Postcoder(G,{onDisambiguation:function(H){Localisation.writeResults(H)
},onSuccess:function(){Localisation.hidePanel()
},panel:A,panelOpts:{width:"320px",theme:"dark",modal:true,closeOnMaskClick:false},onConfirm:function(){if(F){F();
return true
}return false
}})
},showPanel:function(){D.show()
},writeResults:function(G){B.empty();
B.append(G);
if(glow.dom.get(G).get("li").length>0){B.prepend("<p><strong>Multiple Locations Found</strong></p><p>Please refine your location by choosing a place name from the list.</p>")
}},setCallback:function(G){F=G
}}
}();
glow.onDomReady(function(){Localisation.init()
});
