(function($) {
	$.fn.hybridModelNav = function(options) {
		var opts = $.extend({}, $.fn.hybridModelNav.defaults, options);
		
		var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
		
		return this.each(function() {
			$this = $(this);
			
			var url = window.location.href.replace(o.regionalUrlOffset, "");  
			var location = url.split("/");
			if(!o.baseUrl) o.baseUrl = location[3]; 
			if(!o.location && location[4]) {
				o.location = location[4];
			}
			
			if(!o.model) o.model = location[3];
			
			if(o.xmlSource.indexOf(".xml") < 0) {
				o.xmlSource = o.xmlSource + ".xml";
			}
			
			if(o.xmlSource.indexOf("/") < 0) {
				o.xmlSource = o.baseXMLPath + o.xmlSource
			}
			
			if(o.xmlSource && !o.xmlData && o.xmlSource.indexOf("?")) {
				var url = o.xmlSource.split("?");
				o.xmlSource = url[0];
				o.xmlData = url[1];
			}
			
			$.ajax({
				type: 		o.requestType,  
				url: 		o.xmlSource,
				data: 		o.xmlData,
				dataType: 	"xml",
				success: 	function(xml) {
					
					$this.append('<div id="modelnav_left"></div><div id="modelnav_right"></div><div id="modelnav_items"></div>');
					$(xml).find('model').each(function(i){
						if(i != 0){
							$('#modelnav_items').append('<div class="modelnav_div"></div>');
						}
						var halfHeight = parseInt($(this).attr('height'))
						$('#modelnav_items').append("<a id='"+$(this).attr('id')+"' class='model_item "+$(this).attr('id')+"' href='"+$(this).find('url').text()+o.regionalUrlOffset+"'>"+$(this).find('title').text()+"</a>");
						if($(this).attr('id').toLowerCase() == o.model.toLowerCase()){
						    $('.model_item:last').addClass('model_on');
						}
					});
					//$this.append('<div class="modelnav_right"><span class="side_left"></span></div>');
					
					var doc = $(document);
					doc.trigger('ajax:complete', ["hybridModelNav"]);
				},
				error: 		function(){
					$this.html('error reading data source');
				}
			});
			
		});
		
	}
	
	$.fn.hybridModelNav.defaults = {
		baseXMLPath: 		"/global/hybrid/hybridModelNav/data/", 
		xmlSource: 			null, 			 								// location of leftNav XML data 
		xmlData: 			null,											// data to be passed to XML source if dynamically generated
		requestType: 		"GET",											// request type for AJAX call
		baseUrl: 			null,			  								// location from which to build all links in nav
		regionalUrlOffset:	"/en/us/",										// appeneded to all links to adjust for locality 
		location: 			null,			 	 							// location of page, if NULL, will be scraped from URL
		baseImgPath: 		"/global/hybrid/hybridModelNav/assets/images/",	// root location of images
		modelkey: 			null
	}
}) (jQuery);