//test
//-----------menu items--------------------//

function addMenuEventsTo(div){
	div.addEvent('mouseover',menuRollover);
	div.addEvent('mouseout',menuRollOut);
	storeMenuSettings(div)
	}
	
function storeMenuSettings(div){
	img = $E('img', div);
	src = img.getProperty('src');
	div.normal = new Asset.image(src, {alt: img.getProperty('alt')});
    src_over = (src.substring(0, src.lastIndexOf("."))) + "_h" + src.substr(src.lastIndexOf(".")); 
	div.over =  new Asset.image(src_over, {alt: img.getProperty('alt')});
}
	
function menuRollover(event){
	img = $E('img', this);
	img.setProperty('src', this.over.src);
}
function menuRollOut(event){
	img = $E('img', this);
	img.setProperty('src', this.normal.src);
}
	
//--------------Wobbling--------------------------//

function addWobbleTo(div){
	div.addClass("wobble");
	addWobbleEventsTo(div);
	}
function addWobbleEventsTo(div){
	div.addEvent('mouseover',wobbleRollover);
	storeWobbleValues(div);
	}
function storeWobbleValues(div){

	mbot = (div.getStyle('margin-bottom')) ? div.getStyle('margin-bottom').toInt(): 0;
	mtop = (div.getStyle('margin-top')) ? div.getStyle('margin-top').toInt(): 0;
	mleft = (div.getStyle('margin-left')) ? div.getStyle('margin-left').toInt(): 0;
	mright = (div.getStyle('margin-right')) ? div.getStyle('margin-right').toInt():0;

	div.startPosition = (mtop +'px ' + mright + 'px ' + mbot +'px ' + mleft +'px');
	div.endPosition = ((mtop - 5) +'px ' + mright + 'px ' + (mbot + 5)+'px ' + mleft +'px');
	}
	
var lastRolled;

function wobbleRollover(event){
	if (lastRolled != this){
		this.setStyle('margin', this.endPosition );
		moveback.delay(200,this);
	}
	lastRolled = this;
	}
	

function moveback(){
	this.setStyle('margin', this.startPosition);
	this.isRolling=false;
	}

//--------SubmitRolls------------------------//

function createFormButton(input){
    div = new Element('div').addClass('button_link').addClass('black').setHTML(input.value).injectBefore(input);
    div.addEvent('click',input.onclick);
    addButtonEventsTo(div);
    input.remove();
}

function submitform(action){
    this.action.value=action;
    this.submit("hello");

}

function findParent(parentType,el){
    while (el.getParent()){
        if (el.getParent().tagName == parentType ){
            return el.getParent();
        }
        el = el.getParent();
    }
    return null;
}

//--------ButtonRolls------------------------//

function addButtonEventsTo(div){
	div.addEvent('mouseover',buttonRollover);
	div.addEvent('mouseout',buttonRollOut);
	div.addEvent('mousedown',buttonPress);
	}
function buttonRollover(event){
	this.setStyle('background-image', 'url(' + root + '/images/buttons/button_over.gif)' );
}
function buttonRollOut(event){
	this.setStyle('background-image', 'url(' + root + '/images/buttons/button_normal.gif)' );
}
function buttonPress(event){
	this.setStyle('background-image', 'url(' + root + '/images/buttons/button_down.gif)' );
}

//-------------CLASS LOADERS---------------//

function addWobble(){
	
	found = $$('div.wobble');
	found.each( function(div){
		addWobbleEventsTo(div);
	});
    console.info("wobble added");
}

function addButtonRolls(){
	
	found = $$('div.button_link');
	found.each( function(div){
		addButtonEventsTo(div);
	});
}

function addSubmitRolls(){
    found = $$('input.button');
    found.each(function(input){
        createFormButton(input);
    });
}

function addMenuRolls(){
	
	found = $$('div.nav_link');
	found.each( function(div){
		addMenuEventsTo(div);
	});
	found = $$('div.sub_nav_link');
	found.each( function(div){
		addMenuEventsTo(div);
	});
}

//window.addEvent('domready',addMenuRolls);
window.addEvent('domready',addWobble);
//window.addEvent('domready',addButtonRolls);
//window.addEvent('domready',addSubmitRolls);
