/*jslint evil: false, strict: false, undef: true, white: false, onevar:false, browser:true, plusplus:false */
/*global jQuery,window, Cufon:true */

(function($, app){

	var initTypography = function(){
		// Main menu items
		Cufon.replace('#header .dealer-logo a', { fontFamily: 'book', hover: true });

		// Main menu items
		Cufon.replace('#mainmenu a', { fontFamily: 'book', hover: true });

		// Headers
		Cufon.replace('h2', { fontFamily: 'book', hover: true });
	};
        
	/**
	*	Init lightbox
	*/
	var initLightbox = function(){
		$('a[rel*=lightbox]').lightBox();
	}
        
	/**
	* 	Equalize heights
	*                
	*		@param { jQuery Object }		columns			An object containing all the elements 
	*		@param { Number}				colsPrRow		Number of columns in a row
	*
	*/
	var equalizeHeights = function(columns, selector){

		var currentTallest = 0,
		currentRowStart = 0,
		currentDiv,
		rowDivs = [],
		$el,
		topPosition = 0,
		cssSelector = "";
		
		// use default height selector if not set
		cssSelector = selector || "height";
		
		$(columns).each(function() {

			$el = $(this);
			topPosition = $el.position().top;

			if (currentRowStart != topPosition) {

			// we just came to a new row.  Set all the heights on the completed row
			for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
				rowDivs[currentDiv].css(cssSelector, currentTallest);
			}

			// set the variables for the new row
			rowDivs.length = 0; // empty the array
			currentRowStart = topPosition;
			currentTallest = $el.height();
			rowDivs.push($el);

			} else {
				// another div on the current row. Add it to the list and check if it's taller
				rowDivs.push($el);
				currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);
			}

			// do the last row
			for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
				// add last class if needed
				rowDivs[currentDiv].css(cssSelector, currentTallest);
			}
		});
	};


	/**
	*	Used cars list
	*		- initialize table sorting and sort by second row by default.
	*		- mark table rows with odd/even classes.
	*
	*/
	var initUsedCarsList = function(){

		$(document).ready( function(){
			$("table.used-cars").tablesorter({
				sortList:[[1,0]],
				widgets: ['zebra']
			});
		});
	};
	
	
	/**
	*	Prepare Links
	*/
	var prepareLinks = function(){

		// find all popup links
		var popupLinks = $('a[rel*=popup]');

		popupLinks.each(
			function(index, link){
				var options = link.rel.split("|");
				var topOffset = Math.round( ( $(window).height() - options[3] ) / 2 );
				var leftOffset = Math.round( ( $(window).width() - options[2] ) / 2 );

				link.onclick = function() {
					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;
				};
			}
		);

		// external links and PDF
		$('a[rel=external], a[rel=pdf]').click(function(){
			this.target = "_blank";
		});
	};
        
        
	/**
	*	Init frontpage        
	*/
	var initFrontpage = function(){
		// equalize used-cars containers
		equalizeHeights($('.used-cars .column .content'));

		// equalize used-cars headers
		equalizeHeights($('.used-cars .column .model'));
	};


	// once the dom is ready, start initializing stuff
	$(document).ready( function(){
		initTypography();
		initLightbox();
		prepareLinks();
		
		// frontpage
		if ( $('.frontpage .used-cars').length > 0 )
			initFrontpage();
			
		// used cars list
		if ( $('table.used-cars').length > 0)
			initUsedCarsList();
	});
        
}( jQuery, window.IFHA ));

