$(function()
{
  $('#auth_form').css('z-index', 10000);
  $('.ads').css('z-index', 9000);
  
  // sign in
  
  $('#sign_in').click(function()
  {
    $('form[name="sign_in"]').slideToggle("fast");
    $(this).toggleClass("down");
  });
  
  // dropdown

  $('.dropdown a.down, .dropdown a.up').click(function()
  {
    $(this).parent().find('ul').slideToggle("fast");
    $(this).toggleClass('down');
    $(this).toggleClass('up');
  });

  // categories

  $('#categories .category').click(function()
  {
    $('#categories li ul').slideUp('fast');
    $(this).parent().find('ul').slideDown('fast');
  });

  // slideshow
/*
  $("#slideshow").scrollable
  ({
    interval: 5000,
    loop: true,
    speed: 1000,
    size: 7,
    easing: 'custom',
    next: '#partners .next a',
    prev: '#partners .previous a'
  });
*/

$('#slideshow').before('<div id="nav-slideshow">').cycle(
  {
    fx: 'fade',
    timeout: 6000,
    height: 281,
    fit: 1,
    containerResize: 0,
    pager:  '#nav-slideshow'
  });


$('#all-news').before('<div id="nav-news">').cycle(
  {
    fx: 'fade',
    timeout: 6000,
    height: 165,
    fit: 1,
    containerResize: 0,
    pager:  '#nav-news'
  });


  /*
  $("#other_ads").scrollable
  ({
    interval: 5000,
    loop: true,
    speed: 1000,
    size: 1,
    easing: 'custom'
  });*/

  $.easing.custom = function(x, t, b, c, d)
  {
    if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
    return c/2*((t-=2)*t*t*t*t + 2) + b;
  };

  // region ajax

  $('#region_id').change(function()
  {
    getCities(this, $('#city_id'));
  });

  $('#search_region_id').change(function()
  {
    $('#search_city_id').attr('disabled', 'disabled');
    $(this).parents('form:first').submit();
  });
  
  $('#search_store').change(function()
  {
    $(this).parents('form:first').submit();
  });
  
  $('#search_city_id').change(function()
  {
    $(this).parents('form:first').submit();
  });
  
  $('form[name="search"]').submit(function()
  {
    var input = $($(this).find('input[name="keywords"]'));
    
    if(input.val() == 'Mots-clés')
      input.val('');
      
    return true;
  });

  function getCities(regionsSelect, citiesSelect)
  {
    var getJSON = false;

    citiesSelect.children('option[value!=""]').remove();

    if($(regionsSelect).val() != '')
    {
      getJSON = $.getJSON('http://www.submagazine.ca/location/cities/' + $(regionsSelect).val(), function(json)
      {
        if(json.length > 0)
        {
          citiesSelect.removeAttr('disabled');

          for(var i = 0; i < json.length; i++)
          {
            citiesSelect.append('<option value="' + json[i].id + '">' + json[i].name + '</option>');
          }
        }
      });
    }

    if(getJSON == false)
    {
      citiesSelect.attr('disabled', 'disabled');
    }
  }

  // form

  $('.submitForm').live('click', function()
  {
    $(this).parents('form:first').trigger('submit');
  });

  // popup

  $('a.openPopup').click(function()
  {
    window.open($(this).attr('href'), 'popup', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=650,height=545,left=355,top=134');
    return false;
  });
  
  // ajax
  
  $('.ajax').live('click', function()
  {
    var rel = $(this).attr('rel');
    
    $.get($(this).attr('href'), function(data)
    {
      $(rel).html(data);
    });
    
    return false;
  });
  
  /*
  $('form').live('submit', function(e)
  {
    alert('dsf');

    e.returnValue = false;
    e.preventDefault();
    return false;
  });*/

  // top-bar

  $('#sort').change(function()
  {
    $('form[name="top-nav"]').submit();
  });
});

var addthis_language = "fr";
