jQuery(function(){
    
    // Init scroll max size
    var nodes_w  = document.getElementById('slider1').children.length*150+1;
    var window_w = $(window).width() - $('#result-panel').outerWidth();
    var scroll_w = nodes_w > window_w ? window_w : nodes_w;
    
    $("#slider1").mopSlider({
        'w':scroll_w ,
        'h':382,
        'sldW':845,
        'btnW':190,
        'itemMgn':1,
        'indi':"",
        'shuffle':1,
        'auto':'n',
        'move':1000,
        'interval':2000,
        'type':'paper'
    });
    $('#slider1').delegate('.slidediv','mouseover',function(){
        $(this).addClass("selectedtest");
    }).delegate('.slidediv','mouseout',function(){
        $(this).removeClass("selectedtest");
    });
    
    $(".txt-small").tipTip({
        'attribute':'rel', 
        'defaultPosition' : 'right',
        'activation':'focus'
    });
	   
    search_coupons = function()
    {
        // Display loading
        $('#search-loading-mask').show();
        $('#search-loading').show();
        
        $.post('/user/index/search-ajax/', $(document.forms['search']).serialize(), function(result) 
        {
            // Hide loading
            $('#search-loading-mask').hide();
            $('#search-loading').hide();
                
            //i_estatSearch(city, cat, '', result.total);
                
            $('#slider1').html(result.output);
            $('#result-panel').html(result.panel);
                
            // Update mobSlider with new width content
            mobSlider_update(result.total*150+1);
               
        //menu_home();
        }
        , 'json');
    }
    
    $('#categories').selectmenu({
        change: function(){
            
            // Delete subcategories
            if( document.forms['search'].subcategory.value.length )
            {
                document.forms['search'].subcategory.value = '';
                document.forms['search'].keyword.value = '';
                $('#close-keyword').show();
            }
            
            search_coupons();
        },
        style:'dropdown',
        icons: [
        {
            find: '.fr', 
            icon: 'ui-icon-script'
        },
        {
            find: '.image', 
            icon: 'ui-icon-image'
        }
        ]
    });    
    
    $('#city').clearableTextField({
        'callback': function(){
            $('#city').val('Saisissez une ville au choix_');
            $('#city_value').val('');
            $('#city_autocomplete').hide(); 
            search_coupons();
        }
    });

    $('form#search').submit(function(){
        return false;
    });

    $('#categories-menu li.cat-choix-default  a').html('Toutes les catégories');
   
    // Slider gps diameter
    $('#cursor-diameter').sliderIt(function(){
        search_coupons();
    });
    $('#cursor-diameter2').sliderIt(function()
    {
        var val = $('#gps_diameter2').val();
      
        // Update form search
        var target = $('#cursor-diameter');
        target.find('a.scroll').css('left', (val*0.8)+'px');
        target.find('input:hidden').val(val);
        target.find('span.noir').html(val + ' Km');
      
        // Make search
        search_coupons();
    });
  
    // Keywords autocomplete
    //$('#keyword').autocompleteIt('/user/index/get-keywords', function(id, label)
    $('#keyword').autocompleteIt('/user/index/get-keywords', false, function(id, label)
    {
        $('#keyword').val(label);
      
        // Clear old ID value
        document.forms['search'].merchant.value     = '';
        document.forms['search'].subcategory.value  = '';
            
        // Merchant
        if( id.length >= 9 && id.substr(0, 9) == 'merchant-' )
        {
            document.forms['search'].merchant.value = id.split('merchant-')[1];
        }
      
        // SubCategory
        if( id.length >= 9 && id.substr(0, 9) == 'category-' )
        {
            document.forms['search'].subcategory.value = id.split('category-')[1];
        }
      
        search_coupons();
      
        // Show close-key
        $('#close-keyword').show();
    });
  
    // City autocomplete
    $('#search-city').autocompleteIt('/user/index/get-cities', false, function(id, label)
    {
        document.forms['search'].city.value      = id;
        document.forms['search'].city_name.value = label;
        search_coupons();
      
        // Show close-key
        $('#close-city').show();
    });
  
    // Click on delete cross
    $('#close-city').click(function()
    {
        document.forms['search'].city.value      = '';
        document.forms['search'].city_name.value = 'Saisissez une ville au choix';
        $(this).hide();
        search_coupons();
        return false;
    });
  
    // Click on delete cross
    $('#close-keyword').click(function()
    {
        document.forms['search'].merchant.value     = '';
        document.forms['search'].subcategory.value  = '';
        document.forms['search'].keyword.value  = 'RECHERCHE PAR MOT CLE OU ENSEIGNE';
        $(this).hide();
        search_coupons();
        return false;
    });
  
    // Disable text selection for slider coupons
    $('#slider1').disableSelection();

});

