(function() {
var inView = msnbc.utils.isInViewport;
msnbc.modules.FloatingNav = msnbc.modules.Module.extend({
setup: function(data, container) {
this.SHORT_NAME = 'fn';
this.MAX_TEASES = 6;
this.MIN_TEASES = 2;
this.columns = [];
this.moduleList = data;
this.container = container;
this.locationCode = 0;
this.hideCode = 0;
this.slices = [];
this.lastTop = -1;
this.hitBottom = false;
this.animating = false;
},
render: function(moduleList) {
this.moduleList = moduleList || this.moduleList;
if (jQuery.browser.msie && jQuery.browser.version < 7) {
return false;
};
var self = this;
var tdata = [];
jQuery.each(this.moduleList,
function(i) {
var t = this.getNavTease(this);
if (t) {
tdata.push(t);
self.slices.push(this.__container);
}
if (tdata.length >= self.MAX_TEASES) return false;
});
if (tdata.length < this.MIN_TEASES) return;
$(this.container.slot).addClass('rendered');
var cols = tdata.length + 2;
this.columns = this.container.applyLayout(this.container.slot, cols, true);
$(this.container.slot).removeClass('x' + cols);
jQuery.each(this.columns,
function(j) {
if (j > 0 && j < cols - 1) {
self.renderItem(this, tdata[j - 1], j);
} else {
self.renderItem(this, null, j);
}
});
$(window).bind('scroll',
function(e) {
if (self.lastTop < $(this).scrollTop() && !self.getAnimating()) {
self.highlightSlices();
}
self.lastTop = $(this).scrollTop();
self.toggleFooter();
}).bind('resize',
function(e) {
self.highlightSlices();
self.toggleFooter();
});
this.highlightSlices();
this.toggleFooter();
this.navitems = $(this.container.slot).find('div.nav-item');
this.handleCookieCode(this.locationCode + this.hideCode);
},
marginMove: function(hilite) {
var m = ($(window).width() - $(this.container.slot).width() ) / 2;
$(this.container.slot).css('margin-left', m);
},
renderItem: function(slot, item, i) {
var self = this;
if (i == 0) {
$(slot).addClass('nav-title').append('Jump to');
return;
} else if (i == this.columns.length - 1) {
$(slot).addClass('controls').append('Move menuHide');
$(slot).find('.move').click(function(e) {
e.preventDefault();
if ($(self.container.slot).hasClass('top')) {
self.moveToBottom();
} else {
self.moveToTop();
}
});
$(slot).find('.hide').click(function(e) {
e.preventDefault();
if ($(self.container.slot).hasClass('hide')) {
self.showNav();
} else {
self.hideNav();
}
});
return;
};
var index = 'i' + i;
$(slot).append('
');
var tease = item['abstract'];
if (tease.match(/http/)) {
tease = msnbc.utils.makeAIMS(tease, 'thumb-s');
} else if (item.label.match(/text/i)) {
tease = "http://msnbcmedia.msn.com/i/MSNBC/Templates/cccTemplatesStory_v2/img/sp-article-tease.gif";
$(slot).addClass('article');
}
$(slot).find('div.img:last').append('
');
$(slot).bind('mouseenter',
function(e) {
if (!self.getAnimating()) {
self.unhighlightAll();
$(this).removeClass('x0p5').addClass('active x1p5');
}
self.marginMove();
}).bind('click',
function(e) {
self.setAnimating();
var slice = self.slices[i - 1];
var dy = Math.ceil($(slice.slot).offset().top);
if (self.locationCode == 1) {
dy -= $(self.container.slot).height();
}
self.watchAnimation('html,body', 'scrollTop', dy, 500,
function() {
self.resetAnimating();
});
if (msnbc.document.quilt.openOnScroll.length > 0) var rescroll = true;
$('html,body').stop().animate({
scrollTop: dy
},
800,
function() {
if (rescroll) {
var dy2 = Math.ceil($(slice.slot).offset().top);
if (self.locationCode == 1) {
dy2 -= $(self.container.slot).height();
}
if (dy2 > dy) {
$('html,body').animate({
scrollTop: dy2
},
200);
}
};
self.unhighlightAll();
});
});
$(this.container.slot).bind('mouseleave',
function(e) {
self.unhighlightAll();
self.marginMove();
});
},
watchAnimation: function(selectr, prop, targetval, updateInt, callback) {
var maxtimeout = 50000;
var self = this;
var interval;
interval = setInterval(function() {
var flag = false;
$(selectr).each(function() {
if ($(this)[prop] && $(this)[prop]() === targetval) {
flag = true;
}
});
if (flag) {
clearInterval(interval);
callback.call(this, self);
}
},
updateInt);
},
setAnimating: function() {
this.animating = true;
},
resetAnimating: function() {
this.animating = false;
},
getAnimating: function() {
return this.animating;
},
highlightByIndex: function(index) {
if (!this.getAnimating()) {
$(this.navitems).eq(index).parent().removeClass('x0p5').addClass('active x1p5');
}
},
unhighlightByIndex: function(index) {
$(this.navitems).eq(index).parent().addClass('x0p5').removeClass('active x1p5');
//this.marginMove();
},
unhighlightAll: function() {
for (var i = this.slices.length - 1; i >= 0; i--) {
this.unhighlightByIndex(i);
};
},
highlightSlices: function() {
var self = this;
this.marginMove();
if (this.hitBottom) return;
var highest;
jQuery.each(this.slices,
function(i) {
if (inView(this.slot, -200)) {
highest = i + 1;
if (highest >= self.slices.length) {};
}
});
this.unhighlightAll();
this.highlightByIndex(highest);
this.marginMove();
},
toggleFooter: function() {
var scroll_pos = $(window).height() + $(window).scrollTop();
var displaynav = true;
if (inView('#header', $(window).height() / 10) || inView('#default-footer', -$(window).height() / 10)) {
displaynav = false;
}
if (displaynav) {
$("#floating-nav").fadeIn(200);
} else {
$("#floating-nav").fadeOut(200);
this.resetAnimating();
}
},
moveToBottom: function() {
$(this.container.slot).removeClass('top');
this.locationCode = 0;
this.saveCookie();
},
moveToTop: function() {
$(this.container.slot).addClass('top');
this.locationCode = 1;
this.saveCookie();
},
hideNav: function() {
$(this.container.slot).addClass('hide').find('div.controls span.hide').text('Show');
this.hideCode = 3;
this.saveCookie();
this.marginMove();
//$(this.container.slot).css('margin-left', ((document.body.offsetWidth * 0.5) - 44));
},
showNav: function() {
$(this.container.slot).removeClass('hide').find('div.controls span.hide').text('Hide');
this.marginMove();
this.hideCode = 0;
this.saveCookie();
},
handleURLParams: function(params) {},
handleCookieCode: function(code) {
switch (code) {
case 0:
this.moveToBottom();
this.showNav();
break;
case 3:
this.moveToBottom();
this.hideNav();
break;
case 1:
this.moveToTop();
this.showNav();
break;
case 4:
this.moveToTop();
this.hideNav();
break;
};
},
saveCookie: function() {
if (msnbc.document.cookieMgr) {
var code = this.hideCode + this.locationCode;
msnbc.document.cookieMgr.addCookieCommand(msnbc.document.settings.DEFAULT_COOKIE_NAME, this.SHORT_NAME, code);
}
},
getInterface: function() {
return {
names: this.SHORT_NAME,
commands: {
generic: function(params) {
this.handleURLParams(params);
},
cookie: function(code) {
this.handleCookieCode(code);
}
}
};
}
});
})();