var mammoth_main = {
	navStops:{},
	curentImg:"",
	slideImgEnabled:true,
	slideVidEnabled:true,
	imgStart:1,
	imgEnd:8,
	vidStart:1,
	vidEnd:3,

	init:function(){
		mammoth_main.trackNav();
		window.firstimteout = setTimeout(function(){mammoth_main.fadeIn(document.getElementById("content"),null);document.getElementById("loading").style.display = "none";},1);
	},

	slideImg:function(dir){
		function slide(left,goal,steps){
			if(steps < 20){
				left = Math.ceil(left + (Math.pow(((1 / 20) * steps),1.5) * (goal - left)));
				steps++;
				thumbs.style.marginLeft = left + "px";
				mammoth_main.slideImg.timeout = setTimeout(function(){slide(left,goal,steps);},10);
			}else{
				clearTimeout(mammoth_main.slideImg.timeout);
				thumbs.style.marginLeft = goal + "px";
				mammoth_main.slideImgEnabled = true;
			}
		}
		var thumbs = document.getElementById("imgSlider").getElementsByTagName("table")[0];
		var width = document.getElementById("imgSlider").offsetWidth;
		var left = thumbs.offsetLeft;
		var a = (thumbs.offsetWidth % 480);
		var end = document.getElementById("imgSlider").getElementsByTagName("img").length;
		if(dir == "right" && mammoth_main.slideImgEnabled){
			clearTimeout(mammoth_main.slideImg.timeout);
			if(left >= (thumbs.offsetWidth*-1 + a + width)){
				mammoth_main.slideImgEnabled = false;
				slide(left,(thumbs.offsetLeft - width),0);
				mammoth_main.imgStart += 8;
				mammoth_main.imgEnd += 8;
				document.getElementById("imgCount").innerHTML = mammoth_main.imgStart + " - " + (mammoth_main.imgEnd > end ? end : mammoth_main.imgEnd);
			}
		}else if(dir == "left" && mammoth_main.slideImgEnabled){
			clearTimeout(mammoth_main.slideImg.timeout);
			if(left <= 0 - width){
				mammoth_main.slideImgEnabled = false;
				slide(left,(thumbs.offsetLeft + width),0);
				mammoth_main.imgStart -= 8;
				mammoth_main.imgEnd -= 8;
				document.getElementById("imgCount").innerHTML = (mammoth_main.imgStart <= 0 ? 1 : mammoth_main.imgStart) + " - " + mammoth_main.imgEnd;
			}
		}
	},

	slideVid:function(dir){
		function slide(top,goal,steps){
			if(steps < 20){
				top = Math.ceil(top + (Math.pow(((1 / 20) * steps),1.5) * (goal - top)));
				steps++;
				thumbs.style.marginTop = top + "px";
				mammoth_main.slideVid.timeout = setTimeout(function(){slide(top,goal,steps);},10);
			}else{
				clearTimeout(mammoth_main.slideImg.timeout);
				thumbs.style.marginTop = goal + "px";
				mammoth_main.slideVidEnabled = true;
			}
		}
		var thumbs = document.getElementById("vidSlider");
		var height = document.getElementById("vidSliderBox").offsetHeight;
		var top = thumbs.offsetTop;
		var a = (thumbs.offsetHeight % 237);
		var end = document.getElementById("vidSlider").getElementsByTagName("img").length;
		if(dir == "down" && mammoth_main.slideVidEnabled){
			clearTimeout(mammoth_main.slideVid.timeout);
			if(top >= (thumbs.offsetHeight*-1 + a + height)){
				mammoth_main.slideVidEnabled = false;
				slide(top,(thumbs.offsetTop - height),0);
				mammoth_main.vidStart += 3;
				mammoth_main.vidEnd += 3;
				document.getElementById("vidCount").innerHTML = mammoth_main.vidStart + " - " + (mammoth_main.vidEnd > end ? end : mammoth_main.vidEnd);
			}
		}else if(dir == "up" && mammoth_main.slideVidEnabled){
			clearTimeout(mammoth_main.slideVid.timeout);
			if(top <= 0 - height){
				mammoth_main.slideVidEnabled = false;
				slide(top,(thumbs.offsetTop + height),0);
				mammoth_main.vidStart -= 3;
				mammoth_main.vidEnd -= 3;
				document.getElementById("vidCount").innerHTML = (mammoth_main.vidStart <= 0 ? 1 : mammoth_main.vidStart) + " - " + mammoth_main.vidEnd;
			}
		}
	},

	swapImg:function(old,img){
		mammoth_main.currentImg = img.src;
		old.parentNode.style.background = "url(" + old.src + ") no-repeat";
		old.style.filter = "alpha(0)";
		old.style.opacity = "0";
		old.src = img.src;
		for(var i=0;i<document.getElementById("imgSlider").getElementsByTagName("img").length;i++){
			if(document.getElementById("imgSlider").getElementsByTagName("img")[i].src != img.src){
				document.getElementById("imgSlider").getElementsByTagName("img")[i].className = "borderthumbnailfake";
			}
		}
		mammoth_main.fadeIn(old);
	},

	swapVid:function(old,img,title,sub){
		document.getElementById("vidTitle").innerHTML = title;
		document.getElementById("vidSub").innerHTML = sub;
		old.innerHTML = img;
	},

	trackNav:function(){
		function moveHover(steps,goal,instant){
			clearTimeout(mammoth_main.trackNav.timeout);
			if(steps < 20 && !instant){
				var left = Math.ceil(img.offsetLeft + Math.pow((1/20)*steps++,2.5)*(goal - img.offsetLeft));
				img.style.left = left + "px";
				mammoth_main.trackNav.timeout = setTimeout(function(){moveHover(steps,goal);},15);
			}else if(instant){
				clearTimeout(mammoth_main.trackNav.timeout);
				img.style.left = goal + "px";
				img.style.visibility = "visible";
			}
		}
		var img = document.getElementById("navHover");
		var t = document.getElementById("nav").getElementsByTagName("a");
		for(var i=0;i<t.length;i++){
			var goal = Math.ceil(((t[i].offsetWidth - img.width)*0.5 + t[i].offsetLeft));
			mammoth_main.navStops[t[i].className] = goal;
			t[i].onmouseover = (function(goal){
				return function(){
					moveHover(0,goal);
				}
			})(goal);
			t[i].onmouseout = function(){
				moveHover(0,mammoth_main.navStops[document.getElementById("page").className]);
			}
		}
		moveHover(0,mammoth_main.navStops[document.getElementById("page").className],true);
	},

	fadeIn:function(obj,callback){
		function changeAlpha(num){
			clearTimeout(obj.fadeTimeout);
			if(num < 1){
				num += 0.1;
				obj.style.filter = "alpha(opacity=" + num*100 + ")";
				obj.style.opacity = num;
				obj.fadeTimeout = setTimeout(function(){changeAlpha(num);},30);
			}else{
				obj.style.filter = "alpha(opacity=100)";
				obj.style.opacity = "1";
				if(typeof(callback) == "function") callback();
			}
		}
		obj.style.filter = "alpha(opacity=0)";
		obj.style.opacity = "0";
		obj.style.display = "block";
		changeAlpha(0);
	}
}

mammoth_common.eventAdd(window,"load",mammoth_main.init);