msnbc.modules.Slideshow = msnbc.modules.Module.extend({
// the setup function is called immediately after the page renders
// assuming there's a Slideshow module on the pages
setup: function(data, container){
this.SHORT_NAME = 'ss';
this.PAGEVIEW_COUNT = 1;
this.data = data;
this.markup = container.markup;
this.title = container.title;
this.slot = container.slot;
this.rendered = false;
this.clicked = false;
this.ssData = {};
this.ssid = null;
this.currIndex = 0;
this.currData = null;
this.totalImages = null;
this.behaviorCount = 0;
this.rendered = false;
this.autoplay = false;
this.playPaused = true;
this.playTimer = null;
this.playDelay = 5000;
this.disablePrev = false;
this.disableNext = false;
// setup share links
this.shareMenu = this.addComponent("msnbc.components.ShareMenu");
this.shareMenu.setup($(this.title).text(), new msnbc.misc.URLCommand(this.SHORT_NAME));
this.shareMenu.insert($(this.title).parent());
},
render: function(){
if (this.rendered) return;
if (this.data.content[0] && this.data.content[0].filename != "") {
this.ssid = this.ssid || this.data.content[0].filename; // use the id sent in the URL command if set by handleURLParams
} else {
// no slideshow tease in ContentModule content or invalid data
// TODO: do something generic
msnbc.warn('[modules.Slideshow::render] No slideshow id found');
}
this.getData();
},
setupNavigation: function(){
var self = this;
if($(".slideshow .controls").text()!="") { return false; }
$(".slideshow").find('.controls').append('
Current slideshow
'); //this.ssData.title
$(".slideshow").find('.controls').append('PrevNextPlayLarger')
.find('span')
.click(function(e){
self.clicked = true;
e.preventDefault();
if ( $(this).hasClass('next') && self.disableNext==false ) {
self.playPaused=false;
self.currIndex++;
self.playPause(self.currIndex);
self.renderByIndex(self.currIndex);
} else if ( $(this).hasClass('prev') && self.disablePrev==false ) {
self.playPaused=false;
self.currIndex--;
self.playPause(self.currIndex);
self.renderByIndex(self.currIndex);
} else if ( $(this).hasClass('fullscreen') ) {
self.playPaused=false;
self.playPause(self.currIndex);
self.popFullScreen(self.currIndex);
} else if ( $(this).hasClass('play') || $(this).hasClass('pause') ) {
self.playPause(self.currIndex);
}
}
);
$(".slideshow-photo").mousemove(function(e){
if(($(".slideshow-photo .hitstate.prev.totaldisabled").length<=0) && ($(".slideshow-photo .hitstate.next.totaldisabled").length<=0)){
$(".slideshow-photo .hitstate.prev").addClass("pover");
$(".slideshow-photo .hitstate.next").addClass("nover");
$(".slideshow-photo .hitstate.prev, .slideshow-photo .hitstate.next").css("display","block");
if(e.pageX>=Math.floor($(this).offset().left+($(this).width()/2))){
$(".slideshow-photo .hitstate.prev").removeClass("pactive");
$(".slideshow-photo .hitstate.next").addClass("nactive");
} else {
$(".slideshow-photo .hitstate.prev").addClass("pactive");
$(".slideshow-photo .hitstate.next").removeClass("nactive");
}
}
});
$(".slideshow-photo").click(function(e){
self.clicked = true;
e.preventDefault();
self.playPaused=false;
if(($(".slideshow-photo .hitstate.prev.totaldisabled").length<=0) && ($(".slideshow-photo .hitstate.next.totaldisabled").length<=0)){
if(e.pageX>=Math.floor($(this).offset().left+($(this).width()/2)) && self.disableNext==false){
self.currIndex++;
self.playPause(self.currIndex);
self.renderByIndex(self.currIndex);
$(".slideshow-photo .hitstate").css("opacity",0);
} else if (self.disablePrev==false) {
self.currIndex--;
self.playPause(self.currIndex);
self.renderByIndex(self.currIndex);
$(".slideshow-photo .hitstate").css("opacity",0);
}
}
});
$(".slideshow-photo").mouseout(function(){
$(".slideshow-photo .hitstate.prev").removeClass("pactive");
$(".slideshow-photo .hitstate.next").removeClass("nactive");
$(".slideshow-photo .hitstate.prev").removeClass("pover");
$(".slideshow-photo .hitstate.next").removeClass("nover");
$(".slideshow-photo .hitstate.prev, .slideshow-photo .hitstate.next").css("display","none");
});
var list = "";
list += '';
jQuery.each(this.ssData.slideshow, function(){
list += '';
});
list += '
';
list += '';
list += ' of '+this.totalImages+' photos
';
$(".slideshow .related").html(list);
var tempWidth = 0;
var numItems = this.ssData.slideshow.length;
var barWidth = $(".slideshow .related ul").width()-numItems-2;
var segWidth = Math.floor(barWidth/numItems);
var stopPoint = barWidth-(segWidth*numItems);
$(".slideshow .related ul li.slide").each(function(i){
if(i<=stopPoint){ tempWidth = segWidth+1; } else { tempWidth = segWidth; }
$(this).width(tempWidth);
});
$(".slideshow .related li:last-child").addClass("last");
$(".slideshow .related ul li.slide")
.hover(function(e){
$(".slideshow .related .art img").attr("src",msnbc.utils.makeAIMS("http://msnbcmedia.msn.com/j/"+self.ssData.slideshow[$("li.slide").index(this)].slide.src, 'thumb'));
$(".slideshow .related .art").css("left",($(this).position(this).left-$(".slideshow .related ul").position().left-2-($(".slideshow .related .art").width()/2)+($(this).width()/2)));
$(".slideshow .related .art").css("display","block");
},
function(e){
$(".slideshow .related .art").css("display","none");
}
);
$(".slideshow .related ul li.slide")
.click(function(e){
e.preventDefault();
self.playPaused=false;
self.playPause($("li.slide").index(this));
self.renderByIndex($("li.slide").index(this));
}
);
},
renderByIndex: function(index){
var self = this;
if(index<0 || index==this.totalImages+1) return;
this.currIndex = index;
$(".slideshow-photo .hitstate, .slideshow-photo .image img").css("opacity",0);
if(index==this.totalImages){
this.renderEndSlate();
this.moveTeases();
return;
}
var currData = this.ssData.slideshow[index].slide;
var thisHeadline = null;
$(".slideshow .related .count").html(index+1);
var myimg = new Image();
myimg.onload = function(){
$(".slideshow-photo .endslate").css("display","none");
$(".slideshow-photo .image img").attr("src",myimg.src);
$(".slideshow-photo .image").width(myimg.width);
$(".slideshow-photo .image").height(myimg.height);
if(currData.headline){
var thisHeadline = currData.headline;
$(".slideshow-photo .image img").attr("title",thisHeadline);
$(".slideshow-photo .image img").attr("alt",thisHeadline);
thisHeadline = ""+thisHeadline+"
";
} else {
var maxCaptLength = 80;
if(currData.caption.length>=maxCaptLength){
$(".slideshow-photo .image img").attr("title",msnbc.utils.truncate(currData.caption, maxCaptLength, false));
$(".slideshow-photo .image img").attr("alt",msnbc.utils.truncate(currData.caption, maxCaptLength, false));
} else {
$(".slideshow-photo .image img").attr("title",currData.caption);
$(".slideshow-photo .image img").attr("alt",currData.caption);
}
}
if(thisHeadline){
$(".slideshow-photo .caption").html(thisHeadline+currData.caption);
} else {
$(".slideshow-photo .caption").html(currData.caption);
}
$(".slideshow-photo cite").html(currData.credits);
$(".slideshow-photo .caption, .slideshow-photo cite").width(myimg.width);
if(myimg.width<=638){
//if photo is less than container minus border, center it
marginWidth = Math.floor(($(".slideshow-photo").width()-myimg.width)/2);
$(".slideshow-photo .image, .slideshow-photo .caption").css("margin-left",marginWidth);
$(".slideshow-photo cite").css("margin-right",marginWidth-8);
} else {
//if photo is max container size, align it left
$(".slideshow-photo .image, .slideshow-photo .caption").css("margin-left","0px");
$(".slideshow-photo cite").css("margin-right","0px");
}
self.moveButtons(myimg.width,myimg.height);
$(".slideshow-photo .image img, .slideshow-photo .hitstate").animate({opacity:1},{queue:false,duration:500});
clearTimeout(self.playTimer);
if(index<=self.totalImages && self.playPaused==false){
self.playTimer=setTimeout(function(){self.renderByIndex(index+1)}, self.playDelay)
}
};
//src must be set after onload handler
myimg.src = msnbc.utils.makeAIMS("http://msnbcmedia.msn.com/j"+currData.src, 'grid-8x3');
this.highlightProgress(index);
this.disableButtons(index);
this.behavior();
if (!self.playPaused) {
try {
$A.Track(this);
}
catch(e) {
msnbc.log(e);
msnbc.log("Omniture error on playPaused");
}
}
},
playPause: function(index){
var self = this;
clearTimeout(self.playTimer);
if (self.playPaused) {
if(index==self.totalImages){ index=-1; } //start over if you click play on the endslate
self.playTimer=setTimeout(function(){self.renderByIndex(index+1)}, self.playDelay);
$(".slideshow .controls .button .play").addClass("pause");
$(".slideshow .controls .button .pause").removeClass("play");
$(".slideshow .controls .button .pause").html("Pause");
} else {
$(".slideshow .controls .button .pause").addClass("play");
$(".slideshow .controls .button .play").removeClass("pause");
$(".slideshow .controls .button .play").html("Play");
}
self.playPaused=!self.playPaused;
},
popFullScreen: function(index){
var popindex = index+1;
if(popindex>this.totalImages){
popindex-=1;
}
SSOpen2(this.ssid,popindex);
return false;
},
highlightProgress: function(index){
var listItems = $(".slideshow .related ul").children();
$(listItems).removeClass("past");
$(listItems).removeClass("active");
for(i=0;i=this.totalImages) {
$(".slideshow .bprev, .slideshow .controls .prev").removeClass("pdisabled");
$(".slideshow .bnext, .slideshow .controls .next").addClass("ndisabled");
$(".slideshow .hitstate.prev, .slideshow .hitstate.next").addClass("totaldisabled");
$(".slideshow-photo:hover").css("cursor","default");
this.disableNext = true;
} else {
this.disablePrev = false;
this.disableNext = false;
$(".slideshow .bprev, .slideshow .prev, .slideshow .controls .prev").removeClass("pdisabled");
$(".slideshow .bnext, .slideshow .next, .slideshow .controls .next").removeClass("ndisabled");
$(".slideshow .hitstate.prev, .slideshow .hitstate.next").removeClass("totaldisabled");
$(".slideshow-photo:hover").css("cursor","hand");
$(".slideshow-photo:hover").css("cursor","pointer");
}
},
moveButtons: function(width,height){
var marginX = parseInt($(".slideshow-photo .image").css("margin-left"),10);
var moveY = Math.floor((height/2)-25);
$(".slideshow-photo .prev, .slideshow-photo .next").css("top",moveY);
$(".slideshow-photo .prev").css("left",marginX+4); // margin + padding
$(".slideshow-photo .next").css("left",marginX+width+4-55); // margin + img width + padding - arrow
},
moveImgCount: function(){
var barX = $(".slideshow .related").position().left;
var selX = $(".slideshow .related .active").position().left;
var segWidth = $(".slideshow .related .active").width();
var capWidth = $(".slideshow .related .imgcount").width();
var barWidth = $(".slideshow .related").width();
var diffX = Math.abs(segWidth-capWidth)/2;
$(".slideshow .related .imgcount").css("left",selX-barX-diffX);
$(".slideshow .related .imgcount").css("float","left");
var capX = $(".slideshow .related .imgcount").position().left;
if(capX<=barX+2){
$(".slideshow .related .imgcount").css("left",2);
} else if ((capX+capWidth)>=(barX+barWidth)) {
$(".slideshow .related .imgcount").css("left",0);
$(".slideshow .related .imgcount").css("float","right");
}
},
renderEndSlate: function(){
$(".slideshow-photo .image img").css("opacity",1);
clearTimeout(this.playTimer);
this.disableButtons(this.currIndex);
this.playPaused = false;
this.playPause(this.currIndex);
this.loadEndSlateData();
var marginSize = parseInt($(".slideshow-photo .image").css("margin-left"),10);
var paddingSize = parseInt($(".slideshow-photo .image").css("padding-top"),10);
var borderSize = parseInt($(".slideshow-photo .image").css("border-top-width"),10);
marginSize = marginSize+paddingSize+borderSize;
$(".slideshow-photo .endslate").width($(".slideshow-photo .image").width());
$(".slideshow-photo .endslate").height($(".slideshow-photo .image").height());
$(".slideshow-photo .endslate").css("margin-left",marginSize);
$(".slideshow-photo .endslate").css("margin-top",paddingSize+borderSize);
$(".slideshow-photo .endslate").css("display","block");
},
moveTeases: function(){
if($(".slideshow-photo .endslate").width()<300){
$(".slideshow-photo .endslate .container").width($(".slideshow-photo .endslate").width()-20);
}
$(".slideshow-photo .endslate .container ul li").height($(".slideshow-photo .endslate .container ul li img").height());
$(".slideshow-photo .endslate .container").css("margin-left",-$(".slideshow-photo .endslate .container").width()/2);
$(".slideshow-photo .endslate .container").css("margin-top",($(".slideshow-photo .endslate").height()-$(".slideshow-photo .endslate .container").height())/2);
},
loadEndSlateData: function(){
var self = this;
var endslate = '';
$(".slideshow-photo .endslate").html(endslate);
if (this.data.content.length>1) {
// don't include first tease
for (var i=1; i < this.data.content.length; i++) {
var tease = this.data.content[i];
if (!tease.filename) continue; // skip if bad data ...
$(".slideshow-photo .endslate ul").append('');
};
$(".slideshow-photo .endslate li").click(function(e){
e.preventDefault();
var index = $(this).attr('class').match(/\d+/);
index = parseInt(index);
SSOpen2(self.data.content[index].filename);
});
} else {
// need to get data from TWIP and photoblog
var tr = new msnbc.misc.TeaseReader();
//twip
tr.get(3917677, function(t){
$(".slideshow-photo .endslate ul").append('');
$(".slideshow-photo .endslate li.t1").click(function(e){
e.preventDefault();
var id = t.link.match(/SSOpen\d?[(]['"]?(\d+)/i)[1];
if (t.link.match(/SSOpen2/i)) {
SSOpen2(id);
} else {
SSOpen(parseFloat(id), '0');
}
});
});
// photoblog
tr.get(14739573, function(t){
$(".slideshow-photo .endslate ul").append('');
// something is preventing href from working above. hack below
$(".slideshow-photo .endslate li.t2").click(function(e){
window.location = "http://photoblog.msnbc.msn.com";
});
});
}
},
behavior: function(params){
if ( ++this.behaviorCount >= this.PAGEVIEW_COUNT ) {
this.behaviorCount = 0;
this.pageview();
}
},
pageview: function(){
var DapAd = new msnbc.ads.DapAd("msnbc.ads.DapAd");
var self = this;
try {
jQuery.each(this.ads, function() {
if (self.currIndex > 0) {
DapAd.refresh(self.slot, this)
}
});
} catch(e) {
msnbc.log(e);
msnbc.log("Slideshow pageview ad refresh error");
}
var urlCommand = new msnbc.misc.URLCommand(this.SHORT_NAME, null, [{slide:this.currIndex+1}]);
self.shareMenu.updateShareCommand(urlCommand);
if(self.clicked){
self.dispatchEvent("contextChange", urlCommand);
}
},
onSuccess:function(data, textStatus) {
try{
this.ssData = eval('('+data+')');
} catch(e) {
// bad data
this.onError(null, 'bad data after successful call', e);
return;
}
this.totalImages = this.ssData.slideshow.length;
this.setupNavigation();
this.renderByIndex(this.currIndex);
this.dispatchEvent("rendered", this, {});
this.rendered = true;
},
onError:function(req, status, e) {
msnbc.warn("[msnbc.modules.RelatedImagesTest::onError] "+ e + "/" + status);
$(this.markup).append(' Error: '+ status);
},
getData: function(){
var self = this;
var url = "http://"+window.location.host+"/id/"+this.ssid + "/displaymode/1267";
try {
jQuery.get(url, function(d, s){
self.onSuccess(d,s);
});
} catch(e) {
this.onError(null, 'before ajax', e);
}
},
handleURLParams: function(params){
if (params.id) {
this.ssid = params.id;
}
if (params.slide) {
this.currIndex = parseInt(params.slide)-1;
};
},
getInterface: function(){
return {
names: this.SHORT_NAME,
commands: {
generic: function(params) { this.handleURLParams(params); }
}
};
},
getNavTease: function(nav){
return {
"label": 'Photos',
"icon": 'photo',
"abstract": this.data.content[0].image || '',
"title": $(this.title).find('span.subhead').text(),
"href": "#photos",
"source": this.data.content[0].source || ''
};
}
});