Event.observe(window, 'load', function() {
	if($('contact_map')) {		
		google_map('contact_map', '2000 Hogback, Ann Arbor MI');
	}
	
	if(cf = $$('div.contact_form')) {
		for(x = 0; x < cf.length; x++)
			get_contact_form(cf[x]);
	}	
	
	//bind_login();
});

Event.observe(window, 'dom:loaded', function() {
	$('top_menu').getElementsBySelector('a').each(function(n) {
		if(n.href == 'http://quotes.canfieldequipment.com/') {
			n.onclick = function() { quote_tool(); return false; };
			throw $break;
		}
	});
	
	//myLightWindow = new lightwindow();
});

function quote_tool() {	
	window.open('http://quotes.canfieldequipment.com/','popup','width=800,height=600,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no'); return false
}

function bind_login() {
	ln = $('form-login');
	
	if(!ln)
		return;
	
	if(ln.task.value == 'login') {
		ln.username.setAttribute('title', 'Please enter your user name.');
		ln.passwd.setAttribute('title', 'Please enter your password.');
		$(ln.username).addClassName('required');
		$(ln.passwd).addClassName('required');	
	}
	
	ln.setAttribute('callback', 'handle_login(o)');
	
	if(!ln.getElementsBySelector('.progress').length)
		ln.appendChild(Builder.node('div', { className: 'progress', style: 'float: right' }));
	
	bind_form(ln);
}

function handle_login(o) {	
	ln 		= $('form-login');
	
	xmlDoc 	= create_xml_doc(o.responseText);		
	html	= getText(xmlDoc, 'html');
	
	if(html && html.length) {
		ln.parentNode.innerHTML = html;
		bind_login();
	}
}

function get_contact_box(ttl) {
	contact_box = $('popup_contact_box');
	
	ovr 		= overlay('popup_contact_box');
	close_js 	= "$('popup_contact_box').hide(); $('" + ovr + "').hide();";
	
	if(!contact_box) {
		contact_box = Builder.node('div', { id: 'popup_contact_box' }, [
			Builder.node('div', { className: 'ttl', onclick: close_js }, [
				Builder.node('div', { className: 'cl', onclick: close_js }, '[ x ]'),
				Builder.node('span')
			]),			
			Builder.node('div', { className: 'frm' })
		]);
		
		document.body.appendChild(contact_box);
	}

	contact_box.getElementsBySelector('div.ttl span')[0].innerHTML = ttl ? ttl : 'Contact Canfield';
	
	get_contact_form(contact_box.getElementsBySelector('div.frm')[0], ttl);	
	
	center_box(contact_box);
}

function get_contact_form(elt) {
	loader(elt, 'images/loader.gif', 'Loading Contact Form...');
	
	new Ajax.Request('/staticfiles/contact_form.php?j=' + ut(), { 
		method: 'get',
		onSuccess: function(o) {
			elt.innerHTML = o.responseText;			
			bind_form(elt.getElementsByTagName('form')[0]);
		}
	});
}

function center_bubble(frm) {
	frm_anchor 	= $(frm.getAttribute('name') + '_alert');
	alert_box  	= $('custom_alert');
	
	if(!frm_anchor || !alert_box)
		return false;
	
	anchor_pos		= frm_anchor.cumulativeOffset();
	anchor_width	= frm_anchor.getWidth();
	
	alert_height	= alert_box.getHeight();
	alert_width		= alert_box.getWidth();

	alert_box.setStyle({
		left: 	(anchor_pos.left + anchor_width - alert_width + 120) + "px",
		top: 	(anchor_pos.top - alert_height + 7) + "px"
	});		
	
	alert_content	= $('alert_content');
	ac_height		= alert_content.getHeight();
	
	alert_content.setStyle({
		top: Math.floor((113 - ac_height) / 2 + 31) + 'px'
	});
}

function google_map(gid, address) {	
	geocoder = new google.maps.Geocoder();

	geocoder.geocode( { address: address, partialmatch: true }, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK && results.length) {
			if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
				// draw the map with the geocoded coordinates
				var myOptions = {
					zoom: 8,
					center: results[0].geometry.location,
					mapTypeId: google.maps.MapTypeId.ROADMAP			
				};		
				
				var map = new google.maps.Map(document.getElementById(gid), myOptions);		
				
				var marker = new google.maps.Marker({
					position: results[0].geometry.location, 
					map: map
				});   

				google.maps.event.addListener(marker, 'click', function() {
					window.open('http://maps.google.com/maps?f=d&hl=en&saddr=&daddr=' + address, '_directions','toolbar=no, directories=no, location=no, status=no, menubar=no, resizable=1, scrollbars= 1, width=770, height=540');
				});	
				
			}
		} else {
			// fail
		}
	});
}