

jQuery(document).ready(function($) {
    // $() will work as an alias for jQuery() inside of this function
    
    
    /* ### FRAMSIDAN ### ##############################################################
     *
     * Kolla ifall vi är på framsidan. Är vi det vill vi 
     * gå igenom alla widgets och sätta lite klasser.
     */
    
    if( $("#frontpage-content").length > 0 ) {
    	
	   	var index 			= 0;
    	var max_height 		= 0;
    	var widget_counter 	= 1;    
    	
    	$("#frontpage-content .widget").each( function() {    	
    		
    		console.log( max_height, $(this).height());
    		
    		if( $(this).height() > max_height ) {
    			max_height = $(this).height();
    			index = widget_counter - 1;
    		}    		
    		
    		if( widget_counter <= 3 ) { 
    			$(this).addClass('top_row');
    		}    		
    		
    		if( widget_counter == 2 || widget_counter == 5 || widget_counter == 8 ) { 
    			$(this).addClass('middle_column');
    			$(this).css('width', "300px");
				$(this).css('padding-right', "5px");
				$(this).css('padding-left', "5px");
				$(this).css('margin-right', "5px");
				$(this).css('margin-left', "5px");
    		}
    		
    		// Om vi passerat tredje elementet så lägger vi till
    		// en hr innan vi går vidare
    		
    		if( widget_counter == 3 ) {
    			$(this).after("<hr />");
    		}
    		
    		widget_counter++;
    	});
    	
	   	// Gör alla widgets lika höga
    	$("#frontpage-content .widget").each( function() { $(this).css('height', max_height + "px"); } );
    	
    }
    
    
    
    
    
    
    
    
   /* ### HANTVERKSLISTAN ### ##########################################################
    * 
    * Nedanstående avsnitt hanterar hantverkslistan under hantverkshjälpen.
    * Den visar/döljer hantverkare i listan beroende på vad användaren gör 
    * för val i listorna över verksamhetsorter och kategorier.
    */
    
    $("#hantverkarlistan-orter").change( visaValdaHantverkare );    
    $("#hantverkarlistan-kategorier").change( visaValdaHantverkare );
    
    function visaValdaHantverkare( e )
    {
      // Vi går först igenom hela listan och visar alla hantverkare
      $("#hantverkarlistan .hantverkare").each( function( index ){
        $(this).css( "display", "table-row" );
      });
      
      // Vi går igenom varje rad i tabellen och kollar om vi ska tända/släcka den
      $("#hantverkarlistan .hantverkare").each( function( index ){
        
        var haystack_orter      = $(this).children(".orter").html().toLowerCase();
        var haystack_kategorier = $(this).children(".kategorier").html().toLowerCase();
        
        var target_ort          = $("#hantverkarlistan-orter option:selected").val().toLowerCase();  
        var target_kategori     = $("#hantverkarlistan-kategorier option:selected").val().toLowerCase();
        
        if( target_ort == "0" && target_kategori == "0" ){
          $(this).css( "display", "table-row" );
        }
        
        else if( target_ort == "0" && target_kategori != "0" ){
          if( haystack_kategorier.indexOf(target_kategori) == -1 ){
            $(this).css( "display", "none" );
          }
        }
        
        else if( target_ort != "0" && target_kategori == "0" ){
          if( haystack_orter.indexOf(target_ort) == -1 ){
            $(this).css( "display", "none" );
          }
        }
        
        else{
          if( haystack_kategorier.indexOf(target_kategori) == -1 || haystack_orter.indexOf(target_ort) == -1 ){
            $(this).css( "display", "none" );
          }
        }        
        
      });
      
    }
    
    
    $("#hantverkarlistan tr:odd").addClass('odd');
    
    
    /* ### END HANTVERKSLISTAN ############################################################ */
    
});

