freshmini.posta.pricelist=function()
{}
freshmini.posta.pricelist.CONST=function()
{}
freshmini.posta.pricelist.STR=function()
{}
freshmini.posta.pricelist.CONST.SERVICE_ALL="/en/services";freshmini.posta.pricelist.CONST.SERVICE_DATA="/en/service_data";freshmini.posta.pricelist.STR.SERVICES="Services";freshmini.posta.pricelist.ComboBox=function(params)
{this.containerId=params.container_id;this._parent_id=params.parent_id;this._createElements();this._multiple_selection=params.multiple_selection?true:false;this._has_default=params.default_value?true:false;this._items=new Array();if(this._has_default)
{this._default_value=params.default_value;}
if(this._multiple_selection)
{this.selectedValue=new Array();this.selectedIndex=new Array();this._disabled=new Object();}
else
{this.selectedValue=null;this.selectedIndex=-1;}
this._displayValue();$("#"+this.containerId+" :first-child :first").bind("click",freshmini.posta.utils.injectMethod(this,this._comboclicked));this._listitemdiv=null;this._events=new Object();this._events["selectionchanged"]=new Array();}
freshmini.posta.pricelist.ComboBox.prototype={_createElements:function()
{var div=document.createElement("div");div.id=this.containerId;var div1=document.createElement("div");div1.className="fm-pricecombo";div.appendChild(div1);var a=document.createElement("a");a.href="#";div1.appendChild(a);$("#"+this._parent_id).append(div);},_displayValue:function()
{var val=this._multiple_selection?this._getNotSelectedValue():this.selectedValue?this.selectedValue:this._getNotSelectedValue();$("#"+this.containerId+" a:first").attr("innerHTML",val);},_getNotSelectedValue:function()
{return this._has_default?this._default_value:"no selection";},_isSelected:function(index)
{if(this._multiple_selection)
{for(var i=0;i<this.selectedIndex.length;i++)
{if(this.selectedIndex[i]==index)
{return true;}}
return false;}
else
{return index==this.selectedIndex;}},_createListItem:function(label,index)
{var item=document.createElement("li");item.className="fm-pricecombo-listitem";if(this._isSelected(index))
{item.className+=" fm-pricecombo-selected";}
if(index==-1)
{item.className+=" fm-pricecombo-default";}
if(!this._multiple_selection)
{var a=document.createElement("a");item.appendChild(a);a.href="#";a.innerHTML=label.toString();a.__fm_combo=this;a.__fm_index=index;jQuery(a).bind("click",this._listitemclicked);}
else
{var div_input=document.createElement("div");div_input.className="fm-pricecombo-input";item.appendChild(div_input);var input=document.createElement("input");input.type="checkbox";div_input.appendChild(input);input.__fm_combo=this;input.__fm_index=index;if(this._isSelected(index))
{input.checked=true;}
jQuery(input).bind("click",this._checklistitemclicked);var a=document.createElement("a");item.appendChild(a);a.href="#";a.innerHTML=label.toString();a.__fm_combo=this;a.__fm_index=index;a.__fm_check=input;if(this._disabled[index])
{input.disabled=true;a.disabled=true;}
else
{jQuery(a).bind("click",this._checklistitemclicked);}}
return item;},_createList:function(div)
{if(this._has_default&&!this._multiple_selection)
{div.appendChild(this._createListItem(this._default_value,-1));}
for(var index=0;index<this._items.length;index++)
{if(this._items[index]._visible)
{div.appendChild(this._createListItem(this._items[index].label,index,this._items[index]._enabled));}}},_checklistitemclicked:function(e)
{var found=false;for(var index=0;index<this.__fm_combo.selectedIndex.length;index++)
{if(this.__fm_combo.selectedIndex[index]==this.__fm_index)
{this.__fm_combo.selectedIndex.splice(index,1);this.__fm_combo.selectedValue.splice(index,1);if(this.__fm_check)
{this.__fm_check.checked=false;}
found=true;break;}}
if(!found)
{this.__fm_combo.selectedIndex.push(this.__fm_index);this.__fm_combo.selectedValue.push(this.__fm_combo._items[this.__fm_index].label);if(this.__fm_check)
{this.__fm_check.checked=true;}}
this.__fm_combo._triggerEvent("selectionchanged",{id:this.__fm_combo.containerId,selected_value:this.__fm_combo.selectedValue,selected_index:this.__fm_combo.selectedIndex});},_listitemclicked:function(e)
{this.__fm_combo.selectedValue=this.innerHTML;this.__fm_combo.selectedIndex=this.__fm_index;this.__fm_combo._displayValue();this.__fm_combo._triggerEvent("selectionchanged",{id:this.__fm_combo.containerId,selected_value:this.innerHTML,selected_index:this.__fm_index});this.__fm_combo._close();},_isOpened:function()
{return this._listitemdiv;},_comboclicked:function(e)
{if(!this._isOpened())
{this._open();}
else
{this._close();}},_open:function()
{this._listitemdiv=document.createElement("div");this._listitemdiv.className="fm-pricecombo-list";this._listitemdiv.id=this.containerId+"_listitem";var ul=document.createElement("ul");this._listitemdiv.appendChild(ul);this._createList(ul);$("#"+this.containerId).append(this._listitemdiv);var temp=this;$("body").bind("click",function(e)
{if(temp._isOpened())
{for(var elm=e.srcElement||e.target;elm;elm=elm.parentElement||elm.parentNode)
{if(elm.id==temp.containerId)
{return;}}
temp._close();}});},_close:function()
{jQuery(this._listitemdiv).remove();this._listitemdiv=null;},_triggerEvent:function(type,args)
{for(var index=0;index<this._events[type].length;index++)
{if(this._events[type][index].context)
{var method=freshmini.posta.utils.injectMethod(this._events[type][index].context,this._events[type][index].method,[args]);method(args);}
else
{this._events[type][index].method(args);}}},addItem:function(item)
{var item_obj={label:item,_visible:true};if(this._multiple_selection&&!item._enabled)
{item_obj._enabled=true;}
this._items.push(item_obj);},clearItems:function()
{this._items=new Array();},hideItem:function(index)
{this._items[index]._visible=false;},showItem:function(index)
{this._items[index]._visible=true;},attachEvent:function(type,callback,object)
{if(this._events[type])
{this._events[type].push({method:callback,context:object});}},setCheckItemDisabled:function(index,disabled)
{this._disabled[index]=disabled;if(this._isOpened())
{var ni=index+1;$(":nth-child("+ni+") a",this._listitemdiv).attr("disabled",disabled);$(":nth-child("+ni+") input",this._listitemdiv).attr("disabled",disabled);if(disabled)
{$(":nth-child("+ni+") a",this._listitemdiv).unbind("click",this._checklistitemclicked);}
else
{$(":nth-child("+ni+") a",this._listitemdiv).bind("click",this._checklistitemclicked);}}},unselectItem:function(index)
{if(this._multiple_selection)
{for(var i=0;i<this.selectedIndex.length;i++)
{if(this.selectedIndex[i]==index)
{this.selectedIndex.splice(i,1);this.selectedValue.splice(i,1);$(":nth-child("+(index+1).toString()+") input",this._listitemdiv).attr("checked",false);break;}}}
else
{this.selectedValue=null;this.selectedIndex=-1;}
this._displayValue();},clearSelection:function()
{if(this._multiple_selection)
{this.selectedValue=new Array();this.selectedIndex=new Array();this._disabled=new Object();}
else
{this.selectedValue=null;this.selectedIndex=-1;}
this._displayValue();}}
freshmini.posta.pricelist.LayeredCombo=function(container_id)
{this._container_id=container_id;this._combos=new Array();this._events=new Object();this._events["selectionchanged"]=new Array();}
freshmini.posta.pricelist.LayeredCombo.prototype={_getNewId:function()
{return this._container_id+this._combos.length.toString();},_indexFromId:function(id)
{for(var index=0;index<this._combos.length;index++)
{if(this._combos[index].containerId==id)
{return index;}}
return-1;},_createCombo:function(data)
{var combo=new freshmini.posta.pricelist.ComboBox({default_value:data.default_value,parent_id:this._container_id,container_id:this._getNewId(),multiple_selection:data.multiple_selection});combo.attachEvent("selectionchanged",this._combo_selectionchanged,this);return combo;},_checkVisibility:function()
{var show=true;for(var index=0;index<this._combos.length;index++)
{if(show)
{$("#"+this._combos[index].containerId).show();if(this._combos[index].selectedIndex==-1)
{show=false;}}
else
{$("#"+this._combos[index].containerId).hide();this._combos[index].clearSelection();}}},_combo_selectionchanged:function(data)
{this._checkVisibility();this._triggerEvent("selectionchanged",{index:this._indexFromId(data.id),selected_value:data.selected_value,selected_index:data.selected_index});},_triggerEvent:function(type,args)
{for(var index=0;index<this._events[type].length;index++)
{if(this._events[type][index].context)
{var method=freshmini.posta.utils.injectMethod(this._events[type][index].context,this._events[type][index].method,[args]);method(args);}
else
{this._events[type][index].method(args);}}},addLayer:function(data)
{var combo=this._createCombo(data);this._combos.push(combo);for(var index=0;index<data.items.length;index++)
{combo.addItem(data.items[index]);}
this._checkVisibility();},removeLayer:function(index)
{if(index>=this._combos.length)
{return;}
$("#"+this._combos[index].containerId).remove();this._combos.splice(index,1);},attachEvent:function(type,callback,object)
{if(this._events[type])
{this._events[type].push({method:callback,context:object});}},getSelectedIndex:function(index)
{return this._combos[index].selectedIndex;},setDisabled:function(index,disabled)
{for(var i=0;i<disabled.length;i++)
{this._combos[index].setCheckItemDisabled(i,disabled[i]);if(disabled[i])
{this._combos[index].unselectItem(i);}}},getCount:function()
{return this._combos.length;},resetFrom:function(index)
{for(var i=index;i<this._combos.length;i++)
{this._combos[i].clearSelection();}
this._checkVisibility();},reset:function(index)
{this._combos[index].clearSelection();this._checkVisibility();},hideItem:function(combo,item)
{this._combos[combo].hideItem(item);},showItem:function(combo,item)
{this._combos[combo].showItem(item);}}
freshmini.posta.pricelist.PriceCalculator=function(data)
{this._rate=30.126;this._symbol_eur="€";this._symbol_sk="Sk";this._id_eur=data.price_eur_id;this._id_sk=data.price_sk_id;this._price=0;this._addon=0;this._show();}
freshmini.posta.pricelist.PriceCalculator.prototype={_round:function(number,precision)
{var t=Math.pow(10,precision);return Math.round(number*t)/t;},_toSkk:function(eur,precision)
{var res=eur*this._rate;return precision?this._round(res,precision):res;},_show:function()
{if(isNaN(this._price))
{$("#"+this._id_eur).html("<span>"+this._price+"</span>");$("#"+this._id_sk).html("<span>"+this._price+"</span>");}
else
{$("#"+this._id_eur).attr("innerHTML",this._round(this._price+this._addon,2).toFixed(2));$("#"+this._id_sk).attr("innerHTML",this._toSkk(this._price+this._addon,2).toFixed(2));}},setBase:function(price)
{this._price=parseFloat(price);if(isNaN(this._price))
{this._price=price;}
this._show();},resetAdditional:function()
{this._addon=0;this._show();},addAdditional:function(price)
{var n=parseFloat(price);if(isNaN(n))
{return;}
this._addon+=n;this._show();}}
freshmini.posta.pricelist.MapClient=function(data)
{this._container_id=data.combo_container_id;this._note_id=data.note_id;this._price_calculator=new freshmini.posta.pricelist.PriceCalculator(data);this._combos=new freshmini.posta.pricelist.LayeredCombo(this._container_id);this._combos.attachEvent("selectionchanged",this._combo_selectionchanged,this);this._base_price=null;var temp=this;$("#"+data.reset_id).bind("click",function(){temp.reset();});this._loadServices();}
freshmini.posta.pricelist.MapClient.prototype={_request:function(url,service_id,callback)
{$.get(url,service_id?{service_id:service_id}:null,callback,"json");},_loadServices:function()
{var temp=this;this._request(freshmini.posta.pricelist.CONST.SERVICE_ALL,null,function(data)
{temp._services=data.services;temp._initialize();});},_subserviceChanged:function(data)
{this._combos.removeLayer(3);this._combos.removeLayer(2);while(this._combos_data.length>2)
{this._combos_data.pop();}
while(this._service_data.labels.length>1)
{this._service_data.labels.pop();}
if(data.selected_index==-1)
{return;}
var id=this._service_data.subservices[data.selected_index].id;var temp=this;this._request(freshmini.posta.pricelist.CONST.SERVICE_DATA,id,function(data)
{temp._subserviceDataLoaded(data);});},_serviceChanged:function(data)
{this._index_subservice=null;this._price_calculator.setBase("0");this._price_calculator.resetAdditional();this._combos.removeLayer(3);this._combos.removeLayer(2);this._combos.removeLayer(1);while(this._combos_data.length>1)
{this._combos_data.pop();}
if(data.selected_index==-1)
{return;}
var note=this._services[data.selected_index].note;$("#"+this._note_id).html(note);var id=this._services[data.selected_index].id;var temp=this;this._request(freshmini.posta.pricelist.CONST.SERVICE_DATA,id,function(data)
{temp._serviceDataLoaded(data);});},_combo_selectionchanged:function(data)
{if(data.index==0)
{this._serviceChanged(data);return;}
if(data.index==this._index_subservice)
{this._subserviceChanged(data);}
this._combos.resetFrom(data.index+1);if(data.index==this._index_kind&&data.selected_index!=-1)
{this._hideKinds(this._service_data.kinds[data.selected_index].id);}
if(data.index>=this._index_type)
{this._checkRelations();}
if(data.selected_index==-1)
{this._price_calculator.setBase("0");this._price_calculator.resetAdditional();return;}
if(this._base_price&&data.index<this._base_price)
{this._price_calculator.setBase(0);if(this._index_complement)
{this._setAdditionalPrice();}}
else if(this._base_price&&data.index==this._base_price)
{this._setBasePrice();}
if(this._index_complement&&this._base_price&&data.index>=this._base_price)
{this._setAdditionalPrice();}},_hideKinds:function(kind)
{if(this._index_type==null)
{return;}
for(var index_type=0;index_type<this._service_data.types.length;index_type++)
{if(!this._dataForTypeKindExist(this._service_data.types[index_type].id,kind))
{this._combos.hideItem(this._index_type,index_type);}
else
{this._combos.showItem(this._index_type,index_type);}}},_dataForTypeKindExist:function(type,kind)
{for(var index=0;index<this._service_data.data.length;index++)
{if(this._service_data.data[index].type==type&&this._service_data.data[index].kind==kind)
{return true;}}
return false;},_getPrice:function(kind,type)
{for(var index=0;index<this._service_data.data.length;index++)
{if(!type)
{if(this._service_data.data[index].kind==kind)
{return this._service_data.data[index].price;}}
else if(this._service_data.data[index].kind==kind&&this._service_data.data[index].type==type)
{return this._service_data.data[index].price;}}
return"0";},_setBasePrice:function()
{var kind=this._combos_data[this._index_kind].data[this._combos.getSelectedIndex(this._index_kind)].id;if(this._index_type)
{var type=this._combos_data[this._index_type].data[this._combos.getSelectedIndex(this._index_type)].id;this._price_calculator.setBase(this._getPrice(kind,type));}
else
{this._price_calculator.setBase(this._getPrice(kind));}},_setAdditionalPrice:function()
{var complements=this._getSelectedComplements();this._price_calculator.resetAdditional();for(var i=0;i<complements.length;i++)
{this._price_calculator.addAdditional(this._getPrice(complements[i]));}},_getSelectedComplements:function()
{var selection=this._combos.getSelectedIndex(this._index_complement);var complements=new Array();for(var i=0;i<selection.length;i++)
{complements.push(this._combos_data[this._index_complement].data[selection[i]].id);}
return complements;},_getComplementIndex:function(id)
{for(var i=0;i<this._combos_data[this._index_complement].data.length;i++)
{if(this._combos_data[this._index_complement].data[i].id==id)
{return i;}}},_resolveDisabled:function(disabled,parent_id)
{for(var i=0;i<this._service_data.relations.length;i++)
{if(this._service_data.relations[i].parent==parent_id)
{disabled[this._getComplementIndex(this._service_data.relations[i].child)]=false;}}},_checkRelations:function()
{if(!this._index_complement)
return;var index=0;index++;var kind=this._combos_data[this._index_kind].data[this._combos.getSelectedIndex(this._index_kind)].id;var complements=this._getSelectedComplements();var disabled=new Array();var i=0;for(i=0;i<this._combos_data[this._index_complement].data.length;i++)
{disabled.push(true);}
this._resolveDisabled(disabled,kind);for(i=0;i<complements.length;i++)
{this._resolveDisabled(disabled,complements[i]);}
this._combos.setDisabled(this._index_complement,disabled);},_prepareData:function()
{this._combos_data.push({label:this._service_data.labels[0],data:this._service_data.kinds,multiple_selection:false});this._combos_data.push({label:this._service_data.labels[1],data:this._service_data.types,multiple_selection:false});this._combos_data.push({label:this._service_data.labels[2],data:this._service_data.complements,multiple_selection:true});this._index_kind=1;this._index_type=2;this._index_complement=null;this._index_subservices=null;this._base_price=2;},_serviceDataLoaded:function(data)
{this._service_data=data;if(this._service_data.subservices)
{this._combos_data.push({label:this._service_data.labels[0],data:this._service_data.subservices,multiple_selection:false});this._createCombo(1);this._index_subservice=1;this._index_kind=-1;this._index_type=null;this._index_complement=null;this._base_price=null;}
else
{this._prepareData();this._createCombo(1);this._createCombo(2);}},_replaceMultipleStringSeparator:function(array)
{for(var i=0;i<array.length;i++)
{var l=array[i].label.indexOf('|');if(l<0)
continue;array[i].label=array[i].label.substr(0,l)+" - "+array[i].label.substr(l+1);}
return array;},_subserviceDataLoaded:function(data)
{this._service_data.labels=this._service_data.labels.concat(data.labels);this._service_data.kinds=this._replaceMultipleStringSeparator(data.kinds);this._service_data.types=data.types;this._service_data.data=data.data;this._combos_data.push({label:this._service_data.labels[1],data:this._service_data.kinds,multiple_selection:false});this._index_kind=2;this._createCombo(2);this._index_complement=null;if(this._service_data.labels.length==2)
{this._index_type=null;this._base_price=2;}
else
{this._combos_data.push({label:this._service_data.labels[2],data:this._service_data.types,multiple_selection:false});this._createCombo(3);this._index_type=3;this._base_price=3;}},_createCombo:function(index)
{var items=new Array();for(var i=0;i<this._combos_data[index].data.length;i++)
{items.push(this._combos_data[index].data[i].label);}
this._combos.addLayer({default_value:this._combos_data[index].label,multiple_selection:this._combos_data[index].multiple_selection,items:items});},_initialize:function()
{this._combos_data=new Array();this._combos_data.push({label:("- "+freshmini.posta.pricelist.STR.SERVICES+" -"),data:this._services,multiple_selection:false});this._createCombo(0);},reset:function()
{this._serviceChanged({index:0,selected_index:-1,selected_value:""});this._combos.reset(0);$("#"+this._note_id).html('');}}
$(document).ready(function(){new freshmini.posta.pricelist.MapClient({combo_container_id:"price_selectors",price_eur_id:"price_eur",price_sk_id:"price_sk",reset_id:"price_reset",note_id:"price_note"})});