/**
 * Function to get the TControl page ID from the supplied URL
 */
function getPageId(url) {
	var parts = url.split('index.php?id=');
	if(!parts[1])
		return null;
	var parts = parts[1].split(/[^0-9]/);
	return parseInt(parts[0]);
}

/**
 * Get product option ID
 */
function getOptionId(url) {
	var parts = url.split('&option=');
	if(!parts[1])
		return null;
	var parts = parts[1].split(/[^0-9]/);
	return parseInt(parts[0]);
}

/**
 * Do following on page load
 */
$(document).ready(function() {

	$(':first-child').addClass('first-child');

	// Preselect option
	$('select[name=option_0]').val(getOptionId(location.href));


	// On product grid, and .end_of_row class to div at end of row
	// Allows line break and margin to be added via CSS
	$('div.product_grid div').each(function(i, item) {
  	if((i+1) % 3 == 0) {
    	$(item).addClass('end_of_row');
		}
	})
	
	// Add .selected class to any li element if it has a child anchor
	// with href of current page
	$('li a').each(function(i, item) {
		if(getPageId($(item).attr('href')) == getPageId(location.href))
			$(item).parent('li').addClass('selected');
	})
})