(function($){$.widget=function(key,options){var errors=new $.errors();var territory=options.territory||'GB';var api=new $.api(key,errors,options);this.cinemas=function(options){$.extend(cinemaSettings,options);};this.films=function(options){$.extend(filmSettings,options);};this.dates=function(options){$.extend(dateSettings,options);};this.performances=function(options){$.extend(performanceSettings,options);};this.init=function(){errors.hide();widgetItems.push(new $.widget.cinemas(cinemaSettings));widgetItems.push(new $.widget.films(filmSettings));widgetItems.push(new $.widget.dates(dateSettings));widgetItems.push(new $.widget.performances(performanceSettings));params.territory=territory;widgetItems.sort(function(a,b){return a.get_settings().order-b.get_settings().order;});$.each(widgetItems,function(i,item){item.get_listTarget().change(choose);var filter=item.get_settings().filter;if(filter.length>0){fixedParams[item.get_paramName()]=filter;if(filter.length===1&&i>0){populate(i,false);}}});populate(0,true);};var choose=function(event){var selectedIndex;$.each(widgetItems,function(i,item){if(item.get_listTarget().get(0).id===event.target.id){selectedIndex=i;}});var settings=widgetItems[selectedIndex].get_settings();if(settings.onSelect&&typeof settings.onSelect=='function'){settings.onSelect($(event.target).find('option:selected').text());}
widgetItems[selectedIndex].get_listTarget().find("option[value='']").remove();params[widgetItems[selectedIndex].get_paramName()]=$(event.target).find('option:selected').val();widgetItems[selectedIndex].displayFields($(event.target).find('option:selected').data('details'));populate(selectedIndex+1,true);};var populate=function(index,recurse){$.each(widgetItems,function(i,item){if(i>index){item.resetList(widgetItems[index].get_labels().choose);}
if(i>index-1){widgetItems[i].hideDisplay();delete params[widgetItems[i].get_paramName()];}});widgetItems[index].populateList(api,recurse,$.extend({},fixedParams,params));};var widgetItems=[];var fixedParams={};var params={};var cinemaSettings={};var filmSettings={};var dateSettings={};var performanceSettings={};};var widgetItem=function(spec){var settings=$.extend({filter:[],full:true,defaultFirst:false,displayFields:true},spec.settings);var labels=$.extend({choose:'Select a '+spec.paramName,empty:'No '+spec.paramName+'s available'},spec.labels);var that={};that.get_listTarget=function(){return spec.listTarget;};that.get_paramName=function(){return spec.paramName;};that.get_displayTarget=function(){return spec.displayTarget;};that.get_settings=function(){return settings;};that.get_labels=function(){return labels;};var resetList=function(label){spec.listTarget.empty();labelList(label);};that.resetList=resetList;var labelList=function(label){spec.listTarget.append($('<option></option>').attr('value','').text(label));};that.labelList=labelList;var hideDisplay=function(){spec.displayTarget.hide();};that.hideDisplay=hideDisplay;that.displayFields=function(item){if(item&&settings.displayFields){that.writeFields(item,spec.displayTarget);spec.displayTarget.show();}};return that;};$.widget.cinemas=function(options){var that=widgetItem({listTarget:$('#cineworld_cinemas'),displayTarget:$('#cineworld_selected_cinema'),paramName:'cinema',settings:$.extend({order:1},options.settings),labels:options.labels});that.writeFields=options.writeFields||function(cinema,target){target.find('.address').text(cinema.address);target.find('.postcode').text(cinema.postcode);target.find('.telephone').text(cinema.telephone);target.find('.link').attr('href',cinema.cinema_url);};that.populateList=function(api,recurse,params){api.fetchCinemas($.extend({full:that.get_settings().full},params),function(data){that.get_listTarget().empty();var options=[];$.each(data.cinemas,function(){var option=$('<option></option>').attr('value',this.id).text(this.name);option.data('details',this);if(that.get_settings().favourite&&that.get_settings().favourite===this.id){if(that.get_labels().otherGroup){options.unshift($('<optgroup></optgroup>').attr('label',that.get_labels().otherGroup));}
options.unshift(option);if(that.get_labels().favouriteGroup){options.unshift($('<optgroup></optgroup>').attr('label',that.get_labels().favouriteGroup));}}else{options.push(option);}});if(options.length===0){that.labelList(that.get_labels().empty);}else if(options.length>1){that.labelList(that.get_labels().choose);}
$.each(options,function(){that.get_listTarget().append(this);});if(options.length===1){if(recurse){that.get_listTarget().change();}else{that.displayFields(data.cinemas[0]);}}else if(options.length>1&&that.get_settings().defaultFirst){that.get_listTarget().get(0).selectedIndex=1;that.get_listTarget().change();}});};return that;};$.widget.films=function(options){var that=widgetItem({listTarget:$('#cineworld_films'),displayTarget:$('#cineworld_selected_film'),paramName:'film',settings:$.extend({order:2},options.settings),labels:options.labels});var assets=$.extend({poster:'http://www.cineworld.com/assets/images/poster_placeholder.jpg',still:'http://www.cineworld.com/assets/images/still_placeholder.jpg'},options.assets);that.writeFields=options.writeFields||function(film,target){target.find('.title').text(film.title);var poster_url=assets.poster;if(film.poster_url!==undefined){poster_url=film.poster_url;}
target.find('img.large').attr('src',poster_url);var still_url=assets.still;if(film.still_url!==undefined){still_url=film.still_url;}
target.find('img.still').attr('src',still_url);target.find('img').attr('alt',film.title);var classification='';if(film.classification!==undefined){classification=film.classification;}
target.find('.classification').text(classification);var advisory='';if(film.advisory!==undefined){advisory=film.advisory;}
target.find('.advisory').text(advisory);var film_link='';var disable=false;if(film.film_url!==undefined){film_link=film.film_url;}else{disable=true;}
var link=target.find('.link');link.attr('href',film_link);if(disable){link.addClass('disabled').click(function(){return false;});}else{link.removeClass('disabled').unbind('click');}};that.populateList=function(api,recurse,params){api.fetchFilms($.extend({full:that.get_settings().full},params),function(data){that.get_listTarget().empty();var options=[];$.each(data.films,function(){var option=$('<option></option>').attr('value',this.edi).text(this.title);option.data('details',this);if(that.get_settings().favourite&&that.get_settings().favourite===this.edi){if(that.get_labels().otherGroup){options.unshift($('<optgroup></optgroup>').attr('label',that.get_labels().otherGroup));}
options.unshift(option);if(that.get_labels().favouriteGroup){options.unshift($('<optgroup></optgroup>').attr('label',that.get_labels().favouriteGroup));}}else{options.push(option);}});if(options.length===0){that.labelList(that.get_labels().empty);}else if(options.length>1){that.labelList(that.get_labels().choose);}
$.each(options,function(){that.get_listTarget().append(this);});if(options.length===1){if(recurse){that.get_listTarget().change();}else{that.displayFields(data.films[0]);}}else if(options.length>1&&that.get_settings().defaultFirst){that.get_listTarget().get(0).selectedIndex=1;that.get_listTarget().change();}});};return that;};$.widget.dates=function(options){var that=widgetItem({listTarget:$('#cineworld_dates'),displayTarget:$('#cineworld_selected_date'),paramName:'date',settings:$.extend({order:3},options.settings),labels:options.labels});that.writeFields=options.writeFields||function(date,target){target.text(formatDate(convertSimpleDate(date)));};that.populateList=function(api,recurse,params){api.fetchDates(params,function(data){that.get_listTarget().empty();var singleResult=false;var options=[];$.each(data.dates,function(){var option=$('<option></option>').attr('value',this).text(formatDate(convertSimpleDate(this)));option.data('details',this);if(that.get_settings().favourite&&that.get_settings().favourite===this){if(that.get_labels().otherGroup){options.unshift($('<optgroup></optgroup>').attr('label',that.get_labels().otherGroup));}
options.unshift(option);if(that.get_labels().favouriteGroup){options.unshift($('<optgroup></optgroup>').attr('label',that.get_labels().favouriteGroup));}}else{options.push(option);}});if(options.length===0){that.labelList(that.get_labels().empty);}else if(options.length>1){that.labelList(that.get_labels().choose);}
$.each(options,function(){that.get_listTarget().append(this);});if(options.length===1){if(recurse){that.get_listTarget().change();}else{that.displayFields(data.dates[0]);}}else if(options.length>1&&that.get_settings().defaultFirst){that.get_listTarget().get(0).selectedIndex=1;that.get_listTarget().change();}});};var convertSimpleDate=function(value){var date=new Date();date.setFullYear(value.substr(0,4));date.setMonth(value.substr(4,2)-1);date.setDate(value.substr(6,2));return date;};var daysOfWeek=['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];var months=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];var formatDate=function(date){return daysOfWeek[date.getDay()]+' '+date.getDate()+' '+months[date.getMonth()];};return that;};$.widget.performances=function(options){var legendTarget=$('#cineworld_legend');var legend=$.extend({showRegular:true},options.legend);var that=widgetItem({listTarget:$('#cineworld_performances'),displayTarget:$('#cineworld_selected_performance'),paramName:'performance',settings:$.extend({order:4},options.settings),labels:options.labels});that.populateList=function(api,recurse,params){api.fetchPerformances(params,function(data){that.get_listTarget().empty();$.each(data.performances,function(){var listItem;if(this.available){var url=this.booking_url+'&key='+api.get_key();if(api.get_campaignCode()){url+='&campaignCode='+api.get_campaignCode();}
anchor=$('<a></a>').attr('class',this.type+' available').attr('href',url).text(this.time);if(that.get_settings().onSelect&&typeof that.get_settings().onSelect=='function'){anchor.click(function(){that.get_settings().onSelect($(this).text());});}
listItem=$('<li></li>').append(anchor);}else{listItem=$('<li></li>').append($('<span></span>').attr('class','unavailable').text(this.time));}
that.get_listTarget().append(listItem);});legendTarget.empty();$.each(data.legends,function(){if(this.code!=='reg'||legend.showRegular){legendTarget.append($('<li></li>').attr('class',this.code).text(this.description));}});});};return that;};$.api=function(key,errorObj,options){var errors=errorObj;var settings=$.extend({local:false,serverAddress:'http://www.cineworld.com',apiRoot:'/api/quickbook',cinemasPath:'/cinemas',filmsPath:'/films',datesPath:'/dates',performancesPath:'/performances'},options);var security={key:key};this.get_key=function(){return key;};this.get_campaignCode=function(){return settings.campaignCode;};this.fetchCinemas=function(params,callback){fetchData(settings.cinemasPath,params,callback);};this.fetchFilms=function(params,callback){fetchData(settings.filmsPath,params,callback);};this.fetchDates=function(params,callback){fetchData(settings.datesPath,params,callback);};this.fetchPerformances=function(params,callback){fetchData(settings.performancesPath,params,callback);};var fetchData=function(path,params,callback){errors.hide();if(settings.before&&typeof settings.before=='function'){settings.before();}
$.ajax({url:(settings.local?'':settings.serverAddress)+settings.apiRoot+path,type:'GET',data:$.extend(params,security),dataType:'jsonp',complete:function(){if(settings.after&&typeof settings.after=='function'){settings.after();}},success:function(data){if(data.errors){errors.show(data.errors);}else{callback(data);}}});};};$.errors=function(){var self=this;var errorsTarget=$('#cineworld_errors');this.hide=function(){errorsTarget.find('.list').empty();errorsTarget.hide();};this.show=function(errors){var list=errorsTarget.find('.list');$.each(errors,function(){list.append('<li>'+this+'</li>');});errorsTarget.fadeIn('slow');};};})(jQuery);