// Alaska Effects
// (cleanup in progress)

// Variables
var navfeaty = 0;       // height of navbar + featurebar
var y = 0;              // saves scroll position
var navvisible = true;  // navbar visible or hidden?

var timeOutId = 0;
var jitterBuffer = 750;

// #Navigation
function init() {
	// Get height of navbar + featurebar
	// navfeaty = document.getElementById('featuredstrip').offsetHeight;
	 navfeaty = 56;
	
	// Register event handlers
	if(window.addEventListener) {
		window.addEventListener('scroll', function() { catchScroll(); }, false);
		document.getElementById('navstrip').addEventListener('mouseover', function() { catchMouseOver(); }, false);
		document.getElementById('navstrip').addEventListener('mouseout', function() { catchMouseOut(); }, false);
	}
	else if(window.attachEvent) {
		window.attachEvent('onscroll', function() { catchScroll(); });
		document.getElementById('navstrip').attachEvent('onmouseover', function() { catchMouseOver(); });
		document.getElementById('navstrip').attachEvent('onmouseout', function() { catchMouseOut(); });
	}
	// call Scroll handler in case page got reloaded while scrolled down
	catchScroll();
	activatePlaceholders();
}

// Set a safe pause for scroll handler
function catchScroll() {
	y = getScrollTop();
	if(y == 0) { dimnavbar(); } // don't timeout if user scrolled back to top
	else {
    	if(timeOutId) clearTimeout(timeOutId);
    	timeOutId = setTimeout(function() { dimnavbar() }, jitterBuffer);
    }
}

// Get proper scroll position
function getScrollTop(){
    if(typeof pageYOffset != 'undefined') { return pageYOffset; } //most browsers
    else {
        var B = document.body; //IE 'quirks'
        var D = document.documentElement; //IE with doctype
        D = (D.clientHeight)? D: B;
        return D.scrollTop;
    }
}

// Dim navbar if user scrolled down
function dimnavbar() {
	if(y >= navfeaty && navvisible == true) {
		new Effect.Move('navstrip', { y: -45, duration: 0.6, queue: {position:'end', scope: 'navmove', limit:2} } );
		new Effect.Appear('navhandle', { duration: 0.6 } );
		navvisible = false;
	}
	
	else if(y < navfeaty && navvisible == false) {
		new Effect.Move('navstrip', { y: 45, duration: 0.2, queue: {position:'end', scope: 'navmove', limit:2} } );
		new Effect.Fade('navhandle', { duration: 0.2 } );
		navvisible = true;
	}
}

// Show Navbar on mouseover
function catchMouseOver() {
	if(timeOutId) clearTimeout(timeOutId);
	if(y >= navfeaty && navvisible == false) {
		new Effect.Move('navstrip', { y: 45, duration: 0.2, queue: {position:'end', scope: 'navmove', limit:2} } );
		new Effect.Fade('navhandle', { duration: 0.2 } );
		navvisible = true;
	}
}
function catchMouseOut() {
	timeOutId = setTimeout(function() { dimnavbar() }, jitterBuffer);
}

// #Featured
// Scroll through featured content
function featscroll(direction, max) {	
	var scr = document.getElementById('featured');
	max = 900 * max - 900;
	if(direction == 1) {
		if(scr.scrollLeft == max) scr.scrollLeft = 0;
		else scr.scrollLeft += 900;
	}
	if(direction == 0) {
		if(scr.scrollLeft == 0) scr.scrollLeft = max;
		else scr.scrollLeft -= 900;
	}
}

// #Notification
// Close notification/modal
function dismiss() { document.getElementById('notification').style.display = 'none'; }

// #STORE
// Store categories display
function showstorecat(id) {
	with(document) {
		getElementById('shopmusic').style.display = 'none';
		getElementById('shopcheckout').style.display = 'none';
		getElementById('shopgobins').style.display = 'none';
		getElementById(id).style.display = 'block';
	}
}


// FIXES
// html attribute Placeholder for IE9/etc.
function activatePlaceholders() {
	var detect = navigator.userAgent.toLowerCase();
	if(detect.indexOf("safari") > 0) return false;
	var inputs = document.getElementsByTagName("input");
	for(var i=0;i<inputs.length;i++) {
		if(inputs[i].getAttribute("type") == "text" || inputs[i].getAttribute("type") == "email") {
			if(inputs[i].getAttribute("placeholder") && inputs[i].getAttribute("placeholder").length > 0) {
				inputs[i].value = inputs[i].getAttribute("placeholder");
				inputs[i].onfocus = function() {
					if (this.value == this.getAttribute("placeholder")) this.value = "";
					return false;
				}
				inputs[i].onblur = function() {
					if (this.value.length < 1) this.value = this.getAttribute("placeholder");
				}
			}
		}
	}
}

