annotate media/js/thickbox.js @ 12:d771b81e927e

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