List = {
	DISPLAY : 3,
	SPEED : 400,
	WIDTH : 743,
	CATEGORIES : [],

	init : function() {		
		if ($('#latest_products').size() > 0) {
			$('.product_category').each(function() {
				category = new Object()
				category.name = $(this).attr('id');
				category.page = 1;
				category.size = $(this).find('ul.products_list li').size();
				List.CATEGORIES.push(category);
			});
		}
		
		for(x in List.CATEGORIES) {
			$("#" + List.CATEGORIES[x].name + " .next").attr('rel', List.CATEGORIES[x].name);
			$("#" + List.CATEGORIES[x].name + " .prev").attr('rel', List.CATEGORIES[x].name);
			$("#" + List.CATEGORIES[x].name + " .numbers span").attr('rel', List.CATEGORIES[x].name);
		}
		
		$("#latest_products .next").click(function() {
			List.move('next', $(this).attr('rel'));
			return false;
		});
		$("#latest_products .prev").click(function() {
			List.move('prev', $(this).attr('rel'));
			return false;
		});
		$("#latest_products .numbers span").click(function() {
			var current = parseInt($(this).text()) - 1;
			List.animate(current, $(this).attr('rel'));
			List.setNumbers(current, $(this).attr('rel'));
			return false;
		});
	},
	move : function(dir, id) {
		var current = parseInt($('#' + id + ' .numbers .current').text())-1;
		if (dir == 'next') {
			if (current == List.DISPLAY-1) return;
			var current = (current+1);
		} else {
			if (current == 0) return;
			var current = (current-1);
		}
		List.animate(current, id);
		List.setNumbers(current, id);
	},
	animate : function(current, id) {
		var left = -List.WIDTH * current;
		$('#' + id + ' ul').animate({marginLeft : left +'px'});
	},
	setNumbers : function(current, id) {
		$('#' + id + ' .numbers .current').removeClass('current');
		$('#' + id + ' .numbers span:eq(' + current +')').addClass('current');
		
		if (current > 0) {
			$('#' + id + ' .prev').addClass('prev_active');
		} else {
			$('#' + id + ' .prev_active').removeClass('prev_active');
		}		
		if (current == List.DISPLAY-1) {
			$('#' + id + ' .next').addClass('next_inactive');
		} else {
			$('#' + id + ' .next_inactive').removeClass('next_inactive');
		}
	}
}




function initLinks() {
	$("a[rel='external']").attr('target', '_blank');
	$("a[rel='popup']").click(function() {
		var width = 800; 
		var height = 600;
		window.open($(this).attr('href'),'player','toolbar=0,scrollbars=no,menubar=0,resizable=0,width='+width+',height='+height + ',left=' + ((screen.width - width)/2) + ',top=' + ((screen.height - height)/2));
		return false;
	});
}
function initRedirect() {
	if ($("#redirect_link").size() > 0) {
		// window.location=$("#redirect_link").attr('href');
		setTimeout("window.location='" + $("#redirect_link").attr('href') +"'", 1000);
	};
}

function init() {
	initLinks();
	initRedirect();
	List.init();
}


$(document).ready(init);
