// Menu ----->

var menu = false;
var selected = false;
var selectedSub = false;

function menuOn(id) { // Main Menu
	if (menu != id) {
		if (menu) {
			menuOff(menu);
		}
		Effect.Appear(id,{duration: 0.25});
		menu = id;
	}
	else {
		menuOff(id);
	}
}

function menuOff(id) {
	Element.hide(id);
	menu = false;
}

function selectMenu(id) { // Menu
	if (selected != id) {
		if (selected) {
			unselectMenu(selected);
		}
		$(id).className = "selected";
		Effect.Appear(id+'-sub',{duration: 0.25});
		selected = id;
	}
	else {
		unselectMenu(id);
	}
}

function unselectMenu(id) {
	$(id).className = "";
	Element.hide(id+'-sub');
	selected = false;
}

function selectSubMenu(id) { // Sub Menu
	if (selectedSub != id) {
		if (selectedSub) {
			unselectSubMenu(selectedSub);
		}
		$(id).className = "selected";
		Effect.Appear(id+'-sub',{duration: 0.25});
		selectedSub = id;
	}
	else {
		unselectSubMenu(id);
	}
}

function unselectSubMenu(id) {
	$(id).className = "";
	Element.hide(id+'-sub');
	selectedSub = false;
}



//// Process ////

// Process View ----->

function processOn() {
	Effect.Appear('process-background',{
		duration: 0.25,
		to: 0.75,
		afterFinish: function() {
			Element.setStyle('process-content', {visibility: "visible"});
		}
	});
}

function processOff() {
	Element.setStyle('process-content', {visibility: "hidden"});
	Effect.Fade('process-background',{duration: 0.25});
}



// Process Toggle Images ----->

function enlargeImg(id) {
	Element.hide(id + '-lo');
	Element.show(id + '-hi');
}

function reduceImg(id) {
	Element.hide(id + '-hi');
	Element.show(id + '-lo');
}



//// Draggables ////

// Get Window Dimensions ----->

function getWindowDimensions() {
	var dimensions = {width: 0, height: 0};
	if (typeof(window.innerWidth) == 'number') {
		// Non-IE ----->
		dimensions.width = window.innerWidth;
		dimensions.height = window.innerHeight;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		// IE 6+ in 'standards compliant mode' ----->
		dimensions.width = document.documentElement.clientWidth;
		dimensions.height = document.documentElement.clientHeight;
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		// IE 4 compatible ----->
		dimensions.width = document.body.clientWidth;
		dimensions.height = document.body.clientHeight;
	}
	return dimensions;
}



// Randomize Image Sizes ----->

function randomSize() {
	var imgs = document.getElementsByClassName('draggable-image-lo');
	for (var i = 0; i < imgs.length; i++) {
		var randomNum = (Math.random() * 2) + 1.5;
		var dragImgDim = imgs[i].getDimensions();
		var newImgWidth = Math.floor(dragImgDim.width / randomNum);
		var newImgHeight = Math.floor(dragImgDim.height / randomNum);
		imgs[i].style.width = newImgWidth + "px";
		imgs[i].style.height = newImgHeight + "px";
	}
}



// Randomize Image Placement ----->

function randomPos() {
	var windowSize = getWindowDimensions();
	var elements = document.getElementsByClassName('draggable');
	for (var i = 0; i < elements.length; i++) {
		var dragDim = elements[i].getDimensions();
		var newX = Math.floor(Math.random() * windowSize.width - dragDim.width);
		var newY = Math.floor(Math.random() * windowSize.height - dragDim.height);
		elements[i].style.left = Math.max(Math.floor(Math.random() * dragDim.width), newX) + "px";
		elements[i].style.top = Math.max(Math.floor(Math.random() * dragDim.height), newY) + "px";
	}
}



// Make Images Draggable ----->

var dragCount = false;
var draggables = new Array();

function initDrag() {
	for (var i = 0; i < dragCount; i++) {
		draggables.push('drag-' + i);
		new Draggable(draggables[i],{putontop:true});
	}
}



// Scroller ----->

function scroller(direction) {
	var speed = 2; // Set speed in pixels
	var wrap = $('scroll-content-wrapper'); // Content wrapper
	var wrapheight = Element.getHeight(wrap);
	var obj = $('scroll-content'); // Content to scroll
	var objheight = Element.getHeight(obj);
	if (direction == "down") {
		if (parseInt(obj.style.top) >= (wrapheight - objheight))
		obj.style.top = parseInt(obj.style.top) - speed + "px";
	}
	else if (direction == "up") {
		if (parseInt(obj.style.top) <= 0)
		obj.style.top = parseInt(obj.style.top) + speed + "px";
		if(parseInt(obj.style.top) > 0) obj.style.top = 0 + "px";
	}
	timerID = setTimeout("scroller('" + direction + "')",20)
}

function scrollerStop() {
	clearTimeout(timerID);
	timerID = null;
}

function initScroller() {
	if (!$('scroll-content')) return;
	else if (Element.getHeight('scroll-content') > Element.getHeight('scroll-content-wrapper'))
		Element.show('scroll-content-scroller') // Show scroller
}



// Events ----->

Event.observe(window, 'load', randomPos, false);
Event.observe(window, 'load', randomSize, false);
Event.observe(window, 'load', initDrag, false);
Event.observe(window, 'load', initScroller, false);