thickbox.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Thickbox 3.1 - One Box To Rule Them All.
  3. * By Cody Lindley (http://www.codylindley.com)
  4. * Copyright (c) 2007 cody lindley
  5. * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
  6. */
  7. // Identify the url of the current script to load the loadingAnimation.gif animation
  8. var src=document.getElementsByTagName('script');
  9. var src=src[src.length-1].getAttribute('src');
  10. // remove the inc/lib/javascript/thickbox.js part (30 characters)
  11. var src=src.substr(0,src.length-30);
  12. var tb_pathToImage = src+"img/loadingAnimation.gif";
  13. var close_image = src+"img/close.png";
  14. /*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/
  15. //on page load call tb_init
  16. $(document).ready(function(){
  17. tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
  18. imgLoader = new Image();// preload image
  19. imgLoader.src = tb_pathToImage;
  20. });
  21. //
  22. function load_thick (hrefinfo,textinfo) {
  23. imgLoader = new Image();// preload image
  24. imgLoader.src = tb_pathToImage;
  25. name_user=textinfo.replace(/\+/g," ");
  26. tb_show(name_user,hrefinfo, '');
  27. }
  28. //add thickbox to href & area elements that have a class of .thickbox
  29. function tb_init(domChunk){
  30. $(domChunk).click(function(){
  31. var t = this.title || this.name || null;
  32. var a = this.href || this.alt;
  33. var g = this.rel || false;
  34. tb_show(t,a,g);
  35. this.blur();
  36. return false;
  37. });
  38. }
  39. function tb_show(caption, url, imageGroup) {
  40. //function called when the user clicks on a thickbox link
  41. try {
  42. if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
  43. $("body","html").css({height: "100%", width: "100%"});
  44. $("html").css("overflow","hidden");
  45. if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
  46. $("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
  47. $("#TB_overlay").click(tb_remove);
  48. }
  49. }else{//all others
  50. if(document.getElementById("TB_overlay") === null){
  51. $("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
  52. $("#TB_overlay").click(tb_remove);
  53. }
  54. }
  55. if(tb_detectMacXFF()){
  56. $("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
  57. }else{
  58. $("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
  59. }
  60. if(caption===null){caption="";}
  61. $("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
  62. $('#TB_load').show();//show loader
  63. var baseURL;
  64. if(url.indexOf("?")!==-1){ //ff there is a query string involved
  65. baseURL = url.substr(0, url.indexOf("?"));
  66. }else{
  67. baseURL = url;
  68. }
  69. var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
  70. var urlType = baseURL.toLowerCase().match(urlString);
  71. if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){//code to show images
  72. TB_PrevCaption = "";
  73. TB_PrevURL = "";
  74. TB_PrevHTML = "";
  75. TB_NextCaption = "";
  76. TB_NextURL = "";
  77. TB_NextHTML = "";
  78. TB_imageCount = "";
  79. TB_FoundURL = false;
  80. if(imageGroup){
  81. TB_TempArray = $("a[@rel="+imageGroup+"]").get();
  82. for (TB_Counter = 0; ((TB_Counter < TB_TempArray.length) && (TB_NextHTML === "")); TB_Counter++) {
  83. var urlTypeTemp = TB_TempArray[TB_Counter].href.toLowerCase().match(urlString);
  84. if (!(TB_TempArray[TB_Counter].href == url)) {
  85. if (TB_FoundURL) {
  86. TB_NextCaption = TB_TempArray[TB_Counter].title;
  87. TB_NextURL = TB_TempArray[TB_Counter].href;
  88. TB_NextHTML = "<span id='TB_next'>&nbsp;&nbsp;<a href='#'>Next &gt;</a></span>";
  89. } else {
  90. TB_PrevCaption = TB_TempArray[TB_Counter].title;
  91. TB_PrevURL = TB_TempArray[TB_Counter].href;
  92. TB_PrevHTML = "<span id='TB_prev'>&nbsp;&nbsp;<a href='#'>&lt; Prev</a></span>";
  93. }
  94. } else {
  95. TB_FoundURL = true;
  96. TB_imageCount = "Image " + (TB_Counter + 1) +" of "+ (TB_TempArray.length);
  97. }
  98. }
  99. }
  100. imgPreloader = new Image();
  101. imgPreloader.onload = function(){
  102. imgPreloader.onload = null;
  103. // Resizing large images - orginal by Christian Montoya edited by me.
  104. var pagesize = tb_getPageSize();
  105. var x = pagesize[0] - 150;
  106. var y = pagesize[1] - 150;
  107. var imageWidth = imgPreloader.width;
  108. var imageHeight = imgPreloader.height;
  109. if (imageWidth > x) {
  110. imageHeight = imageHeight * (x / imageWidth);
  111. imageWidth = x;
  112. if (imageHeight > y) {
  113. imageWidth = imageWidth * (y / imageHeight);
  114. imageHeight = y;
  115. }
  116. } else if (imageHeight > y) {
  117. imageWidth = imageWidth * (y / imageHeight);
  118. imageHeight = y;
  119. if (imageWidth > x) {
  120. imageHeight = imageHeight * (x / imageWidth);
  121. imageWidth = x;
  122. }
  123. }
  124. // End Resizing
  125. TB_WIDTH = imageWidth + 30;
  126. TB_HEIGHT = imageHeight + 60;
  127. $("#TB_window").append("<div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton'><img src='"+close_image+"'/></a></div>");
  128. $("#TB_window").append("<a href='' id='TB_ImageOff' title='Close'><img id='TB_Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' alt='"+caption+"'/></a>");
  129. $("#TB_window").append("<div id='TB_caption'>"+caption+"<div id='TB_secondLine'>" + TB_imageCount + TB_PrevHTML + TB_NextHTML + "</div></div>")
  130. $("#TB_closeWindowButton").click(tb_remove);
  131. if (!(TB_PrevHTML === "")) {
  132. function goPrev(){
  133. if($(document).unbind("click",goPrev)){$(document).unbind("click",goPrev);}
  134. $("#TB_window").remove();
  135. $("body").append("<div id='TB_window'></div>");
  136. tb_show(TB_PrevCaption, TB_PrevURL, imageGroup);
  137. return false;
  138. }
  139. $("#TB_prev").click(goPrev);
  140. }
  141. if (!(TB_NextHTML === "")) {
  142. function goNext(){
  143. $("#TB_window").remove();
  144. $("body").append("<div id='TB_window'></div>");
  145. tb_show(TB_NextCaption, TB_NextURL, imageGroup);
  146. return false;
  147. }
  148. $("#TB_next").click(goNext);
  149. }
  150. document.onkeydown = function(e){
  151. if (e == null) { // ie
  152. keycode = event.keyCode;
  153. } else { // mozilla
  154. keycode = e.which;
  155. }
  156. if(keycode == 27){ // close
  157. tb_remove();
  158. } else if(keycode == 190){ // display previous image
  159. if(!(TB_NextHTML == "")){
  160. document.onkeydown = "";
  161. goNext();
  162. }
  163. } else if(keycode == 188){ // display next image
  164. if(!(TB_PrevHTML == "")){
  165. document.onkeydown = "";
  166. goPrev();
  167. }
  168. }
  169. };
  170. tb_position();
  171. $("#TB_load").remove();
  172. $("#TB_ImageOff").click(tb_remove);
  173. $("#TB_window").css({display:"block"}); //for safari using css instead of show
  174. };
  175. imgPreloader.src = url;
  176. }else{//code to show html
  177. var queryString = url.replace(/^[^\?]+\??/,'');
  178. var params = tb_parseQuery( queryString );
  179. TB_WIDTH = (params['width']*1) + 30 || 630; //defaults to 630 if no paramaters were added to URL
  180. TB_HEIGHT = (params['height']*1) + 40 || 440; //defaults to 440 if no paramaters were added to URL
  181. ajaxContentW = TB_WIDTH - 30;
  182. ajaxContentH = TB_HEIGHT - 45;
  183. if(url.indexOf('TB_iframe') != -1){// either iframe or ajax window
  184. urlNoQuery = url.split('TB_');
  185. $("#TB_iframeContent").remove();
  186. if(params['modal'] != "true"){//iframe no modal
  187. $("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='Close'><img src='"+close_image+"'/></a></a></div></div><iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' > </iframe>");
  188. }else{//iframe modal
  189. $("#TB_overlay").unbind();
  190. $("#TB_window").append("<iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;'> </iframe>");
  191. }
  192. } else {// not an iframe, ajax
  193. if($("#TB_window").css("display") != "block"){
  194. if(params['modal'] != "true"){//ajax no modal
  195. $("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton'><img src='"+close_image+"'/></a></a></div></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
  196. }else{//ajax modal
  197. $("#TB_overlay").unbind();
  198. $("#TB_window").append("<div id='TB_ajaxContent' class='TB_modal' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
  199. }
  200. }else{//this means the window is already up, we are just loading new content via ajax
  201. $("#TB_ajaxContent")[0].style.width = ajaxContentW +"px";
  202. $("#TB_ajaxContent")[0].style.height = ajaxContentH +"px";
  203. $("#TB_ajaxContent")[0].scrollTop = 0;
  204. $("#TB_ajaxWindowTitle").html(caption);
  205. }
  206. }
  207. $("#TB_closeWindowButton").click(tb_remove);
  208. if(url.indexOf('TB_inline') != -1){
  209. $("#TB_ajaxContent").append($('#' + params['inlineId']).children());
  210. $("#TB_window").unload(function () {
  211. $('#' + params['inlineId']).append( $("#TB_ajaxContent").children() ); // move elements back when you're finished
  212. });
  213. tb_position();
  214. $("#TB_load").remove();
  215. $("#TB_window").css({display:"block"});
  216. }else if(url.indexOf('TB_iframe') != -1){
  217. tb_position();
  218. if($.browser.safari){//safari needs help because it will not fire iframe onload
  219. $("#TB_load").remove();
  220. $("#TB_window").css({display:"block"});
  221. }
  222. }else{
  223. $("#TB_ajaxContent").load(url += "&random=" + (new Date().getTime()),function(){//to do a post change this load method
  224. tb_position();
  225. $("#TB_load").remove();
  226. tb_init("#TB_ajaxContent a.thickbox");
  227. $("#TB_window").css({display:"block"});
  228. });
  229. }
  230. }
  231. if(!params['modal']){
  232. document.onkeyup = function(e){
  233. if (e == null) { // ie
  234. keycode = event.keyCode;
  235. } else { // mozilla
  236. keycode = e.which;
  237. }
  238. if(keycode == 27){ // close
  239. tb_remove();
  240. }
  241. };
  242. }
  243. } catch(e) {
  244. //nothing here
  245. }
  246. }
  247. //helper functions below
  248. function tb_showIframe(){
  249. $("#TB_load").remove();
  250. $("#TB_window").css({display:"block"});
  251. }
  252. function tb_remove() {
  253. $("#TB_imageOff").unbind("click");
  254. $("#TB_closeWindowButton").unbind("click");
  255. $("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
  256. $("#TB_load").remove();
  257. if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
  258. $("body","html").css({height: "auto", width: "auto"});
  259. $("html").css("overflow","");
  260. }
  261. document.onkeydown = "";
  262. document.onkeyup = "";
  263. return false;
  264. }
  265. function tb_position() {
  266. $("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
  267. if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
  268. $("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
  269. }
  270. }
  271. function tb_parseQuery ( query ) {
  272. var Params = {};
  273. if ( ! query ) {return Params;}// return empty object
  274. var Pairs = query.split(/[;&]/);
  275. for ( var i = 0; i < Pairs.length; i++ ) {
  276. var KeyVal = Pairs[i].split('=');
  277. if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
  278. var key = unescape( KeyVal[0] );
  279. var val = unescape( KeyVal[1] );
  280. val = val.replace(/\+/g, ' ');
  281. Params[key] = val;
  282. }
  283. return Params;
  284. }
  285. function tb_getPageSize(){
  286. var de = document.documentElement;
  287. var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  288. var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
  289. arrayPageSize = [w,h];
  290. return arrayPageSize;
  291. }
  292. function tb_detectMacXFF() {
  293. var userAgent = navigator.userAgent.toLowerCase();
  294. if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
  295. return true;
  296. }
  297. }
  298. function TB_special(pi_href){
  299. // get caption: either title or name attribute
  300. var caption = this.title || this.name || "";
  301. // get rel attribute for image groups
  302. var group = this.rel || false;
  303. // display the box for the elements href
  304. TB_show(caption, pi_href, group);
  305. }