/**
 *	Grid functions
 *
 *	Developed for Greenflame
 *	November 2011
 *
 *	Version 1.0
 */
 
//-------------------------------------------------------------------


/**
 *	Scale grid items
 *
 *	This function scales the div's in the grid view so they have
 *	the same height and images plus text stand in the same row
 */
function scaleGridItems() {	
	// Remove height style on all elements
	$(".grid *").css('height','');
	
	// Set number of elements in row
	var _cols = 3;
	
	// Scale billede a container
	var _count = 0;
	var _height = 0;
	var _rowNum = 0;	
	var _rowHeights = new Array();
	var _productCount = 0;
	$(".grid .billede a").each(function() {
		if(_count < _cols) {
			if($(this).height() > _height) {
				_height = $(this).height();
				_rowHeights[_rowNum] = _height;
			};
			_count++;
		} else {
			_rowHeights[_rowNum] = _height;
			_rowNum++;
			_height = $(this).height();
			_count = 1;
			_rowHeights[_rowNum] = _height;
		};
		_productCount++;
	});
	
	var _currentRow = 0;
	var _colCount = 0;
	$(".grid .billede a").each(function() {
		if(_colCount < _cols) {
			$(this).height(_rowHeights[_currentRow]);
			_colCount++
		} else {
			_colCount = 1;
			_currentRow++;
			$(this).height(_rowHeights[_currentRow]);
		};
	});
	
	
	// Scale farver container
	var _count = 0;
	var _height = 0;
	var _rowNum = 0;	
	var _rowHeights = new Array();
	var _productCount = 0;
	$(".grid .billede ul").each(function() {
		if(_count < _cols) {
			if($(this).height() > _height) {
				_height = $(this).height();
				_rowHeights[_rowNum] = _height;
			};
			_count++;
		} else {
			_rowHeights[_rowNum] = _height;
			_rowNum++;
			_height = $(this).height();
			_count = 1;
			_rowHeights[_rowNum] = _height;
		};
		_productCount++;
	});
	
	var _currentRow = 0;
	var _colCount = 0;
	$(".grid .billede ul").each(function() {
		if(_colCount < _cols) {
			$(this).height(_rowHeights[_currentRow]);
			_colCount++
		} else {
			_colCount = 1;
			_currentRow++;
			$(this).height(_rowHeights[_currentRow]);
		};
	});

	
	// Scale billede container
	var _count = 0;
	var _height = 0;
	var _rowNum = 0;	
	var _rowHeights = new Array();
	var _productCount = 0;
	$(".grid .billede").each(function() {
		if(_count < _cols) {
			if($(this).height() > _height) {
				_height = $(this).height();
				_rowHeights[_rowNum] = _height;
			};
			_count++;
		} else {
			_rowHeights[_rowNum] = _height;
			_rowNum++;
			_height = $(this).height();
			_count = 1;
			_rowHeights[_rowNum] = _height;
		};
		_productCount++;
	});
	
	var _currentRow = 0;
	var _colCount = 0;
	$(".grid .billede").each(function() {
		if(_colCount < _cols) {
			$(this).height(_rowHeights[_currentRow]);
			_colCount++
		} else {
			_colCount = 1;
			_currentRow++;
			$(this).height(_rowHeights[_currentRow]);
		};
	});
		
	
	// Scale overskrift
	var _count = 0;
	var _height = 0;
	var _rowNum = 0;	
	var _rowHeights = new Array();
	var _productCount = 0;
	$(".grid .item h3").each(function() {
		if(_count < _cols) {
			if($(this).height() > _height) {
				_height = $(this).height();
				_rowHeights[_rowNum] = _height;
			};
			_count++;
		} else {
			_rowHeights[_rowNum] = _height;
			_rowNum++;
			_height = $(this).height();
			_count = 1;
			_rowHeights[_rowNum] = _height;
		};
		_productCount++;
	});
	
	var _currentRow = 0;
	var _colCount = 0;
	$(".grid .item h3").each(function() {
		if(_colCount < _cols) {
			$(this).height(_rowHeights[_currentRow]);
			_colCount++
		} else {
			_colCount = 1;
			_currentRow++;
			$(this).height(_rowHeights[_currentRow]);
		};
	});
	
	
	// Scale beskrivelse container
	var _count = 0;
	var _height = 0;
	var _rowNum = 0;	
	var _rowHeights = new Array();
	var _productCount = 0;
	$(".grid .beskrivelse").each(function() {
		if(_count < _cols) {
			if($(this).height() > _height) {
				_height = $(this).height();
				_rowHeights[_rowNum] = _height;
			};
			_count++;
		} else {
			_rowHeights[_rowNum] = _height;
			_rowNum++;
			_height = $(this).height();
			_count = 1;
			_rowHeights[_rowNum] = _height;
		};
		_productCount++;
	});
	
	var _currentRow = 0;
	var _colCount = 0;
	$(".grid .beskrivelse").each(function() {
		if(_colCount < _cols) {
			$(this).height(_rowHeights[_currentRow]);
			_colCount++
		} else {
			_colCount = 1;
			_currentRow++;
			$(this).height(_rowHeights[_currentRow]);
		};
	});
	
	
	// Scale specifikationer container
	var _count = 0;
	var _height = 0;
	var _rowNum = 0;	
	var _rowHeights = new Array();
	var _productCount = 0;
	$(".grid .specifikationer").each(function() {
		if(_count < _cols) {
			if($(this).height() > _height) {
				_height = $(this).height();
				_rowHeights[_rowNum] = _height;
			};
			_count++;
		} else {
			_rowHeights[_rowNum] = _height;
			_rowNum++;
			_height = $(this).height();
			_count = 1;
			_rowHeights[_rowNum] = _height;
		};
		_productCount++;
	});
	
	var _currentRow = 0;
	var _colCount = 0;
	$(".grid .specifikationer").each(function() {
		if(_colCount < _cols) {
			$(this).height(_rowHeights[_currentRow]);
			_colCount++
		} else {
			_colCount = 1;
			_currentRow++;
			$(this).height(_rowHeights[_currentRow]);
		};
	});
	
	
	// Scale pris container
	var _count = 0;
	var _height = 0;
	var _rowNum = 0;	
	var _rowHeights = new Array();
	var _productCount = 0;
	$(".grid ul.pris").each(function() {
		if(_count < _cols) {
			if($(this).height() > _height) {
				_height = $(this).height();
				_rowHeights[_rowNum] = _height;
			};
			_count++;
		} else {
			_rowHeights[_rowNum] = _height;
			_rowNum++;
			_height = $(this).height();
			_count = 1;
			_rowHeights[_rowNum] = _height;
		};
		_productCount++;
	});
	
	var _currentRow = 0;
	var _colCount = 0;
	$(".grid ul.pris").each(function() {
		if(_colCount < _cols) {
			$(this).height(_rowHeights[_currentRow]);
			_colCount++
		} else {
			_colCount = 1;
			_currentRow++;
			$(this).height(_rowHeights[_currentRow]);
		};
	});
	
	
	// Scale status container
	var _count = 0;
	var _height = 0;
	var _rowNum = 0;	
	var _rowHeights = new Array();
	var _productCount = 0;
	$(".grid .status").each(function() {
		if(_count < _cols) {
			if($(this).height() > _height) {
				_height = $(this).height();
				_rowHeights[_rowNum] = _height;
			};
			_count++;
		} else {
			_rowHeights[_rowNum] = _height;
			_rowNum++;
			_height = $(this).height();
			_count = 1;
			_rowHeights[_rowNum] = _height;
		};
		_productCount++;
	});
	
	var _currentRow = 0;
	var _colCount = 0;
	$(".grid .status").each(function() {
		if(_colCount < _cols) {
			$(this).height(_rowHeights[_currentRow]);
			_colCount++
		} else {
			_colCount = 1;
			_currentRow++;
			$(this).height(_rowHeights[_currentRow]);
		};
	});
	
	
	// Scale valg container
	var _count = 0;
	var _height = 0;
	var _rowNum = 0;	
	var _rowHeights = new Array();
	var _productCount = 0;
	$(".grid .valg").each(function() {
		if(_count < _cols) {
			if($(this).height() > _height) {
				_height = $(this).height();
				_rowHeights[_rowNum] = _height;
			};
			_count++;
		} else {
			_rowHeights[_rowNum] = _height;
			_rowNum++;
			_height = $(this).height();
			_count = 1;
			_rowHeights[_rowNum] = _height;
		};
		_productCount++;
	});
	
	var _currentRow = 0;
	var _colCount = 0;
	$(".grid .valg").each(function() {
		if(_colCount < _cols) {
			$(this).height(_rowHeights[_currentRow]);
			_colCount++
		} else {
			_colCount = 1;
			_currentRow++;
			$(this).height(_rowHeights[_currentRow]);
		};
	});
	
	
	// Scale knapper container
	var _count = 0;
	var _height = 0;
	var _rowNum = 0;	
	var _rowHeights = new Array();
	var _productCount = 0;
	$(".grid .knapper").each(function() {
		if(_count < _cols) {
			if($(this).height() > _height) {
				_height = $(this).height();
				_rowHeights[_rowNum] = _height;
			};
			_count++;
		} else {
			_rowHeights[_rowNum] = _height;
			_rowNum++;
			_height = $(this).height();
			_count = 1;
			_rowHeights[_rowNum] = _height;
		};
		_productCount++;
	});
	
	var _currentRow = 0;
	var _colCount = 0;
	$(".grid .knapper").each(function() {
		if(_colCount < _cols) {
			$(this).height(_rowHeights[_currentRow]);
			_colCount++
		} else {
			_colCount = 1;
			_currentRow++;
			$(this).height(_rowHeights[_currentRow]);
		};
	});
}


