function close_all_suggestions() { $(".suggest:visible").each(function(index,elem){ elem=$(elem); text_field=$('#'+elem.data('field')); if(text_field.size()>0 && text_field.data('type')=='strict') { id_field=$('#'+elem.data('field')+'_id'); text_field.val(id_field.data('name')); } elem.hide(); }); } $(function(){ select_setup("search_input_category"); select_setup("search_input_audience"); function select_setup(input_id) { var input_obj=$('#'+input_id); input_obj.on('click',function(e){ if($('.suggest',input_obj).is(':hidden')) close_all_suggestions(); $('.suggest',input_obj).toggle(); e.stopPropagation(); }); $('.suggest li',input_obj).mouseenter(function(e){ $('.suggest li', input_obj).removeClass('highlight'); $(e.target).addClass('highlight'); }); $('.suggest li',input_obj).click(function(e){ var obj=$(e.target); $('input',input_obj).val(obj.data('id')); $('.pseudoinput',input_obj).removeClass('placehldr'); $('.pseudoinput',input_obj).html(obj.text()); $('.iconContainerRight',input_obj).show(); }); $('.iconContainerRight',input_obj).click(function(e){ $('input',input_obj).val(''); $('.pseudoinput',input_obj).html($('.pseudoinput',input_obj).data('placehldr')); $('.pseudoinput',input_obj).addClass('placehldr'); $('.iconContainerRight',input_obj).hide(); e.stopPropagation(); }); } }); $(function(){ $('#search_input_date').on('click',function(e){ if($('#search_calendar').is(':hidden')) close_all_suggestions(); $('#search_calendar').toggle(); e.stopPropagation(); }); $('#search_input_date .iconContainerRight').on('click',function(e){ $('#search_date').val(''); $('#search_date_text').html($('#search_date_text').data('placehldr')); $('#search_date_text').addClass('placehldr'); $('#search_input_date .iconContainerRight').hide(); return false; }); curr_date = new Date(); cal_month=curr_date.getMonth(); cal_year=curr_date.getFullYear(); drawCal(cal_month,cal_year); function drawCal(month,year) { var cal=$('#calendar_module'); var month_names=["jaanuar","veebruar","märts","aprill","mai","juuni","juuli","august","september","oktoober","november","detsember"]; var weekdays=["E","T","K","N","R","L","P"]; cal.append('
' +'«' +month_names[month]+' '+year +'»' +'
'); for(wd=0; wd<7; wd++) { $(''+weekdays[wd]+'').appendTo($('.cal_body',cal)); } var current_date = new Date(); var date_pointer=start_date; while(date_pointer.getTime()<=last_date.getTime()) { for(wd=0; wd<7; wd++) { var classes=''; if(current_date.getFullYear()==date_pointer.getFullYear() && current_date.getMonth()==date_pointer.getMonth() && current_date.getDate()==date_pointer.getDate()) classes+=' currentdate'; if(date_pointer.getMonth()!=month) classes+=' other_month'; var str_month=((date_pointer.getMonth()+1)<10 ? '0'+(date_pointer.getMonth()+1) : (date_pointer.getMonth()+1)); var str_month_name=month_names[date_pointer.getMonth()]; var str_day=(date_pointer.getDate()<10 ? '0'+date_pointer.getDate() : date_pointer.getDate()); $(''+date_pointer.getDate()+'').appendTo($('.cal_body',cal)) .addClass(classes.trim()) .data('val',date_pointer.getFullYear()+'-'+str_month+'-'+str_day) .data('text',str_day+'. '+str_month_name+' '+date_pointer.getFullYear()) .on('click',dateclick); date_pointer=new Date(date_pointer.getFullYear(), date_pointer.getMonth(), date_pointer.getDate()+1); } } } $('#search_calendar>a').on('click',dateclick); function dateclick(e) { $('#search_date').val($(e.target).data('val')); $('#search_date_text').html($(e.target).data('text')); $('#search_date_text').removeClass('placehldr'); $('#search_input_date .iconContainerRight').show(); $('#search_calendar').hide(); return false; } }); $(function(){ suggest_setup('search_location', 'strict'); suggest_setup('search_keyword', 'normal'); function suggest_setup(id, type) { $('#'+id).data('type',type); $('#'+id).keydown(suggest_keydn_handle); $('#'+id).keyup(suggest_keyup_handle); $('#'+id).data('prev_value',$('#'+id).val()); $('#'+id+'_suggest').data('field',id); $('#'+id+'_clear').on('click',function(){ $('#'+id+'_id').val(''); $('#'+id+'_id').data('name',''); $('#'+id).val(''); $('#'+id+'_clear').hide(); }); } function suggest_keydn_handle(e) { if(e.which == 13 || e.which == 9) { var id_field=$('#'+e.target.id+'_id'); var text_field=$(e.target); var suggest_obj=$('#'+e.target.id+'_suggest'); var clear_btn_obj=$('#'+e.target.id+'_clear'); var selected=$("ul li.highlight", suggest_obj); if(selected.size()==1) { id_field.val(selected.data('id')); id_field.data('name',selected.html()); text_field.val(selected.html()); clear_btn_obj.show(); if(e.which == 13) e.preventDefault(); } else if(text_field.val()!=id_field.data('name')) { if(text_field.data('type')=='strict') { text_field.val(id_field.data('name')); if(e.which == 13) e.preventDefault(); } else { id_field.val(''); id_field.data(''); clear_btn_obj.show(); } } clearandclose(suggest_obj); } else if(e.keyCode == 38 || e.keyCode == 40) { e.preventDefault(); var suggest_obj=$('#'+e.target.id+'_suggest'); var list=$('ul li', suggest_obj); var current_selected_li=$('ul li.highlight', suggest_obj); for (var i=0; i0) list.eq(i-1).trigger('mouseenter'); } var selected_obj=$('ul li.highlight',suggest_obj); if(suggest_obj.height() < (selected_obj.position().top+selected_obj.height())) suggest_obj.scrollTop(suggest_obj.scrollTop() + selected_obj.position().top + selected_obj.height() - suggest_obj.height()); if(selected_obj.position().top < 0) suggest_obj.scrollTop(suggest_obj.scrollTop() + selected_obj.position().top); break; } } } } function suggest_keyup_handle(e) { var list_obj=$('#'+e.target.id+'_suggest ul'); var sugg_obj=$('#'+e.target.id+'_suggest'); var id_field=$('#'+e.target.id+'_id'); var clear_btn=$('#'+e.target.id+'_clear'); if(e.which != 13 && e.target.value!=$(e.target).data('prev_value')) { if(e.target.value.length>1) { clear_btn.show(); $.getJSON('/json/search.php?'+e.target.id+'='+encodeURIComponent(e.target.value), function(data){ if(data.length>0) { list_obj.empty(); sugg_obj.show(); $.each(data, function(key, val) { list_obj.append('
  • '+val.name+'
  • '); }); if($(e.target).data('type')=='strict') $('li', list_obj).first().addClass('highlight'); $('li', list_obj).mouseenter(function(hover_e){ $('li', list_obj).removeClass('highlight'); $(hover_e.target).addClass('highlight'); }); $('li', list_obj).on('click',function(click_e){ e.target.value=$(click_e.target).html(); id_field.val($(click_e.target).data('id')).data('name',e.target.value); sugg_obj.hide(); clear_btn.show(); click_e.stopPropagation(); }); } else { clearandclose(sugg_obj); } }); } else { clearandclose(sugg_obj); if(e.target.value.length == 0) { id_field.val(''); id_field.data('name',''); clear_btn.hide(); } else { clear_btn.show(); } } } $(e.target).data('prev_value',e.target.value); } function clearandclose(sugg_obj) { $('ul',sugg_obj).empty(); sugg_obj.hide(); } $('html').on('click',function(){ close_all_suggestions(); }); });