Skip to content

Commit

Permalink
Update ncage.js
Browse files Browse the repository at this point in the history
New images, removes broken images
  • Loading branch information
Fricke committed Sep 17, 2014
1 parent bf367f8 commit 1f66af8
Showing 1 changed file with 68 additions and 51 deletions.
119 changes: 68 additions & 51 deletions ncage.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
//nCage
(function($) {

var self = $.nCage = new function(){};

$.extend(self, {
nCageImgs : [
(function ($) {

var self = {
nCageImgs: [
'http://upload.wikimedia.org/wikipedia/commons/thumb/3/33/Nicolas_Cage_2011_CC.jpg/220px-Nicolas_Cage_2011_CC.jpg',
'http://upload.wikimedia.org/wikipedia/commons/thumb/f/f3/Nicolas_Cage_-_66%C3%A8me_Festival_de_Venise_(Mostra).jpg/220px-Nicolas_Cage_-_66%C3%A8me_Festival_de_Venise_(Mostra).jpg',
'http://content8.flixster.com/rtactor/40/33/40334_pro.jpg',
Expand All @@ -15,7 +13,7 @@
'http://cache2.artprintimages.com/LRG/10/1062/Y4UL000Z.jpg',
'http://www3.pictures.fp.zimbio.com/Nicholas+Cage+David+Letterman+-EtX2RCI91al.jpg',
'http://resources2.news.com.au/images/2009/11/04/1225794/400950-nicolas-cage.jpg',
'http://www.topnews.in/uploads/Nicolas-Cage1.jpg',
'http://2.bp.blogspot.com/-YRqSEM3HY-I/UCnI3Q9t8EI/AAAAAAAAEW8/hKt3GlOgwbY/s1600/nicolas-cage11.jpg',
'http://d2tq98mqfjyz2l.cloudfront.net/image_cache/1335739369248357_animate.gif',
'http://thetrustadvisor.com/wp-content/uploads/2013/03/nicolas-cage.jpg',
'http://starsmedia.ign.com/stars/image/article/908/908074/nicolas-cage-20080905025038648-000.jpg',
Expand All @@ -25,67 +23,86 @@
'http://images.contactmusic.com/newsimages/nicolas_cage_552048.jpg',
'http://www.apnatimepass.com/nicolas-cage-in-stolen-movie-10.jpg',
'http://24.media.tumblr.com/e68455822f14c29d43bacbc19f15ed36/tumblr_mr1kquuOvD1rimb2bo1_400.jpg',
'http://doubleaardvarkmedia.com/wp-content/uploads/2013/07/nicolas_cage_1193538-450-x-450.jpg',
'http://static2.businessinsider.com/image/4adcd99800000000009ed0dd/how-nicolas-cage-spent-his-way-to-the-poorhouse.jpg',
'http://www1.pictures.zimbio.com/pc/Nicolas+Cage+Nicolas+Cage+Emma+Stone+Croods+AbN87pQpWsjl.jpg',
'http://signaltower.co/wp-content/uploads/2013/03/crazy-nicholas-cage-placeholder-image.jpg',
"http://hsstorewebproject.weebly.com/uploads/2/0/2/7/20276235/3310319_orig.jpg",
"http://www.wow247.co.uk/wp-content/uploads/2013/08/Nicolas-Cage-ghost-rider.jpg",
"http://cinemastationblog.files.wordpress.com/2011/12/cage_vampireskiss.jpg",
"http://www.cosmo.com.ua/i/photos_publication/1627/400_535/H88d0tP2.jpg",
"http://www.thefancarpet.com/uploaded_assets/images/actor/318/Nicolas_Cage_16081_Medium.jpg",
"http://cdn-media.extratv.com/archive/images/news/0418/nic-cage.jpg",
"http://www.tiranganj.com/wp-content/uploads/2014/07/nicolas-cage-promo.jpg",
"http://31.media.tumblr.com/tumblr_ljygvqVZhC1qzl30go1_400.png",
"http://www.gotchamovies.com/ul/photos/movie/o/12f6f18c38dae91747f9c3c9c12bd0a4-sc.jpg",
"http://blu.stb.s-msn.com/i/B8/7DEBEB33E1379AB1F0B67DE637AE3.jpg",
"http://i1.ytimg.com/vi/ghhpe7n7Gi0/maxresdefault.jpg",
"http://31.media.tumblr.com/tumblr_m2wkus8ilr1r4etbjo1_r1_500.gif",
"http://cdn-media.extratv.com/archive/images/news/0418/nic-cage.jpg",
"http://www.tiranganj.com/wp-content/uploads/2014/07/nicolas-cage-promo.jpg",
"http://31.media.tumblr.com/tumblr_ljygvqVZhC1qzl30go1_400.png",
"http://www.gotchamovies.com/ul/photos/movie/o/12f6f18c38dae91747f9c3c9c12bd0a4-sc.jpg",
"http://blu.stb.s-msn.com/i/B8/7DEBEB33E1379AB1F0B67DE637AE3.jpg",
"http://i1.ytimg.com/vi/ghhpe7n7Gi0/maxresdefault.jpg",
"http://31.media.tumblr.com/tumblr_m2wkus8ilr1r4etbjo1_r1_500.gif",
"http://docfilms.uchicago.edu/dev/images/2014/winter/2014-01-30-02.jpg",
"http://38.media.tumblr.com/tumblr_ls47w7ZoeN1qbgguvo1_1280.png",
"http://ughalex.files.wordpress.com/2013/05/tumblr_lxamtuvmvq1r4etbjo1_5001.gif?w=820",
"http://25.media.tumblr.com/tumblr_llhctdm0jU1qje621o1_500.gif",
"http://24.media.tumblr.com/tumblr_lx8id0omG11r4etbjo1_500.gif",
],
handleImages : function (lstImgs, time)
{
$.each($('img'), function(i,item) {
handleImages: function (lstImgs, time) {
$.each($('img'), function (i, item) {
//Skip if image is already replaced
if($.inArray($(item).attr('src'), lstImgs) == -1)
{
var h = $(item).height();
var w = $(item).width();

//If image loaded
if(h > 0 && w > 0)
{
//Replace
$(item).css('width', w + 'px').css('height', h + 'px');
$(item).attr('src', lstImgs[Math.floor(Math.random() * lstImgs.length)]);
}
else
{
//Replace when loaded
$(item).load(function(){
//Prevent 'infinite' loop
if($.inArray($(item).attr('src'), lstImgs) == -1)
{
var h = $(item).height();
var w = $(item).width();
$(item).css('width', w + 'px').css('height', h + 'px');
$(item).attr('src', lstImgs[Math.floor(Math.random() * lstImgs.length)]);
}
});
}
}
if ($.inArray($(item).attr('src'), lstImgs) == -1) {
var h = $(item).height();
var w = $(item).width();

//If image loaded
if (h > 0 && w > 0) {
self.handleImg(item, lstImgs);
}
else {
//Replace when loaded
$(item).load(function () {
//Prevent 'infinite' loop
if ($.inArray($(item).attr('src'), lstImgs) == -1) {
self.handleImg(item, lstImgs);
}
});
}
}
});

//Keep replacing
if (time > 0) {
setTimeout(function () { self.handleImages(lstImgs, time); }, time);
}
}
});
},
handleImg: function (item, lstImgs) {
$(item).error(function () {
//Handle broken imgs
self.handleBrokenImg(item, lstImgs);
});

self.setRandomImg(item, lstImgs);
},
setRandomImg: function (item, lstImgs) {
var h = $(item).height();
var w = $(item).width();
$(item).css('width', w + 'px').css('height', h + 'px');
$(item).attr('src', lstImgs[Math.floor(Math.random() * lstImgs.length)]);
},
handleBrokenImg: function (item, lstImgs) {

var brokenImg = $(item).attr('src');
var index = lstImgs.indexOf(brokenImg);
if (index > -1) {
lstImgs.splice(index, 1);
}
self.setRandomImg(item, lstImgs);
},
};

//Run on jQuery ready
$(function(){
$(function () {
self.handleImages(self.nCageImgs, 3000);
});

//Set global variable
$.nCage = self;

})(jQuery);


0 comments on commit 1f66af8

Please sign in to comment.