var expandBox = {
	foldIn: false,
	delay: 0,
	timeout: null,
	
	init: function(){
		$('.box-content.expand').each(function(){
			$(this).mouseover(function(){
        clearTimeout(expandBox.timeout);
				expandBox.foldIn = false;
				
				if($(this).hasClass('expand')){
					expandBox.resize($(this));
				}
			});
			$(this).mouseout(function(){
		    var el = $(this);
				expandBox.foldIn = true;
				expandBox.timeout = setTimeout(function(){
				  expandBox.reset(el);
				}, expandBox.delay);
			});
		});	
	},
	resize: function(el){
    el.animate({
      'height': el.find('.submenu').height() + 'px'
    }, 200, function(){
      el.removeClass('expand');
    });
	},
	reset: function(el){
		if(expandBox.foldIn){
			if( el.height() < '170' ){
				el.animate({
				  'height': 'auto'
				}, 200, function(){
				  el.addClass('expand');
				  expandBox.foldIn = false;
				});
			} else {
				el.animate({
				  'height': '170px'
				}, 200, function(){
				  el.addClass('expand');
				  expandBox.foldIn = false;
				});
			}
		} 
	}
}

var editorial = {
  init: function(){
    $('#editorial-tips li').each(function(){
      $(this).find('.title').mouseover(function(){
        editorial.setSelected($(this).parents('li').attr('id'));
      });
    });
  },
  
  setSelected: function(selectedElement){
    $('#editorial-tips li').each(function(){
      if(selectedElement != $(this).attr('id'))
        $(this).removeClass('selected');
      else
        $(this).addClass('selected');
    });
  }
}

var mapDisplay = {
  text: '',
  
  init: function(){
    if($('#display-on-map').length > 0){
      
      mapDisplay.text = $('#display-on-map').html();
      
      $('.map').fadeTo('fast', 0.3);
      
      $('#display-on-map').click(function(e){
        if($('#results-map').height() < 200){
          $(this).html('Verberg kaart');
          $('.map').fadeTo('fast', 1);
          $('#results-map').animate({
            'height': '435px'
          });
          $(this).shown = true;
        }else{
          $(this).html(mapDisplay.text);
          $('.map').fadeTo('fast', 0.3);
          $('#results-map').animate({
            'height': '35px'
          });
          $(this).shown = false;
        }
        return false;
      });
    }
  }
}

var search = {
  className: '',
  
  init: function() {
    $('#small-search li a').each(function() {
      if($(this).hasClass('current')) {
        search.className = $(this).attr('id').substr(5, $(this).attr('id').length - 5);
        search.setFields();
      }
      $(this).click(function(){
        $('#label').val($(this).attr('rel'));
        
        $('#small-search select').each(function(){
          $(this)[0].selectedIndex = 0;
        });
                
        $('#small-search li a').removeClass('current');
        $(this).addClass('current');
        search.className = $(this).attr('id').substr(5, $(this).attr('id').length - 5); 
        search.setFields();
        return false;
      });
    });
    
    if($('#search-result select#category_id').val() != '') {
      $('#suggestions').hide();
    }
    
    $('#search-result select#category_id').change(function() {
      if($(this).val() != '') {
        $('#suggestions').slideUp('slow');
      } else {
        $('#suggestions').slideDown('slow');
      }
    });
  },
  
  setFields: function(){
    $('#small-search-form p').hide().filter('.' + search.className).show();
  }
}

var agenda = {
  init: function() {
    $('#calendar-month li.item > a').each(function(index){
      $this = $(this);
      
      console.log($('#' + $this.attr('id') + '-overlay').html());
      
      $this.qtip({
        content: $('#' + $this.attr('id') + '-overlay').html(),
        position: {
          corner: {
            tooltip: 'rightTop', // Use the corner...
            target: 'leftMiddle' // ...and opposite corner
          }
        },
        show: {
          when: {
            event: 'mouseover'
          },
          solo: true
        },
        hide: {
          delay: 500,
          fixed: true,
          when: {
            event: 'mouseout'
          }
        },
        style: {
          border: {
            width: 0,
            radius: 10,
            color: '#000000'
          },
          width: 220,
          color: '#ffffff',
          background: '#000000',
          padding: 0,
          textAlign: 'left',
          tip: true
        }
      });
    });
  }
}

$(document).ready(function(){
  
  $('.fhInputClear').fhInputClear();
	expandBox.init();
	expandBox.foldIn = true;
	$('.box-content.expand').each(function(){
    expandBox.reset($(this));
	});
  mapDisplay.init();
	editorial.init();
	search.init();
	agenda.init();
	
	$("a[rel^=prettyPhoto]").prettyPhoto({theme:'light_rounded'});
  
  $('a').each(function(el){
    $(this).filter('[rel*=external]').attr('target', '_blank');
  });
  
  $('#city_autocomplete').autocomplete('/get-autocomplete-cities');
});