
(function($){$.fn.cuiSpotlight=function(options){var settings=$.extend({fadeSpeed:500,idleTime:10000,dataSource:undefined,location_id:-1,page_ref:'unknown',autostart:'false',imgWidth:598,imgHeight:170},options||{});var cuiUid=CICT.genUid();var cuiObjId='cui-spotlight-'+cuiUid;$(this).attr('id',cuiObjId);var self=this;var data;var curIdx=0;var containers={};var buttons=[];var locked=false
function _createDom(){var c=''+'<div class="cui-imageContainer cui-rounded" style="cursor:pointer;">'+'</div>'+'<div class="cui-buttonContainer">'+'</div>';self.html(c);containers.img=$(self.children('.cui-imageContainer'));containers.cntr=$(self.children('.cui-buttonContainer'));_getData();};function _getData(){if(settings.dataSource!==undefined){$.ajax({url:settings.dataSource,context:self,data:{'location_id':settings.location_id,'page_ref':settings.page_ref},type:'POST',dataType:'json',success:function(d){data=d.banners;_createFirstImage();_createButtons();_assignEventListeners();_autoStart();},error:function(d){var msg='Er is een probleem opgetreden. '
+'Het ziet er naar uit dat de verwijzing naar het databestand niet juist is.';CICT.log({'type':'error','msg':msg});}});}else{var msg='Er is een probleem opgetreden. '
+'De brongegevens voor deze module konden niet worden gevonden.';CICT.log({'type':'error','msg':msg});}};function _createFirstImage(){CICT.log({type:'info',msg:'_createFirstImage'});self.css({'height':settings.imgHeight+'px'});if(data[0].bgimg){containers.img.css({'background-image':'url('+data[0].bgimg+')','background-repeat':'no-repeat','background-position':'left top','width':settings.imgWidth+'px','height':settings.imgHeight+'px'});}
if(data[0].html){containers.img.html(data[0].html);}else{containers.img.html('');}};function _createButtons(){CICT.log({type:'info',msg:'_createButtons'});var c='<form><div>';var i,nm;var cnt;for(i=0;i<data.length;i++){nm=cuiObjId+'-btn'+i;data[i].elmname=nm;c+='<input type="radio" id="'+nm+'" name="'+nm+'" value="'+i+'"';if(i==0){c+=' checked="checked"';}
c+=' /><label for="'+nm+'">&nbsp;</label>';}
c+='</div></form>';containers.cntr.html(c).ready(function(){containers.cntr.find('div').buttonset().ready(function(){var cnt=0;$(this).find('label').each(function(){var tooltip=false;if(typeof data[cnt]!=='undefined'){if(typeof data[cnt].txt!=='undefined'){tooltip=data[cnt].txt;}}
if(tooltip){}else{}
cnt++;});});});};function _assignEventListeners(){CICT.log({type:'info',msg:'_assignEventListeners'});containers.img.bind('click',function(evt){if(!locked){if(data[curIdx].url!==null){if(data[curIdx].url.substr(0,9)=='javascript'){eval(data[curIdx].url);}else{CICT.loadPage(data[curIdx].url,'_self');}}};});var i,nm,radio;if(data.length>=1){for(i=0;i<data.length;i++){nm=cuiObjId+'-btn'+i;containers.cntr.find('#'+nm).bind('click',function(evt){evt.preventDefault();_triggerStop();if(curIdx!==Math.floor($(this).val())){var tmpIdx=Math.floor($(this).val());nm=cuiObjId+'-btn'+curIdx;radio=$('#'+nm);radio[0].checked=false;radio.button("refresh");curIdx=tmpIdx;nm=cuiObjId+'-btn'+curIdx;radio=$('#'+nm);radio[0].checked=true;radio.button("refresh");_changeImage(curIdx);}});}}};function _autoStart(){if(String(settings.autostart)=='true'){_triggerStart();}else{}};function _changeImage(imgIdx){try{if(data[imgIdx].bgimg){containers.img.css({'background-image':'url('+data[imgIdx].bgimg+')','background-repeat':'no-repeat','background-position':'left top','width':settings.imgWidth+'px','height':settings.imgHeight+'px'});}else{containers.img.css({'background-image':'none'});}
if(data[imgIdx].html){containers.img.html(htmlspecialchars_decode(data[imgIdx].html));}else{containers.img.html('');}
curIdx=imgIdx;var nm=cuiObjId+'-btn'+curIdx;var radio=$('#'+nm);radio[0].checked=true;radio.button("refresh");}catch(e){try{settings.idleTime=0;_triggerStop();}catch(e){}
var msg='Er is een probleem opgetreden. '
+'Het ziet er naar uit afbeelding nr. '+curIdx+' niet kon worden gevonden.';CICT.log({type:'error',msg:msg});}};function _fadeOut(){self.children('.cui-imageContainer').delay(data[curIdx].delay).animate({opacity:0},settings.fadeSpeed,function(){var nm=cuiObjId+'-btn'+curIdx;var radio=$('#'+nm);radio[0].checked=false;radio.button("refresh");curIdx++;if(curIdx>=data.length){curIdx=0;}
_changeImage(curIdx);_fadeIn();});};function _fadeIn(){containers.img.animate({opacity:1},settings.fadeSpeed,function(){_fadeOut();});};function _fadeInterrupt(){try{containers.img.clearQueue();containers.img.stop(true,true).css({opacity:1});}catch(err){CICT.log({type:'warn',msg:err});}};function _triggerStart(){CICT.log({type:'info',msg:(cuiObjId+': triggerStart')});_fadeOut();};function _triggerStop(){CICT.log({type:'info',msg:(cuiObjId+': triggerStop')});_fadeInterrupt();};_createDom();return this;};})(jQuery);
