/********************************************************************
|*
|*	jquery script for page
|*
|********************************************************************/

$(document).ready(function()
{

   /**************************
    *    PAGINATION
    *************************/
   load_products = function(){

      if( $(this).attr('href')  )
      {
         //odstranimo event
         $(this).unbind();
         mypage = $(this).attr('href');
         mypage = mypage.replace('#/','/');
         
         mypage_tmp = mypage.split('/')
         mypage = mypage_tmp[mypage_tmp.length -1];
         
         
         tmp_url = window.location.href
         tmp_url = tmp_url.split('#/');
         
         ajax_url = tmp_url[0] +'/'+ mypage;
         
      }

      else
      {
         tmp_url = window.location.hash
         tmp_url = tmp_url.replace( '#/', '' );
         
         loc = window.location.href
         loc = loc.replace( '#/', '/' );
         
         console.log( loc );
         ajax_url = loc
      }
      
      $.ajax({
         url: ajax_url,
         type: "POST",
         dataType: "html",
         beforeSend: function()
         {
            $('.pagination').html('<img src="/images/ajax-loader.gif" alt="Nalagam" />');

         },
         success: function( data )
         {
            $('#catalog').html( data );
            $('.pagination a').click( load_products );
         },
         complete: function()
         {
            $( 'html, body' ).animate( {
               scrollTop: 180
            }, 'slow'  );
         }
      });
   }

   $('.pagination a').click( load_products );


   if( $('#catalog').length > 0 )
   {
      tmp_url = window.location.hash
      tmp_url = tmp_url.replace( '#/', '' );


      if( tmp_url == '' )
      {
         $('#catalog').show();
      }
      else
      {
         load_products( );
         $('#catalog').show();
      }
   }
   
   
   /***********************************
    * FILTER
    ***********************************/
   
   filtering = function( e ){
      
      e.preventDefault();
      
      var new_link = 'http://' + window.location.host ;
      
      current_path = window.location.href;
      
      if( current_path.indexOf('replike') > -1 )
         new_link = new_link + '/replike'
      
      else if( current_path.indexOf('original') > -1  )
         new_link = new_link + '/original'
      
      else if( current_path.indexOf('pnevmatike') > -1  )
         new_link = new_link + '/pnevmatike'
      
      
     
     
      
      $('#filter-form > p > select').each( function( )
      {
         if( $(this).find(":selected").val() != 'all' )
         {
            new_link = new_link +'/'+ $(this).attr('name') + '/' +  $(this).find(":selected").val()
         }
      });
      
      $('#filter-form > p > input[type="text"]').each( function()
      {
         
         new_name = $(this).val();
         
         if( new_name.length > 0 )
         {
            new_name = new_name.replace( " ", "+" );
            new_link = new_link +'/'+ $(this).attr('name') + '/' +  new_name ;
         }
      });
      
      window.location = new_link
      
   }
   
   $('#filter').click( filtering );
   $('#filter-form').submit( filtering );
   
   
   
   $('#filter-view').click( function() {
      $('.filter').slideToggle();
      
   });

});
