$.ui.dialog.defaults.bgiframe=true;(function(){var G=window.Geo={di:null,_edit:null,dialogId:'#c-geo',dialogOpenLink:'.geoPopupLink',dialogElements:'#c-geo .region',dialogElementPrefix:'#c-geo',dialogConfig:{title:"Выбор географического охвата",autoOpen:false,width:560,minWidth:560,resizable:false,modal:true,closeByOverlayClick:true},list:'#regionsList',listElementPrefix:'#region',btn:'#regionsBtn',init:function(options){options=options||{};this.binding=options.binding||false;G.dialogConfig.title=options.title||G.dialogConfig.title;G.di=$(G.dialogId).dialog(G.dialogConfig);$(G.dialogOpenLink).click(function(event){event.preventDefault();G.open();});if(G.binding){$('#c-geo form.chosen').submit(function(event){event.preventDefault();G.submit(this.rgn.value);});}
G.region.init(options.region);G.search.init(options.search);$(G.list+' dd').hover(function(){$(this).css('border-color','#728a4d').find('span').show();},function(){$(this).css('border-color','#dce7b0').find('span').hide();});},open:function(){G._edit=null;G.search.clear();G.di.dialog('open');},close:function(){G.di.dialog('close')},click:function(id){G.region.get({rgn:id,exclude:G.chosen.getAllKey(G._edit).join(',')},G.region.draw);},submit:function(id){var tpl=TEMPLATE,title=[],rgn=G._chosen;G.chosen.push(id,null);for(var i=0,l=rgn.length;i<l;i++){title.push('<a href="/navi/?rgn='+rgn[i].id+'" target="_blank">'+rgn[i].name+'</a>')}
tpl=Template.tag(tpl,'#id#',id);tpl=Template.tag(tpl,'#title#',title.join(' / '));tpl=Template.tag(tpl,'#elementId#','region'+id);tpl=Template.tag(tpl,'#hiddenName#','regionId');tpl=Template.tag(tpl,'#editTitle#','Изменить регион');tpl=Template.tag(tpl,'#editOnclick#','Geo.edit('+id+'); return false;');tpl=Template.tag(tpl,'#removeOnclick#','Geo.remove('+id+'); return false;');if(G._edit){G.chosen.remove(G._edit);Template.replace(G.listElementPrefix+G._edit,tpl);}else{Template.insert(G.list,tpl);}
$('#region'+id).hover(function(){$(this).css('border-color','#728a4d').find('span').show();},function(){$(this).css('border-color','#dce7b0').find('span').hide();});if(G.islimit()){$(G.btn).hide();}
G.close();},remove:function(id){G.chosen.remove(id);Template.remove(G.listElementPrefix+id);if(!G.islimit()){$(G.btn).show();}
if(!G.chosen.length){}},edit:function(id){G._edit=id;G.search.clear();G.region.get({rgn:id,exclude:G.chosen.getAllKey(G._edit).join(',')},G.region.draw);G.di.dialog('open');},islimit:function(){return(G.chosen.length>=6);}}
var CLICK_TEMPLATE='Geo.click(#id#); return false;',BASE_TEMPLATE='<a href="#" onclick="'+CLICK_TEMPLATE+'">#title#</a>',BASE_BOLD_TEMPLATE='<a href="#" onclick="'+CLICK_TEMPLATE+'"><b>#title#</b></a>',COUNT_TEMPLATE='&nbsp;#count#',ITEM_TEMPLATE='<dd>#item#</dd>',REGIONS_TITLE='Выбор региона по справочнику',NO_FIND_TITLE='К сожалению, в найденных по Вашему запросу регионах ресурсов не обнаружено',ABRAKADABRA_TITLE='По Вашему запросу ничего не найдено',NO_FIND_TEXT='<dd><p style="font-size: 1.4em">Если вы указали название населенного пункта, попробуйте указать название области или страны, или воспользуйтесь <a href="#" onclick="Geo.search.clear(); return false;" style="font-size: 1em;">поиском по рубрикатору</a></p></dd>',FIND_TITLE='Найдено регионов: #n# <a href="#" onclick="Geo.search.clear(); return false;" class="dashed">Очистить результаты поиска</a>',NO_CHOSEN='Кликните по ссылке в верхней части окна для выбора нужного региона. Подвердите выбор кнопкой &laquo;Выбрать&raquo;';G.region={init:function(options){options=options||{};this.url=options.url||'';this.data=options.data||{};},get:function(data,fn){for(var key in this.data){data[key]=this.data[key];}
if(G.region.cache.isCached(serialize(data))){fn(G.region.cache.get(serialize(data)));return;}
var loader=setTimeout(function(){$('#c-geo').prev().addClass('loader3').css({backgroundPosition:'255px center'});},80);$.ajax({url:this.url,type:"get",data:data,dataType:"json",success:function(json){G.region.cache.push(serialize(data),json)
fn(json);clearTimeout(loader);$('#c-geo').prev().removeClass('loader3');}});},draw:function(json){$('#c-geo dl.search .title').html(REGIONS_TITLE);if(json.regions){$('#c-geo dl.search .select').hide();$('#c-geo dl.search dd').remove();$('#c-geo dl.search').append(G.region.layout(json.cities,json.regions));$('#c-geo .chosen span').html(NO_CHOSEN);}else{$('#c-geo dl.search .select').html(G.region.layoutNav(json.chosen)).show();$('#c-geo dl.search dd').remove();$('#c-geo dl.search').append(G.region.layout(json.children));$('#c-geo .chosen span').html(G.region.layoutChosen(json.chosen));$('#c-geo form.chosen input[name=rgn]').val(json.chosen[json.chosen.length-1].id);}
$('#c-geo form.chosen input[type=submit]').attr('disabled',(json.can?'':'disabled'))},layout:function(cities,regions){var cities=cities||[],regions=regions||[],ctemp=[],rtemp='',temp,atemp=[];for(var i=0,l=cities.length;i<l;i++){temp=Template.tag(BASE_TEMPLATE,'#id#',cities[i].id);temp=Template.tag(temp,'#title#',cities[i].name);if(cities[i].count){temp+=Template.tag(COUNT_TEMPLATE,'#count#',cities[i].count);}
ctemp.push(temp);}
ctemp=ctemp.length?Template.tag(ITEM_TEMPLATE,'#item#',ctemp.join(', &nbsp; ')):'';for(var i=0,l=regions.length;i<l;i++){temp=Template.tag(BASE_BOLD_TEMPLATE,'#id#',regions[i].id);temp=Template.tag(temp,'#title#',regions[i].name);if(regions[i].count){temp+=Template.tag(COUNT_TEMPLATE,'#count#',regions[i].count);}
temp+=': &nbsp; ';for(var j=0,len=regions[i].children.length;j<len;j++){temp+=Template.tag(BASE_TEMPLATE,'#id#',regions[i].children[j].id);temp=Template.tag(temp,'#title#',regions[i].children[j].name);if(regions[i].children[j].count){temp+=Template.tag(COUNT_TEMPLATE,'#count#',regions[i].children[j].count);}
atemp.push(temp);temp='';}
rtemp+=atemp.length?Template.tag(ITEM_TEMPLATE,'#item#',atemp.join(', &nbsp; ')):'';atemp=[];}
return ctemp+rtemp;},layoutChosen:function(arr){var atemp=[];G._chosen=arr;for(var i=0,l=arr.length;i<l;i++){atemp.push(arr[i].name);}
atemp[atemp.length-1]='<b>'+atemp[atemp.length-1]+'</b>';return atemp.join(' / ');},layoutNav:function(arr){var atemp=[],temp;temp=Template.tag(BASE_TEMPLATE,'#id#',0);temp=Template.tag(temp,'#title#','Все');atemp.push(temp);for(var i=0,l=arr.length;i<l;i++){temp=Template.tag(BASE_TEMPLATE,'#id#',arr[i].id);temp=Template.tag(temp,'#title#',arr[i].name);atemp.push(temp);}
atemp[atemp.length-1]=$(atemp[atemp.length-1]).text();return atemp.join(' / ');}}
G.search={init:function(options){options=options||{};this.url=options.url||'';this.min=options.min||2;this.data=options.data||{};$("#c-geo .srch").submit(function(event){event.preventDefault();G.search.get({word:this.word.value,exclude:G.chosen.getAllKey(G._edit).join(',')},G.search.draw);});$("#c-geo .srch input[type=text]").keyup(function(event){$('#c-geo .srch input[type=submit]').attr('disabled',(this.value.length<Geo.search.min?'disabled':''));})},get:function(data,fn){for(var key in this.data){data[key]=this.data[key];}
if(G.search.cache.isCached(serialize(data))){fn(G.search.cache.get(serialize(data)));return;}
var loader=setTimeout(function(){$('#c-geo').prev().addClass('loader3').css({backgroundPosition:'255px center'});},80);$.ajax({url:this.url,type:"get",data:data,dataType:"json",success:function(json){G.search.cache.push(serialize(data),json);fn(json);clearTimeout(loader)
$('#c-geo').prev().removeClass('loader3');}});},draw:function(json){$('#c-geo dl.search dd').remove();$('#c-geo dl.search .select').hide();if(json.regions&&json.regions.length&&json.found){$('#c-geo dl.search .title').html(Template.tag(FIND_TITLE,'#n#',json.regions.length));$('#c-geo dl.search').append(G.search.layout(json.regions));}
else{$('#c-geo dl.search .title').html(json.found?NO_FIND_TITLE:ABRAKADABRA_TITLE);$('#c-geo dl.search').append(NO_FIND_TEXT);}},layout:function(regions){var atemp,temp,rtemp='';for(var i=0,l=regions.length;i<l;i++){atemp=[];for(var j=0,len=regions[i].length;j<len;j++){temp=Template.tag(BASE_TEMPLATE,'#id#',regions[i][j].id);temp=Template.tag(temp,'#title#',regions[i][j].name);atemp.push(temp);}
rtemp+=atemp.length?Template.tag(ITEM_TEMPLATE,'#item#',atemp.join(' / ')):'';}
return rtemp;},clear:function(){$('#c-geo .srch input[type=text]').val('');$('#c-geo .srch input[type=submit]').attr('disabled','disabled');G.click(0);}}
function serialize(a){var s=[];if(a.constructor==Array){for(var i=0;i<a.length;i++)
s.push(a[i].name+"="+encodeURIComponent(a[i].value));}else{for(var j in a)
s.push(j+"="+encodeURIComponent(a[j]));}
return s.join("&");}
var Chosen={_db:{},length:0,push:function(key,data){this._db[key]=data;this.length++;},get:function(key){return this._db[key];},getAll:function(){return this._db;},getAllKey:function(exclude){var temp=[],exclude=exclude||null;for(var key in this._db){if(key!=exclude)temp.push(key);}
return temp;},remove:function(key){delete this._db[key];this.length--;}}
var Cache={_db:{},length:0,push:function(key,data){this._db[key]=data;this.length++;},get:function(key){return this._db[key];},remove:function(key){delete this._db[key];this.length--;},isCached:function(key){return(key in this._db)}}
var Template={remove:function(id){$(id).remove();},insert:function(to,template){$(to).append(template);},replace:function(old,newTemplate){$(old).replaceWith(newTemplate);},tag:function(template,tag,str){return template.replace(new RegExp(tag,'g'),str);}}
var TEMPLATE=''+'<dd id="#elementId#">'+'<input type="hidden" name="#hiddenName#" value="#id#" />'+'<span class="fr">'+'<a href="#" onclick="#editOnclick#" class="dashed">#editTitle#</a> '+'<a href="#" onclick="#removeOnclick#"><img src="/static/top100/images/ico/ico-close.gif" alt="" class="hlp" /></a>'+'</span>'+'#title#'+'</dd>';G.chosen=Chosen;G.region.cache=Cache;G.search.cache=Cache;})();$.ui.dialog.defaults.bgiframe=true;(function(){var T=window.Type={di:null,_edit:null,dialogId:'#c-types',dialogOpenLink:'.typePopupLink',dialogElements:'#c-types .type',dialogElementsHide:'#c-types .gtype',dialogElementPrefix:'#c-type',dialogElementPrefixHide:'#g-type',dialogConfig:{title:"Выбор типа ресурса ",autoOpen:false,width:440,minWidth:440,resizable:false,modal:true,closeByOverlayClick:true},list:'#typesList',listElementPrefix:'#type',btn:'#typesBtn',init:function(options){T.di=$(T.dialogId).dialog(T.dialogConfig);$(T.dialogOpenLink).click(function(event){event.preventDefault();T.open();});T.getDataFromHTML();$(T.list+' dd').hover(function(){$(this).css('border-color','#728a4d').find('span').show();},function(){$(this).css('border-color','#dce7b0').find('span').hide();});},getDataFromHTML:function(){var els=$(T.dialogElements),prefix=T.dialogElementPrefix.replace('#',''),a;for(var i=0,l=els.length;i<l;i++){a=els.eq(i).find('a');T.cache.push(els.eq(i).attr('id').replace(prefix,''),{title:a.text(),href:a.attr('href')});}},open:function(){T._edit=null;$(T.dialogElements).show();$(T.dialogElementsHide).hide();for(var key in T.chosen.getAll()){$(T.dialogElementPrefix+key).hide();$(T.dialogElementPrefixHide+key).show();}
T.di.dialog('open');},close:function(){T.di.dialog('close')},click:function(id){T.chosen.push(id,null);var tpl=TEMPLATE;tpl=Template.tag(tpl,'#id#',id);tpl=Template.tag(tpl,'#href#',Cache.get(id).href);tpl=Template.tag(tpl,'#title#',Cache.get(id).title);tpl=Template.tag(tpl,'#elementId#','type'+id);tpl=Template.tag(tpl,'#hiddenName#','typeId');tpl=Template.tag(tpl,'#editTitle#','Изменить тип');tpl=Template.tag(tpl,'#editOnclick#','Type.edit('+id+'); return false;');tpl=Template.tag(tpl,'#removeOnclick#','Type.remove('+id+'); return false;');if(T._edit){T.chosen.remove(T._edit);Template.replace(T.listElementPrefix+T._edit,tpl);}else{Template.insert(T.list,tpl);}
$('#type'+id).hover(function(){$(this).css('border-color','#728a4d').find('span').show();},function(){$(this).css('border-color','#dce7b0').find('span').hide();});if(T.islimit()){$(T.btn).hide();}
T.close();},remove:function(id){T.chosen.remove(id);Template.remove(T.listElementPrefix+id);if(!T.islimit()){$(T.btn).show();}
if(!T.chosen.length){}},edit:function(id){T._edit=id;$(T.dialogElements).show();$(T.dialogElementsHide).hide();for(var key in T.chosen.getAll()){$(T.dialogElementPrefix+key).hide();$(T.dialogElementPrefixHide+key).show();}
T.di.dialog('open');},islimit:function(){return(T.chosen.length>=3);}}
var Chosen={_db:{},length:0,push:function(key,data){this._db[key]=data;this.length++;},get:function(key){return this._db[key];},getAll:function(){return this._db;},remove:function(key){delete this._db[key];this.length--;}}
var Cache={_db:{},length:0,push:function(key,data){this._db[key]=data;this.length++;},get:function(key){return this._db[key];},remove:function(key){delete this._db[key];this.length--;}}
var Template={remove:function(id){$(id).remove();},insert:function(to,template){$(to).append(template);},replace:function(old,newTemplate){$(old).replaceWith(newTemplate);},tag:function(template,tag,str){return template.replace(new RegExp(tag,'g'),str);}}
var TEMPLATE=''+'<dd id="#elementId#">'+'<input type="hidden" name="#hiddenName#" value="#id#" />'+'<span class="fr">'+'<a href="#" onclick="#editOnclick#" class="dashed">#editTitle#</a> '+'<a href="#" onclick="#removeOnclick#"><img src="/static/top100/images/ico/ico-close.gif" alt="" class="hlp" /></a>'+'</span>'+'<a href="#href#" target="_blank">#title#</a>'+'</dd>';T.chosen=Chosen;T.cache=Cache;})();
