VWAG.showDealerships = function(city, fade){
	if(city == 'nav1'){
		var div = city;
	}else{
		var div = 'nav1';
	}
	var cityArray = ['saskatoon', 'mapleridge', 'kelowna', 'regina', 'vernon', 'princealbert'];
	for(var i = 0; i < cityArray.length; i++){
		if($(cityArray[i])){
			$(cityArray[i]).setStyle('display', 'none');
		}
	}
	$(div).setStyle('display', 'none');
	if(fade == 'fade'){
		$(city).setStyle('opacity', 0);
	}
	$(city).setStyle('display', '');
	var fadeUp = new Fx.Tween(city, {transition: Fx.Transitions.Sine.easeOut, duration: 500});
	if(fade == 'fade'){
		$(city).fx = fadeUp.start('opacity', 0, 1);
	}
	
	Cufon.replace('#navFloat a', {hover: true});
	Cufon.replace('.navText');
}

// AJAX FOR INVENTORY
VWAG.inventoryAjax = function(dealership, stocktype, orderby, direction){
	var make = $('make_search').get('value');
	var model = $('model_search').get('value');	
	var yr = $('year_search').get('value');
	
	if(make == 'all' && model == 'all'){
		filter = '';
	}else if(make == 'all' && model != 'all'){
		filter = "model = '" + model + "'";
	}else if(make != 'all' && model == 'all'){
		filter = "make = '" + make + "'";
	}else if(make != 'all' && model != 'all'){
		filter = "make = '" + make + "' AND model = '" + model + "'";
	}
	
	var req = new Request({  
		method: 'post',  
		url: '../_php_dat/get_inventory.php',  
		data: {'dealership' : dealership, 'stocktype': stocktype, 'orderby': orderby, 'direction': direction, 'filter': filter, 'year': yr},
		onRequest: function(){
			$('ajax-loading').setStyle('visibility', 'visible');
		},  
		onComplete: function(response){
			if(response == 'error'){
				$('ajax-loading').setStyle('visibility', 'hidden');
				alert('php error!');
			}else{
				$('ajax-inventory').set('html', response);
				$('ajax-loading').setStyle('visibility', 'hidden');
				VWAG.applyListingMouseEffect();
				VWAG.divResize();
			}
		},
		onFailure: function(){
			$('ajax-loading').setStyle('visibility', 'hidden');
			alert('ajax error!');
		}
	}).send();
}

VWAG.populateModelSelect = function(dealership, stocktype){
	var make = $('make_search').get('value');
	var req = new Request({  
		method: 'post',  
		url: '../_php_dat/populate_model_select.php',  
		data: {'make' : make, 'dealership': dealership, 'stocktype': stocktype},
		onRequest: function(){
			$('model_search').setProperty('disabled', 'disabled');
		},  
		onComplete: function(response){
			if(response == 'error'){
				$('ajax-loading').setStyle('visibility', 'hidden');
				alert('php error!');
			}else{
				$('model_select_div').set('html', response);
				VWAG.inventoryAjax(dealership, stocktype, 'make', 'DESC');
				VWAG.repositionInventoryElements();
			}
		},
		onFailure: function(){
			$('ajax-loading').setStyle('visibility', 'hidden');
			alert('ajax error!');
		}
	}).send();
}

VWAG.keywordAjax = function(dealership, stocktype, keywords, orderby, direction){
	var req = new Request({  
		method: 'post',  
		url: '../_php_dat/inventory_keyword_search.php',  
		data: {'dealership': dealership, 'stocktype': stocktype, 'search' : keywords, 'order': orderby, 'direction': direction},
		onRequest: function(){
			$('ajax-loading').setStyle('visibility', 'visible');
		},  
		onComplete: function(response){
			if(response == 'error'){
				$('ajax-loading').setStyle('visibility', 'hidden');
				alert('php error!');
			}else{
				$('ajax-inventory').set('html', response);
				$('ajax-loading').setStyle('visibility', 'hidden');
				$('make_search').selecetedIndex = 0;
				$('model_search').selectedIndex = 0;
				VWAG.divResize();
				VWAG.applyListingMouseEffect();
			}
		},
		onFailure: function(){
			$('ajax-loading').setStyle('visibility', 'hidden');
			alert('ajax error!');
		}
	}).send();
}

