
$(document).ready(function(){
//	var $topnav = $("#topnav");
//	$("#topnav li.n").hover(function(){
//		$(this).addClass("selected");
//	}, function(){
//		$(this).removeClass("selected");
//	});
	
	$("#sidebar a.tab").click(function(){
		var parent = $(this).parent().parent();
		var list = $(this).attr("rel");
		parent.find("ul.apps_list").hide();
		parent.find("#"+list).show();
		parent.find("a.tab").removeClass("selected");
		$(this).addClass("selected");
	});
	
	carousle = new carous($("#carousel"), "carousle");
	carousle.init();
	
});

carous = function(elm, fname) {
	
	var self = this;
	var $carousel = elm ;
	var $items = $carousel.find("li");
	self._current = 0;
	
	$carousel.find(".holder").click(function(){
		location.href = $carousel.find(".opac_text a").attr("href");
	});
	
	// Map
	$items.each(function(){
		$(this)
			.data("i", self._current)
			.data("lock", false);
		self._current++;
	});
	
	$items.last().css({"border-bottom": 0});
	
	$items.hover(function(){
		self.mark($(this).data("i"));
		self._current = $(this).data("i");
		clearTimeout(self.s);
	}, function(){
		self.loop();
	});
	
	this.mark = function(i) {
		var item = $items.eq(i);
		if (!item.data("lock")) {
			item.data("lock", true);
			$items.removeClass("highlight");
			item.addClass("highlight");
			$carousel.find(".opac_text a").html(item.find("a").attr("title")).attr("href", item.find("a").attr("href"));
			$carousel.find(".holder").animate({opacity: 0}, 200, function(){
				$(this).css({"background-image": "url("+item.find("a").attr("rel")+")"})
				.animate({opacity: 1}, 200, function(){
					item.data("lock", false);
				});
			});
		}
	};
	
	this.next = function() {
		self._current++;
		if (self._current > $items.length-1) {
			self._current = 0;
		}
		this.mark(self._current);
	};
	
	this.loop = function() {
		self.s = setInterval(fname+".next()", 6000);
	};
	
	this.init = function() {
		self.next();
		self.loop();
	};
	
};


