function Film() { var self = this; this.consts = {'ugc_url':'http://www.ugc.fr/', 'process_url':'index.php'}; this._attr = {}; this._anchor = {}; this._ban = {status:false, reason:''}; this._timetable = {}; this.$Main = $('main'); this.ban = function(bStatus, sReason) { sReason = sReason || ''; if(!bStatus) return this._ban; else if(sReason!='') { this._ban.status = true; this._ban.reason = sReason; this.feedback('Hidding movie "'+this.name()+'". Ban reason: '+this.ban().reason); this.anchor().addClass('banned'); } else this.error('Setting ban status with no reason'); } this.id = function(iFilmId) { return this.attr('id', iFilmId); } this.name = function(sName) { sName = sName?(sName + '').toLowerCase().replace(/^([a-z\u00E0-\u00FC])|\s+([a-z\u00E0-\u00FC])/g, function($1) {return $1.toUpperCase();}):''; return this.attr('name', sName); }; this.poster = function(sPoster) { if(sPoster) sPoster = this.getPosterUrl(sPoster, 'small'); return this.attr('poster', sPoster); }; this.backgroundPoster = function(sPoster) { if(sPoster) sPoster = this.getPosterUrl(sPoster, 'medium'); return this.attr('poster_bg', sPoster); }; this.getPosterUrl = function(sUrl, sSize) { var sExt = sUrl.substr(sUrl.lastIndexOf('.')+1).toLowerCase(); if(sExt=='jpg') sExt = 'jpeg'; return this.consts.process_url+'?a=get_poster&type='+sExt+'&data[id]='+this.id()+'&data[url]='+encodeURIComponent(sUrl)+'&data[size]='+sSize; }; this.edito = function(sEdito) { return this.attr('edito', sEdito); }; this.trailer = function(sTrailer) { sTrailer = sTrailer?self.consts.ugc_url+sTrailer:''; return this.attr('trailer', sTrailer); }; this.attr = function(sName, sValue) { if(!sValue) return this._attr[sName]; else { sValue = $.trim(sValue); if(sValue!='') this._attr[sName] = sValue; else this.error('Trying to set an empty film attribute: '+sName); } }; /* this.removeSpinner = function() { this.anchor().find('.timetable').removeClass('fa fa-fw fa-spinner fa-spin'); } */ this.getRank = function() { var sEdito = this.edito() || ''; var iRank = 0; if(sEdito=='Nouveau') iRank += 10000000; else if(sEdito.slice(-7)=='semaine') iRank += 100000*parseInt(sEdito.substr(0, sEdito.indexOf('è'))); iRank += parseInt(this.id(), 10); return iRank; }; this.anchor = function() { if(Object.keys(this._anchor).length > 0) return this._anchor; else { //Add film to DOM this._anchor = $('
', {'class':'title'})
.append($('', {'class':'name', 'target':'_blank', 'href':this.consts.ugc_url+'film.html?id='+this.id()})
.append($('', {'class':'fa fa-fw fa-icon fa-film'}))
.append(this.name()))
.append($('', {'class':'edito'+(this.edito()?' fa fa-edito':'')}).text(this.edito()?' '+this.edito():'')))
.append($(' ', {'class':'trailer'})
.append($('', {'target':'_blank', 'href':this.trailer()})
.append($('', {'class':'fa fa-fw fa-icon fa-trailer'}))
.append('Trailer')))
.append($(' ')
.append($('', {'class':'cinema', 'target':'_blank', 'href':self.consts.ugc_url+'cinemaAction!execute.action?page=7&id='+iCinemaId})
.append($('', {'class':'fa fa-fw fa-icon fa-cinema'}))
.append($('', {'class':'cinema_name'}).text(sCinema)))
.append($('', {'class':'lang'}).text(sLang))
.appendTo(self.anchor().find('.timetable'));
var test = self.timetable(iCinemaId, sLang);
if(!test) console.log('cinema '+iCinemaId+' lang '+sLang+' film '+self.id());
$.each(self.timetable(iCinemaId, sLang).sort(self.timeCompare), function(iKey, oTime){
$Timetable
.append($('', {'class':'button', 'href':oTime.booking, 'target':'_blank', 'title':'Book screening'}).text(oTime.time))
.append(' '); //for line break
});
}
});
};
this.timeCompare = function(sTime1, sTime2)
{
return (Date.parse('01/01/1970 '+sTime1.time+':00') > Date.parse('01/01/1970 '+sTime2.time+':00'));
};
this.feedback = function(sMsg)
{
$('#feed_content').append($(' ').text(sMsg));
};
this.error = function(sMsg)
{
console.log('Film Error: '+sMsg);
};
}
function Affiche(oDate)
{
var self = this;
this.date = {};
this.date.now = oDate;
this.date.today = new Date(this.date.now.getFullYear(), this.date.now.getMonth(), this.date.now.getDate());
this.date.timestamp = this.date.today / 1;
this._cinemasList = {};
this._films = {};
this._wip = 0;
this.consts = { days:["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
months:["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]};
this.$Header = $('header');
this.$Main = $('main');
this.$Footer = $('footer');
this.$Feedback = $('#feedback');
this.$Settings = $('#settings');
this.wip = function(iProgress)
{
if(!iProgress) return this._wip;
else
{
this._wip += iProgress;
if(this._wip==0) this.onPostLoad();
}
};
this.onPostLoad = function()
{
$Films = $('.film');
//Display Header
this.$Header.css('visibility', 'visible');
//Sort films
$Films.sort(function(oFilm1, oFilm2){return ($(oFilm1).data('rank') < $(oFilm2).data('rank'))?1:-1;}).detach();
//Remove movies with no screening
$Films.not('.banned').each(function(iKey, hFilm){
var $Film = $(hFilm);
if($Film.find('.timetable').is(':empty'))
{
var oFilm = self.film($Film.data('id'));
self.feedback('Hiding movie "'+oFilm.name()+'". No screening today');
$Film.addClass('banned');
}
});
//Remove loading
this.$Main.find('#buffer').hide();
//Add films (except bans)
$Films.appendTo(this.$Main).not('.banned').slideDown('slow');
this.$Footer.add(this.$Feedback).show();
};
this.loadCinemas = function()
{
this.setHeader();
$.each(asCinemas, function(iCinemaId, sCinemaName){
getUgcPage( 'filmsAjaxAction!getFilmsForPageCinema.action',
{cinemaId:iCinemaId, cinemaCode:'', page:'7', filmId:'', filmId_widget:'', '_':Date.now()},
function(hDom){self.addCinema(iCinemaId, hDom)},
function(){},
'html');
});
};
this.addCinema = function(iCinemaId, hDom)
{
var $Page = $(hDom);
var $Films = $Page.find('.FilmDiv');
this.wip($Films.length);
$Films.each(function()
{
var $Film = $(this);
var iFilmId = $Film.find('input[name="filmId"]').val();
var oFilm = self.film(iFilmId);
if(!oFilm)
{
oFilm = new Film();
//ID
oFilm.id(iFilmId);
//Edito
var sEdito = $.trim($Film.find('.FilmEditorial').text());
oFilm.edito(sEdito);
//Name
oFilm.name($Film.find('.FilmTitle').text());
//Poster
var sPosterUrl = $Film.find('.FilmThumbnail img').attr('src');
oFilm.poster(sPosterUrl);
oFilm.backgroundPoster(sPosterUrl.replace('152x217/FFFFFF', '274x410/EEEEE8'));
//Trailer
oFilm.trailer($Film.find('.FilmIntroLink a').attr('href'));
//Minimum info acquired: book place on DOM
oFilm.anchor();
//Ban if wrong edito
if(sEdito=='Avant-première' || sEdito=='UGC Culte' || sEdito=='Soirée Club') oFilm.ban(true, sEdito);
//Append film to Affiche
self.film(iFilmId, oFilm);
}
//Timetable
if(!oFilm.ban().status)
{
getUgcPage( 'filmsAfficheAction!loadDatesList.action',
{filmId:oFilm.id(), cinemaId:iCinemaId, cinemaCode:'', day:''},
function(jData){
if(!(self.date.timestamp in jData.dates)) self.wip(-1);
else
{
getUgcPage( 'filmsAfficheAction!reloadSeancesList.action',
{filmId:oFilm.id(), cinemaId:iCinemaId, cinemaCode:'', day:self.date.timestamp},
function(hData){
var sLang = '';
var asLangs = [];
var $Timetable = $(hData);
var oTimes = [];
$Timetable.children().each(function(iKey, oDiv){
$Div = $(oDiv);
if($Div.attr('class')=='Line') //New Lang
{
if(sLang!='') asLangs.push(sLang);
sLang = $.trim($Div.find('.FilmTitle').text());
}
else
{
$Div.find('a').each(function(iKey, oLink){
var $Time = $(oLink);
oTime = {'time':$.trim($Time.find('span').text()), 'booking':oFilm.consts.ugc_url+$Time.attr('href')};
oFilm.timetable(iCinemaId, sLang, oTime);
});
}
});
asLangs.push(sLang);
oFilm.renderTimetable(iCinemaId, asLangs);
self.wip(-1);
},
function(){
self.wip(-1);
self.error('Ajax Error detected on movie "'+oFilm.name()+'" @'+asCinemas[iCinemaId]+'. UGC website Temporarily Unavailable');
},
'html');
}
},
function(){
self.wip(-1);
self.error('Ajax Error detected on movie "'+oFilm.name()+'" @'+asCinemas[iCinemaId]+'. UGC website Temporarily Unavailable');
},
'json');
}
else self.wip(-1);
});
};
this.setHeader = function()
{
//Date on top
var iDay = this.date.today.getDate();
$('#date')
.attr('datetime', this.date.today.getFullYear()+'-'+(("0" + (this.date.today.getMonth() + 1)).slice(-2))+'-'+(("0" + iDay).slice(-2)))
.append(this.consts.days[this.date.today.getDay()]+', '+this.consts.months[this.date.today.getMonth()]+' '+iDay)
.append($('').text((iDay%10==1)?'st':((iDay%10==2)?'nd':((iDay%10==3)?'rd':'th'))));
//Load Settings
this.setSettings();
}
this.setSettings = function()
{
//Date
this.setDates();
//Cinemas
this.cinemas();
//Event
this.$Header.find('#btn_settings').click(function(){
self.$Settings.show();
});
this.$Main.click(function(){self.$Settings.hide();});
$(window).keyup(function(e){if(e.keyCode == 27) self.$Settings.hide();});
//Validattion
this.$Settings.find('#ok_settings').click(function(){
//Cinemas
var asCines = {};
self.$Settings.find('.include').each(function(iKey, oCinema){
$Cine = $(oCinema);
asCines[$Cine.data('id')] = $Cine.text();
});
self.cinemas(asCines);
//Day
location.reload();
});
};
this.setDates = function()
{
};
this.cinemas = function(asSetCinemas)
{
if(!asSetCinemas) //Set cookie/default cinemas and get list
{
var asCookieCinemas = Cookies.getJSON('cinemas');
if(!asCookieCinemas) asCinemas = {'10':'Les Halles', '12':'Bercy', '20':'La Défense'};
else asCinemas = asCookieCinemas;
getUgcPage( 'headerReservationAction!reloadCinemasList.action',
{'regionsAndCinemasBean.region':'', 'regionsAndCinemasBean.cinema':'', '_':Date.now()},
self.displaySettingsCinemas,
function(){self.error('Error: Could not load cinema list');},
'json');
}
else //Set new cinemas as cookie
{
Cookies.set('cinemas', asSetCinemas);
asCinemas = asSetCinemas;
}
};
this.displaySettingsCinemas = function(jData)
{
this._cinemasList = jData.cinemas;
var $Cinemas = self.$Settings.find('.cinemas');
$.each(this._cinemasList, function(iKey, sCinemaName){
var iCinemaId = $.trim(iKey);
$Cinemas.append($(' ').text(sMsg));
};
this.error = function(sMsg)
{
console.log('Affiche Error: '+sMsg);
};
}
function getUgcPage(sPath, asData, fOnSuccess, fOnFail, sType)
{
var asParams = {'a':'get_page', 'type':sType, 'data':{'path':sPath, 'vars':asData}};
$.ajax({
url: (new Film()).consts.process_url,
data: asParams,
dataType: sType
}).done(fOnSuccess)
.fail(fOnFail);
}