function Weblink_UserInterface_Search(current){
	var that = this;
	this.$ = $(this);

	var VehicleSearch;
	var ModuleFlash;
	this.init = function(){
		function initTabs(){
			if($('#weblink_updater > div.header div.tabs').size()==0){
				$('#weblink_updater > div.header').append('<div class="tabs"></div>');
				$('#weblink_updater > div.header div.tabs')
					.append('<img class="byfirmware" src="'+_getText('weblink-by-firmware-tab-off-src')+'"/>')
					.append('<img class="byvehicle" src="'+_getText('weblink-by-vehicle-tab-on-src')+'"/>')
					.append('<div class="protocol"><select id="protocol" disabled><option value="">'+_getText('weblink-select-a-protocol')+'</option></select></div>')
					.append('<div class="clear-both-fix"></div>').hide();
				var SI = new Weblink_ServerInterface();
				SI.$.one('dataLoaded',function(evt,data){
					$('div.protocol select#protocol').change(
							function(evt){
								var val = $(this).val(); 
								if(val){
									if(typeof(tracking) != "undefined"){
										tracking.addInfo("Changed protocol to "+val);
									}
									current.protocol = val; 
									initDivs();
									try{
										VehicleSearch.changeProtocol();
									}
									catch(e){} 
								}
							});
					var i = 1;
					encryptr = false;
					for(x in data){$('#weblink_updater > div.header div.tabs')
						$('div.protocol select#protocol').append(createOption(data[x].id,data[x].Name));
						//If protocol is #5 (Encrypted 2 way)
						if(data[x]['id'] == 5){
							encryptr= i;
						}
						i++;
					}
					if(!encryptr){
						$('div.protocol select#protocol').get(0).selectedIndex = 1;
						$('div.protocol select#protocol').change();
						$('div.protocol select#protocol').removeAttr('disabled');
						$('.byvehicle').each(switchTabs);
						showTabs();
					} else {
						encryptrSelected(encryptr,switchTabs);
					}
				});
				SI.getCompatibleProtocol(current.device,true);
				$('.byfirmware.off').live('click',switchTabs);
				$('.byvehicle.off').live('click',switchTabs);
				$('.byvehicle').removeClass('off').addClass('on');
				$('.byfirmware').removeClass('on').addClass('off');
			}
		}
		function switchTabs(){
			$(this).attr('src',$(this).attr('src').replace(/\.off\./,'.on.'));
			$(this).removeClass('off').addClass('on');
			
			if($(this).hasClass('byfirmware')){
				var other = $('.byvehicle');

				VehicleSearch.hide();
				FirmwareSearch.show();
			} else {
				var other = $('.byfirmware');
				VehicleSearch.show();
				FirmwareSearch.hide();
			}
			
			other.attr('src',other.attr('src').replace(/\.on\./,'.off.'));
			other.removeClass('on').addClass('off');
		}
		function initDivs(){
			if($("#weblink_updater > div.body div.search").size()==0){
				$("#weblink_updater > div.body").append('<div class="search"></div>');
			}
			VehicleSearch = new Weblink_UserInterface_Search_Vehicle(current);
			VehicleSearch.$.bind('vehicleSelected', that.hide);
			VehicleSearch.$.bind('vehicleSelected',  function(){that.$.triggerHandler('firmwareSelected');});
			VehicleSearch.init();
			
			FirmwareSearch = new Weblink_UserInterface_Search_Firmware(current);
			FirmwareSearch.$.bind('firmwareSelected', that.hide);
			FirmwareSearch.$.bind('firmwareSelected', function(){that.$.triggerHandler('firmwareSelected');});
			FirmwareSearch.init();
			
			if(current.starter && current.starter.get('Product') && in_array(current.starter.get('Product').id,[9,11,13])) CM6000Detected();
		}
		initTabs();
	};
	
	this.show = function(){
		$("#weblink_updater > div.body div.search").show();
		showTabs();
	};
	this.hide = function(){
		$("#weblink_updater > div.body div.search").hide();
		cleanTabs();
	};
	
	function showTabs(){
		$('#weblink_updater > div.header div.tabs').show();

	}
	function cleanTabs(){
		$('#weblink_updater > div.header div.tabs').hide();
	}
	
	function CM6000Detected(){
		//Called when cm6000 is detected
		var content = '<div class="warning cm6000">'+
		'<div><span>'+_getText('weblink-CM6000-notice-title')+'</span>'+
		'<span>'+_getText('weblink-CM6000-notice-header')+'</span>'+
		_getText('weblink-CM6000-notice-body')+
		'</div>'+
		'<div class="clear-fix"></div>'+
		'</div>';
		$('#weblink_updater > div.header div.tabs').before(content);
		$('#weblink_updater > div.header div.warning.cm6000 a.get-tabs-image').click(function(){ErrorMessage('<img src="/weblink/images/alerts/compu/cm6000.jpg"/>',{width:630,height:430,'title':_getText('weblink-CM6000-notice-connector-image')})});
		$('#weblink_updater > div.header div.warning.cm6000').hide().slideDown();
	}
	
	function encryptrSelected(protocol,switchTabs){
		//Called when encrypted protocol is available
		var content = '<div class="warning encryptr">'+
		'<img class="encryptr-logo" src="/weblink/images/icons/encryptr/logo.gif"/>'+
		'<div><span>'+_getText('weblink-encryptr-notice-title')+'</span><span>'+_getText('weblink-encryptr-notice-attention')+' </span>'+
		_getText('weblink-encryptr-notice-body')+
		'<div><img class="proceed" src="'+_getText('weblink-encryptr-proceed-button')+'"/><img class="no-proceed" src="'+_getText('weblink-encryptr-noproceed-button')+'"/></div>'
		'</div>'+
		'<div class="clear-fix"></div>'+
		'</div>';
		$('#weblink_updater > div.header div.tabs').before(content);
		function selectProtocol(i,bool){
			$('div.warning.encryptr').slideUp(function(){
				$('div.protocol select#protocol').get(0).selectedIndex = i;
				$('div.protocol select#protocol').change();
				if(!bool){
					$('div.protocol select#protocol').removeAttr('disabled');
					$('div.protocol select#protocol option[value=5]').remove();
					//$('div.protocol').hide();
				}
				$('.byvehicle').each(switchTabs);
				showTabs();
				$(this).remove();
				
			});
		}
		
		
		//Modify product image at the top to include little encryptr icon
		var pr_img = $('img.product-image')
		pr_img.attr('src',pr_img.attr('src').replace(/\/2\./,'/2-encryptr.'));
		
		//make cursor => pointer
		$('img.proceed,img.no-proceed').addClass('buttons');
		//If proceed Select protocol 5 and dont argue
		$('img.proceed').click(function(){
			selectProtocol(protocol,true);
		});
		//Else select first protocol available and unfreeze drop-down
		$('img.no-proceed').click(function(){
			selectProtocol(1,false);
			//Ok, forget little icon
			pr_img.attr('src',pr_img.attr('src').replace(/\/2\-encryptr\./,'/2.'));
		});
		
		$('#weblink_updater > div.header div.warning.encryptr').hide().slideDown();
	}
	function encryptrNotSelected(){
		$('#weblink_updater > div.header div.warning.encryptr').slideUp(function(){$(this).remove()});
	}
}
