DB_carousel = {
	
	TagToName : new Array(),
	Features : new Array(),
	hash : new Object(),
	pageWidth : 768,
	numPages : 2,
	numPerPage : 7,
	leftOffset : 0,
	
	init : function(element, numPerPage) {
		if(!$.browser.msie) DB_hash.read();
		var anchors = $(element).children("ul").children("li").children("a");
		anchors.each(function() {
				$(this).children("img").before("<b></b>");
		});
		anchors.mouseenter(function() { // Add hover events for all
				DB_carousel.load(parseInt($(this).parent().index()));
		});
		anchors.click(function() { // Add click events for all
				return false; // Stop propogation
		});
		anchors.animate({"opacity":"0.65"},0); // Fade all
		
		if (!$.browser.msie && DB_hash.hash.Item != undefined)
			DB_carousel.load(DB_hash.hash.Item);
		else
			DB_carousel.load(0);
		if (numPerPage == undefined) numPerPage = this.numPerPage;
		this.pageWidth = $("#carousel").width();
		this.numPages = Math.ceil($("#carousel").children("ul").children("li").size() / numPerPage);
		this.leftOffset = parseInt($("#carousel ul").css("margin-left"));
	},
	
	load : function(id) {
		
		if(this.Current == id)
			return false;
				
		if(this.Current != undefined)
			$("#carousel ul li a").animate({"opacity":"0.65"},250);
				
		//Set current group 
		this.CurrentGroup = this.getPageNumber(id);//$("#folio"+id).attr("page");
		if (!$.browser.msie) DB_hash.set("Group", this.CurrentGroup);
		//Set Project Id
		if (!$.browser.msie) DB_hash.set("Item", id);
		this.Previous = this.Current;
		this.Current = id;
			
		//If we aren't on the same carousel item
		if(this.Previous != this.Current) {
			$($("#carousel ul li a")[this.Current]).animate({"opacity":"1"},250);
			DB_carousel.goToPg(this.CurrentGroup)
			DB_carousel.select();
		}
		
	},
	
	getTagName : function(json) {
		DB_carousel.TagToName = new Array();
		for(i=0;i<json.Results[0].Properties.features.Enums.length;i++) {
			DB_carousel.TagToName[json.Results[0].Properties.features.Enums[i].Tag] = json.Results[0].Properties.features.Enums[i].Name
		}
		
		txt = "";
		for(i=0;i<DB_carousel.Features.length;i++) {
			txt += "<li>"+DB_carousel.Features[i]+"</li>";
		}
			
			
		$("#siteFeatures").html(txt);
		
	},
	
	getPageNumber : function(id) {
		return Math.floor((parseInt(id) / this.numPerPage) + 1);
	},
	
	getFirstElement : function(pg) {
		return $("#carousel ul li")[this.getFirstElementId(pg)];
	},
	
	getFirstElementId : function(pg) {
		return (pg - 1) * this.numPerPage;
	},
	
	getLastElement : function(pg) {
		return $("#carousel ul li")[this.getLastElementId(pg)];
	},
	
	getLastElementId : function(pg) {
		return this.getFirstElementId(pg) + this.numPerPage - 1;
	},
	
	select : function() {
		pos = $($("#carousel ul li")[this.Current]).position();
		pos.left = pos.left - ((this.CurrentGroup - 1) * DB_carousel.pageWidth);
		$("#carousel_hover").animate({"marginLeft": pos.left+"px"});
		$("#carousel_hover").fadeTo(100, 1);
	},
	
	nextPg : function(){
		DB_hash.read();
		pg = ++DB_hash.hash.Group;
		// if(pg <= DB_carousel.numPages)
		//	DB_carousel.goToPg(pg);
			
		//Select first item on next pg
		$(this.getFirstElement(pg)).each(function(){
			id = $(this).index();
			DB_carousel.load(id);
		});
	},
	
	prevPg : function(){
		DB_hash.read();
		pg = --DB_hash.hash.Group;
		
		//if(pg > 0)
		//	DB_carousel.goToPg(pg);

		//select last item on pg
		$(this.getLastElement(pg)).each(function(){
			id = $(this).index();
			DB_carousel.load(id);
		});
	},
	
	goToPg : function(pg){
		$("#carousel_hover").fadeTo(0, 0.6);
		pos = -((pg-1) * parseInt(DB_carousel.pageWidth)) + this.leftOffset;
		$('#carousel ul').animate({marginLeft: pos}, "slow");
	}

}
