var list_html = '';
$(function(){
  $('#whatson-reset').click(function(e){
    document.location.href = '/whats-on/';
  });

  $('#add-another-show-all').hide();

  $('#create-your-map').click(function(e){
    if($('#search-fields').height() == 0) {
      $('#search-fields').show().animate({
        height: '600px'
      }, 400, function(){
        if(!$('#create-your-map').is('.dont-auto-display')) {
          $('#add-another-link').click();
        }
      });

      $('#search-results-wrapper').animate({
        opacity: 0.1
      }, 400);

      $(this).next('span').addClass('up-arrow');
    }
    e.preventDefault();
  });

  $('#add-another-link').click(function(e){
    $('#add-another ul, #add-another-show-all').show();
    e.preventDefault();
  });

  $('#search-fields .search-wrapper').each(function() {
    $this = $(this);
    $select = $this.find('select');
    replaceSelect( $select );
    if( !$(this).is('.choose-wrapper') && !$(this).is('.is_visible')) {
      $this.hide();
    }
  });

  $('#add-another ul a').click(function(e){
    section_id = $(this).data('section');
    $('#search-wrap'+section_id).remove().appendTo('#option-listing').show().find('.search-replaced-select').show().parent().find('label').addClass('coloured');
    $('#add-another-link span').removeClass('accessibility');
    $('#add-another-show-all').hide();
    $(this).parent().hide().parent().hide();
    e.preventDefault();
  });

  $('.fake-link').live('click', function(e){
    $('#whatson-submit').show();
    $link = $(this);
    $li = $link.parent();
    $ul = $li.parent();
    $scroll = $ul.parent();
    $el = $scroll.parent();
    $wrapper = $el.parent();
    index = $link.data('index');

    select_id = $link.data('parent');
    $select = $('#'+select_id);

    val = $link.data('value');
    /*if(val=="{all}") {
      $select.get(0).selectedIndex = 0;∂
      kind = select_id.split('-');
      $('#add-link-'+kind[0]).show();
      $wrapper.hide();
    } else {*/
      $select.get(0).selectedIndex = index;
      text = $link.text();
      $wrapper.find('.selected-value').text( text );
      $wrapper.find('label').removeClass('coloured');
      $el.hide();
    //}
    e.preventDefault();
  })

  $('.selected-value').live('click', function(e){
   $(this).parent().parent().parent().find('.search-replaced-select').show();
   // $(this).closest('.search-replaced-select').show();
    //.find('.search-replaced-select').show();
    e.preventDefault();
  });

  initSearchCriteriaCarosel();
  if(!$.browser.msie) {
    initSearchHoverEffect();
  }
});

function replaceSelect( $item ) {
  $wrapper = $item.parent().parent().parent();

  list_html = '';
  var column_height = 7;
  var column_count = 1;

  list_html += '<ul class="din-font replaced-select clearfix">';

  hide = true;
  $item.find('option').each(function(i) {
    text = $(this).text();
    val = $(this).val();
    if(val != '') {
      if( $(this).is(':selected') ) {
        hide = false;
      }
      list_html += '<li><a class="fake-link" href="#" data-index="'+ i +'" data-value="'+ val +'" data-parent="'+ $item.attr('id') +'">'+text +'</a></li>';
      if((i%column_height)==0) {
        list_html += '</ul><ul class="din-font replaced-select clearfix">';
        column_count++;
      }
    }
  });

  list_html += '</ul>';

  $item.hide( );

  if(!hide) {
    $wrapper.addClass('is_visible');
  }

  $replace = $wrapper.find('.search-replaced-select .search-column-holder');
  $replace.html( list_html ).css('width', (column_count*253)+'px').parent().hide();
}

function initSearchHoverEffect() {
  $(".fake-link").live('mouseover mouseout', function(event){
    effected_item = $(this);
    if(event.type == 'mouseover') {
			if(!$(effected_item).hasClass("hovered")) {
				$(effected_item).addClass("hovered");
				$(effected_item).animate( { backgroundColor: 'rgba(255, 255, 255, .3)' }, 200);
			}
    } else {
			$(effected_item).removeClass("hovered");
			$(effected_item).animate( { backgroundColor: 'transparent' }, 200);
    }

  });
}
