
!function(){var hasStorage=Modernizr.sessionstorage;var storage=hasStorage?window.sessionStorage:{};var nativeStorageMethods={init:function(key){storage[this.sessionKey]=storage[this.sessionKey]||'{}';},set:function(data){storage[this.sessionKey]=JSON.stringify(data);},get:function(){return JSON.parse(storage[this.sessionKey]);},drop:function(){delete storage[this.sessionKey];}};var fallbackStorageMethods={init:function(){storage[this.sessionKey]={};},set:function(data){storage[this.sessionKey]=data;},get:function(){return storage[this.sessionKey];},drop:function(){storage[this.sessionKey]={};}};var JSONSessionStore=function(sessionStorageKey){this.sessionKey=sessionStorageKey;this.init();};JSONSessionStore.prototype=fallbackStorageMethods;var root=this;var defaults={el:'#vehicleTools'};var SelectorView=function(options){this.options=$.extend({},defaults,options);this.template=this.options.template||Handlebars.compile($('#selectorTemplate').html());this.mediator=this.options.mediator;this.userSelection={};this.vehicleDataCache=new JSONSessionStore('vehicleDataCache');localStorage.setItem('userSelection',JSON.stringify(this.userSelection));this.init();};SelectorView.prototype={dataOptions:{bsData:{className:"no-knockback body-styles"},colorData:{"className":"no-knockback colour-selector"}},init:function(){this.$el=$(this.options.el);this.$nextAngleSwitcher=$('.x-axis-wrapper > .pointer.right');this.$prevAngleSwitcher=$('.x-axis-wrapper > .pointer.left');this.$el.on('click','.vehicle-selector',function(e){e.preventDefault();});this.$el.on('click','.body-styles ul li',this.bodyStyleSelected.bind(this));this.$el.on('click','.colour-selector ul li',this.colorSelected.bind(this));this.$el.on('click','.exterior-image ul li',this.exteriorImageSelected.bind(this));this.$nextAngleSwitcher.on({'click':this.exteriorImageSwitcher.bind(this),'mouseover':this.removePreview.bind(this)});this.$prevAngleSwitcher.on({'click':this.exteriorImageSwitcher.bind(this),'mouseover':this.removePreview.bind(this)});this.$el.on('click','.interior-image ul li',this.interiorImageSelected.bind(this));this.$el.on('click','.idf-image .js-overlay',this.idfSelected.bind(this));this.$el.on('click','.selector-options',this.hideOptions.bind(this));this.$el.on('click','.selector-options ul li',this.addClassSelected);this.mediator.subscribe('vehicleChange',this.updateUSC,this);this.mediator.subscribe('vehicleDataRequested',this.render,this);this.mediator.subscribe('exteriorImageData',this.updateExteriorImages,this);this.mediator.subscribe('interiorImageData',this.updateInteriorImages,this);this.mediator.subscribe('idfData',this.updateIdf,this);},updateUSC:function(payload){this.currentUSC=payload.data.uscCode;this.currentSeries=payload.data.seriesCode;},render:function(data){this.exteriorAngleImagePosition=0;this.aggregatedSelectorData=data;this.htmlBuffer='';this.defaultConfigModel=new EP2.SelectorDefaultConfigModel(data);this.exteriorImages=new EP2.VehicleExteriorImageModel(data.exteriorImages,data.uscCode,data.seriesCode,data.vehicleType,data.catalogId,{onSuccess:this.updateImages.bind(this)},data.presId);this.defaultConfigModel.subscribe('change:colors',this.updateColorSelector,this);if(this.aggregatedSelectorData.supportSelector==='true'){this.htmlBuffer+=this.renderBodyStyles();this.htmlBuffer+=this.renderColors();this.htmlBuffer+=this.renderExteriorImages();this.htmlBuffer+=this.renderInteriorImages();}
if(this.defaultConfigModel.get('idf').options.length>0){this.htmlBuffer+=this.renderIDF();}
this.$el.html(this.htmlBuffer);if(this.aggregatedSelectorData.supportSelector==='true'){this.notifyUserSelection(true);this.updateThumbnails();this.$el.fadeIn();}
this.vehicleType=data.vehicleType;if(this.vehicleType=="CV"){$('.body-styles-wrapper').fadeOut();}
if(this.exteriorAngleImagePosition===0){this.$prevAngleSwitcher.fadeOut('fast');}
if(this.aggregatedSelectorData.supportSelector==='true'){if(data&&(!data.images||data.images.length<1||data.images[0]==="")){this.notifyUserSelection(true,false);}}},notify:function(data){var data=data;this.mediator.notify('vehicleDataRequested',data);},addToDataSet:function(vehicleCode,vehicleData,selectorData){this.currentVehicle=vehicleCode;var aggregatedData=$.extend({},vehicleData,selectorData);var dataSet=this.vehicleDataCache.get();dataSet[vehicleCode]=aggregatedData;this.vehicleDataCache.set(dataSet);this.notify(aggregatedData);},bodyStyleSelected:function(e){e.preventDefault();var idx=$(e.target).closest('li').data('idx');var selectedBodyStyle=$('.body-styles').find('.selected').data('usc-code'),selectedColorIdx=$('.colour-selector').find('.selected').data('idx'),selectedExteriorIdx=$('.exterior-image').find('.selected').data('idx');var fetchBodyStyleData=this.defaultConfigModel.get('bodystyles').options[idx];var vehicleCode=selectedBodyStyle;var seriesCode=this.currentSeries;var vehicletype="PV";var gforcemode=EP2.getGforceMode();console.log(this.defaultConfigModel);$.ajax({url:'/cs/ContentServer?pagename='+EP2.getSiteName()
+'/EP2/body/cars/xaxis_detailed/SelectorBlock',error:function(){},dataType:'json',data:{uscCode:vehicleCode,seriesCode:seriesCode,vehicleType:vehicletype,gforceMode:gforcemode},success:this.addToDataSet.bind(this,vehicleCode,fetchBodyStyleData)});if(this.selectedBodyStyle===this.defaultConfigModel.get('bodystyles').options[idx])
return;this.defaultConfigModel.setColors(idx);this.selectedBodyStyle=this.defaultConfigModel.get('bodystyles').options[idx];this.emptyLocalStorageUserSelection();this.notifyUserSelection();this.updateThumbnails();},colorSelected:function(e){e.preventDefault();var idx=$(e.target).closest('li').data('idx');var configureButton=$('#ctaDetails .btnLink:first > a');var configureButtonUrl=configureButton.attr('href'),configureButtonUrl=configureButtonUrl.split('?');var selectedBodyStyle=$('.body-styles').find('.selected').data('usc-code'),selectedBodyStyleIdx=$('.body-styles').find('.selected').data('idx'),selectedExteriorIdx=$('.exterior-image').find('.selected').data('idx'),selectedcolour=$(e.target).closest('li').data('usc-code');configureButton.attr('href',configureButtonUrl[0]+"?features=C"+selectedBodyStyle.replace(/:/g,';')+":C"+selectedcolour);if(this.selectedBodyStyle===this.defaultConfigModel.get('colors').options[idx])
return;this.selectedColor=this.defaultConfigModel.get('colors').options[idx];this.emptyLocalStorageUserSelection();this.notifyUserSelection();this.updateThumbnails();},exteriorImageSelected:function(e){e.preventDefault();this.emptyLocalStorageUserSelection();var idx=$(e.target).closest('li').data('idx');var listSize=this.$el.find('.exterior-image li').length;var selectedListIdx=this.$el.find('.exterior-image li.selected').data('idx');var selectedIdx=$(e.target).parent().data('idx');this.exteriorAngleImagePosition=idx;if(this.aggregatedSelectorData.imagePack){this.$el.find('.exterior-image-wrapper > img').get(0).src=this.exteriorImages.get(idx,'exteriorImagePack','thumbnail');this.mediator.notify('vehicleImageChange',{index:idx,images:this.exteriorImages.get(idx,'exteriorImagePack')});}else{this.$el.find('.exterior-image-wrapper > img').get(0).src=this.exteriorImages.get(idx,'exteriorImages','thumbnail');this.mediator.notify('vehicleImageChange',{index:idx,images:this.exteriorImages.get(idx,'exteriorImages')});}
var totalSize=listSize-1;if(selectedIdx!==0&&selectedIdx<totalSize){this.$nextAngleSwitcher.fadeIn();this.$prevAngleSwitcher.fadeIn();}else if(selectedIdx===0){this.$nextAngleSwitcher.fadeIn();this.$prevAngleSwitcher.fadeOut();}else if(selectedIdx===totalSize){this.$prevAngleSwitcher.fadeIn();this.$nextAngleSwitcher.fadeOut();}},exteriorImageSwitcher:function(e){e.preventDefault();var $thisAttr=$(this).parent().attr('class');var el=this.$el.find('.exterior-image .selected');var idx=this.$el.find('.exterior-image .selected').data('idx');var listSize=this.$el.find('.exterior-image li').length;if($(e.target).closest('a').hasClass('right')){if((idx+1)===listSize){return;}
var nextList=el.next('li');var nextListIdx=nextList.data('idx');this.exteriorAngleImagePosition=nextListIdx;el.removeClass('selected');nextList.addClass('selected');if(nextListIdx+1===listSize)$(e.target).closest('a').fadeOut();if(nextListIdx+1<listSize)this.$prevAngleSwitcher.fadeIn();this.updateselectedAngle(nextListIdx);this.emptyLocalStorageUserSelection();}else if($(e.target).closest('a').hasClass('left')){if(idx===0){return;}
var prevList=el.prev('li');var prevListIdx=prevList.data('idx');this.exteriorAngleImagePosition=prevListIdx;el.removeClass('selected');prevList.addClass('selected');if(prevListIdx===0)$(e.target).closest('a').fadeOut();if(prevListIdx>0)this.$nextAngleSwitcher.fadeIn();this.updateselectedAngle(prevListIdx);this.emptyLocalStorageUserSelection();}},emptyLocalStorageUserSelection:function(){localStorage.removeItem('userSelection');localStorage.removeItem('userSelectionImg');var selectedIdx=$('.exterior-image').find('.selected').data('idx');var listSize=this.$el.find('.exterior-image li').length;var totalSize=listSize-1;var configureButton=$('#ctaDetails .btnLink:first > a');var configureButtonUrl=configureButton.attr('href'),configureButtonUrl=configureButtonUrl.split('?');var selectedBodyStyle=$('.body-styles').find('.selected').data('usc-code'),selectedcolour=$('.colour-selector').find('.selected').data('usc-code');configureButton.attr('href',configureButtonUrl[0]+"?features=C"+selectedBodyStyle.replace(/:/g,';')+":C"+selectedcolour);if(selectedIdx!==0&&selectedIdx<totalSize){this.$nextAngleSwitcher.fadeIn();this.$prevAngleSwitcher.fadeIn();}else if(selectedIdx===0){this.$nextAngleSwitcher.fadeIn();this.$prevAngleSwitcher.fadeOut();}else if(selectedIdx===totalSize){this.$prevAngleSwitcher.fadeIn();this.$nextAngleSwitcher.fadeOut();}
if(this.aggregatedSelectorData.imagePack){this.mediator.notify('vehicleImageChange',{index:selectedIdx,images:this.exteriorImages.get(selectedIdx,'exteriorImagePack')});}else{this.mediator.notify('vehicleImageChange',{index:selectedIdx,images:this.exteriorImages.get(selectedIdx,'exteriorImages')});}},updateselectedAngle:function(idx){if(this.aggregatedSelectorData.imagePack){this.$el.find('.exterior-image-wrapper > img').get(0).src=this.exteriorImages.get(idx,'exteriorImagePack','thumbnail');this.mediator.notify('vehicleImageChange',{index:idx,images:this.exteriorImages.get(idx,'exteriorImagePack')});}else{this.$el.find('.exterior-image-wrapper > img').get(0).src=this.exteriorImages.get(idx,'exteriorImages','thumbnail');this.mediator.notify('vehicleImageChange',{index:idx,images:this.exteriorImages.get(idx,'exteriorImages')});}},removePreview:function(e){e.preventDefault();$('figure.vehicle-preview-rollover').remove();},interiorImageSelected:function(e){e.preventDefault();var idx=$(e.target).closest('li').data('idx');this.$el.find('.interior-image-wrapper > img').get(0).src=this.exteriorImages.get(idx,'interiorImages','thumbnail');this.mediator.notify('interiorImageSelected',{index:idx});},idfSelected:function(e){e.preventDefault();var $this=$(e.target).closest('.js-overlay'),thumbSrc=$this.find('img').attr('src');this.$el.find('.idf-image-wrapper > img').get(0).src=thumbSrc;},addClassSelected:function(){$(this).addClass('selected').siblings('.selected').removeClass('selected');},notifyUserSelection:function(reset,silent){this.selectedBodyStyle=reset?this.defaultConfigModel.get('bodystyles').options[0]:this.selectedBodyStyle||this.defaultConfigModel.get('bodystyles').options[0];if(this.selectedBodyStyle.availableColors!=undefined){this.selectedColor=reset?this.selectedBodyStyle.availableColors.options[0]:this.selectedColor||this.selectedBodyStyle.availableColors.options[0];}
if(!this.isValidColorSelection()){}
if(!silent){var userConfig={b:this.selectedBodyStyle,c:this.selectedColor};this.exteriorImages.fetch(userConfig);this.mediator.notify('userConfigChange',userConfig);}},isValidColorSelection:function(){if(this.selectedBodyStyle.availableColors!=undefined){var aCols=this.selectedBodyStyle.availableColors.options,len=aCols.length;while(len--){if(this.compare(aCols[len],this.selectedColor))
return true;}}
return false;},compare:function(a,b){if(!a.usc==b.usc){return false;}
return true;},hideOptions:function(){this.$el.find('.selector-options').addClass('clicked');setTimeout(this.showOptions.bind(this),750);},showOptions:function(){this.$el.find('.selector-options').removeClass('clicked');},hide:function(speed){this.$el.fadeOut(speed);},show:function(){this.$el.fadeIn();},renderBodyStyles:function(){var retrieveUserSelection=JSON.parse(localStorage.getItem('userSelection'));var data=this.defaultConfigModel.get('bodystyles');var len=data.options.length;var match=false;if(this.selectedBodyStyle&&data.options.length>0){while(len--){if(this.selectedBodyStyle.usc===data.options[len].usc){data.options[len].selected=true;match=true;}}}
if(!match){if(!$.isEmptyObject(retrieveUserSelection)){data.options[retrieveUserSelection.bodyStyleOptIdx].selected=true;}else{data.options[0].selected=true;}}
return this.template($.extend({},this.dataOptions.bsData,data));},renderColors:function(){var data=this.defaultConfigModel.get('colors');if(data!==undefined){var len=data.options.length;var match=false;if(this.selectedColor&&data.options.length>0){while(len--){if(this.selectedColor.usc===data.options[len].usc){data.options[len].selected=true;match=true;}}}
if(!match){data.options[0].selected=true;}
return this.template($.extend({},this.dataOptions.colorData,data));}},renderExteriorImages:function(data){var data=data||{options:[]};data.className='exterior-image';this.defaultConfigModel.indexIt(data.options);if(data.options.length>0){data.options[this.exteriorAngleImagePosition].selected=true;}
return this.template(data);},renderInteriorImages:function(data){var data=data||{options:[]};data.className='interior-image';this.defaultConfigModel.indexIt(data.options);return this.template(data||{});},renderIDF:function(data){var data=this.defaultConfigModel.get('idf');data.className='idf-image';this.defaultConfigModel.indexIt(data);return this.template(data);},updateColorSelector:function(){this.$el.find('.'+this.dataOptions.colorData.className+'-wrapper').replaceWith(this.renderColors());},updateImages:function(data){if(typeof data.exteriorImagePack==='object'&&data.exteriorImagePack.options.length>0){this.updateExteriorImages(data.exteriorImagePack);}else{this.updateExteriorImages(data.exteriorImages);}
this.updateInteriorImages(data.interiorImages);this.updateIdf(data.idf);this.mediator.notify('selectorReady',this.aggregatedSelectorData);},updateExteriorImages:function(data){this.$el.find('.exterior-image-wrapper').replaceWith(this.renderExteriorImages(data));this.aggregatedSelectorData.exteriorImages=data;if(data.options.length>0){this.mediator.notify('vehicleImageChange',{index:this.exteriorAngleImagePosition,images:data.options[this.exteriorAngleImagePosition].images});if(this.aggregatedSelectorData.imagePack){this.$el.find('.exterior-image-wrapper > img').get(0).src=this.exteriorImages.get(this.exteriorAngleImagePosition,'exteriorImagePack','thumbnail');}else{this.$el.find('.exterior-image-wrapper > img').get(0).src=this.exteriorImages.get(this.exteriorAngleImagePosition,'exteriorImages','thumbnail');}}else{this.mediator.notify('imageResourceUnavailable',{});this.$el.find('.exterior-image-wrapper').fadeOut();}},updateInteriorImages:function(data){if(data.options.length>0){this.$el.find('.interior-image-wrapper').replaceWith(this.renderInteriorImages(data));$('.interior-image-wrapper > img').attr("src",data.options[0].thumbnail[0]);}else{this.$el.find('.interior-image-wrapper').fadeOut();}
this.aggregatedSelectorData.interiorImages=data;},updateIdf:function(data){var list=$('.idf-image-wrapper ul');if(list.length){if(list.is(':empty')){list.fadeOut();}else{this.$el.find('.idf-image-wrapper').replaceWith(this.renderIDF(data));}}else{$('.idf-image-wrapper').fadeOut();}
this.aggregatedSelectorData.idf=data;},updateThumbnails:function(){this.$el.find('.'+this.dataOptions.bsData.className.replace(' ','.')+'-wrapper > img').get(0).src=this.selectedBodyStyle.imagePath;if(this.selectedColor!=undefined){this.$el.find('.'+this.dataOptions.colorData.className.replace(' ','.')+'-wrapper > img').get(0).src=this.selectedColor.imagePath;}}};root.SelectorView=function(options){return new SelectorView(options);};}.call(EP2);
