$.extend($, { ford: {} });
$.extend($.ford, { modal: {} });
$.extend($.ford.modal, { postal: {} });
$.extend($.ford.modal.postal, { obj: null });
$.extend($.ford.modal.postal, { load: function() {
	$.ford.modal.postal.obj = $('<div>').dialog({
		autoOpen: false, 
		modal: true,
		resizable: false,
		width: 442
	});
	$.ford.modal.postal.obj.html('Loading...');
	$.ajax({
		url: _F.root + '/' + _F.lang + '/modal/postalCode.html',
		success: function(data) {
			$.ford.modal.postal.obj.html(data);
			$('.js_enter_postal').focus();
			$('.js_modal_save').bind('click', function() {
				if($.ford.validation.isPostal($('.js_enter_postal').val())) {
					$('.js_enter_postal').removeClass('error');
					$.ford.common.postalCode.change($('.js_enter_postal').val());
					$('html').trigger('postalChange');
					$.ford.modal.postal.close(true);
				} else {
					$('.js_enter_postal').addClass('error');
				}
			});
			$('.js_modal_close').bind('click', function() {
				$.ford.modal.postal.close($(this).hasClass('js_cookie'));
			});
		}
	});
}});
$.extend($.ford.modal.postal, { open: function() {
	if($.ford.modal.postal.obj == null) {
		$.ford.modal.postal.load();
	}
	$.ford.modal.postal.obj.dialog('open');
}});
$.extend($.ford.modal.postal, { close: function(setCookie) {
	if($.ford.modal.postal.obj != null) {
		$.ford.modal.postal.obj.dialog('close');
	}
	if(setCookie == true) {
		$.cookie('zip_ignore', 'true', { expires: 365, path: '/', domain: null, secure: false});
	}
}});