VWAG.keywordSearch = function(dealership, stocktype){
	var keywords = $('keyword_search').get('value');
	if(keywords == '-Search by Keyword-' || keywords == 'Please Type Here' || keywords == ''){
		$('keyword_search').set('value', 'Please Type Here');
	}else{
		var req = new Request({  
			method: 'post',  
			url: '../_php_dat/inventory_keyword_search.php',  
			data: {'dealership': dealership, 'stocktype': stocktype, 'search' : keywords},
			onRequest: function(){
				$('ajax-loading').setStyle('visibility', 'visible');
			},  
			onComplete: function(response){
				if(response == 'error'){
					$('ajax-loading').setStyle('visibility', 'hidden');
					alert('php error!');
				}else{
					$('ajax-inventory').set('html', response);
					$('ajax-loading').setStyle('visibility', 'hidden');
					$('make_search').selectedIndex = 0;
					$('model_search').selectedIndex = 0;
					VWAG.divResize();
					VWAG.applyListingMouseEffect();
				}
			},
			onFailure: function(){
				$('ajax-loading').setStyle('visibility', 'hidden');
				alert('ajax error!');
			}
		}).send();
	}
}

VWAG.divResize = function(){
	if(VWAG.bodyElement.get('id') == 'inventory'){
		if($('ajax-inventory') && $('inventory-top')){
			var topHeight = $('inventory-top').getHeight();
			var winHeight = window.getHeight();
			var sortHeight = $('sort_by_div').getHeight();
			var listingHead = $( 'listing-header' ).getHeight();
			
			if(Browser.Engine.trident4){
				var offset = 41;
			}else{
				var offset = 40;
			}
			var divHeight = winHeight - topHeight - offset;
			var searchDivHeight = winHeight - topHeight - sortHeight - offset;
			var listingDivHeight = winHeight - offset;
			var listingDivHeight2 = winHeight - listingHead - offset;
			divHeight += 'px';
			searchDivHeight += 'px';
			listingDivHeight += 'px';
			listingDivHeight2 += 'px';
			$('ajax-inventory').setStyle('height', divHeight);
			$('search_result_div').setStyle('height', searchDivHeight);
			$( 'listing-content' ).setStyle( 'height', listingDivHeight );
			$( 'listing-main' ).setStyle( 'height', listingDivHeight2 );
		}
	}
}

VWAG.repositionInventoryElements = function(){
	if(Browser.Engine.trident){
		if(VWAG.bodyElement.get('id') == 'inventory'){
			$('keyword_search').setStyle('top', '-4px');
			$('model_search').setStyle('position', 'relative');
			$('model_search').setStyle('top', '-5px');
		}
	}
}

VWAG.applyListingMouseEffect = function(){
	$$('.vehicle_listing').addEvents({
		'mouseenter': function(){
			var elem = this.get('id');
			if(parseInt(elem) % 2){
				this.prevColor = '#e9f8ff';
			}else{
				this.prevColor = '#fff';
			}
			
			var fadeUp = new Fx.Morph(elem, {transition: Fx.Transitions.Sine.easeOut, duration: 500}).start({
				'background-color': [this.prevColor, '#f1f1f1']
			});
			
			$$( '.vehicle_listing' ).each( function(){
				var id = this.get( 'id' );
				if( parseInt( id ) % 2 )
				{
					var color = '#e8f9ff';
				}
				else
				{
					var color = '#fff';
				}
				this.setStyle( 'background-color', color );
			});
		},
		'mouseleave': function(){
			var elem = this.get('id');
			var fadeDown = new Fx.Morph(elem, {transition: Fx.Transitions.Sine.easeOut, duration: 500}).start({
				'background-color': ['#f1f1f1', this.prevColor]
			});
			
			$$( '.vehicle_listing' ).each( function(){
				var id = this.get( 'id' );
				if( parseInt( id ) % 2 )
				{
					var color = '#e8f9ff';
				}
				else
				{
					var color = '#fff';
				}
				this.setStyle( 'background-color', color );
			});
		},
		'click': function(){
			VWAG.getVehicleListing(this.get('id'));
		}
		
	});
	
	if(Browser.Engine.trident){
		var curs = 'hand';
	}else{
		var curs = 'pointer';
	}
	
	$$('div.vehicle_listing').setStyle('cursor', curs);
}

