Index: trunk/extensions/CentralNotice/newCentralNotice.js |
— | — | @@ -53,19 +53,25 @@ |
54 | 54 | if ( bannerList.length == 0 ) return false; |
55 | 55 | |
56 | 56 | var totalWeight = 0; |
| 57 | + // run through the bannerlist and sum the weights of all banners |
57 | 58 | for( var i = 0; i < bannerList.length; i++ ) { |
58 | 59 | totalWeight += bannerList[i].weight; |
59 | 60 | } |
| 61 | + |
| 62 | + // select a random integer between 0 and our total weight |
60 | 63 | var pointer = Math.floor( Math.random() * totalWeight ), |
61 | 64 | selectedBanner = bannerList[0], |
62 | 65 | w = 0; |
| 66 | + // run through the banner list and start accumulating weights |
63 | 67 | for( var i = 0; i < bannerList.length; i++ ) { |
64 | 68 | w += bannerList[i].weight; |
| 69 | + // when the weight tally exceeds the random integer, return the banner and stop the loop |
65 | 70 | if( w < pointer ) { |
66 | 71 | selectedBanner = bannerList[i]; |
67 | 72 | break; |
68 | 73 | } |
69 | 74 | } |
| 75 | + // return our selected banner |
70 | 76 | $.centralNotice.fn.loadBanner( |
71 | 77 | selectedBanner.name |
72 | 78 | ); |