thickbox.js 12 KB

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