(function($){
  $.fn.fakeXFBML = function(fb_tag, params){
    var container = $(this);
    params = params || {};
    fb_tag = $('<fb:' + fb_tag + '></fb:' + fb_tag + '>');
    $.each(params, function( key , value ) {
      fb_tag.attr( key , value );
    });
    fb_tag.appendTo(container);
  };
})(jQuery);

function xsRefreshWizardScooterBrand(type) {
  $.ajax({
    url: '/index.php?p=actions&action=filters/load_scooter_manufacturers',
    data: 'machine_type=' + type,
    cache: false,
    beforeSend: function(){
      $('select#brand').attr('disabled', true);
      $('select#model').attr('disabled', true);
      $('select#brand').html('<option>načítám...</option>');
    },
    success: function(html){
      $('select#brand').html(html);
      $('fieldset.options p.brand span.ico-erase').css('display', 'none');
      $('select#brand').attr('disabled', false);
      $('select#model').attr('disabled', false);
    }
  });
}

function xsRefreshWizardScooterModel(id) {
  $.ajax({
    url: '/index.php?p=actions&action=filters/load_scooter_models',
    data: 'brand_id=' + id + '&machine_type=' + machineType,
    cache: false,
    beforeSend: function(){
      $('select#brand').attr('disabled', true);
      $('select#model').attr('disabled', true);
      $('select#model').html('<option>načítám...</option>');
    },
    success: function(html){
      $('select#model').html(html);
      $('fieldset.options p.model span.ico-erase').css('display', 'none');
      $('select#brand').attr('disabled', false);
      $('select#model').attr('disabled', false);
    }
  });
}

// set global variables
var machineType = '';

// onload events
$(document).ready(function() {

  // Facebook "like box"
  $('div#like-box').fakeXFBML('like-box',{
    'href': 'http://www.facebook.com/pages/Scootershop/131707166918205',
    'height': '64',
    'width': '244',
    'colorscheme': 'light',
    'stream': 'false',
    'header': 'false',
    'show_faces': 'false',
    'border_color': '000000'
  });

  // Facebook "like" button
  $('div#facebook-like-button').fakeXFBML('like',{
    'width': '385',
    'action': 'like',
    'show_faces': 'true',
    'font': 'arial'
  });

  // set machine type
  machineType = $('fieldset.machine-type input:checked').val();

  // search form
  $('#searching-form').submit(function(){
    if ($('#searching-search-text').val() == '') {
      alert('Zadejte prosím hledaný výraz');
      return false;
    }
    if ($('#searching-search-text').val().length < 3) {
      alert('Hledaný výraz musí obsahovat alespoň tři znaky');
      return false
    }
  });

  // filters
  $('fieldset.machine-type p').click(function(){
    machineType = $(this).find('input').val();
    xsRefreshWizardScooterBrand(machineType);
    xsRefreshWizardScooterModel(0);
  });

  $('select#brand').change(function(){
    var brandId = $(this).val();
    xsRefreshWizardScooterModel(brandId);
  });

  $('select#model').change(function(){
    $('fieldset.options p.model span.ico-erase').css('display', 'none');
  });

  // changes layout type
  $('div.sorting select').change(function(){
    $('div.sorting select').attr('readonly', 'readonly');
    $('form#list-settings').submit();
  });

  // lightbox
  $(function() {
    $('a[rel*=lightbox]').lightBox({
      fixedNavigation:true,
      containerResizeSpeed: 350,
      txtImage: 'Obrázek',
      txtOf: 'z'
    });
  });

});