/**
 *	Sort products in grid
 */
var _lastValue = 0;
var _direction = 0;
var _value = 0;
$(document).ready(function() {
	// Sort on init
	$(".sorter a.desc").each(function() {
		$(".sorter a").each(function() {
			$(this).removeClass("desc");
			$(this).removeClass("asc");
		});
		_value = $(this).attr("rel");
		if(_lastValue == _value) {
			_direction = "desc";
			_lastValue = 0;
		} else {
			_direction = "asc";
			_lastValue = _value;
		};
		$.tinysort.defaults.attr = "value";
		$(".grid>.item").tsort('input[name="'+_value+'"]',{order:_direction});
		
		if(_direction == "desc") {
			$(this).addClass("asc");
		} else {
			$(this).addClass("desc");
		};
		
		// Rescale grid items when sorted
		scaleGridItems();
	});	
	
	// Sort on users request
	var _lastValue = $(".sorter a.desc").attr("rel");
	$(".sorter a").click(function() {
		$(".sorter a").each(function() {
			$(this).removeClass("desc");
			$(this).removeClass("asc");
		});
		_value = $(this).attr("rel");
		if(_lastValue == _value) {
			_direction = "desc";
			_lastValue = 0;
		} else {
			_direction = "asc";
			_lastValue = _value;
		};
		$.tinysort.defaults.attr = "value";
		$(".grid>.item").tsort('input[name="'+_value+'"]',{order:_direction});
		
		if(_direction == "desc") {
			$(this).addClass("asc");
		} else {
			$(this).addClass("desc");
		};
		scaleGridItems();
	});
});

