
var Demands=Class.create({initialize:function(){this.loadButtonClass='ajaxLoad';this.loadButtons=[];this.setLoadButtons();},setLoadButtons:function(){this.loadButtons=$$('.'+this.loadButtonClass);for(var i=0;i<this.loadButtons.length;i++){Event.observe($(this.loadButtons[i]),'click',this.loadDemands.bindAsEventListener(this));}},openPopup:function(){var window=$('popup-window');window.show();var elementTop=document.documentElement.scrollTop;var windowTop=elementTop+150;window.setStyle({'height':'400px','width':'50%','position':'absolute','left':'25%','background':'white','top':windowTop+'px'});},closePopup:function(){$('popup-window').hide();demands.enableWindow();},setPopupContent:function(html){$('popup-content').update(html);},setListContent:function(html){$('demands-list').update(html);},disableWindow:function(){var disable=$('disableWindow');var bodyHeight=document.body.clientHeight;var bodyWidth=document.body.clientWidth;var elementTop=document.documentElement.scrollTop;disable.addClassName('disableWindow');if(bodyHeight<800){bodyHeight=800;}
disable.setStyle({'height':bodyHeight+'px','width':bodyWidth+'px'});},enableWindow:function(){var disable=$('disableWindow');disable.removeClassName('disableWindow');disable.setStyle({'height':'0px','width':'0px'});},startLoading:function(){var element=$('loadingIndicator');element.show();var elementTop=document.documentElement.scrollTop;element.setStyle({'top':elementTop+'px'});},endLoading:function(){$('loadingIndicator').hide();},request:function(path,postBody,onCompleteFunction){var opt={method:'post',postBody:postBody,onCreate:function(t){demands.startLoading();},onComplete:onCompleteFunction,on404:function(t){alert('Error 404: location "'+t.statusText+'" was not found.');},onFailure:function(t){alert('Error '+t.status+' -- '+t.statusText);}};new Ajax.Request(path,opt);},afterGetSMSForm:function(response){demands.endLoading();demands.disableWindow();demands.openPopup();demands.setPopupContent(response.responseText);},getSMSForm:function(demandId){this.request(urlRoot+'sms-platba/'+demandId+'/','',this.afterGetSMSForm);},afterSetViewType:function(response){demands.endLoading();var json=response.responseText.evalJSON();demands.loadDemandsFromUrl(json.url);},setViewType:function(typeId){this.request(urlRoot+'nastavit-typ-vypisu-katalogu/'+typeId+'/','',this.afterSetViewType);},afterLoadDemands:function(response){demands.endLoading();demands.setListContent(response.responseText);new Effect.Highlight('demands-list');demands.setLoadButtons();},loadDemands:function(event){var element=event.findElement('A');var path=element.toString();demands.loadDemandsFromUrl(path);event.stop();},loadDemandsFromUrl:function(url){if(urlRoot=='/'){if(url.indexOf('?')>0){url=url.replace('/?','/vypis-nabidek/?');}else{url+='vypis-nabidek/';}}else{var newUrlRoot=urlRoot+'vypis-nabidek/';url=url.replace(urlRoot,newUrlRoot);}
this.request(url,'',this.afterLoadDemands);},afterLoadNewsAll:function(response){var target=$('homepage-list-2');demands.afterLoadNews(response,target);},afterLoadNewFirms:function(response){var target=$('homepage-list-1');demands.afterLoadNews(response,target);},afterLoadNewSupplies:function(response){var target=$('homepage-list-3');demands.afterLoadNews(response,target);},afterLoadNews:function(response,target){demands.endLoading();target.update(response.responseText);},loadNews:function(type,limit){var url=urlRoot+'ajax/';switch(type){case 0:url+='posledni-pridane-poptavky/?limit='+limit;this.request(url,'',this.afterLoadNewsAll);break;case 10:url+='posledni-pridane-nabidky/?limit='+limit;this.request(url,'',this.afterLoadNewSupplies);break;case 11:url+='naposledy-registrovani-dodavatele/?limit='+limit;this.request(url,'',this.afterLoadNewFirms);break;}},getLoginForm:function(){$('help-box').hide();var url=urlRoot+'ajax/prihlasovaci-formular/';demands.request(url,'',this.afterGetLoginForm);},sendActivationMail:function(fermiId){var url=urlRoot+'ajax/aktivacni-mail/'+fermiId+'/';demands.request(url,'',this.afterSendActivationMail);},afterSendActivationMail:function(response){var json=response.responseText.evalJSON();if(!json.status){demands.viewPopupWindow(json.form);}else{alert(json.message);demands.getActivationForm(json.fermiId);}},getActivationForm:function(fermiId){var url=urlRoot+'ajax/aktivacni-formular/'+fermiId+'/';demands.request(url,'',this.afterGetActivationForm);},afterGetActivationForm:function(response){var json=response.responseText.evalJSON();if(!json.status){if(json.form){demands.viewPopupWindow(json.form);}else{alert(json.message);}}else{alert(json.message);window.location.reload(true);}},getRegistrationForm:function(){$('help-box').hide();var url=urlRoot+'ajax/registracni-formular/';demands.request(url,'',this.afterGetRegistrationForm);},sendRegistrationForm:function(form){var url=urlRoot+'ajax/registracni-formular/';demands.request(url,Form.serialize(form),this.afterGetRegistrationForm);},sendActivationForm:function(form,fermiId){var url=urlRoot+'ajax/aktivacni-formular/'+fermiId+'/';demands.request(url,Form.serialize(form),this.afterGetActivationForm);},afterGetRegistrationForm:function(response){var json=response.responseText.evalJSON();if(!json.status){demands.viewPopupWindow(json.form);}else{alert(json.message);if(json.active){demands.getLoginForm();}else{demands.getActivationForm(json.fermiId);}}},afterGetLoginForm:function(response){demands.viewPopupWindow(response.responseText);},viewPopupWindow:function(contains){demands.disableWindow();demands.openPopup();if(contains!=undefined){demands.setPopupContent(contains);}}});