jQuery(document).ready(function($){
  $("#map").after('<div class="map-up"></div><div class="map-down"></div><div class="map-left"></div><div class="map-right"></div>')	
 
  // actions for place add form
	
  // toggle city if by change is selected "other"
  if($('#place_city_id').val() == '0'){
    $('#custom-city').show().addClass('expanded')
  }
	
  $('form.add-place.step-2 #place_city_id').change(function(){
    if($(this).val() == "") return
    if($(this).val() == '0'){
      // if custom city selected, show input
      $('#custom-city').slideDown().addClass('expanded')
      $("#place_city_name").focus()
    } else{
      // hide input if shown
      if($('#custom-city').hasClass("expanded")){
        $('#custom-city').slideUp().removeClass('expanded').val('')
      }
      // query picker with select value
      sel = $(this).get(0)
      value = sel.options[sel.selectedIndex].innerHTML
      $('#GMapCoordPickerQuery_picker').val(value)
      picker.query(value)
    }
  })


  $('form.add-place.step-2 #place_address').blur(function(){
    if($('#place_city_id').val() == '') return
	   
    if($('#place_city_id').val() == 0){
      // take value from custom input
      city_value = $('#place_city_name').val()
    } else{
      // take value from select
      sel = $('#place_city_id').get(0)
      city_value = sel.options[sel.selectedIndex].innerHTML
    }
    // combine with address
    value = city_value + ', ' +$(this).val()
    // query picker
    $('#GMapCoordPickerQuery_picker').val(value)
    picker.query(value)
  })
	
  // prevent onenter submit
  $('form.add-place.step-2 a#submit-form').click(function(event){
    if($(this).hasClass('disabled')){
      return false;
    }
    $(this).addClass('disabled')
    event.preventDefault();
    event.stopPropagation();
    // check if contains phone
    
	 if(phone_alert_displayed == false && ($('#phone_number').val() != '')){
		alert("W polu numer telefonu są niezatwierdzone dane. Aby dodać telefon kliknij przycisk \"dodaj\" obok tego pola zanim wyślesz cały formularz.")
		phone_alert_displayed = true;
      $(this).removeClass('disabled')		
		return false;
	 }
	 
    phones = $('#phones-container input')
    hours = $('#working-times-container input')
    if(phones.length == 0 || hours.length == 0){
     if(confirm("W wysyłanym formularzu brakuje informacji o telefonach i/lub godzinach otwarcia.\nCzy na pewno chcesz wysłać formularz?")){
      $('form.add-place.step-2').submit();
     } else{
       $(this).removeClass('disabled')
     }
    } else{
      $('form.add-place.step-2').submit();
    }

    return false;
  })

  $('form.add-place.step-2 #place_with_delivery').change(function(event){
    if(!this.checked){
      $('#delivery-block').slideUp();
      $('#delivery-block').addClass('hidden')
    } else{
      if($('#delivery-block').hasClass('hidden')){
        $('#delivery-block').slideDown();
      }
    }
  })

  // main category select
	
  $('select#place_category_id').change(function(event){
    checkMainCategory()
  })
	
  // add onload check to clear inputs in categories (run function first time)
  checkMainCategory()
	
  // prevent disabled inputs in categories
  $('form.add-place.step-2').submit(function(event){
    jQuery('ul.checkbox_list input.disabled').attr({
      disabled:false
    })
  })
	

  // filers on map page
  jQuery("#filter a").click(function(event){
    if(jQuery(this).hasClass('active')){
      jQuery(this).removeClass('active')
    } else{
      jQuery(this).addClass('active')
    }
    refreshResults(map, true);
    event.preventDefault()
  });

  jQuery("#filter2 a").click(function(event){

    if(jQuery(this).hasClass('active')){
      jQuery(this).removeClass('active')
    } else{
      jQuery(this).addClass('active')
    }
    refreshResults(map, true);
    event.preventDefault()
  });

  jQuery("#search").submit(function() {
    map.closeExtInfoWindow();
    showaddress(jQuery('#q').val());
    return false;
  });
	
  // prepare ajax indicator
  $('#map').after('<div class="ajax-indicator"><span class="loading">Wczytuję dane...</span></div>');

  $('.ajax-indicator').ajaxStart(function(){
    $(this).show()
  })

  $('.ajax-indicator').ajaxStop(function(){
    $(this).hide()
  })
	
	
  // prepare UI dialog
	
  jQuery('#dialog').dialog({
    autoOpen: false,
    modal: true,
    draggable: false,
    resizable: false,
    width: 990,
    height: 620,
    title: '',
    close: function(event,ui){
      jQuery('#dialog').html('')
    }
  });

  // prevent dialog event
  jQuery('#dialog, div.leaflet-container').live('wheel',function(event,delta){
    event.preventDefault()
    event.stopPropagation()
  })
	
  // bind leaflet links in jquery UI dialog
  jQuery('.show-leaflet').live('click',function(event){
    event.preventDefault();
	proportion=1.5967;
	max=1400;
	min=990;
	height2=jQuery(window).height()-40;
	width2=Math.floor(proportion*height2);
	if (width2>jQuery(window).width()-40)
	{
		width2=jQuery(window).width()-40;
		height2=Math.floor(width2/proportion);
	}
	if (width2>max)
	{
		width2=max;
		height2=Math.floor(max/proportion);
	}
	if (width2<min)
	{
		width2=min;
		height2=Math.floor(min/proportion);
	}	
    jQuery('#dialog').dialog('option' , 'width' , width2);
	jQuery('#dialog').dialog('option' , 'height' , height2);
	jQuery('#dialog').dialog('option' , 'title' , jQuery(this).attr('rel'));
    jQuery('#dialog').dialog('open');
    jQuery('#dialog').load(this.href);
  });
   

   //detect screen size
	detectScreenSize()
	
	
})

function switchLeaflet(el,s){

	swfobject.embedSWF("/flashzoom/zoom.swf", "leaflet-zoom", s.w, s.h, "9.0.0", null, {

    image: el.href
    }, {
    menu: false
  }, {});
  return false;
}

function checkMainCategory(){
  var sel_value = jQuery('select#place_category_id').val()
  jQuery('ul.checkbox_list input.disabled').attr({
    disabled:false
  })
  jQuery('ul.checkbox_list .disabled').removeClass('disabled')
  if(sel_value){
    // find corresponding checkbox and check it
    var input_item = jQuery('input#place_place2_category_list_' + sel_value)
    input_item.attr({
      disabled:true,
      checked:true
    }).addClass('disabled')
    jQuery(input_item.parent()[0]).addClass('disabled')
  }
}

function detectScreenSize(){
	if ((screen.width > 1024) && (screen.height > 768)) {
		jQuery('body').addClass('big-screen')
	} else{
		jQuery('body').addClass('small-screen')
	}
}

