function changePage(e) {
	Event.stop(e)
	mylink = Event.element(e)
	$('vehicules_list_content').setStyle({'opacity' : 0})
	mylink.href.scan(/(.+)-([0-9]+):page-([0-9]+)/,
		function(match){
			id = match[2]
			page = match[3]
		})
	params = 'page=' + page + '&'
	if (mylink.href.include('toutes-les-'))
		params += 'model_id=' + id
	else
		params += 'betamodel_id=' + id
	new Ajax.Updater('vehicules_list_content', '/_vehicules_list.php', {
		method: 'get',
		asynchronous: false,
		parameters: params.toQueryParams(),
		onSuccess: function() {
			new Effect.Appear('vehicules_list_content', {duration: 0.5})
		}
	})
	$$('ul.page_numbering a').each(function(a){
		Event.observe(a, 'click', changePage)
	})
} // changePage

function changeTab(e) {
	Event.stop(e)
	mylink = Event.element(e)
	tab_content = $$('div.tab_content').first()
	tab_content.setStyle({'opacity' : 0})
	mylink.href.scan(/(.+)-([0-9]+):(\w+)/,
		function(match){
			id = match[2]
			tab = match[3]
		})
	if ('fiche' == tab) tab = 'worksheet'
	else if ('equipements' == tab) tab = 'equipments'
	new Ajax.Updater(tab_content, '/_' + tab + '.php', {
		method: 'get',
		asynchronous: false,
		parameters: { id: id },
		onComplete: function() {
			new Effect.Appear(tab_content, {duration: 0.5})
			$$('.tabs li').each(function(li){
				li.removeClassName('current')
			})
			mylink.up('li').addClassName('current')
			$$('p.add_option input').each(function(input) {
				Event.observe(input, 'click', updateTotalPrice)
			})
		}
	})
} // changeTab

function checkForm(e) {
	element = Event.element(e)
	uri = '/vehicule.php'
	var request = new Ajax.Request(uri, {
		method: 'post',
		parameters: Form.serializeElements($(element).getElements()),
		asynchronous: false
	})
	if (request.transport.responseText.empty())
		return
	if (!$('form_messages')) {
		p = new Element('p')
		div = new Element('div', { 'id': 'form_messages', 'class': 'error' })
		div.appendChild(p)
		document.body.insert({ top: div })
	}
	Event.stop(e)
	$('form_messages').hide()
	new Effect.ScrollTo('form_messages')
	new Effect.Appear('form_messages', {duration: 0.5})
	$('form_messages').down('p').update(request.transport.responseText)
	new Effect.Fade('form_messages', {delay: 5})
	new Effect.ScrollTo('form_messages')
} // checkForm

function checkPhoneNumber(e) {
	// IE
	if(e.keyCode < 48 || e.keyCode > 57) {
		e.returnValue = false;
		e.cancelBubble = true;
	}
	// DOM
	if(e.which < 48 || e.which > 57) {
		e.stop();
	}
}

function filterBrand(e) {
	var search_brand = Event.element(e)
	$$('select.search_model').each(function(select) {
		select.immediateDescendants().each(function(opt) {
			if ('' == search_brand[search_brand.selectedIndex].value)
				opt.show()
			else if(opt.getAttribute('label') == search_brand[search_brand.selectedIndex].text) {
				opt.show()
				select.value = opt.down('option').value
			}
			else 
				opt.hide()
		})
	})
} // filterBrand

function filterBrand2(search_brand) {
	$$('select.search_model').each(function(select) {
		select.immediateDescendants().each(function(opt) {
			if ('' == search_brand[search_brand.selectedIndex].value)
				opt.show()
			else if(opt.getAttribute('label') == search_brand[search_brand.selectedIndex].text) {
				opt.show()
				select.value = opt.down('option').value
			}
			else 
				opt.hide()
		})
	})
	
}

function showModels(e) {
	select = Event.element(e)
	location.href = select[select.selectedIndex].value
} // showModels

function showVehicule(e) {
	elem = Event.element(e)
	window.location = elem.up('tr').down('a').href
} // showVehicule

function showLink(e) {
	elem = Event.element(e)
	window.location = elem.up('h2').down('a').href
} // showLink

function showPromoLink(e) {
	elem = Event.element(e)
	window.location = elem.up('div').down('a').href
} // showPromoLink

function updateTotalPrice(e) {
	var options_price = 0
	$$('p.add_option input').each(function(input) {
		if(!isNaN(parseFloat($F(input)))) {
			options_price += parseFloat($F(input))
		}
	})
	$('options_price').update("Total avec options: <span>" + (parseFloat($('total_price').firstChild.nodeValue.gsub(/\s+/, '')) + parseFloat(options_price)) + "</span> € TTC")
} // updateTotalPrice

function launchEvents() {
	$('indicator').setStyle({ top: '1.5em' })
	$('indicator').hide()
	if ($('form_messages'))
		new Effect.Fade('form_messages', {delay: 4})
	$$('select.search_brand').each(function(select) {
		Event.observe(select, 'change', filterBrand)
	})
	$$('p.others select').each(function(select) {
		Event.observe(select, 'change', showModels)
	})
	$$('div.tabs a').each(function(a){
		Event.observe(a, 'click', changeTab)
	})
	$$('ul.page_numbering a').each(function(a) {
		Event.observe(a, 'click', changePage)
	})
	if ($('estimate_form')) Event.observe($('estimate_form'), 'submit', checkForm)
	$$('#vehicules_list_content table tbody tr').each(function(tr) {
		tr.setStyle({cursor: 'pointer'})
		Event.observe(tr, 'click', showVehicule)
	})
	$$('#content h2').each(function(title) {
		title.setStyle({cursor: 'pointer'})
		Event.observe(title, 'click', showLink)
	})
	$$('div.vehicule_infos, div#best_offer').each(function(div) {
		div.setStyle({cursor: 'pointer'})
		Event.observe(div, 'click', showPromoLink)
	})
	if($('edtTelephone'))
		Event.observe($('edtTelephone'), 'keypress', checkPhoneNumber)
} // launchEvents

Ajax.Responders.register({
	onCreate: function() { $('indicator').show() },
	onComplete: function() { 
		if (0 == Ajax.activeRequestCount)
			$('indicator').hide()
	}
})

Event.observe(window, 'load', launchEvents)