Ford = {
	vars: {
		dateFormat: 'mm/dd/yy'
	},
	Page: {},
	init: function() {
		Ford.cufon();
		$('html').ajaxComplete(function() {
			Ford.cufon();
		});			
		// any int work
		$.ajaxSetup({ scriptCharset: "ISO-8859-1" , contentType: "application/json; charset=ISO-8859-1"});
		Ford.Common.PostalCode.set();
		$('.changePostal').bind('click', function(){
			Ford.Common.PostalCode.changepopup();
		});
		Ford.Modal.load();
		if($.datepicker != undefined && _F.lang == 'fr') {
			$.datepicker.regional['fr'] = {clearText: 'Effacer', clearStatus: '',
				closeText: 'Fermer', closeStatus: 'Fermer sans modifier',
				prevText: '&#x3c;PrŽc', prevStatus: 'Voir le mois prŽcŽdent',
				nextText: 'Suiv&#x3e;', nextStatus: 'Voir le mois suivant',
				currentText: 'Courant', currentStatus: 'Voir le mois courant',
				monthNames: ['Janvier','F&#233;vrier','Mars','Avril','Mai','Juin',
				'Juillet','Ao&#251;t','Septembre','Octobre','Novembre','D&#233;cembre'],
				monthNamesShort: ['Jan','F&#233;v','Mar','Avr','Mai','Jun',
				'Jul','Ao&#251;','Sep','Oct','Nov','D&#233;c'],
				monthStatus: 'Voir un autre mois', yearStatus: 'Voir un autre annŽe',
				weekHeader: 'Sm', weekStatus: '',
				dayNames: ['Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi'],
				dayNamesShort: ['Dim','Lun','Mar','Mer','Jeu','Ven','Sam'],
				dayNamesMin: ['Di','Lu','Ma','Me','Je','Ve','Sa'],
				dayStatus: 'Utiliser DD comme premier jour de la semaine', dateStatus: 'Choisir le DD, MM d',
				dateFormat: 'dd/mm/yy', firstDay: 0, 
				initStatus: 'Choisir la date', isRTL: false};
	
			$.datepicker.setDefaults($.datepicker.regional[_F.lang]);
		}
		/* -- Header Search -- */
		$('#bingButton').bind('click', function() {
			$('#searchForm').submit();
		}).live("keypress", function(e) {
			if(e.keyCode == 13) {
				$('#dealerSearchButton').trigger('click');
			}
		});
		$('.bingSearch').bind('focus', function() {
			$(this).addClass('bingSearchActive');
		}).bind('focusout', function() {
			if($(this).val().length == 0) {
				$(this).removeClass('bingSearchActive');
			}
		});	
				
		if(!$.support.placeholder){
			Ford.replacePlaceholder();
		}
		
		$('div#fordNav ul li:visible').bind('mouseenter', function() {
			$(this).find('div.navLeft').toggleClass('leftHover');/*ie6 fix*/
			$(this).find('div.navRight').toggleClass('rightHover');/*ie6 fix*/
			$(this).find('div.navLeft, div.navRight, p').toggleClass('hover');
			$(this).find('div.sub').show();
			Cufon.refresh();/*do not remove! RB*/
		});	
		$('div#fordNav ul li:visible').bind('mouseleave', function() {
			$(this).find('div.navLeft').toggleClass('leftHover');/*ie6 fix*/
			$(this).find('div.navRight').toggleClass('rightHover');/*ie6 fix*/
			$(this).find('div.navLeft, div.navRight, p').toggleClass('hover');
			$(this).find('div.sub').hide();
			Cufon.refresh();/*do not remove! RB*/
		});

		$('div#fordNav ul li:hidden, div#lincNav').bind('mouseenter', function() {
			$(this).toggleClass('hover');
		});
		$('div#fordNav ul li:hidden, div#lincNav').bind('mouseleave', function() {
			$(this).toggleClass('hover');
		});
		$('div#fordNav li[data-url], div#fordNav p#pnsSplashPageLink').bind('click', function() {
			if($(this).data('acode')) {
				Ford.Common.url.goTo($(this).data('url') + '?acode=' + $(this).data('acode'));
			} else {
				Ford.Common.url.goTo($(this).data('url'));
			}
		});
		
		$('.js_postal_changer').bind('click', function() {
			$.ford.modal.postal.open();
		});
	},
	cufon: function() {
		Cufon.replace('.js_fontAntenna');
		Cufon.now();		
	},
	WT: function() {
		$('#_js_wt_page:first').each(function(cnt, object) {
			var group = $(this).attr('data-wtGroup');
			if(group == undefined) {
				group = $(this).parents('[data-wtGroup]:first').data('wtGroup');
			}
			
			var scenario = $(this).attr('data-wtScenario');
			if(scenario == undefined) {
				scenario = $(this).parents('[data-wtScenario]:first').data('wtScenario');
			}
			
			Ford.Common.WT(group, $(this).attr('data-wtSubGroup'), scenario, $(this).attr('data-wtSubScenario'));			
		});
		
		$('._js_wt').live('click', function() {
			var group = $(this).attr('data-wtGroup');
			if(group == undefined) {
				group = $(this).parents('[data-wtGroup]:first').data('wtGroup');
			}
			var subgroup = $(this).attr('data-wtSubGroup');
			if (subgroup == undefined) {
				subgroup = $(this).parents('[data-wtSubGroup]:first').data('wtSubGroup');
			}
			var scenario = $(this).attr('data-wtScenario');
			if(scenario == undefined) {
				scenario = $(this).parents('[data-wtScenario]:first').data('wtScenario');
			}
			
			Ford.Common.WT(group, subgroup, scenario, $(this).attr('data-wtSubScenario'), $(this).attr('data-wtAdClick'));
		});
	},
	include: function(filename) {
		var head = document.getElementsByTagName('head')[0];
		
		script = document.createElement('script');
		script.src = filename;
		script.type = 'text/javascript';
		
		head.appendChild(script);
	},
	Modal: {
		load: function() {
			$('._js_modal_leave').live('click', function(e) {
				e.preventDefault();
				Ford.Modal.Leave.open(e);
			});
			$('._js_modal_lincoln').bind('click', function(e) {
				e.stopImmediatePropagation();
				e.preventDefault();
				Ford.Modal.lincoln.open(e);
				return false;
			});
		},
		Leave: {
			obj: null,
			load: function(e) {
				Ford.Modal.Leave.obj = $('<div>').dialog({
					autoOpen: false, 
					modal: true,
					resizable: false,
					width: 442
				});
				Ford.Modal.Leave.obj.html('Loading...');
				$.ajax({
					url: _F.root + '/' + _F.lang + '/modal/leaveFord.html',
					success: function(data) {
						Ford.Modal.Leave.obj.html();
						Ford.Modal.Leave.obj.html(data);
						$('.js_modal_close').bind('click', function(e) {
							Ford.Modal.Leave.close();
						});
						$('.js_continue').bind('click', function() {
							var newUrl =  $(e.currentTarget).attr('href');
							window.open(newUrl);
							Ford.Modal.Leave.close();
						});
					}
				});					
			},
			open: function(e) {
				Ford.Modal.Leave.load(e);
				Ford.Modal.Leave.obj.dialog('open');
			},
			close: function() {
				if(Ford.Modal.Leave.obj != null) {
					Ford.Modal.Leave.obj.dialog('close');
				}
			}		
		},
		lincoln: {
			obj: null,
			load: function(e) {
				Ford.Modal.lincoln.obj = $('<div>').dialog({
					autoOpen: false, 
					modal: true,
					resizable: false,
					width: 442
				});
				Ford.Modal.lincoln.obj.html('Loading...');
				$.ajax({
					url: _F.root + '/' + _F.lang + '/modal/leaveLincoln.html',
					success: function(data) {
						Ford.Modal.lincoln.obj.html();
						Ford.Modal.lincoln.obj.html(data);
						$('.js_modal_close').bind('click', function(e) {
							Ford.Modal.lincoln.close();
						});
						$('.js_continue').bind('click', function() {
							var newUrl =  $(e.currentTarget).attr('href');
							window.open(newUrl);
							Ford.Modal.lincoln.close();
						});
					}
				});					
			},
			open: function(e) {
				Ford.Modal.lincoln.load(e);
				Ford.Modal.lincoln.obj.dialog('open');
				
				
			},
			close: function() {
				if(Ford.Modal.lincoln.obj != null) {
					Ford.Modal.lincoln.obj.dialog('close');
				}
			}		
		}
		
	},
	replacePlaceholder : function(){
		$('input:text[placeholder]').val(function() {
			$(this).addClass('placeholder');
			return $(this).attr('placeholder');
		});
		$('input:text[placeholder]').bind('focus', function() {
			if($(this).val() == $(this).attr('placeholder')) {
				$(this).val('');
				$(this).removeClass('placeholder');
			}
		}).bind('focusout', function() {
			if($(this).val().length == 0) {
				$(this).addClass('placeholder');
				$(this).val($(this).attr('placeholder'));
			}
		}).parents('form').bind('submit', function() {
		   $(this).children('input:text[placeholder]').val(function() {
			   if($(this).val() == $(this).attr('placeholder')) {
				   $(this).removeClass('placeholder');
				   return "";
			   }
			   return $(this).val();
		    });
		});
	}
};

