/* This one time, Luke Giuliani wrote a switch on an enum that either cured cancer, saved Africa from famine, or achieved world peace.
 * There was user error when it got deployed, so it just wrote this code instead as backup.
	(this javascript is Will's fault though)
 */

window.addEvent('domready', function(){

	$$('.js_replaceme_text').each(function(el, i){
		el.addEvent('focus', function(){
			if (el.get('value') == el.get('title')) {
				el.set('value', '');
			}
		});
		
		el.getParents('form').addEvent('submit', function() {
			if (el.get('value') == el.get('title')) {
				el.set('value', '');
			}
			
			return true;
		});
	});


	Shadowbox.init({
	});

	// do the icons for printing and so on
	if ($('headerIcons')){

		// first show them all
		$('headerIcons').setStyle('display', 'block');
		
		// print
		$('printThisPagePlease').addEvent('click', function(){
			window.print();
		});
		
		
		// add to faves
		$('addToFavorites').addEvent('click', function(){
			title = document.title; 
			url = window.location.href;

			if (window.sidebar) { // Mozilla Firefox Bookmark
				window.sidebar.addPanel(title, url,"");
			} 
			else if( window.external ) { // IE Favorite
				window.external.AddFavorite( url, title); 
			}
			else if(window.opera && window.print) { // Opera Hotlist
				return true; 
			}
		});
		
		// add to faves
		$('emailPageToAFriend').addEvent('mouseenter', function(){
			var linkString = 'mailto:Your%20Friend?subject=' + document.title + '&body=Hi, %0D%0DI thought you might like to know about the page "' + document.title + '" at careers.vic.gov.au. %0D%0DHere is a link:' + location.href + '.';
			$('emailPageToAFriend').set('href', linkString);
		});		
		
		// now we do the text sizes thing
		function fontSize(event, direction){
			event.stop();
			
			var changeables = [];
			var changeMatch = ['h2', 'h3', 'h4', 'h4', 'h5', 'h6', 'p', 'li', 'input', 'select', 'blockquote'];
			
			if ($('main')) {
				changeMatch.each(function(item){
					changeables.extend($('main').getElements(item));
				});
			}
			
			// on the home page, we have this extra chunk.
			if ($$('.articleContent').length) {
				$$('.articleContent').each(function(ac) {
					changeMatch.each(function(item){
						changeables.extend(ac.getElements(item));
					});
				});
			}

			changeables.extend($$('.tippety').getElement('strong'));
			changeables.extend($$('.tippety').getElement('em'));
			
			if ($('footer')) {
				changeables.extend($('footer').getElement('.icons').getElements('span'));
			}			
					  
		  changeables.each(function(el){
			  var delta = 0;
		  	if (direction == 'up'){
		  		delta = 1;
		   	} else if (direction == 'down'){
			   	delta = -1;
		   	}
		   	el.setStyle('font-size', (el.getStyle('font-size').toInt()+delta));
		  }); 
		}

		
		// add size function to elements
		$('fontIncrease').addEvent('click', function(e){
			fontSize(e, 'up');
		});
		$('fontDecrease').addEvent('click', function(e){
			fontSize(e, 'down');
		});		
		
	} //ends icons if
	
	// do our generic hover states for everythings
	$$('.hoverchild').each(function(child){
		child.setStyle('display', 'none');
		child.addEvents({
			'mouseenter': function(e){
				child.addClass('hover');
			},
			'mouseleave': function(e){
				child.removeClass('hover');					
			}
		});
	});
	
	// do hovers
	$$('.hoverdaddy').each(function(el){

		if ($chk(el.getElement('.hoverchild'))){
			el.addEvents({
				'mouseenter': function(e){
					el.getElement('.hoverchild').setStyle('display', 'block');
					el.addClass('hover');
				},
				'mouseleave': function(e){
					if (!(el.getElement('.hoverchild').hasClass('hover'))){
							el.removeClass('hover');					
							el.getElement('.hoverchild').setStyle('display', 'none');
					}
				}			
			});
		}//if
	});
	
	
	// do the icon re-orientations
	$('footer_icons').getElements('.a').each(function(el){
		el.addEvent('click', function(e){
			e.stop();
			window.location = el.get('rel');
		
		});
	});
	
});


