/*
 * Autos.js is used on many automotive sites
 * it will be hosted on a CDN for caching purposes
 * This library requires jQuery
 */
if (typeof AUTOS == 'undefined')
{
	var AUTOS = {
	
		log : function (message)
		{
			if (typeof console.log == 'function')
				console.log(message);
		},
		//these urls will be relative to the current view ONLY
		//if you need urls for any other purpose, define them elsewhere (like in the cache or Global)
		Url : {				 
			base : '',
			current : '',
			current_with_query_string : '',
			post : '',
			get : ''
		},	
		Cache : {}, 	//store cache
		
		//global vars and methods. useful for templating
		Global : 
		{
			hash : false
			
		}, 
		User : {}, //data about currently logged in user		
		Ajax : {		//use for the page specific get/post urls
			url_get : '',
			url_post : ''
		},
		//Geolocation and other stuff
		Geo : 
		{ 
			watchId : false,
			latitude : false,
			longitude : false,
			getPosition : function () 
			{
				navigator.geolocation.getCurrentPosition(function(position) {	
					AUTOGROUPS.Geo.latitude = position.coords.latitude;
    				AUTOGROUPS.Geo.longitude = position.coords.longitude;		
				});
			},
			//gets the position and updates it every so often
			//for mobile devices on the move
			watchPosition : function() 
			{
		  		ATUOGROUPS.Geo.watchId = navigator.geolocation.watchPosition(function(position) {  
		   			AUTOGROUPS.Geo.latitude = position.coords.latitude;
		   			AUTOGROUPS.Geo.longitude = position.coords.longitude; 
		  	 	});   	
			}  	 		
		},	
		/* 
		 * given a select element, this function populates it with the given option values/names
		 */
		populateSelect : function(id, options)
		{		
			var select = document.getElementById(id);
			select.options.length = 0;
			if (typeof options != 'object')
			{ return; }
			for (var key in options)
			{
				var option = new Option(options[key], key); 		
				select.options[select.options.length] = option;		
			}	
		},
		/*
		 * for the given form element, limits the amount of characters the user can enter
		 * optionally can pass a span/div in outputElId that will display the amount of characters left for user
		 */
		characterLimiter : function(formElId, limit, outputElId)
		{
			 
			var el = $('#'+formElId);	
			el.keypress(function(e) { 
				
				//get length and compare to limit
				var val = $(this).val();
				var length = val.length;
				val = val.substr(0, limit);
				$(this).val(val);
				$('#'+outputElId).text(limit - length);
			});
		},
		/* returns all form values as an arry */ 
		getFormValues : function (formId) 
		{
			var inputs = $('#'+formId+' input, #'+formId+' textarea, #'+formId+' select');
		
			var values = {};
			inputs.each(function(e, el) {
				values[el.name] = $(el).val();
			});
			return values;
		},	
		/* given an element id that fits the naming convention, this function will return the numeric id that relates to the database
		* i.e., someelement_anotherpart_xxx   where xxx is the numeric id
		* this function returns the xxx part.  
		*/
		getUniqueIdFromEl : function(elId, separator)
		{	
			if (typeof separator == 'undefined') separator = '_';
			
			if (typeof elId == 'undefined' || !elId) { return false; }
			var parts = elId.split(separator);
			return parts[parts.length - 1];
		},
		/* for all form input and textarea elements that have the calss 'default', 
		* this function adds an event handler so that when the user focuses on one of them
		* it removes the 'default' class and also deletes the default value. 
		* useful for having default values such as 'enter name here' that also have a different colour of text
		*/
		styleDefaultFormValues : function (id) 
		{
			var form = $('#'+id);
			if (form.length == 0)
			{ return false; }		
		
			//get all form fields  and their values
			var values = form.children('input, textarea');			
		
			values.focus(function(e) {			
				if ($(this).hasClass('default'))
				{
					$(this).val('').removeClass('default');	
				}
			});			
			
			//store a copy of the original values
			var oldValues = {};
			values.each (function(index, element) {
				
				var name = $(this).attr('name');

					
				if ($(this).hasClass('default'))
				{
					var val = $(this).val();
					oldValues[name] = val;					
				}
				else
				{
					oldValues[name] = '';
				}
			
			
			});
			
			//set an event for the submit action
			$('#' + id).submit(function(e) { 
				e.preventDefault();
				//go through all form values
				//if one of the values is the same as one of the default values, then we remove it
				
				/*
				values.each(function(index, element) {
				
					var type = $(this).attr('type');
					
					if (type != 'submit')
					{
						var name = $(this).attr('name');						
						var value = $(this).val();
						
						if (typeof oldValues[name] != 'undefined')
							var oldValue = oldValues[name]; 
						else 
							var oldValue = ''; 
						
						if (oldValue == value) $(this).val('');
						
					}
					//console.log(oldValue + ' and ' + newValue);	
					
				
				});			
				*/
				
				values = $('#'+id).children('input, textarea');
				values.each (function(index, element) {
				
					if ($(this).hasClass('default'))
					{ $(this).val('').removeClass('default'); }
					
				});
				
			});
		},
		/*
		 * creates a basic popup window
		 */
		popup : function (url, name, width, height, scrollbars)
		{
			if (!window.focus)
			{ return true; }
		
			var href;
		
			if (typeof(url) == 'string')
			{ href = url; }
			else 
			{ href = url.href; }
			
			if (!width) 
			{ width = '500'; }
			if (!height)
			{ height = '400'; }
			
			if (!scrollbars || scrollbars == 'no') 
			{ scrollbars = 'no'; }
			else
			{ scrollbars = 'yes'; }
		
			window.open (href, name, 'width='+width+',height='+height+',scrollbars='+scrollbars);
			return false;
		},
		
		showMessage : function (message, textColor, duration)
		{
			
			var el = $('#message');
			
			el.html(message);
			
			if (typeof textColor != 'undefined')
				el.css('color', textColor);
				
				
			if (typeof duration != 'undefined')
				el.fadeIn().delay(duration).fadeOut();
			else
				el.fadeIn().delay(3000).fadeOut();
		
		
		
		}
				


		
	};
}


		
	
	

