function replaceFonts () {

	// Dealer logo
	Cufon.replace('.dealer-logo a', { fontFamily: 'Volkswagen' });

	// Main header
	Cufon.replace('#page-title div.content span', { fontFamily: 'Volkswagen' });
	
	// Main menu items
	Cufon.replace('#mainmenu a', { fontFamily: 'Volkswagen', hover: true });

	// Content headers
	Cufon.replace('#content h1', { fontFamily: 'Volkswagen' });

	// Used car
	Cufon.replace('div.used-car h2', { fontFamily: 'Volkswagen' });

}


// Toggle departments openinghours
Departments = Behavior.create({
	initialize : function () {
		//Hide opening hours
		this.element.select( "div.opening-hours" ).invoke('hide');

		var departments = this.element.select("div.entry");
		var columns = [];

		//Equalize heights		
		departments.each( function ( elm, index ){
			columns.push( elm );

			if ( (index + 1) % 2 == 0){
				max_height = columns.invoke("getHeight").max();
				columns.invoke("setStyle", {"minHeight": max_height + "px"});
				columns = [];
			}
		}.bind(this));
		
	},
	onclick: function( e ) {
		var origin = Event.element( e );

		if ( origin.tagName.toLowerCase() == "a" && origin.hasClassName( "toggle-opening-hours" ) ){
			// Locate opening hours
			this.openinghours = origin.up("div.entry").down("div.opening-hours");
			
			if (this.openinghours){
				Effect.toggle(this.openinghours, "blind", { duration: 0.3 });
				origin.toggleClassName("open");
			}
		}
	}
});	

Event.addBehavior({
	'div.departments' : Departments
});


var TogglePickupService = Behavior.create({
	initialize : function() {
		// Show hidden containers if checkbox is initially checked
		$$('p.confirm input').each( function( element ) {
			var container = element.up("div.section").down(".fields");
			if ( element.checked && container ){ 
				container.show(); 
			}
		});
	},
	onclick : function ( e ) {
		var origin = Event.element ( e );
		if ( origin.tagName.toLowerCase() == "input" && ( origin.up( "p.confirm" ) ) ) {
			var container = origin.up("div.section").down(".fields");

			if ( container ) {
				Effect.toggle( container, "blind", { duration: 0.3 });
			}
		}
	}
});

Event.addBehavior({
	'div.book-service' : TogglePickupService
});


var CarValuation = Behavior.create({
	onclick : function ( e ) {
		var origin = Event.element ( e );
	
		if ($$("#radioDamage input").find(function(radio){return radio.checked;}).value == "1" ){		
				$$("#radioDamage select").first().removeAttribute("disabled");
		}		
		if ($$("#radioDamage input").find(function(radio){return radio.checked;}).value== "0" ){
				$$("#radioDamage select").first().setAttribute("disabled", "disabled");
		}		
	}
});

Event.addBehavior({
	'div.car-valuation' : CarValuation
});


function equalizeBoxHeights () {

	if( ! $$("table.boxes tr") ) { return false; }
	var rows = $$("table.boxes tr");
	
	rows.each( function ( row ){
		var columns = row.select(".entry");
		var max_height = columns.invoke("getHeight").max();
		columns.invoke("setStyle", {height: max_height + "px"});
	});
}

/**
*	ProductList
*	Description: equalize product image columns 	
*/
ProductList = Behavior.create({
	initialize : function () {
		var products = $$("div.product-list div.entry");
		var columns = [];
		var productRows = 3;

		products.each( function ( elm, index ){
			columns.push( elm );

			//End of row - or not enough entries to fill a row
			if ( (index + 1) % productRows == 0 || index == products.length-1){
				max_height = columns.invoke("getHeight").max();
				columns.invoke("setStyle", {"height": max_height + "px"});
				
				//Set last class on last entry in row
				if ( (index + 1) % productRows == 0) {
					columns = [];
					elm.addClassName( "last" );
				}	
			}
		}.bind(this));
	}
});


Event.addBehavior({
	'div.product-list' : ProductList
});



//Equalize height of used cars on the frontpage
EqualizeUsedCars = Behavior.create({
	initialize : function () {
		var titles = this.element.select("div.column a.model");
		var boxes = this.element.select("div.column");

		//Equalize heights of titles
		max_height = titles.invoke("getHeight").max();
		titles.invoke("setStyle", {"height": max_height + "px"});

		//Equalize heights of titles
		max_height = boxes.invoke("getHeight").max();
		boxes.invoke("setStyle", {"height": max_height + "px"});
	}
});

Event.addBehavior({
	'div.used-cars' : EqualizeUsedCars
});


function prepareLinks() {
	var links = $A(document.getElementsByTagName('a'));

	//find all popup links
	var popupLinks = links.findAll( 
		function( link ){
			return link.rel.indexOf( "popup|" ) == 0
		}
	);	

	// assign new function to popup links
	popupLinks.each(
		function(link){
			var options = link.rel.split("|");
			var topOffset = Math.round( ( self.screen.height - options[3] ) / 2 );
			var leftOffset = Math.round( ( self.screen.width - options[2] ) / 2 );

			link.onclick = function() {
				// TODO: figure out a better way of specifying options, that allows any order
				var sOptions = "width=" + options[2] + ",height="+ options[3] + ",top=" + topOffset + ",left=" + leftOffset
				if ( options[4] && options[4] == "scrollbars" ) {
					sOptions += ",scrollbars=yes";
				}
				window.open( link.href, options[1], sOptions  );
				return false;
			};
		}
	);	

	// find all links marked as external
	var externalLinks = links.findAll(
		function( link ) { return link.rel == "external" }
	);

	// make sure they open in a new window
	externalLinks.each(	
		function( link ) {
			link.target = "_blank";
		}
	);

	// find all pdf links
	var pdflinks = links.findAll(
		function( link ) { return link.rel == "pdf" }				 
	);

	// mark pdflinks with classname and make sure they open in a new window
	pdflinks.each(
		function( link ) {
			link.target = "_blank";
			Element.addClassName( link, "pdf" );
		}
	);
}

document.observe('dom:loaded', function() {
	// Set class js-enabled on body, to allow for unobtrusive enhancements
	$(document.body).addClassName( "js-enabled" );

	replaceFonts();
	prepareLinks();
});


