if( !window.console ) {
   window.console = { log: function() {} };
}

$( document ).ready( 
   function() {
   
      $( "#menu a" ).addClass( "f-bold" );
      $( "h1" ).addClass( "f-bold" );
      $( "h2" ).addClass( "f-light" );
      $( "h3" ).addClass( "f-book" );
      
      $( "#main .post h3" ).addClass( "text-orange" );
         
      setHeaderImages();
      superfloat_grouped_stores();
      
      no_right_column();
      
      // resets
      $( ".block, .content" ).each( 
         function() {
            $( this ).find( "p:last" ).css({ "margin-bottom": 0 });
         }
      );
      
      
      $( ".store-contact" ).each( 
         function() {
            if( $( this ).children().length == 0 ) $( this ).remove();
         }
      );
      
      
      $( ".content img" ).each( 
         function() {
            if( 
               ( 
                  $( this ).hasClass( "alignnone" ) || 
                  $( this ).hasClass( "alignleft" ) || 
                  $( this ).hasClass( "alignright" )
               )
               &&
               $( this ).parent().is( "a" ) 
            ) {
               
               $( this ).parent().lightBox({});  
            }
         }
      );
      
      if( $.browser.msie && parseFloat( $.browser.version ) < 8 ) ie_haxx();
      
      
      setTimeout( setMenu, 500 );
      
      
      
      
      $( "#main .post a img" ).each( 
      	function() {
      		$( this ).parent().css({ borderBottom: 0 });
      	}
      );
      
   }
);


function setMenu() {
   
   
   $( "#menu li:first" ).css({ "padding-left": 0 });
      
      
   var allWidth = 0;
   $( "#menu li" ).each( 
      function() {
         allWidth += $( this ).outerWidth();
      }
   );
   
   var restWidth = ( $.browser.msie && $.browser.version < 8 ? 962 : 941 ) - allWidth;
   
   var numOfMenus = $( "#menu li:not( :first )" ).length;
   
   $( "#menu li:not( :first )" ).each( 
      function() {
         
         var current_padding = parseInt( $( this ).css( "padding-left" ) );
         var new_padding = Math.floor( current_padding + ( restWidth / numOfMenus ) );
         $( this ).css( "padding-left", new_padding );
         
      }
   );
}




function ie_haxx() {
   
   $( "*" ).each( 
      function() {
         
         if( $( this ).css( "display" ) == 'inline-block' ) {
            $( this ).css({
               display: "inline",
               zoom: 1
            });
         }
      
      }
   );
}


var current_header_image = -1;
var toggleSpeed = 8000;
var fadeSpeed = 1500;


function setHeaderImages() {
   
   var width = $( ".header-images" ).width();
   var height = $( ".header-images" ).height();
   
   $( ".header-images a" ).attr( "href", home ).removeAttr( "target" );
   
   
   var header_images = $( ".header-images img" )
      .each( 
         // trigger load-event.
         function() {
            $( this ).attr( "src", $( this ).attr( "src" ) + "?rand=" + Math.round( Math.random() * 100000 ) );
         }
      )
      .load(
         function() {
            
            var mw = $( this ).width();
            var mh = $( this ).height();
            
            var diff = width / mw;
            //$( this ).css({ width: width, height: mh * diff });
            
         }
      );

   toggleHeaderImage( true );
   
   
   $( "input[type=text], textarea" ).each( 
      function() {
         
         if( !$( this ).attr( "id" ) ) {
            $( this ).attr( "id", $( this ).attr( "name" ) );
         }
      }
   );
   
   $( "label" ).parent().css({ position: "relative" });
   
   $( "label" ).each(
      function() { 
         
         if( !$( "#" + $( this ).attr( "for" ) ).is( "input[type=text], textarea" ) ) return;
         
         $( this )
            .css({ 
               "position": "absolute", 
               "left": 0, "top": 0, 
               "text-transform": "none", 
               "line-height": "2.3em", 
               "text-indent": "0.5em",
               "z-index": 100,
               "margin": 0, "padding": 0,
               "color": "#666", "font-style": "italic"
            })
            .inFieldLabels();
            
      }
   );
   
}

function toggleHeaderImage( direct ) {

   direct = ( direct || false );
   
   var header_images = $( ".header-images li" );
   
   header_images.eq( current_header_image ).fadeOut( !direct ? fadeSpeed : 0 );
   
   var new_header_image;
   do {
      new_header_image = Math.floor( Math.random() * header_images.length );
   } while( current_header_image == new_header_image && header_images.length > 1 );
   
   header_images.eq( new_header_image ).fadeIn( !direct ? fadeSpeed : 0 );
   
   current_header_image = new_header_image;
   
   setTimeout( toggleHeaderImage, toggleSpeed );
}      

function superfloat_grouped_stores() {
   
   if( $( ".grouped-stores" ).length == 0 ) return;
   
   var allHeight = $( ".grouped-stores" ).height();
   var store_categories = $( ".grouped-stores > li" );
   
   var leftColumn = $( "<li>" ).css({ float: "left", width: 280 }).appendTo( $( ".grouped-stores" ) );
   var rightColumn = $( "<li>" ).css({ float: "right", width: 280 }).appendTo( $( ".grouped-stores" ) );
   
   $( "<li>" ).addClass("clearer").appendTo( $( ".grouped-stores" ) );
   
   
   var filledHeight = 0;
   store_categories.each( 
      function() {
         $( this ).appendTo( ( filledHeight < allHeight * 0.5 ) ? leftColumn : rightColumn );
         filledHeight += $( this ).height();  
         
         
      }
   );  
}





function no_right_column() {
   
   if( $( "#right-column ul li" ).length > 0 ) return;
   
   
   $( "#right-column" ).remove();
   $( "#left-column" ).css({
      width: "inherit",
      float: "none"
   });
   
   

}
