
(function($){samo.search_tour=function(){var _GT=null,_ROOT_URL=samo.ROUTES.search_tour.url,$module_container=$('#search_tour'),_controls=$('.STATEINC,.TOWNFROMINC,.TOURINC,.SPOINC,.PROGRAMINC,.TOWNTO,.STARS,.MEAL,.CURRENCY,.NIGHTS_FROM,.NIGHTS_TILL,.ADULT,.CHILD,.FILTER,.FREIGHT,.PRICE_MIN,.PRICE_MAX,.CHECKIN_BEG,.CHECKIN_END',$module_container),DOLOAD=(parseInt($.getParameter('DOLOAD',true))==1);samo.cache_controls($module_container);var $resultset=$('.resultset',$module_container);if(typeof samo.init=='undefined'){$.getScript(getParams('INIT',true));}else{$.event.trigger('ajaxStart');var items={},HOTELS=$('.HOTELS',$module_container);$('input',HOTELS).each(function(i){var el=$(this),$gs=$.map(el.attr('samo:gs').split(','),function(e,i){return parseInt(e);});var item={inc:parseInt(this.value),title:' '+el.parent().text().toLowerCase().replace(/[^a-z0-9]/,' '),town:parseInt(el.attr('samo:town')),GroupStar:$gs,ref:el.parent().get(0)};el.removeAttr('samo:town').removeAttr('samo:gs');items[i]=item;});$.data(HOTELS.get(0),'items',items);$('.checklistbox').find(':checked').each(function(){if(!this.defaultChecked){this.checked=false;}});$.event.trigger('ajaxStop');if(DOLOAD){initResultset();price_page_url();}}
samo.price_helpers($module_container);$('input.date',$module_container).datePicker().mask(Date.mask);$('input.price',$module_container).numeric().SpinButton({step:100,min:0,max:10000000});$('.TOWNFROMINC,.STATEINC',$module_container).bind('change',function(){$('.TOWNTO_ANY,.MEAL_ANY,.STARS_ANY,.HOTELS_ANY',$module_container).attr('checked',true);$('.TOWNTO,.MEAL,.STARS,.HOTELS',$module_container).empty();$('.TOURINC,.SPOINC,.PROGRAMINC',$module_container).find('option').remove();});$('.TOURINC,.SPOINC',$module_container).bind('change',function(){$('.TOWNTO_ANY,.HOTELS_ANY',$module_container).attr('checked',true);$('.TOWNTO,.HOTELS',$module_container).empty();});$('.HOTELS_SEL',$module_container).bind('click',function(){samo.hotels_show(!this.checked);});$('.TOWNFROMINC,.STATEINC,.TOURINC,.SPOINC,.MEAL,.HOTELS',$module_container).bind('change',function(){var name=this.name||$(this).attr('name');if(name!='MEAL'&&name!='HOTELS'){setTimeout(function(){samo.reset_hotels_filter();if(name!='STATEINC'||$.controlValue(name,false,$module_container)){$.getScript(getParams(name),true);}},0);}});var $hotels=$.data(samo.controls.HOTELS,'items');$(samo.controls.HOTELS).listen('click','input',function(){$('.HOTELS_ANY',$module_container).get(0).checked=$('.HOTELS input:checked',$module_container).length==0;});$('.STARS,.TOWNTO',$module_container).listen('click','input',function(){var $groupbox=$(this).parents('div.groupbox');if($groupbox.length){if($(this).is('.group')){$groupbox.find('input').attr('checked',this.checked).parent().not('.groupname').css('display',(this.checked)?'block':'none');}else{var $all=$groupbox.find('input').not('.group');var checked=$all.filter(':checked').length;if($all.length==checked||checked==0){$groupbox.find('input.group').attr('checked',checked>0);if(0==checked){$groupbox.find('label').not('.groupname').css('display','none');}}}}
samo.hotels_filter();});$('.MEAL',$module_container).checklistbox();$(samo.controls.CURRENCY).bind('change',function(){var $currency_to=$(this).val();$('input[@name=PRICE_MAX],input[@name=PRICE_MIN]',$module_container).each(function(){if($(this).val()){var exrate=($(this).attr('samo:CURRENCY')!=$currency_to)?samo.CROSS_RATES[$(this).attr('samo:CURRENCY')][$currency_to]:1;var newValue=Math.ceil(parseInt($(this).attr('samo:u_value'))*exrate);$(this).val(newValue).attr('_pv_',newValue).triggerHandler('blur');}});});$('input[@name=PRICE_MAX],input[@name=PRICE_MIN]',$module_container).bind('blur',function(){var currency=$('.CURRENCY option:selected',$module_container).val(),value=$(this).val();$(this).attr('samo:u_value',value).attr('samo:CURRENCY',currency);});$(samo.controls.CHECKIN_BEG).bind('dpClosed',function(e,selectedDates){var d=selectedDates[0];if(d){var newDate=d.asString();$(samo.controls.CHECKIN_END).dpSetStartDate(newDate).dpSetSelected(newDate).val(newDate);}}).bind('blur',function(){var newDate=this.value;$(samo.controls.CHECKIN_END).dpSetSelected(newDate).val(newDate);});$(samo.controls.NIGHTS_FROM).bind('change',function(){$(samo.controls.NIGHTS_TILL).val(this.value);});$(samo.controls.NIGHTS_TILL).bind('change',function(){if(parseInt(this.value)<parseInt(samo.controls.NIGHTS_FROM.value)){$(samo.controls.NIGHTS_FROM).val(this.value);}});if($('#NOHOTELS').is(':checked')){$('#HOTELSCONTAINER').css('display','none');}
$('input[@name=SUBFILTER]').bind('click',function(){$('#HOTELSCONTAINER').css('display',(($('#NOHOTELS').is(':checked'))?'none':'block'));});$(samo.controls.CHILD).bind('change',function(){if(this.value>0){$('#child_ages').css('visibility','visible');for(var $i=1;$i<=3;$i++){if($i<=this.value){$('#age_'+$i).css('visibility','visible');}else{$('#age_'+$i).css('visibility','hidden').val('');}}}else{$('#child_ages').css('visibility','hidden').find('.age').css('visibility','hidden').val('');}});$('#child_ages .age').numeric(' ').attr('title',samo.LANG.CHILD_AGES);if(samo.controls.CHILD.value>0){$(samo.controls.CHILD).triggerHandler('change');}
$(samo.controls.hotelsearch).bind('keyup',function(e){if(e.keyCode==27){$.event.trigger('clearSearch');e.stopPropagation();return;}
$('.HOTELS_SEL',$module_container).attr('checked',false);samo.hotels_filter();}).bind('clearSearch',function(){this.value='';samo.hotels_filter();});$('.title_right',$module_container).toggle(function(){var self=this;$('.div_hotel_param',$module_container).slideUp(function(){$(self).text($(self).attr('samo:hide_title'));});},function(){var self=this;$('.div_hotel_param',$module_container).slideDown(function(){$(self).text($(self).attr('samo:show_title'));});});samo.loadBtn.bind('click',getPrices);function hide_hotel_params(){if($('.div_hotel_param',$module_container).is(':visible')){$('.title_right',$module_container).triggerHandler('click');}}
function getPrices(options){if(!samo.checkStateAndTownfrom($module_container)){return false;}
if(!$('.HOTELS label:visible,.HOTELS input:checked',$module_container).length){return false;}
var defaults={usehotelinc:false,page:1},settings=$.extend({},defaults,options);if(settings.usehotelinc){var hotels=$.getParameter('HOTELINC',true);if(hotels.length){hotels=hotels.split(',');$.each(hotels,function(i,e){$('input[@value="'+e+'"]',$('.HOTELS',$module_container)).attr('checked',true);});$('.HOTELS_SEL',$module_container).get(0).checked=true;samo.hotels_show();}
if($('input:checked',$('.HOTELS',$module_container)).length){$('.HOTELS_ANY',$module_container).get(0).checked=false;}}
price_page_url();$.getScript(samo.price_url+settings.page,initResultset,true);}
function price_page_url(){samo.price_url=getParams('PRICES')+getHotels()+'&PRICEPAGE=';}
$(samo.controls.CURRENCY).bind('change',price_page_url);function getHotels(){var result=$('.HOTELS',$module_container).checklistbox_value();return(result.length)?'&HOTELS='+result:'';}
function getParams(action){var useGET=arguments[1]||false;var params={};params.samo_action=action;_controls.each(function(){var name=this.name||$(this).attr('name');var value=$.controlValue(this,useGET);if(name&&value&&value!==0){params[name]=value;}});if(!params.MEAL){value=$.getParameter('MEALINC',1);if(value){params.MEALINC=value;}}
var $ages=[];$('.age',$('#child_ages')).each(function(){if(this.value>0)$ages.push(parseInt(this.value));});params.AGES=$ages.sort(function(a,b){return a-b}).join(',');params.PACKET=$.radioValue('SUBFILTER')||0;if(params.CHILD){var $ages=[];$('.age',$('#child_ages')).each(function(){if(this.value.length)$ages.push(parseInt(this.value));});params.AGES=$ages.sort(function(a,b){return a-b}).join(',');}
return _ROOT_URL+$.param(params);}
samo.price_clickable($module_container);$resultset.listen('click','span.page',function(){$.getScript(samo.price_url+$(this).attr('samo:page'),initResultset,true);});function initResultset(){$('table.res tr:odd td').addClass('silver');$('.price_legend',$module_container).show();var pager=$('div.pager',$module_container);if(pager.length){pager.get(0).scrollIntoView(false);}
$resultset.listen('mouseover','span',function(){if($(this).is('.Y'))$(this).attr('title',samo.LANG.TOUR_SEARCH_RES_YES_PLACE);if($(this).is('.N'))$(this).attr('title',samo.LANG.TOUR_SEARCH_RES_NO_PLACE);if($(this).is('.R'))$(this).attr('title',samo.LANG.TOUR_SEARCH_RES_REQUEST_PLACE);});if(samo.ROUTES.all_prices){$('table.res').listen('click','td.all_prices',function(){var params={},self=this;params.TOWNFROMINC=$(self).parents('tr:first').attr('samo:TOWNFROMINC');params.CLAIMINC=$(self).parents('tr:first').find('td.price').attr('samo:claimInc');$('HOTELINC,SPOINC,TOURINC'.split(',')).each(function(){params[this]=$(self).attr('samo:'+this);});params.CHECKIN_BEG=$(self).attr('samo:CHECKIN');window.open(samo.ROUTES.all_prices.url+$.param(params),'all_prices'+$(self).attr('samo:HOTELINC'));});}
$('table.res').listen('click','img.crosstour',function(){var params={samo_action:'CONTENT'},$tr=$(this).parents('tr:first');params.TOWNFROMINC=$tr.attr('samo:TOWNFROMINC');params.STATEINC=$tr.attr('samo:STATEINC');params.CLAIM=$tr.find('td.price').attr('samo:claimInc');$.getScript(samo.ROOT_URL+$.param(params));});}
samo.hotels_filter=function(){var $grps=[],$twns=[],$all_stars=false,$all_towns=false;$('.HOTELS_SEL',$module_container).get(0).checked=false;var townto_any=$('.TOWNTO_ANY',$module_container).get(0);var grpstar_any=$('.STARS_ANY',$module_container).get(0);var hotels=$.data($('.HOTELS',$module_container).get(0),'items');$('.TOWNTO input:checked',$module_container).each(function(){$twns.push(parseInt(this.value));});$all_towns=townto_any.checked=!$twns.length;$('.STARS input:checked',$module_container).each(function(){$grps.push(parseInt(this.value));});$all_stars=grpstar_any.checked=!$grps.length;var $filter=$.trim($('.hotelsearch',$module_container).val()).toLowerCase(),$strfilter=$filter.length,hide=[],show=[];$.each(hotels,function(idx,$hotel){var found=($all_towns||$.inArray($hotel.town,$twns)!==-1);if(found&&!$all_stars){found=false;$.each($hotel.GroupStar,function(_idx,$grp){if($.inArray($grp,$grps)!==-1){found=true;return;}});}
if(found&&$strfilter){found=$hotel.title.indexOf($filter)>0;}
var $label=$hotel.ref;if(found){$label.style.display='block';}else{$label.style.display='none';}});};samo.reset_hotels_filter=function(){$('.TOWNTO_ANY,.STARS_ANY',$module_container).each(function(){this.checked=true;var name=this.name.replace(/_ANY/,'');$('.'+name+' :checked',$module_container).attr('checked',false);});$('.hotelsearch',$module_container).val('');};samo.hotels_show=function(show_all){show_all=show_all||false;if(show_all){samo.hotels_filter();}else{samo.reset_hotels_filter();$('.HOTELS input').not(':checked').parent().css('display','none');$('.HOTELS input:checked').parent().css('display','block');}};var do_filter=false;$('.TOWNTO_ANY,.STARS_ANY,.HOTELS_ANY,.MEAL_ANY',$module_container).bind('click',function(){var name=this.name.replace(/_ANY/,'');if(this.checked){$('.'+name+' input:checked',$module_container).attr('checked',false);if(name=='HOTELS'){$('.hotelsearch',$module_container).val('');$('.HOTELS_SEL',$module_container).get(0).checked=false;}
if(name=='TOWNTO'){$('.TOWNTO label').not('.groupname').css('display','none');}
samo.hotels_filter();}}).each(function(){var name=this.name.replace(/_ANY/,'');this.checked=!$('.'+name+' input:checked',$module_container).length;if(!do_filter&&!this.checked&&name!='MEAL'){samo.hotels_filter();do_filter=true;}});}
$(document).bind('ready',samo.search_tour);})(samo.jQuery);