var navImageMouseEnter = function() {
	var src = this.src.split('.png');
	this.src = src[0] + '_over.png';
};

var navImageMouseLeave = function() {
	var src = this.src.split('_over.png');
	this.src = src[0] + '.png';
};

var submitLead = function() {
	/*var lead = {};
	$$('.leadField').each(function(field) {
		lead[field.name] = field.get('value');
	});
	new Request.JSON({
		url: 'leads',
		method: 'post',
		data: lead,
		onComplete: function() {
			hidePanel();
			// Delay the showing of the thank you so hidePanel won't take it away too
			(function() { showPanel('panel_thankYou'); }).delay(800);
		}
	}).send();*/
	
	hidePanel();
	(function() { showPanel('panel_thankYou'); }).delay(800);
};

var showPanel = function(event) {
	var panel = null;
	if (event.target)
		panel = $(event.target.get('tag') == 'a' ? event.target.rel : event.target.alt);
	else
		panel = $(event);
	//console.log( event.target );
	if (!panel) return;
	var overlay = $('layout_overlay');
	var width = panel.getSize().x, height = panel.getSize().y;
	width = width || 600; // check si.css for div.panel's width
	
	$('layout_overlay_notice').setStyles({ opacity: 0.0, display: 'block' });
	overlay.setStyles({
		opacity: 0.0, display: 'block'
	});
	panel.setStyles({
		left: 		(window.getSize().x / 2) - (width / 2), 
		top: 		0,
		opacity: 	0.0,
		display: 	'block'
	});
	new Fx.Tween(overlay, { duration: 300, property: 'opacity' }).start(1.0);
	var transition = new Fx.Transition(Fx.Transitions.Elastic, 0.5);
	new Fx.Morph(panel, { duration: 1000, transition: transition.easeOut }).start({
		opacity: 1.0, top: height ? ((window.getSize().y / 2) - (height / 2)) : 60
	});
	gCurrentPanel = panel;
};

var hidePanel = function(event) {
	if (!gCurrentPanel) return;
	
	//console.log('hiding', gCurrentPanel.id);
	var overlay = $('layout_overlay');
	var notice = $('layout_overlay_notice');
	
	new Fx.Morph(gCurrentPanel, { duration: 300 }).start({
		opacity: 0.0, top: 0
	}).chain(function() {
		gCurrentPanel.setStyle('display', 'none');
	});
	new Fx.Tween(notice, { property: 'opacity' }).start(0.0).chain(function() {
		notice.setStyle('display', 'none');
	});
	new Fx.Tween(overlay, { duration: 300, property: 'opacity' }).start(0.0).chain(function() {
		overlay.setStyle('display', 'none');
	});
}

var rotateFeatures = function() {
	//var nextFeature = gCurrentFeature.getNext() || $('feature1');
	var nextFeature = gCurrentFeature.getNext() || $$('#layout_feature a')[0];
	gCurrentFeature.fade('out');
	nextFeature.fade('in');
	gCurrentFeature = nextFeature;
};

window.addEvent('domready', function() {
	if ($$('.wpcf7-form').length) {
		$$('.wpcf7-form')[0].removeChild($$('.wpcf7-response-output')[0]);
		$$('.wpcf7-submit')[0].addEvent('click', submitLead);
	}	
	
	//Clear "New ticket" form
	if (document.querySelectorAll){
		var textinputs = document.querySelectorAll('.ostbody input[type="text"]');
		for (var i = 0; i < textinputs.length; i++) {
			textinputs[i].set('value','');
		}
    }
	
	// Setup nav rollovers
	$$('img.nav').each(function(img) {
		img.addEvent('mouseenter', navImageMouseEnter);
		img.addEvent('mouseleave', navImageMouseLeave);
	});
	
	if ($('work_table')) {
		if (window.location.hash && window.location.hash != '' && window.location.hash != '#') {
			//console.log('>' + window.location.hash + '<');
			var work = 'work_' + window.location.hash.slice(1);
			// Fake clicking on one of the images
			showPanel({ target: new Element('a', { rel: work }) });
		}
	}
	// Setup the overlay and panels system
	if ($('layout_overlay')) $('layout_overlay').addEvent('click', hidePanel);
	$$('.showsPanel').each(function(e) {
		e.addEvent('click', showPanel);
	});
	
	// Setup features rotation
	if ($('layout_feature') && $$('#layout_feature a')[0]) {
		var features = $$('#layout_feature a'); features.shift();
		features.each(function(elem) { elem.fade('hide'); });
		gCurrentFeature = $$('#layout_feature a')[0];
		gFeatureRotateId = rotateFeatures.periodical(7300);
	}
});
