PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ruhlamat_website/Manage/js/lightbox.js

http://demoasp.googlecode.com/
JavaScript | 689 lines | 353 code | 114 blank | 222 comment | 87 complexity | 3598a933ba52c5260315e76e44ca0117 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. // -----------------------------------------------------------------------------------
  2. //
  3. // Lightbox v2.02
  4. // by Lokesh Dhakar - http://www.huddletogether.com
  5. // 3/31/06
  6. //
  7. // For more information on this script, visit:
  8. // http://huddletogether.com/projects/lightbox2/
  9. //
  10. // Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
  11. //
  12. // Credit also due to those who have helped, inspired, and made their code available to the public.
  13. // Including: Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.org), Thomas Fuchs(mir.aculo.us), and others.
  14. //
  15. //
  16. // -----------------------------------------------------------------------------------
  17. /*
  18. Table of Contents
  19. -----------------
  20. Configuration
  21. Global Variables
  22. Extending Built-in Objects
  23. - Object.extend(Element)
  24. - Array.prototype.removeDuplicates()
  25. - Array.prototype.empty()
  26. Lightbox Class Declaration
  27. - initialize()
  28. - start()
  29. - changeImage()
  30. - resizeImageContainer()
  31. - showImage()
  32. - updateDetails()
  33. - updateNav()
  34. - enableKeyboardNav()
  35. - disableKeyboardNav()
  36. - keyboardAction()
  37. - preloadNeighborImages()
  38. - end()
  39. Miscellaneous Functions
  40. - getPageScroll()
  41. - getPageSize()
  42. - getKey()
  43. - listenKey()
  44. - showSelectBoxes()
  45. - hideSelectBoxes()
  46. - pause()
  47. - initLightbox()
  48. Function Calls
  49. - addLoadEvent(initLightbox)
  50. */
  51. // -----------------------------------------------------------------------------------
  52. //
  53. // Configuration
  54. //
  55. var fileLoadingImage = "/manage/images/loading.gif";
  56. var fileBottomNavCloseImage = "/manage/images/closelabel.gif";
  57. var resizeSpeed = 7; // controls the speed of the image resizing (1=slowest and 10=fastest)
  58. var borderSize = 10; //if you adjust the padding in the CSS, you will need to update this variable
  59. // -----------------------------------------------------------------------------------
  60. //
  61. // Global Variables
  62. //
  63. var imageArray = new Array;
  64. var activeImage;
  65. if(resizeSpeed > 10){ resizeSpeed = 10;}
  66. if(resizeSpeed < 1){ resizeSpeed = 1;}
  67. resizeDuration = (11 - resizeSpeed) * 0.15;
  68. // -----------------------------------------------------------------------------------
  69. //
  70. // Additional methods for Element added by SU, Couloir
  71. // - further additions by Lokesh Dhakar (huddletogether.com)
  72. //
  73. Object.extend(Element, {
  74. getWidth: function(element) {
  75. element = $(element);
  76. return element.offsetWidth;
  77. },
  78. setWidth: function(element,w) {
  79. element = $(element);
  80. element.style.width = w +"px";
  81. },
  82. setHeight: function(element,h) {
  83. element = $(element);
  84. element.style.height = h +"px";
  85. },
  86. setTop: function(element,t) {
  87. element = $(element);
  88. element.style.top = t +"px";
  89. },
  90. setSrc: function(element,src) {
  91. element = $(element);
  92. element.src = src;
  93. },
  94. setHref: function(element,href) {
  95. element = $(element);
  96. element.href = href;
  97. },
  98. setInnerHTML: function(element,content) {
  99. element = $(element);
  100. element.innerHTML = content;
  101. }
  102. });
  103. // -----------------------------------------------------------------------------------
  104. //
  105. // Extending built-in Array object
  106. // - array.removeDuplicates()
  107. // - array.empty()
  108. //
  109. Array.prototype.removeDuplicates = function () {
  110. for(i = 1; i < this.length; i++){
  111. if(this[i][0] == this[i-1][0]){
  112. this.splice(i,1);
  113. }
  114. }
  115. }
  116. // -----------------------------------------------------------------------------------
  117. Array.prototype.empty = function () {
  118. for(i = 0; i <= this.length; i++){
  119. this.shift();
  120. }
  121. }
  122. // -----------------------------------------------------------------------------------
  123. //
  124. // Lightbox Class Declaration
  125. // - initialize()
  126. // - start()
  127. // - changeImage()
  128. // - resizeImageContainer()
  129. // - showImage()
  130. // - updateDetails()
  131. // - updateNav()
  132. // - enableKeyboardNav()
  133. // - disableKeyboardNav()
  134. // - keyboardNavAction()
  135. // - preloadNeighborImages()
  136. // - end()
  137. //
  138. // Structuring of code inspired by Scott Upton (http://www.uptonic.com/)
  139. //
  140. var Lightbox = Class.create();
  141. Lightbox.prototype = {
  142. // initialize()
  143. // Constructor runs on completion of the DOM loading. Loops through anchor tags looking for
  144. // 'lightbox' references and applies onclick events to appropriate links. The 2nd section of
  145. // the function inserts html at the bottom of the page which is used to display the shadow
  146. // overlay and the image container.
  147. //
  148. initialize: function() {
  149. if (!document.getElementsByTagName){ return; }
  150. var anchors = document.getElementsByTagName('a');
  151. // loop through all anchor tags
  152. for (var i=0; i<anchors.length; i++){
  153. var anchor = anchors[i];
  154. var relAttribute = String(anchor.getAttribute('rel'));
  155. // use the string.match() method to catch 'lightbox' references in the rel attribute
  156. if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('lightbox'))){
  157. anchor.onclick = function () {myLightbox.start(this); return false;}
  158. }
  159. }
  160. // The rest of this code inserts html at the bottom of the page that looks similar to this:
  161. //
  162. // <div id="overlay"></div>
  163. // <div id="lightbox">
  164. // <div id="outerImageContainer">
  165. // <div id="imageContainer">
  166. // <img id="lightboxImage">
  167. // <div style="" id="hoverNav">
  168. // <a href="#" id="prevLink"></a>
  169. // <a href="#" id="nextLink"></a>
  170. // </div>
  171. // <div id="loading">
  172. // <a href="#" id="loadingLink">
  173. // <img src="images/loading.gif">
  174. // </a>
  175. // </div>
  176. // </div>
  177. // </div>
  178. // <div id="imageDataContainer">
  179. // <div id="imageData">
  180. // <div id="imageDetails">
  181. // <span id="caption"></span>
  182. // <span id="numberDisplay"></span>
  183. // </div>
  184. // <div id="bottomNav">
  185. // <a href="#" id="bottomNavClose">
  186. // <img src="images/close.gif">
  187. // </a>
  188. // </div>
  189. // </div>
  190. // </div>
  191. // </div>
  192. var objBody = document.getElementsByTagName("body").item(0);
  193. var objOverlay = document.createElement("div");
  194. objOverlay.setAttribute('id','overlay');
  195. objOverlay.style.display = 'none';
  196. objOverlay.onclick = function() { myLightbox.end(); return false; }
  197. objBody.appendChild(objOverlay);
  198. var objLightbox = document.createElement("div");
  199. objLightbox.setAttribute('id','lightbox');
  200. objLightbox.style.display = 'none';
  201. objBody.appendChild(objLightbox);
  202. var objOuterImageContainer = document.createElement("div");
  203. objOuterImageContainer.setAttribute('id','outerImageContainer');
  204. objLightbox.appendChild(objOuterImageContainer);
  205. var objImageContainer = document.createElement("div");
  206. objImageContainer.setAttribute('id','imageContainer');
  207. objOuterImageContainer.appendChild(objImageContainer);
  208. var objLightboxImage = document.createElement("img");
  209. objLightboxImage.setAttribute('id','lightboxImage');
  210. objImageContainer.appendChild(objLightboxImage);
  211. var objHoverNav = document.createElement("div");
  212. objHoverNav.setAttribute('id','hoverNav');
  213. objImageContainer.appendChild(objHoverNav);
  214. var objPrevLink = document.createElement("a");
  215. objPrevLink.setAttribute('id','prevLink');
  216. objPrevLink.setAttribute('href','#');
  217. objHoverNav.appendChild(objPrevLink);
  218. var objNextLink = document.createElement("a");
  219. objNextLink.setAttribute('id','nextLink');
  220. objNextLink.setAttribute('href','#');
  221. objHoverNav.appendChild(objNextLink);
  222. var objLoading = document.createElement("div");
  223. objLoading.setAttribute('id','loading');
  224. objImageContainer.appendChild(objLoading);
  225. var objLoadingLink = document.createElement("a");
  226. objLoadingLink.setAttribute('id','loadingLink');
  227. objLoadingLink.setAttribute('href','#');
  228. objLoadingLink.onclick = function() { myLightbox.end(); return false; }
  229. objLoading.appendChild(objLoadingLink);
  230. var objLoadingImage = document.createElement("img");
  231. objLoadingImage.setAttribute('src', fileLoadingImage);
  232. objLoadingLink.appendChild(objLoadingImage);
  233. var objImageDataContainer = document.createElement("div");
  234. objImageDataContainer.setAttribute('id','imageDataContainer');
  235. objImageDataContainer.className = 'clearfix';
  236. objLightbox.appendChild(objImageDataContainer);
  237. var objImageData = document.createElement("div");
  238. objImageData.setAttribute('id','imageData');
  239. objImageDataContainer.appendChild(objImageData);
  240. var objImageDetails = document.createElement("div");
  241. objImageDetails.setAttribute('id','imageDetails');
  242. objImageData.appendChild(objImageDetails);
  243. var objCaption = document.createElement("span");
  244. objCaption.setAttribute('id','caption');
  245. objImageDetails.appendChild(objCaption);
  246. var objNumberDisplay = document.createElement("span");
  247. objNumberDisplay.setAttribute('id','numberDisplay');
  248. objImageDetails.appendChild(objNumberDisplay);
  249. var objBottomNav = document.createElement("div");
  250. objBottomNav.setAttribute('id','bottomNav');
  251. objImageData.appendChild(objBottomNav);
  252. var objBottomNavCloseLink = document.createElement("a");
  253. objBottomNavCloseLink.setAttribute('id','bottomNavClose');
  254. objBottomNavCloseLink.setAttribute('href','#');
  255. objBottomNavCloseLink.onclick = function() { myLightbox.end(); return false; }
  256. objBottomNav.appendChild(objBottomNavCloseLink);
  257. var objBottomNavCloseImage = document.createElement("img");
  258. objBottomNavCloseImage.setAttribute('src', fileBottomNavCloseImage);
  259. objBottomNavCloseLink.appendChild(objBottomNavCloseImage);
  260. },
  261. //
  262. // start()
  263. // Display overlay and lightbox. If image is part of a set, add siblings to imageArray.
  264. //
  265. start: function(imageLink) {
  266. hideSelectBoxes();
  267. // stretch overlay to fill page and fade in
  268. var arrayPageSize = getPageSize();
  269. Element.setHeight('overlay', arrayPageSize[1]);
  270. new Effect.Appear('overlay', { duration: 0.2, from: 0.0, to: 0.8 });
  271. imageArray = [];
  272. imageNum = 0;
  273. if (!document.getElementsByTagName){ return; }
  274. var anchors = document.getElementsByTagName('a');
  275. // if image is NOT part of a set..
  276. if((imageLink.getAttribute('rel') == 'lightbox')){
  277. // add single image to imageArray
  278. imageArray.push(new Array(imageLink.getAttribute('href'), imageLink.getAttribute('title')));
  279. } else {
  280. // if image is part of a set..
  281. // loop through anchors, find other images in set, and add them to imageArray
  282. for (var i=0; i<anchors.length; i++){
  283. var anchor = anchors[i];
  284. if (anchor.getAttribute('href') && (anchor.getAttribute('rel') == imageLink.getAttribute('rel'))){
  285. imageArray.push(new Array(anchor.getAttribute('href'), anchor.getAttribute('title')));
  286. }
  287. }
  288. imageArray.removeDuplicates();
  289. while(imageArray[imageNum][0] != imageLink.getAttribute('href')) { imageNum++;}
  290. }
  291. // calculate top offset for the lightbox and display
  292. var arrayPageSize = getPageSize();
  293. var arrayPageScroll = getPageScroll();
  294. var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 15);
  295. Element.setTop('lightbox', lightboxTop);
  296. Element.show('lightbox');
  297. this.changeImage(imageNum);
  298. },
  299. //
  300. // changeImage()
  301. // Hide most elements and preload image in preparation for resizing image container.
  302. //
  303. changeImage: function(imageNum) {
  304. activeImage = imageNum; // update global var
  305. // hide elements during transition
  306. Element.show('loading');
  307. Element.hide('lightboxImage');
  308. Element.hide('hoverNav');
  309. Element.hide('prevLink');
  310. Element.hide('nextLink');
  311. Element.hide('imageDataContainer');
  312. Element.hide('numberDisplay');
  313. imgPreloader = new Image();
  314. // once image is preloaded, resize image container
  315. imgPreloader.onload=function(){
  316. Element.setSrc('lightboxImage', imageArray[activeImage][0]);
  317. myLightbox.resizeImageContainer(imgPreloader.width, imgPreloader.height);
  318. }
  319. imgPreloader.src = imageArray[activeImage][0];
  320. },
  321. //
  322. // resizeImageContainer()
  323. //
  324. resizeImageContainer: function( imgWidth, imgHeight) {
  325. // get current height and width
  326. this.wCur = Element.getWidth('outerImageContainer');
  327. this.hCur = Element.getHeight('outerImageContainer');
  328. // scalars based on change from old to new
  329. this.xScale = ((imgWidth + (borderSize * 2)) / this.wCur) * 100;
  330. this.yScale = ((imgHeight + (borderSize * 2)) / this.hCur) * 100;
  331. // calculate size difference between new and old image, and resize if necessary
  332. wDiff = (this.wCur - borderSize * 2) - imgWidth;
  333. hDiff = (this.hCur - borderSize * 2) - imgHeight;
  334. if(!( hDiff == 0)){ new Effect.Scale('outerImageContainer', this.yScale, {scaleX: false, duration: resizeDuration, queue: 'front'}); }
  335. if(!( wDiff == 0)){ new Effect.Scale('outerImageContainer', this.xScale, {scaleY: false, delay: resizeDuration, duration: resizeDuration}); }
  336. // if new and old image are same size and no scaling transition is necessary,
  337. // do a quick pause to prevent image flicker.
  338. if((hDiff == 0) && (wDiff == 0)){
  339. if (navigator.appVersion.indexOf("MSIE")!=-1){ pause(250); } else { pause(100);}
  340. }
  341. Element.setHeight('prevLink', imgHeight);
  342. Element.setHeight('nextLink', imgHeight);
  343. Element.setWidth( 'imageDataContainer', imgWidth + (borderSize * 2));
  344. this.showImage();
  345. },
  346. //
  347. // showImage()
  348. // Display image and begin preloading neighbors.
  349. //
  350. showImage: function(){
  351. Element.hide('loading');
  352. new Effect.Appear('lightboxImage', { duration: 0.5, queue: 'end', afterFinish: function(){ myLightbox.updateDetails(); } });
  353. this.preloadNeighborImages();
  354. },
  355. //
  356. // updateDetails()
  357. // Display caption, image number, and bottom nav.
  358. //
  359. updateDetails: function() {
  360. Element.show('caption');
  361. Element.setInnerHTML( 'caption', imageArray[activeImage][1]);
  362. // if image is part of set display 'Image x of x'
  363. if(imageArray.length > 1){
  364. Element.show('numberDisplay');
  365. Element.setInnerHTML( 'numberDisplay', "Image " + eval(activeImage + 1) + " of " + imageArray.length);
  366. }
  367. new Effect.Parallel(
  368. [ new Effect.SlideDown( 'imageDataContainer', { sync: true, duration: resizeDuration + 0.25, from: 0.0, to: 1.0 }),
  369. new Effect.Appear('imageDataContainer', { sync: true, duration: 1.0 }) ],
  370. { duration: 0.65, afterFinish: function() { myLightbox.updateNav();} }
  371. );
  372. },
  373. //
  374. // updateNav()
  375. // Display appropriate previous and next hover navigation.
  376. //
  377. updateNav: function() {
  378. Element.show('hoverNav');
  379. // if not first image in set, display prev image button
  380. if(activeImage != 0){
  381. Element.show('prevLink');
  382. document.getElementById('prevLink').onclick = function() {
  383. myLightbox.changeImage(activeImage - 1); return false;
  384. }
  385. }
  386. // if not last image in set, display next image button
  387. if(activeImage != (imageArray.length - 1)){
  388. Element.show('nextLink');
  389. document.getElementById('nextLink').onclick = function() {
  390. myLightbox.changeImage(activeImage + 1); return false;
  391. }
  392. }
  393. this.enableKeyboardNav();
  394. },
  395. //
  396. // enableKeyboardNav()
  397. //
  398. enableKeyboardNav: function() {
  399. document.onkeydown = this.keyboardAction;
  400. },
  401. //
  402. // disableKeyboardNav()
  403. //
  404. disableKeyboardNav: function() {
  405. document.onkeydown = '';
  406. },
  407. //
  408. // keyboardAction()
  409. //
  410. keyboardAction: function(e) {
  411. if (e == null) { // ie
  412. keycode = event.keyCode;
  413. } else { // mozilla
  414. keycode = e.which;
  415. }
  416. key = String.fromCharCode(keycode).toLowerCase();
  417. if((key == 'x') || (key == 'o') || (key == 'c')){ // close lightbox
  418. myLightbox.end();
  419. } else if(key == 'p'){ // display previous image
  420. if(activeImage != 0){
  421. myLightbox.disableKeyboardNav();
  422. myLightbox.changeImage(activeImage - 1);
  423. }
  424. } else if(key == 'n'){ // display next image
  425. if(activeImage != (imageArray.length - 1)){
  426. myLightbox.disableKeyboardNav();
  427. myLightbox.changeImage(activeImage + 1);
  428. }
  429. }
  430. },
  431. //
  432. // preloadNeighborImages()
  433. // Preload previous and next images.
  434. //
  435. preloadNeighborImages: function(){
  436. if((imageArray.length - 1) > activeImage){
  437. preloadNextImage = new Image();
  438. preloadNextImage.src = imageArray[activeImage + 1][0];
  439. }
  440. if(activeImage > 0){
  441. preloadPrevImage = new Image();
  442. preloadPrevImage.src = imageArray[activeImage - 1][0];
  443. }
  444. },
  445. //
  446. // end()
  447. //
  448. end: function() {
  449. this.disableKeyboardNav();
  450. Element.hide('lightbox');
  451. new Effect.Fade('overlay', { duration: 0.2});
  452. showSelectBoxes();
  453. }
  454. }
  455. // -----------------------------------------------------------------------------------
  456. //
  457. // getPageScroll()
  458. // Returns array with x,y page scroll values.
  459. // Core code from - quirksmode.org
  460. //
  461. function getPageScroll(){
  462. var yScroll;
  463. if (self.pageYOffset) {
  464. yScroll = self.pageYOffset;
  465. } else if (document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict
  466. yScroll = document.documentElement.scrollTop;
  467. } else if (document.body) {// all other Explorers
  468. yScroll = document.body.scrollTop;
  469. }
  470. arrayPageScroll = new Array('',yScroll)
  471. return arrayPageScroll;
  472. }
  473. // -----------------------------------------------------------------------------------
  474. //
  475. // getPageSize()
  476. // Returns array with page width, height and window width, height
  477. // Core code from - quirksmode.org
  478. // Edit for Firefox by pHaez
  479. //
  480. function getPageSize(){
  481. var xScroll, yScroll;
  482. if (window.innerHeight && window.scrollMaxY) {
  483. xScroll = document.body.scrollWidth;
  484. yScroll = window.innerHeight + window.scrollMaxY;
  485. } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
  486. xScroll = document.body.scrollWidth;
  487. yScroll = document.body.scrollHeight;
  488. } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
  489. xScroll = document.body.offsetWidth;
  490. yScroll = document.body.offsetHeight;
  491. }
  492. var windowWidth, windowHeight;
  493. if (self.innerHeight) { // all except Explorer
  494. windowWidth = self.innerWidth;
  495. windowHeight = self.innerHeight;
  496. } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
  497. windowWidth = document.documentElement.clientWidth;
  498. windowHeight = document.documentElement.clientHeight;
  499. } else if (document.body) { // other Explorers
  500. windowWidth = document.body.clientWidth;
  501. windowHeight = document.body.clientHeight;
  502. }
  503. // for small pages with total height less then height of the viewport
  504. if(yScroll < windowHeight){
  505. pageHeight = windowHeight;
  506. } else {
  507. pageHeight = yScroll;
  508. }
  509. // for small pages with total width less then width of the viewport
  510. if(xScroll < windowWidth){
  511. pageWidth = windowWidth;
  512. } else {
  513. pageWidth = xScroll;
  514. }
  515. arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
  516. return arrayPageSize;
  517. }
  518. // -----------------------------------------------------------------------------------
  519. //
  520. // getKey(key)
  521. // Gets keycode. If 'x' is pressed then it hides the lightbox.
  522. //
  523. function getKey(e){
  524. if (e == null) { // ie
  525. keycode = event.keyCode;
  526. } else { // mozilla
  527. keycode = e.which;
  528. }
  529. key = String.fromCharCode(keycode).toLowerCase();
  530. if(key == 'x'){
  531. }
  532. }
  533. // -----------------------------------------------------------------------------------
  534. //
  535. // listenKey()
  536. //
  537. function listenKey () { document.onkeypress = getKey; }
  538. // ---------------------------------------------------
  539. function showSelectBoxes(){
  540. selects = document.getElementsByTagName("select");
  541. for (i = 0; i != selects.length; i++) {
  542. selects[i].style.visibility = "visible";
  543. }
  544. }
  545. // ---------------------------------------------------
  546. function hideSelectBoxes(){
  547. selects = document.getElementsByTagName("select");
  548. for (i = 0; i != selects.length; i++) {
  549. selects[i].style.visibility = "hidden";
  550. }
  551. }
  552. // ---------------------------------------------------
  553. //
  554. // pause(numberMillis)
  555. // Pauses code execution for specified time. Uses busy code, not good.
  556. // Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
  557. //
  558. function pause(numberMillis) {
  559. var now = new Date();
  560. var exitTime = now.getTime() + numberMillis;
  561. while (true) {
  562. now = new Date();
  563. if (now.getTime() > exitTime)
  564. return;
  565. }
  566. }
  567. // ---------------------------------------------------
  568. function initLightbox() { myLightbox = new Lightbox(); }
  569. Event.observe(window, 'load', initLightbox, false);