// fitmentOptionsModel.js
var count; // decides to make Ajax call in case of failure
var modelArray =new Array();
$(function(){
      if(!document.getElementById('modelDDL'))
       { 
		$('body').append('<div id="modelDDL" style="display:none;">Choose...</div><div id="autoCurtain" style="display:none;"/><iframe id="curtainIframe" style="display:none;"/>');
	    preReq = false;
	}
		_DDL = $('#modelDDL');
		_DDLtgr = $('#modDDLtgr');
		_curtain = $('#autoCurtain');
		_ifr = $('#curtainIframe');
		
		fnReg=true;
		
		$('#yearId').change(function(){
			$(_DDLtgr).text('Choose...');
			fnReg=true;
		});
		
		$('#makeId').change(function(){
		
			fnReg=true;
			$(_DDL).empty();			
			$(_DDLtgr).text('Choose...');

			$(models[$('#yearId').val()][$('#makeId').val()]).each(function(i,value){
			modelArray[i]=value;
				$(_DDL).append('<a href="#'+i+'">'+value+'</a>');
				preReq = true
			})
		});
		$(_DDLtgr).hover(function(){
			$(this).css('background','#fff url('+jspStoreImgDir+'img/icons/DDL_win_over.gif) no-repeat top right');
		},function(){
			$(this).css('background','#fff url('+jspStoreImgDir+'img/icons/DDL_win.gif) no-repeat top right');
		});
		
		$(_DDLtgr).toggle(function(){
			var _global = $(this);
			var _pos = _global.offset();
			var ie6 = $.browser.msie && /6.0/.test(navigator.userAgent) ? true : false;
			if(_DDL.children().length<20){
				_ddlHgt = (_DDL.children().length)*20
			}else{
				_ddlHgt = 270
			}
			if(preReq){
				$(_DDL).css('width','550px')
			}else{
				if(ie6){
					_ddlHgt = 18
				}else{
					_ddlHgt = 16
				}
			}
			if($.browser.msie){
				topPos = _pos.top+(_global.height()+1)
			}else{
				topPos = _pos.top+(_global.height()+3)
			}
			if (ie6) {
				if(preReq){
					_ddlLeft = _pos.left-(_DDL.width()/1.75/*+(_global.width()/8)*/)+8
				}else{
					_ddlLeft = _pos.left
				}
			}else{
				if(preReq){
					_ddlLeft = _pos.left-(_DDL.width()/1.75/*+(_global.width()/8)*/)
				}else{
					_ddlLeft = _pos.left;
				}
			}
			
			$(_DDL).css({
				top:topPos,
				left:_ddlLeft,
				height:_ddlHgt
			}).show();
			$(_curtain).show();
			$(_ifr).css({
				top:topPos,
				left:_ddlLeft,
				width:$(_DDL).width()+2,
				height:$(_DDL).height()+2
			}).show();
			if(fnReg){
				$(_DDL).children('a').click(function(){
					if($(this).text().length>15){
						sVehic = $(this).text().substr(0,15)+'...'
					}else{
						sVehic = $(this).text()
					};
					$(this).addClass('selected').siblings().removeClass('selected');
					$(_global).text(sVehic);
					var modelValue=$(this).text();
					var result=compare(modelArray,modelValue);
					
					$('#modelId').val(result);
					count=1;
					loadVehicleOptions();
					$(_DDLtgr).click();
					return false;
				})
			  fnReg = false;
			}
		},function(){
			$(_DDL).hide();
			$(_curtain).add(_ifr).hide();
		});
		$(_curtain).click(function(){
			$(_DDLtgr).click();
		})
	});
