function isElement(o){
	return (
		typeof HTMLElement === "object" ? o instanceof HTMLElement :
		typeof o === "object" && o.nodeType === 1 && typeof o.nodeName==="string"
		);
}

function removeChildNodes(element) {
	if(element.hasChildNodes())	{
		while(element.childNodes.length >= 1)		{
			element.removeChild(element.firstChild);
		}
	}
}

window.onload = function() 
{
	registerNavigation('main-navigation');
	
	var agentName = navigator.userAgent;
	if(agentName.match(/iPad|iPhone/i)) {
		var headerVideo = document.getElementById("headerVideo");
		headerVideo.load();
		headerVideo.play();
		if(headerVideo.paused) {
			headerVideo.controls = "controls";
		}
	}
};


var activeNavEl;
function registerNavigation(navListID) 
{
	var navList = document.getElementById(navListID);
	var links = navList.getElementsByTagName("a");
	var navListX = getElemetX(navList);
	var navH;
	
	for( var x = 0; x < links.length; x++ ) 
    {
		if(isElement(links[x])) 
        {
			if( links[x].id == "fplayer-button" ) 
            {
				continue;
			}
            
			links[x].id = "nav-link-" + x;
			links[x].elW = links[x].offsetWidth;
			links[x].elX = getElemetX(links[x]) - navListX;
			links[x].onmouseover = function() 
            {
				navLinkOver(this);
			}
            
			if(links[x].className == "active") 
            {
				activeNavEl = links[x];
			}
            
			navH = links[x].offsetHeight;
		}
	}
	
	navList.onmouseout = function() 
    {
		navLinkOver(activeNavEl);
	}
	
	navLinkOver(activeNavEl);
	document.getElementById("nav-bar").style.bottom = navH + "px";
}

function navLinkOver( element ) 
{
    if( element )
    {
	    var navBar = document.getElementById( "nav-bar" );
	    
	    navBar.style.width = element.elW + "px";
	    navBar.style.left = element.elX + "px";
    }
}

function getElemetX(element) {
	var _x = 0;
	while(element && !isNaN(element.offsetLeft)) {
		_x += element.offsetLeft - element.scrollLeft;
		element = element.offsetParent;
	}
	return _x;
}


function customMouseScroll(elementId, spinSteps, disposition, wraperSize, autoRotate) {
	this.element = document.getElementById(elementId);
	this.spinSteps = spinSteps;
	this.disposition = disposition;
	this.wraperSize = wraperSize;
	var pointer = this;
	
	this.slideElement = document.getElementById(elementId + "-slide");
	this.prevButton = document.getElementById(elementId + "-prev-button");
	this.nextButton = document.getElementById(elementId + "-next-button");
	this.slideBoxPosition = 0;
	this.previousTimer = null;
	this.nextTimer = null;
	
	if(this.disposition == "horizontal") {
		this.slideElementSize =  this.slideElement.offsetWidth;
	} else {
		this.slideElementSize =  this.slideElement.offsetHeight;
	}
	
	if(this.element.addEventListener) {
		this.element.addEventListener('DOMMouseScroll', function(ev){
			pointer.onMouseWheelSpin(ev);
			return false;
		}, false);
		this.element.addEventListener('mousewheel', function(ev){
			pointer.onMouseWheelSpin(ev);
			return false;
		}, false);
	}	else {
		this.element.onmousewheel = function() {
			pointer.onMouseWheelSpin();
			return false;
		};
	}
	
	if(autoRotate) {
		this.autoRotate = autoRotate;
		this.autoRotateTimer = setTimeout( function(){
			pointer.autoRotater();
		}, autoRotate);
		
		this.element.onmouseover = function() {
			pointer.stopAutoRotater();
		};
		this.element.onmouseout = function() {
			pointer.startAutoRotater();
		};
	}
	
	this.hideNavButtons();
}

customMouseScroll.prototype.autoRotater = function() {
	var spined = this.handleMouseSpin("next");
	if(spined === false) {
		this.slideBoxPosition = 0;
		if(this.disposition == "horizontal") {
			this.slideElement.style.left = "0px";
		} else {
			this.slideElement.style.top = "0px";
		}
	}
	
	var pointer = this;
	var autoRotate = this.autoRotate;
	this.autoRotateTimer = setTimeout( function(){
		pointer.autoRotater();
	}, autoRotate);
}

