/**************************************************
 * Jodange
 * Public JavaScript
 **************************************************/

/* Perform initial setup tasks when the DOM is loaded
--------------------------------------------------*/

window.addEvent('domready', function() {	
	setupDefaultInputs();
	setupHomeGadgets();
//	setupGadgetLandingFocusFlash();
});

// Go through all form elements with class="default" 
// Set the class to "defaultText" (turns it light grey)
// and set up toggling of that class with focus/blur
function setupDefaultInputs() {
//	if(window.ie6) return;
	
	inputs = $$('input.default');
	
	inputs.each(function(input) {
		input.removeClass('default');
		input.addClass('defaultText');
		input.defaultValue = input.value;
		
		input.addEvent('focus', function() {
			this.removeClass('defaultText');
			if(this.value == this.defaultValue) 
				this.value = '';
		});
		
		input.addEvent('blur', function() {
			if(this.value == this.defaultValue || this.value == '') 
				this.addClass('defaultText');
			if(this.value == '') 
				this.value = this.defaultValue;
		});
	});
}

// If this is the gadget landing page, flash the focus opinion
function setupGadgetLandingFocusFlash() {
	var focus = $E('.gadgetLanding #focus');
	if($defined(focus)) {
		var flashFocus = new Fx.Style(focus, 'background-color', { duration: 250 });
		flashFocus.set('#ffffe0');
		flashFocus.start('#fae4a2').chain(function() {
			this.start('#ffffe0');
		}).chain(function() {
			this.start('#fae4a2');
		}).chain(function() {
			this.start('#ffffe0');
		}).chain(function() {
			this.start('#fae4a2');
		}).chain(function() {
			this.start('#ffffe0');
		});
	}
}

// Set up homepage gadget switcher
function setupHomeGadgets() {
	// Keep track of the active gadget
	var active = false;
	
	// Loop through gadgets
	var gadgets = $ES('#home #gadgets .gadget');
	gadgets.each(function(gadget) {
		
		// Get teaser and full elements
		var teaser = $E('.teaser', gadget);
		var full = $E('.full', gadget);
		
		// Get the location of the gadget's content from the href
		var contentURL = $E('a', teaser).href;
		
		// Set up effects
		var teaserSlide = new Fx.Slide(teaser);
		var fullSlide = new Fx.Slide(full);
		
		// If this is the initially active gadget, register it
		if(gadget.hasClass('active')) {
			active = gadget;
			teaser.setStyle('display', 'block');
			teaserSlide.hide();
			gadget.contentLoaded = true;
		}
		
		// Otherwise, show full and hide it
		else {
			full.setStyle('display', 'block');
			fullSlide.hide();
			gadget.contentLoaded = false;
		}
		
		// Activate function
		gadget.activate = function() {
			if(window.gecko) full.setStyle('visibility', 'visible');
			teaserSlide.stop();
			fullSlide.stop();
			teaserSlide.slideOut();
			fullSlide.slideIn();
			active.deactivate();
			active = this;
			if(!this.contentLoaded) {
				this.loadContent();
			}
		};
		
		// Deactivate function
		gadget.deactivate = function() {
			if(window.gecko) full.setStyle('visibility', 'hidden');
			teaserSlide.stop();
			fullSlide.stop();
			teaserSlide.slideIn();
			fullSlide.slideOut();
		};
		
		// Teaser hover
		teaser.addEvent('mouseover', function() {
			this.addClass('hover');
		});
		teaser.addEvent('mouseout', function() {
			this.removeClass('hover');
		});
		
		// Teaser onclick
		teaser.addEvent('click', function(e) {
			e = new Event(e).stop();
			gadget.activate();
		});
		
		// Load gadget content (AJAX)
		gadget.loadContent = function() {
			new Ajax(contentURL, { method: 'get', update: full, onSuccess: function() { gadget.contentLoaded = true; } }).request();
		};
	});
}