// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload){
				oldonload();
			}
			func();
		}
	}
}

function preparePopups() {

	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;

	var links = document.getElementsByTagName("a");

	for ( var i=0; i < links.length; i++) {
		//Checkif popup is in the styles
		if(links[i].className.indexOf("popup") != -1 ) {
			title = links[i].getAttribute("title")			

			links[i].onclick = function() {	
				return popup(this,title,this.className);
			}
			links[i].onkeypress = links[i].onclick;
		}

	}
}

function prepareDogRollOvers() {

	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("masthead")) return false;

	var masthead = document.getElementById("masthead");
	var dogs = masthead.getElementsByTagName('a');

	for ( var i=0; i < dogs.length; i++) {

		//Checkif 'dog' is in the styles
		if(dogs[i].className.indexOf("dog") != -1 ) {

			dogs[i].onmouseover = function() {	
				return swapDog(this);
			}
			dogs[i].onmouseout = function() {	
				return swapDog(this);
			}

			dogs[i].onkeypress = dogs[i].onclick;
		}

	}
}

function swapDog(link) {
	var new_image = document.createElement("img");
	var image = link.firstChild;
	// className = 'dog' means we are in the resting state so swap in the ainnimation
	if (link.className == 'dog'){
		link.className = '';
		if (link.id == 'snickering_dog') {
			image.setAttribute("src","/images/snickering_dog.gif");
		}else if (link.id == 'drinking_dog') {
			image.setAttribute("src","/images/drinking_dog.gif");
		}
	}else{
		link.className = 'dog';
		image.setAttribute("src","/images/dog.gif");
	}


}
function popup(target,title,classname) {
	if(classname.indexOf("full") != -1 ) {
		w = 1000
		h = 800
		attributes = 'width='+w+',height='+h+',scrollbars=yes,resizable=yes,toolbar=yes'
	}else{
		w = 640
		h = 480
		attributes = 'width='+w+',height='+h+',scrollbars=yes,resizable=yes'
	}

	window.open(target,'NextGenPopup',attributes)
	return false
}

function prepareSignUpForms(){
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("sidebar")) return false;
	
	var sidebar = document.getElementById("sidebar");
	var inputs = sidebar.getElementsByTagName('input')

	for ( var i=0; i < inputs.length; i++) {

		if(inputs[i].className.indexOf("signup") != -1 ) {
			inputs[i].onclick = function() {	
				this.select();
			}
		}
	}
}
addLoadEvent(preparePopups);
addLoadEvent(prepareDogRollOvers);
addLoadEvent(prepareSignUpForms);