customMouseScroll.prototype.stopAutoRotater = function() {
	clearTimeout(this.autoRotateTimer);
}

customMouseScroll.prototype.startAutoRotater = function() {
	var pointer = this;
	var autoRotate = this.autoRotate;
	this.autoRotateTimer = setTimeout( function(){
		pointer.autoRotater();
	}, autoRotate);
}

customMouseScroll.prototype.onMouseWheelSpin = function(e) {
	var nDelta = 0;
	if(!e) {
		e = window.event;
	}
	
	if(e.wheelDelta) {
		nDelta = e.wheelDelta;

		var regex = /Opera.([0-9]+)\.([0-9]+)/i
		var matches = regex.exec(navigator.userAgent);
		if(matches) {
			if(matches[1] <= 9 && matches[2] < 20) {
				nDelta = -nDelta;
			}
		}
	}	else if(e.detail) {
		nDelta = -e.detail;
	}
	
	if(nDelta > 0) {
		this.handleMouseSpin("prev");
	}
	
	if(nDelta < 0) {
		this.handleMouseSpin("next");
	}
	
	if(e.preventDefault) {
		e.preventDefault();
	}
	
	e.returnValue = false;
}

customMouseScroll.prototype.handleMouseSpin = function(way) {
	var nDelta;
	var isNotLast;
	
	if(way == "prev") {
		nDelta = this.spinSteps;
	} else {
		nDelta = 0 - this.spinSteps;
	}
	
	this.slideBoxPosition += nDelta;
	
	if(this.slideBoxPosition > 0) {
		this.slideBoxPosition = 0;
	}
	if(this.slideBoxPosition < (this.wraperSize - this.slideElementSize)) {
		this.slideBoxPosition = this.wraperSize - this.slideElementSize;
		isNotLast = false;
	} else {
		isNotLast = true;
	}
	
	if(this.disposition == "horizontal") {
		this.slideElement.style.left = this.slideBoxPosition + "px";
	} else {
		this.slideElement.style.top = this.slideBoxPosition + "px";
	}
	
	if(nDelta >= 0) 
    {
		this.prevButton.className = "cmsPrevButton active";
		var prevButton = this.prevButton;
		this.previousTimer = setTimeout( function(){
			prevButton.className = "cmsPrevButton";
		}, 250);
	}
    else
    {
		this.nextButton.className = "cmsNextButton active";
		var nextButton = this.nextButton;
		this.nextTimer = setTimeout( function(){
			nextButton.className = "cmsNextButton";
		}, 250);
	}
	
	this.hideNavButtons( );
	
	return isNotLast;
}

customMouseScroll.prototype.hideNavButtons = function() {
	if(this.slideBoxPosition >= 0) {
		this.prevButton.style.display = "none";
	} else {
		this.prevButton.style.display = "block";
	}
	if(this.slideBoxPosition <= (this.wraperSize - this.slideElementSize)) {
		this.nextButton.style.display = "none";
	} else {
		this.nextButton.style.display = "block";
	}
}

function bgVideoOnPlay(videoId) {
	document.getElementById(videoId).controls = "";
}

function bgVideoLoop(videoId) {
	var video = document.getElementById(videoId);
	video.play();
	if(video.paused && navigator.userAgent.match(/iPad|iPhone/i)) {
		video.controls = "controls";
	}
}


function embedVideoPlayer(videoId, source) {
	var playerEl = document.getElementById("embed-video-player");
	
	switch(source) {
		case "youtube":
			playerEl.src = "http://www.youtube.com/embed/" + videoId + "?rel=0&hd=1&autoplay=1";
			playerEl.style.height = "537px"
			break;
		case "vimeo":
		default:
			playerEl.src = "http://player.vimeo.com/video/" + videoId + "?byline=0&portrait=0&color=ed1c24&autoplay=1";
			playerEl.style.height = "507px"
			break;
	}
	fplayer.pauseSong();
}

