(function($) {
  $.fn.smartColumns = function(options) {
    var opts = $.extend({}, $.fn.smartColumns.defaults, options);

    // Get width
    $(this).css({ 'width' : "100%"})
    var colWrap = $(this).width();
    var colNum = Math.floor(colWrap / opts.dimensions);
    var colFixed = Math.floor(colWrap / colNum);
    $(this).css({ 'width' : colWrap})
    $(this).children('li').css({ 'width' : colFixed}).removeClass('clear');;
    
    // Count new rows
    var rowCount = ( $('li', this).size() / colNum );

    // Group items per row
    for( i=1; i<=rowCount+1;i++){
      lastEl = (colNum*i);
      firstEl = (lastEl-colNum);

      current = $("li:lt("+lastEl+"):not(:lt("+firstEl+"))", this);
      tallest = (opts.minHeight) ? opts.minHeight : 0;
      
      // Get height of tallest item
      current.each(function(i) {
        // Reset height
        $(opts.el, this).children().wrapAll('<div class="holder"></div');
        $('.holder', this).css('height','auto');
        
  			if( $(opts.el, this).height() >= tallest) {
  				tallest = ( $(this).height() ) - opts.elPad
  			}

  		});
  		
  		// Set all items on row to same tallest height
  		if((opts.maxHeight) && tallest > opts.maxHeight) tallest = opts.maxHeight;
			$('.holder', current).height(tallest);

    };

  };
   
  $.fn.smartColumns.defaults = {
    dimensions : 200,
    minHeight : 0,
    maxHeight : 0,
    el : '*',
    elPad : 0
  };

})(jQuery);



//Each time the viewport is adjusted/resized
function onWindowResize() { 
  $('ul.products').smartColumns({ 
	  dimensions: 250,
	  minHeight: 90,
	  el: '.item_body',
	  elPad: 20
	});
  $('ul.products_small').smartColumns({ 
	  dimensions: 250,
	  minHeight: 50,
	  el: 'a',
	  elPad: 20
	});
} 
 
$(document).ready(function () {
  
  if(document.all) { // for IE
    $('body').append('<div id="resizeCheck"></div>');
    $('#resizeCheck').resize( onWindowResize );
  } else { // for Mozilla
    $(window).resize( onWindowResize );
  }
  
  $("#search_keyword, #login_username, #login_password").labelify({ text: "label" });
  
  // Create rounded corners
  $("#content h2.intro, #content .pagination, #content .tags, #content .notice").corners("10px");
  $(".products_small li .price strong, #detail .price strong").corners("4px");
  $("#networksites").corners('top-right 10px bottom-right 10px bottom-left 10px');
  $("#networkbtn").corners('bottom-right 10px');
  
  
  onWindowResize()
  
  /*
  if(bHome) {
	  $('#other_shops').flash({
	    swf: LINKROOTSTATIC+'/flash/frontend/eenkadovoor.swf',
	    width: 900,
	    height: 42,
	    wmode: 'transparent'
	  })
  } else {
	  $('#other_shops').flash({
		    swf: LINKROOTSTATIC+'/flash/frontend/eenkadovoor_noanim.swf',
		    width: 900,
		    height: 42,
		    wmode: 'transparent'
		  })	  
  }
  */
});

