document.observe('dom:loaded',function(){
	var landingImages = $$('img').filter(function(im){
		return im.readAttribute('src').match(/landing/);
	});
	if(landingImages.length > 0){
		landingImages[0].up().insert({'top':landingContactForm(landingImages[0].readAttribute('src').sub(/\.\w+$/,'').sub(/.*\//,''))});
		
		var maps = landingImages[0].up().select('area').filter(function(a){return a.readAttribute('href').match('contact');});
		maps.each(function(ar){
			ar.observe('mousedown',showLandingForm);
			ar.writeAttribute('href','javascript:void(0);');
		});
	}
});

function landingContactForm(page){
	var div = new Element('div',{'id':'landingFormDiv'}).setStyle({
		'width':'500px',
		'height':'800px',
		'position':'absolute',
		'top':'50px'
		}).hide();
	var form = new Element('form',{'id':'landingForm','method':'post','action':SITE_ROOT+'index.php'}).insert(new Element('input',{'type':'hidden','name':'landing_page','value':page}));
	div.insert(form);
	var greyOut = new Element('div',{'id':'greyOut'}).setStyle({'background-color':'#666','width':'100%','height':'100%','position':'fixed'}).setOpacity(.8).hide();
	$$('body')[0].insert({'top':greyOut});
	new Ajax.Request(SITE_ROOT+'index.php',{
		parameters:{'ajax_request':'landing_contact_form'},
		onSuccess:function(t){
			$('landingForm').insert(t.responseText);
		}
	});
	return div;
}
function closeLandingForm(e){
	new Effect.BlindUp('landingFormDiv',{'duration':.3});
	new Effect.Fade('greyOut',{'duration':.4});
	$$('.popClose')[0].stopObserving();
	$$('[name="request_contact"]').invoke('stopObserving');
}
function showLandingForm(e){
	new Effect.BlindDown('landingFormDiv',{'duration':.4});
	new Effect.Appear('greyOut',{'duration':.6,'to':.6});
	$$('.popClose')[0].observe('mousedown',closeLandingForm);
	if($$('[name="request_contact"]').length > 0){
		$$('[name="request_contact"]')[0].observe('mousedown',submitLandingForm);
	}
}
function submitLandingForm(e){
	showLoading();
	_gaq.push(['_trackPageview', '/b2bcontactsubmission']);
	$('landingForm').request({
		parameters:{'ajax_request':'send_landing_form'},
		onComplete:loadingFinished
	});
}
function showLoading(){
	$$('[name="request_contact"]')[0].replace(new Element('img',{'src':SITE_ROOT+'includes/media/images/loading.gif','id':'loading_button_spinner'}));
}
function loadingFinished(t){
	$('loading_button_spinner').replace(new Element('label').insert(t.responseText));
	document.location.href = "http://image3d.com/market/thankyou-landing";
}