var lastHash = location.hash;
function navigateTo(page, hash, onlyChangeContent) {
	var pageWrappers = document.getElementById("content").getElementsByTagName("div");
	for(x in pageWrappers) {
		if(isElement(pageWrappers[x]) && pageWrappers[x].className == "page-wrapper") {
			if(pageWrappers[x].id == "page-" + page) {
				pageWrappers[x].style.display = "block";
			} else {
				pageWrappers[x].style.display = "none";
			}
		}
	}
	
	switch (page) {
		case "news":
			twitterBoxScroll = new customMouseScroll("twitter-news-box", 60, "vertical", 320);
			break;
		case "video":
			videoList = new customMouseScroll("video-list", 301, "horizontal", 902);
			break;
		case "bio":
			photoCarusel = new customMouseScroll("photo-carusel", 912, "horizontal", 902);
			break;
		case "contact":
			twitterBoxScroll = new customMouseScroll("twitter-contact-box", 60, "vertical", 320);
			break;
		case "home":
		default:
			homeBoxesScroll = new customMouseScroll("home-boxes", 301, "horizontal", 902);
			twitterBoxScroll = new customMouseScroll("twitter-box", 60, "vertical", 200);
			break;
	}
	
	var mainNav = document.getElementById("main-navigation").getElementsByTagName("a");
	for(x in mainNav) {
		if(isElement(mainNav[x])) {
			if(mainNav[x].id == "fplayer-button") {
				continue;
			}
			if(mainNav[x].getAttribute("data-name") == page) {
				mainNav[x].className = "active";
				activeNavEl = mainNav[x];
			} else {
				mainNav[x].className = "";
			}
		}
	}
	navLinkOver(activeNavEl);
		
	var footNav = document.getElementById("foot-navigation").getElementsByTagName("a");
	for(x in footNav) {
		if(isElement(footNav[x])) {
			if(footNav[x].getAttribute("data-name") == page) {
				footNav[x].className = "active";
			} else {
				footNav[x].className = "";
			}
		}
	}
	
	var pageTitle;
	if(page != "home" || page != undefined) {
		pageTitle = page.substr(0, 1).toUpperCase() + page.substr(1) + " - ";
	}
		
	if(!onlyChangeContent) {
		if(hash) {
			hash = "#" + hash;
		} else {
			hash = "";
		}
		
		if(window.history.pushState != undefined) {
			if(page == "home") {
				window.history.pushState({
					page:page
				}, pageTitle + "Hadden Sayers Official Website", "/" + hash);
			} else {
				window.history.pushState({
					page:page
				}, pageTitle + "Hadden Sayers Official Website", page + hash);
			}
		} else {
			window.location.hash = "navigate-" + page;

			setInterval(function() {
				if(location.hash != lastHash) {
					autoNavigate(true);
					lastHash = location.hash;
				}
			}, 500);
		}
	}
	
	document.title = pageTitle + "Hadden Sayers Official Website";
	
	return false;
}

//window.onpopstate = function(event) {
//	navigateTo(event.state["page"], null, true);
//}

function autoNavigate(dontRewrite) {
	var regex = /^#navigate-([a-z0-9\-]+)$/i
	var matches = regex.exec(window.location.hash);
	if(matches) {
		if(matches[1]) {
			if(dontRewrite === true) {
				navigateTo(matches[1], null, true);
			} else {
				window.location = matches[1];
			}
		}
	}
}

/*var startDragX;
var startDragY;
var startSliderPosition = 0;
var xDelta = 0;

function dragStart() {
	startDragX = tempX;
	startDragY = tempY;
	//document.getElementById("home-boxes-slide").addEventListener('drag', getMouseXY, false);
}

function dragMove() {
	xDelta = tempX - startDragX;
	
	document.Show.MouseX.value = document.getElementById("home-boxes-slide").offsetLeft;
	
	document.Show.MouseDelta.value = xDelta;
	document.getElementById("home-boxes-slide").style.left = (startSliderPosition + xDelta) + "px";
}

function dragStop() {
	startSliderPosition += xDelta;
}


//document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
	tempX = e.pageX
	tempY = e.pageY 
	
	document.Show.MouseX.value = tempX
  document.Show.MouseY.value = tempY
	return true
}*/
