function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function inputFocus() {
	var inputField = document.getElementsByTagName('input');
	var valueOne = inputField[0].getAttribute('value');
	var valueTwo = inputField[2].getAttribute('value');
	
	inputField[0].onfocus = function() {
		inputField[0].setAttribute('value', '');
	}
	inputField[0].onblur = function() {
		inputField[0].setAttribute('value', valueOne);
	}
	
	inputField[3].onfocus = function() {
		inputField[3].setAttribute('value', '');
	}
	inputField[3].onblur = function() {
		inputField[3].setAttribute('value', valueTwo);
	}
}

// There is no insertAfter method in the DOM so I have to write my own
function insertAfter(newElement, targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement, targetElement.nextSibling);
	}
}

function prepareGallery() {
	if (document.getElementById('scrollbar')) {
		var gallery = document.getElementById('scrollbar');
		var links = gallery.getElementsByTagName('a');
		
		for (var i=0; i < links.length; i++) {
			links[i].onclick = function() {
				return showPic(this);
			}
		}
	}
}

function showPic(whichPic) {
	// First check if the correct Element is in the document
	if (!document.getElementById('image')) return true;
	var source = whichPic.getAttribute('href');
	var placeHolder = document.getElementById('image');
	placeHolder.setAttribute('src', source);
	
	return false;
}

function countBodyChildren() {
	var body_element = document.getElementsByTagName('body')[0];
	alert(body_element.nodeType);
}

// Load with the DOM
addLoadEvent(inputFocus);
addLoadEvent(prepareGallery);


// jQuery
$(document).ready(function() {
    $('div#landing-cycle, div#interior-cycle').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		cleartypeNoBg: true
	});
	
	$('div#landing-cycle, div#interior-cycle').cycle({
		fx: 'fade',
		cleartypeNoBg: true
	});
	
	$('body#flavors ul li:nth-child(odd)').addClass('odd');
	$('body#flavors ul li:nth-child(even)').addClass('even');
	
	// Custom Scrollbar
	$('#scrollbar').jScrollPane({
		scrollbarOnLeft: false
	});
	$('div#scrollbar a').mouseover(function() {
		$(this).css('opacity', '0.9');
	});
	$('div#scrollbar a').mouseoff(function() {
		$(this).css('opacity', '1.0');
	});
	
});