VWAG.getVehicleListing = function( vehicleID )
{
	VWAG.openListingPane('open', 1000);
	new Request({
		method: 'post',
		url: '../_php_dat/get_listing.php',
		data: { 'vehicle': vehicleID },
		onRequest: function()
		{
		},
		onComplete: function( response )
		{
			$( 'listing-content' ).set( 'html', response );
		},
		onFailure: function()
		{
			alert( 'AJAX Error.' );
		}
	}).send();
}

VWAG.openListingPane = function(direction, dur){
	if(dur == '' || isNaN(dur)){
		dur = 500;
	}
	var listingSlide = new Fx.Slide('listing-content', {transition: Fx.Transitions.Sine.easeOut, duration: dur});
	if(direction == 'open'){
		listingSlide.slideIn();
	}else{
		listingSlide.slideOut();
		listingSlide.addEvent( 'complete', function(){
			$( 'listing-content' ).set( 'html', '' );
		});
	}
}

VWAG.scrollToTop = function(){
	var myElement = $(document.body);
	var scroll = new Fx.Scroll(myElement, {
		wait: false,
		duration: 500,
		wheelStops: true,
		transition: Fx.Transitions.Quad.easeInOut
	});

	scroll.toTop();
}

/*/ AUTO COMPLETE /*////////////////////////////////////////////////////////////////////////////////////
/*Array.prototype.find = function(searchStr) {
	var returnArray = [];
	this.each(function(objeto,index){
		if (objeto.contains(searchStr)) {
			returnArray.push(objeto);
			alert('hay uno');
		}
	});
	return returnArray;
}

var autocomplete = new Class({
	
	Implements: [Options, Events],
	
	options: {
		boxWidth: 125,
		results: 10
	},
	
	initialize: function(element, dealership, stocktype, options){
		this.setOptions(options);
		
		// SET SOME VARIABLES //
		this.dealership = dealership;
		this.stocktype = stocktype;
		this.element = element;
		this.begin();
	},
	
	begin: function(){
		new Request({
			method: 'post',
			url: '../_php_dat/inventory_autocomplete.php',
			data: {'dealership': this.dealership, 'stocktype': this.stocktype},
			onRequest: function(){
			},
			onComplete: function(response){
				this.autoArray = response;
				this.autoArray = this.autoArray.split(", ");
				this.setEvents();
			},
			onFailure: function(){
			}
		}).send();
	},
	
	setEvents: function(){
		alert('set events');
	}
	
});*/
/*////////////////////////////////////////////////////////////////////////////////////////////////////*/

