/*
 * File for executing general js function on document ready
 */
$(function() { 

  // add some shadow to the search box header
  $("#search-box .header span").dropShadow({left: 2, top: 2, blur: 0});

  // remove ie flicker.
  if($.browser.msie) try { document.execCommand("BackgroundImageCache", false, true); } catch(error) {};

  // hide advanced search options & drilldown-menu
  $('#search-box .dropdown, #quick-search-results').hide();
  $('#quick-search-results').hide();

  // Construct the tabs in search box
  $("#facts-result > #model-list > #facts-tabs > ul.tabs").tabs();

  // free-text search in extendedsearch
  $('#search-box-article #input-search-form-article #sok.enable-quick-search-article').searchPreview();

  // remove quick search results when clicking outside the layer
  $().click(function(event) {
    if(!$(event.target).parents('div#quick-search-results').length) {
      $('div#quick-search-results').slideUp(200);
    }
  });

  // you shouldnt be able to use the submit button under car facts
  $('#search-box span.submit-search.disable').click(function(event) {
    return false;
  });

  // Activate free-text-search tab when clicking on free search input
  $('#free-text-form .input-text').focus(function(){
    // Dont activate the tab if it's already active
    if ($('#dropdown-tab').is('.ui-tabs-selected')) {
      $('#text-search-tab a').click();
    }
  });

  // hide dropdown layers, slider needs to be calculated in a block layer.
  $('#search-box-facts div.dropdown, #search-box div.dropdown, .search-box div.dropdown, #drilldown-extended').hide().css('left',0);

  // Construct the drilldown menu code is in aom.lib.js.
  $('#search-box-facts ul.drilldown, #search-box ul.drilldown, .search-box ul.drilldown').drilldown();

  //$('#drilldown-extended-link a.extend').click();

  /*
  setTimeout(function() {
    //$('#drilldown-make > a').click();
    $('#drilldown-make div.dropdown a.click-me:eq(0)').click();
  }, 1000);
  */
 
  if ($.browser.msie) {
    // set max size to the large ad image
    var adLargeImageWidth = $('#ad #large img').width();
    var adLargeImageMaxWidth = 638;
    if (adLargeImageWidth > adLargeImageMaxWidth) {
      $('#ad #large img').css('width', adLargeImageMaxWidth);
    }
  }

  // set min width to the boxes in the ajax search layer
  var ajaxBoxHeight = $('#search-meta-layer .section').height();
  var ajaxBoxMinHeight = 150;
  if (ajaxBoxHeight < ajaxBoxMinHeight) {
    $('#search-meta-layer .section').css('height', ajaxBoxMinHeight);
  }

  // Switch images on ad page
  $('#ad #small a').click(function() {
    if($(this).is('.active')) return false;
    // remove active class from active image
    $('#ad #small a.active').removeClass('active'); 
    // add active class to active item
    $(this).addClass('active');
    // replace the large image with the new image src, from active image links href.
    $('#ad #large img').attr('src',this.href);  
    // cancel normal event
    return false;   
  });

  // Toggle other car versions
  $('#facts-result a.popup-link-tag').click(function() {
    $('div.popup-link .dropdown').toggle();
    return false;
  });

  // Toggle emission information
  $('#facts-result a.emission-popup-link-tag').click(function() {
    $('div.emission-popup-link .dropdown').toggle();
    return false;
  });

  // toggle extended facts search
  $('#search-box a#extend-facts-search').click(function() {
    var layer = $('#search-box div#facts-search-extended');
    if (layer.is(':visible')) {
      $(this).removeClass('active');
    } else {
      $(this).addClass('active');
    }
    layer.slideToggle(200);
    return false;
  });

  // Toggle environment details
  $('a.toggler').click(function() {
    var $target = $('#' + this.href.split('#')[1]);
    if($target.length > 0) {
      if(!$target.is(':visible')) {
        $(this).text('Dölj info');
        $target.slideDown();
      } else {
        $(this).text('Mer info');
        $target.slideUp();
      }
    };
    return false;
  });

});