F = Ford;
_F = Ford.vars;
try {
	$.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
		if(Ford.vars.isSsl && options.secure) {
			options.url = 'https://' + location.host + options.url;
		}
	});
} catch(e) {
	//console.log('$.ajaxPrefilter is only available in jQuery 1.5 and up')
}

/**
 * Adds Ford.init to document.ready.
 * It will be run first of the Ford document.readies which makes the initialized variables accessible to subsequent files.
 */
$(function() {
	Ford.init();

	$('.js_navDropDown').parent().children('h2').bind('click', function() {
	    if($(this).parent().children('ul').is(":visible")) {
	        $(this).parent().children('ul').hide();
	    } else {
	        $(this).parent().children('ul').show();
	    }
	});
});

/**
 * Add function to make getting to the data attr a little easer
 * Mark Determan March 6, 2011 
**/
(function( $ ){
	$.fn.data = function(name, data) {
		if(data == undefined) {
			return $(this).attr('data-' + name);
		} else {
			$(this).attr('data-' + name, data);
		}

	};
	$.fn.escape = function(val) {
		if(val == undefined) {
			return escape(this.val());
		}
		return escape(val);
	};
	$.fn.unescape = function(val) {
		if(val == undefined) {
			return unescape(this.val());
		}
		return unescape(val);
	};
	
})( jQuery );


$.extend($.support, { placeholder: !!('placeholder' in document.createElement('input')) });

/**
 * Over ride the text function to make sure that it escapes the text every time.
 * Mark Determan March 8, 2011
 */
jQuery.fn.text = function(text) {
	if ( jQuery.isFunction(text) ) {
		return this.each(function(i) {
			var self = jQuery( this );
	
			self.text( text.call(this, i, self.text()) );
		});
	}
	
	if ( typeof text !== "object" && text !== undefined ) {
		return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( unescape(text) ) );
	}
	
	return jQuery.text( this );
};