window.addEvent('domready', function(){
	// ACCESS BODY ELEMENT //
	VWAG.bodyElement = $$('body')[0];
									 
	// VWAG environment
	VWAG.execute = function(){
	
	// PREPARE AUTO DELETE //
	//VWAG.keywordAuto = new autocomplete($('keyword_search'), $('auto_dealership').get('value'), $('auto_stocktype').get('value'));
	
	// CONTENT DIV HEIGHT CONTROL //
	/*window.addEvent('resize', function(){
		if(VWAG.bodyElement.get('id') == 'inventory'){
			VWAG.divResize();
			
		}
	});
	
	if(VWAG.bodyElement.get('id') == 'inventory'){
		VWAG.divResize();
		
		// CLOSE LISTING PANE //
		VWAG.openListingPane('close', 10);
	}*/
	//////////////////////////////////
	
	var len = 150;
	var c = 0;
	if($('collapseNav-content')){
		var collapseMargin = $('collapseNav-content').clientHeight;	
	}
	var divArray = ['home', 'inventory', 'contact'];
	if($('collapseNav')){
		var divHeight = $('collapseNav').clientHeight;
		$('collapseNav').setStyle('height', divHeight);
	}	
	var onOff = true;
	var dur = 250;
	var firstTime = 0;
	var scrollNum = 1;
	var layer = true;
	var loadedImages = [];
	var ie6 = Browser.Engine.trident4;
	
	// DIV CONTROLS
	if($('loadingDiv')){
		$('loadingDiv').setStyle('opacity', 0);
		$('loadingBG').setStyle('opacity', 0);
	}
	
	if (document.images){
		navon = new Image(89, 22);
		navover = new Image(89, 22);
		if(VWAG.bodyElement.get('id') == 'home'){
			navon.src = 'images/navigationup.jpg';
			navover.src = 'images/navigationup_over.jpg';
		}else if(VWAG.bodyElement.get('id') == 'inventory'){
			navon.src = '../images/navigation.jpg';
			navover.src = '../images/navigation_over.jpg';
		}else{
			navon.src = '../images/navigationup.jpg';
			navover.src = '../images/navigationup_over.jpg';
		}
		
		gobutton = new Image(45, 22);
		gobutton.src = '../images/go_btn.gif';
		gobuttonover = new Image(45, 22);
		gobuttonover.src = '../images/go_btn_over.gif';
		backbutton = new Image(60, 22);
		backbuttonover = new Image(60, 22);
		backbutton.src = '../images/back-button.gif';
		backbuttonover.src = '../images/back-button_over.gif';
	}
	
	if(ie6){
		if($('imgDesc')){
			$('imgDesc').setStyle('background-color', '#000000');
			$('imgDesc').setStyle('filter', 'alpha(opacity = 50)');
		}
	}
	
	function inArray(what, where, force) { 
		var loc = false;
		var u;
		var force = !!force;
	 
		for (u in where) {
			if ((force && where[u] === what) || (!force && where[u] == what)) {
				loc = true;
				break;
			}
		}
	 
		return loc;
	}
	
	function slider(){
		var theSlide = new Fx.Slide('collapseNav-back', {transition: Fx.Transitions.Sine.easeOut, duration: dur});
		var theMove = new Fx.Morph('collapseNav', {transition: Fx.Transitions.Sine.easeOut, duration: dur});
		/*if(Browser.Engine.webkit){
			$('collapseNav-content').setStyle('margin-left', '10px');
		}else{
			$('collapseNav-content').setStyle('margin-left', '10px');
		}*/
		if(onOff){
			theSlide.slideOut();
			//if(VWAG.bodyElement.get('id') != 'inventory'){
				theMove.start({'margin-top': [-(divHeight - 22), 0]});
			//}
			onOff = false;
			if(VWAG.bodyElement.get('id') == 'home'){
				navon.src = 'images/navigation.jpg';
				navover.src = 'images/navigation_over.jpg';
			}else if(VWAG.bodyElement.get('id') == 'inventory'){
				navon.src = '../images/navigationup.jpg';
				navover.src = '../images/navigationup_over.jpg';
			}else{
				navon.src = '../images/navigation.jpg';
				navover.src = '../images/navigation_over.jpg';
			}
		}else{
			theSlide.slideIn();
			//if(VWAG.bodyElement.get('id') != 'inventory'){
				theMove.start({'margin-top': [0, -(divHeight - 22)]});
			//}
			onOff = true;
			if(VWAG.bodyElement.get('id') == 'home'){
				navon.src = 'images/navigationup.jpg';
				navover.src = 'images/navigationup_over.jpg';
			}else if(VWAG.bodyElement.get('id') == 'inventory'){
				navon.src = '../images/navigation.jpg';
				navover.src = '../images/navigation_over.jpg';
			}else{
				navon.src = '../images/navigationup.jpg';
				navover.src = '../images/navigationup_over.jpg';
			}
		}
		theSlide.addEvent('complete', function(){
			$('n_b').src = navon.src;
			VWAG.showDealerships('nav1', 'no fade');
		});
	}
	
	function show(div, fader){
		var changeImage = new Fx.Tween(div, {transition: Fx.Transitions.Sine.easeOut, duration: dur});
		if(fader){
			if($(div).fx){
				$(div).fx.pause();
			}
			$(div).fx = changeImage.start('opacity', 0, 1);
		}else{
			if($(div).fx){
				$(div).fx.pause();
			}
			$(div).fx = changeImage.start('opacity', 1, 0);
		}
		changeImage.addEvent('complete', function(){
		});
	}
	
	function sCrawl(dir){
		if($('scroller').style.marginLeft == undefined || $('scroller').style.marginLeft == null || $('scroller').style.marginLeft == ''){
			var mStart = 0;
		}else{
			var mStart = $('scroller').style.marginLeft;
		}
		if(dir){
			var theScroll = new Fx.Tween('scroller', {transition: Fx.Transitions.Sine.easeOut, duration: 500});
			if(scrollNum === (VWAG.scrollImages - 4)){
				theScroll.start('margin-left', mStart, 0);
				scrollNum = 1;
			}else{
				theScroll.start('margin-left', mStart, -(140 * scrollNum));
				scrollNum++;
			}
			$('rightArrow').removeEvents('click');
			theScroll.addEvent('complete', function(){
				$('rightArrow').addEvent('click', function(){
					sCrawl(1);
				});
			});
		}else{
			var theScroll = new Fx.Tween('scroller', {transition: Fx.Transitions.Sine.easeOut, duration: 500});
			if(scrollNum > 1){
				theScroll.start('margin-left', mStart, (parseInt(mStart) + 140 + 'px'));
				scrollNum--;
			}else{
				theScroll.start('margin-left', mStart, '-' + parseInt(mStart) + (140 * (VWAG.scrollImages - 5)) + 'px');
				scrollNum = VWAG.scrollImages - 4;
			}
			$('leftArrow').removeEvents('click');
			theScroll.addEvent('complete', function(){
				$('leftArrow').addEvent('click', function(){
					sCrawl(0);
				});
			});
		}
	}
	
	function activateLoader(ss){
		var loader = $('loadingDiv');
		var loaderBG = $('loadingBG');
		var loaderTween = new Fx.Tween(loader, {duration: 250});
		var bgTween = new Fx.Tween(loaderBG, {duration: 250});
		if(ss){
			loaderTween.start('opacity', 0, 1);
			if(!ie6){
				bgTween.start('opacity', 0, 0.7);
			}
		}else{
			loaderTween.start('opacity', 1, 0);
			if(!ie6){
				bgTween.start('opacity', 0.7, 0);
			}
		}
	}
	
	VWAG.sImage = function(img, desc){
		var imgLoaded = inArray(img, loadedImages);
		if(!imgLoaded){
			activateLoader(true);
		}
		var gallery = $('imgLoad');
		var imageDesc = $('imgDescription');
	
		new Asset.image(img, {
			onload: function(){
				gallery.empty();
				this.inject(gallery);
				$(imageDesc).set('text', desc);
				Cufon.replace('#imgDescription');
				//imageDesc.empty();
				//var vehicleDescription = new Element('span', {'class': '', 'html': desc}).inject(imageDesc); 
				if(!imgLoaded){
					activateLoader(false);
					loadedImages.push(img);
				}
			}
		});
				
	}
	
	if(VWAG.bodyElement.get('id') != 'home'){
		$('overhomeDiv').setStyle('opacity', 0);
	}
	if(VWAG.bodyElement.get('id') != 'careers'){
		$('overcareersDiv').setStyle('opacity', 0);
	}
	if(VWAG.bodyElement.get('id') != 'contact'){
		$('overcontactDiv').setStyle('opacity', 0);
	}
	
	if($('collapseNav')){
		$('collapseNav').setStyle('margin-top', -(collapseMargin) + 'px');
		
		/*var timer = (function(){
			c++;
			if(c >= len){
				$clear(timer);
				if(onOff){
					slider();
				}
			}
		}).periodical(1);*/
		$('collapseNav-back').setStyle('height', $('collapseNav-content').getHeight() + 'px');
	}
	
	// FORM FIELD POSITIONING FIX //
	VWAG.repositionInventoryElements();
	
	// NAVIGATION ////////////////////////////////////////////////////////////////////////////////////
	if(VWAG.bodyElement.get('id') != 'home'){
		$('nav_home').addEvent('mouseover', function(){
			show('overhomeDiv', 1);
		});
		$('nav_home').addEvent('mouseout', function(){
			show('overhomeDiv', 0);
		});	
		$('nav_home_over').addEvent('mouseover', function(){
			show('overhomeDiv',1);
		});
		$('nav_home_over').addEvent('mouseout', function(){
			show('overhomeDiv',0);
		});
	}
	
	if(VWAG.bodyElement.get('id') != 'careers'){
		$('nav_careers').addEvent('mouseover', function(){
			show('overcareersDiv',1);
		});
		$('nav_careers').addEvent('mouseout', function(){
			show('overcareersDiv', 0);
		});
		$('nav_careers_over').addEvent('mouseover', function(){
			show('overcareersDiv', 1);
		});
		$('nav_careers_over').addEvent('mouseout', function(){
			show('overcareersDiv', 0);
		});
	}
	
	if(VWAG.bodyElement.get('id') != 'contact'){
		$('nav_contact').addEvent('mouseover', function(){
			show('overcontactDiv',1);
		});
		$('nav_contact').addEvent('mouseout', function(){
			show('overcontactDiv', 0);
		});
		$('nav_contact_over').addEvent('mouseover', function(){
			show('overcontactDiv', 1);
		});
		$('nav_contact_over').addEvent('mouseout', function(){
			show('overcontactDiv', 0);
		});
	}
	if($('navBtn')){
		$('navBtn').addEvent('mouseover', function(){
			$('n_b').src = navover.src;
		});
		$('navBtn').addEvent('mouseout', function(){
			$('n_b').src = navon.src;
		});
		$('navBtn').addEvent('click', function(){
			slider();
			pageTracker._trackPageview("NAVIGATION COLLAPSE BUTTON" );
		});
	}
	
	if($('rightArrow') && $('leftArrow')){
		$('rightArrow').addEvent('click', function(){
			sCrawl(1);
		});
		$('leftArrow').addEvent('click', function(){
			sCrawl(0);
		});
	}
	
	$$('.back_to_top').each(function(btn){
		btn.addEvent('click', function(e){
			e.stop();
			VWAG.scrollToTop();
		});
	});
	
	$$('.career_form_submit').each(function(btn){
		btn.addEvent('mouseover', function(){
			this.setProperty('src', '../images/submit_btn_over.gif');
		});
		
		btn.addEvent('mouseout', function(){
			this.setProperty('src', '../images/submit_btn.gif');
		});
	});
	
	if($('back-button')){
		$('back-button').addEvent('mouseover', function(){
			this.src = backbuttonover.src;
		});
		
		$('back-button').addEvent('mouseout', function(){
			this.src = backbutton.src;
		});
	}
	
	if($('go_btn')){
		$('go_btn').addEvent('mouseover', function(){
			this.src = gobuttonover.src;
		});
		
		$('go_btn').addEvent('mouseout', function(){
			this.src = gobutton.src;
		});
	}
	//////////////////////////////////////////////////////////////////////////////////////////////////
	/*if($('nav_saskatoon')){
		$('nav_saskatoon').addEvent('click', function(e){
			e.stop();
			VWAG.showDealerships('saskatoon', 'fade');
		});
	}
	
	if($('nav_mapleridge')){
		$('nav_mapleridge').addEvent('click', function(e){
			e.stop();
			VWAG.showDealerships('mapleridge', 'fade');
		});
	}
	
	if($('nav_kelowna')){
		$('nav_kelowna').addEvent('click', function(e){
			e.stop();
			VWAG.showDealerships('kelowna', 'fade');
		});
	}
	
	if($('nav_vernon')){
		$('nav_vernon').addEvent('click', function(e){
			e.stop();
			VWAG.showDealerships('vernon', 'fade');
		});
	}*/
	Cufon.replace('#navFloat a', {hover: true});
	Cufon.replace('.navText');
	Cufon.replace('#imgDesc');
	Cufon.replace('#imgDescription');
	} ();
});