

// INDEXHIBIT WOBBLEBOX PLUGIN FOR JQUERY 1.4, version 1.0

/* BASED ON __________________________________________________________________________
* Original Thickbox 2.1 - jQuery plugin by Cody Lindley (http://www.codylindley.com)
*
* Copyright (c) 2006, 2007 Cody Lindley (http://www.codylindley.com)
*
* Licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
_____________________________________________________________________________________

Completely rewritten for specific use with Indexhibit & jQuery 1.4

by

Jesper Mathiasen (http://www.wobbleweb.net)

2010

Still licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php


Can now be used for content such as: images, youtube videos, swfobjects, iframes, text, quicktime mov.


*/
function roundNumber(num, dec) {
var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
return result;
}



var whatkind = function(stuff) { // ::::: STEP 2

tbindex = $(stuff).attr('class'); // Set tbindex to class of clicked item
url = $(stuff).attr('href'); // If image determine possible url
tbskip = $(stuff).attr('tbskip'); // Determine if item should be skipped
tbtitle = $(stuff).attr('title'); // determine possible alt text
attrH = $(stuff).attr('height');
attrW = $(stuff).attr('width');


if ($(stuff).hasClass('gmaps')) {
kind = 'gmaps';
} else if ($(stuff).hasClass('qtmov')) {
kind = 'qtmov';
} else if ($(stuff).hasClass('swf')) {
kind = 'swf';
} else if ($(stuff).hasClass('iframe')) {
kind = 'iframe';
} else if ($(stuff).hasClass('utoob')) {
kind = 'utoob';
} else if ($(stuff).hasClass('qtmov')) {
kind = 'qtmov';
} else if ($(stuff).hasClass('iframe')) {
kind = 'iframe';
} else {
kind = undefined;
}

paramset = $('<p></p>');

var params = $(stuff).find('.tbparams span');
$(params).each(function(index) {
$(paramset).append('<param name="'+($(this).attr('name'))+'" value="'+($(this).attr('value'))+'">');
});
};


function nextimg(totalimgcount, url) {

$('.navbut').click(function() { // FUNCTION TO FIRE WHEN CLICKING NEXT BUTTON

var direction = $(this).attr('id'); // are we going forward or backward?

if (direction == 'next') {
var nextint = 1;
var lastint = 2;
var currentint = 0;
var button1 = '.nextbutton';
var button2 = '.prevbutton';
var button3 = '.dummyxyzwb'; // kinda lame solution....
var status1 = 'last';
var status2 = 'first';
var currimg = 2;
} else {
nextint = -1;
lastint = 1;
currentint = 1;
button1 = '.dummyxyzwb';
button2 = '.nextbutton';
button3 = '.prevbutton';
status1 = 'first';
status2 = 'last';
currimg = 0;
}

$('.imgnav').css('opacity', 0);

var currentitem = $('.tbimageholder').attr('tbindex'); // determine index of currently displayed item...

$('.thickbox[tbskip!=true]').each(function(index) { // ...and go through all .thickbox elements to find it, leaving out ones to be skipped...

var currentclass = $(this).attr('class');



if (currentitem == currentclass) { // ... if we do....
var currentindex = index; // ... determine the index position of active element...
var ne = currentindex+nextint; // ...and the index position of the next element
var nextitem = $('.thickbox[tbskip!=true]:eq('+ne+')');
var nexthref = $(nextitem).attr('href'); // determine what the next url is

whatkind(nextitem);

if (nexthref === undefined) {
var nextcontent = $(nextitem).html();// determine what the next content is
}

if ((currentindex+lastint) == totalimgcount) { // if last image is currently displayed...
$(button1).css('visibility', 'hidden').attr(status1, 'true'); // ... remove the next button
$(button2).css('visibility', 'visible').removeAttr(status2);
} else if (currentindex === currentint) { // or if first item is displayed and no 2 is up, display the prev button when it is displayed or remove it depending on direction
$(button2).css('visibility', 'visible').removeAttr(status2);
$(button3).css('visibility', 'hidden').attr(status1, 'true');
}

$('.currentimg').html(currentindex+currimg); // adjust current item number
$('.tbtitle').html(tbtitle); // update title
$('.tbimageholder *:not(.imgnav, .imgnav *, .tbtitle)').remove(); // remove all but image navigation

displayimage(nexthref, ne, totalimgcount, { // and run the displayimage function
tbindex: tbindex,
swfobject: nextcontent,
clicksource: true,
kind: kind,
attrH: attrH,
attrW: attrW,
params: paramset
});
}
});
});



}


function finalpos(alt_imgW, alt_imgH) {
var bodyH = $(window).height();
var bodyW = $(window).width();
var imgnavheight = $('.imgnav').height();

$('#tboverlay').css({height: bodyH, width: bodyW});
var img_top = ((bodyH - (alt_imgH+imgnavheight))/2);
var img_left = ((bodyW - alt_imgW)/2);
$('.tbimageholder').css({top: img_top, left: img_left});

$('.imgnav').css('top', (bodyH-(img_top+imgnavheight)));
// $('.tbimageholder *').fadeIn('slow');

}

function resize(imgW, imgH, rs){

var imgW = imgW;
var imgH = imgH;

if (rs !== true) {

$('.tbimageholder').height(imgH);
$('.tbimageholder').width(imgW);

var alt_imgW = $('.tbimageholder').outerWidth();
var alt_imgH = $('.tbimageholder').outerHeight();

$('.tbimageholder').removeClass('loading');
$('.tbtitle:empty').hide();
return finalpos(alt_imgW, alt_imgH);

} else {
return finalpos(imgW, imgH);
}
}




function tooWresize(item, imgW, imgH, bodyH, bodyW, noattribute) {// if image is too wide to fit

var imgratio = imgH / imgW; // set image ratio
var alt_imgW = bodyW - 130; // determine the max width of content based on body width minus 65px left/right margin
var alt_imgH = roundNumber((alt_imgW * imgratio),0); // set max height based on ratio and width
var img_top = ((bodyH - alt_imgH)/2);
var img_left = ((bodyW - alt_imgW)/2);

if (noattribute === true) { // if content is regular text/image etc...

$(item).width(alt_imgW); // ...set width alt_imgW as we don't want to resize width based on ratio....
var img_topAttr = ((bodyH - imgH)/2);
$('.tbwrap').css({'overflow-x': 'scroll'}); //... but instead want to be able to scroll

resize(alt_imgW, imgH);
} else { // if content is an image or an object/iframe fit for ratio based resizing...
$('.tbimageholder .tbwrap *:first *, .tbimageholder .tbwrap *:first').attr({'height': alt_imgH, 'width': alt_imgW}); // ...resize it, taking into account dual set of attributes
$(item).attr({'height': alt_imgH, 'width': alt_imgW});
resize(alt_imgW, alt_imgH);
}
}



function tooHresize(item, imgW, imgH, bodyH, bodyW, noattribute) {// if image is too high to fit

var imgratio = imgW / imgH;
var alt_imgH = bodyH - 130;
var alt_imgW = roundNumber((alt_imgH * imgratio),0);
var img_top = ((bodyH - alt_imgH)/2);
var img_left = ((bodyW - alt_imgW)/2);

if (noattribute === true) {
$(item).height(alt_imgH).width(imgW);
var img_leftAttr = ((bodyW - imgW)/2);
$('.tbwrap').css({'overflow-y': 'scroll', 'overflow-x': 'hidden', 'padding-right': '15px'});

resize(imgW, alt_imgH);
} else {
$('.tbimageholder .tbwrap *:first *, .tbimageholder .tbwrap *:first').attr({'height': alt_imgH, 'width': alt_imgW});
$(item).attr({'height': alt_imgH, 'width': alt_imgW});
resize(alt_imgW, alt_imgH);
if (alt_imgW > (bodyW-130)) { // if image is still too wide after initial resizing, run tooWresize function.
return tooWresize(item, alt_imgW, alt_imgH, bodyH, bodyW, noattribute);
}
}
}



function noresize(item, imgW, imgH, bodyH, bodyW, attribute){ // initial size assessment function
$(item).show();

if (imgH > (bodyH-130)) { // if image too high
tooHresize(item, imgW, imgH, bodyH, bodyW, attribute);
} else if (imgW > (bodyW-130)) { // if image too wide
tooWresize(item, imgW, imgH, bodyH, bodyW, attribute);
} else { // if image fits already
$('.tbwrap').css({'overflow-y': 'hidden', 'overflow-x': 'hidden'}); // hide scroll bar as content is not overflowing
resize(imgW, imgH);
}
}


function normsize(item, bodyH, bodyW, url, width, height) { // function to determine size and type of content

if (url === false) {

$('.tbimageholder .tbwrap *:not(.tbcontent, .tbcontent *, .tbtitle)').remove(); // remove all content that is not inside .tbcontent
var contentW = $(item).outerWidth();
var contentH = $(item).outerHeight();
var noattr = true;
noresize(item, contentW, contentH, bodyH, bodyW, noattr);
} else if (width > 0 && height > 0) { // if object, ie swf, qt etc
return noresize(item, width, height, bodyH, bodyW);
} else { // if image
var imgW = $(item).outerWidth();
var imgH = $(item).outerHeight();
noresize(item, imgW, imgH, bodyH, bodyW);
}
}


function navigation(item, url, width, height) {

var bodyH = $(window).height();
var bodyW = $(window).width();
$('.imgnav').css('opacity', 1); //... show navigation and....

$(document).bind('keydown',function(e){ // ...enable navigation via arrow keys. 39 = rightArr, 37 = leftArr, 38 = upArr, 40 = downArr, 27 = esc

if (e.which == 39) {
var islast = $('.nextbutton').attr('last');

if (islast === undefined) {
$('.nextbutton').trigger('click');}
} else if (e.which == 37) {
var isfirst = $('.prevbutton').attr('first');
if (isfirst === undefined) {
$('.prevbutton').trigger('click'); }
} else if (e.which == 38 || e.which == 40 || e.which == 27 ) {
$('.closebutton').trigger('click');
}
});

return normsize(item, bodyH, bodyW, url, parseFloat(width), parseFloat(height)); // run function to size it
}


function navi(item, settings, options){

if (settings.clicksource === false && !settings.tbskip) { // if source of click was not prev or next button
$('.imgnav').append(settings.prevtxt, settings.currentindex, settings.totalindex, settings.nexttxt, settings.close); // append navigation
$('.tbimageholder').prepend(settings.tbtitle);

if (options.currentindex == 1) { // hide next or prev link if first or last image is chosen
$('.prevbutton').css('visibility', 'hidden').attr('first', 'true');
} else if (options.currentindex == options.totalimgcount) {
$('.nextbutton').css('visibility', 'hidden').attr('last', 'true');
}

nextimg(options.totalimgcount, options.url); // run function to activate next and previous item functions

} else if (settings.clicksource === false && settings.tbskip == 'true') {
$('.imgnav').append(settings.close);
$('.tbimageholder').prepend(settings.tbtitle);
}

$('.closebutton, #tboverlay').click(function() { // remove img and overlay when close button or overlay is clicked
$('.imgnav span, #tboverlay').unbind('click'); // unbind click, key event handlers
$(document).unbind('keydown');
$(".tbimageholder").fadeOut(200, function() {
$('#tboverlay, .tbimageholder, .imgnav, #tbspinner').remove();
});
});

$('#tbspinner').hide(); // hide spinner




return navigation(item, options.url, options.attrW, options.attrH);
}



function displayimage(url, currentindex, totalimgcount, options){ // FUNCTION TO CALCULATE SIZE AND POSITION OF IMAGE, THEN DISPLAY IT ::: STEP 4
$('#tbspinner').show();
$('.tbimageholder').addClass('loading');
$(document).unbind('keydown'); // make sure keys can't be used while calculations are taking place

var settings = $.extend({ // merge custom alternate nav-texts with defaults
prevtxt: '<span id="prev" class="prevbutton navbut">prev</span>',
nexttxt: '<span id="next" class="nextbutton navbut">next</span>',
close: '<span class="closebutton">close</span>',
currentindex: '<span class="currentimg">'+currentindex+'</span>',
totalindex: '<span class="totalimg"> | '+totalimgcount+'</span>',
clicksource: true
},options||{});

var bodyH = $(window).height();
var bodyW = $(window).width();

if (url !== undefined){ // if src is an image

if (settings.kind === undefined) { // Simplify so return navi is not doubled

var item = $('<img>'); // create it
$(item).attr('src', url); // load it
var isloaded = $(item).attr('complete'); // In case of IE check if image is already cached...

if (isloaded === true) { // ...if it is skip the preload function and ...
$(item).hide(); // hide it

$('.tbimageholder').append(item).attr('tbindex', options.tbindex); // append it

// normsize(item, bodyH, bodyW, url); // run function to size it

return navi(item, settings, { // add navigation
currentindex: currentindex,
totalimgcount: totalimgcount,
url: url,
attrH: settings.attrH,
attrW: settings.attrW
});
}

$(item).load(function() { // preload image

$(item).hide(); // hide it

$('.tbimageholder').append(item).attr('tbindex', options.tbindex); // append it

return navi(item, settings, { // add navigation
currentindex: currentindex,
totalimgcount: totalimgcount,
url: url,
attrH: settings.attrH,
attrW: settings.attrW
});
});
} else if (settings.kind !== undefined) {

var paramsettings = $(settings.params).html();

if (settings.kind == 'gmaps') {
var element = '<iframe width="'+settings.attrW+'" height="'+settings.attrH+'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+url+'&amp;output=embed"></iframe><br /><small><a href="'+url+'&amp;source=embed" style="color:#0000FF;text-align:left">View Larger Map</a></small>';
} else if (settings.kind == 'qtmov') {
var element = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="'+settings.attrW+'" height="'+settings.attrH+'">'+paramsettings+'<param name="scale" value="tofit" /><param name="cache" value="false" /><param name="src" value="'+url+'" /><!--[if !IE]>--><object type="video/quicktime" data="'+url+'" width="'+settings.attrW+'" height="'+settings.attrH+'">'+paramsettings+'<param name="scale" value="tofit" /><param name="cache" value="false" /></object><!--<![endif]--></object>';
} else if (settings.kind == 'utoob') {
var element = '<!--[if !IE]> --><object type="application/x-shockwave-flash" data="'+url+'" width="'+settings.attrW+'" height="'+settings.attrH+'">'+paramsettings+'<!-- <![endif]--><!--[if IE]><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+settings.attrW+'" height="'+settings.attrH+'">'+paramsettings+'<param name="movie" value="'+url+'" /><!--></object><!-- <![endif]-->';
} else if (settings.kind == 'swf') {
var element = '<!--[if !IE]> --><object type="application/x-shockwave-flash" data="'+url+'" width="'+settings.attrW+'" height="'+settings.attrH+'">'+paramsettings+'<!-- <![endif]--><!--[if IE]><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+settings.attrW+'" height="'+settings.attrH+'">'+paramsettings+'<param name="movie" value="'+url+'" /><!--></object><!-- <![endif]-->';
} else if (settings.kind == 'iframe') {
var element = '<iframe src="'+url+'" width="'+settings.attrW+'" height="'+settings.attrH+'"></iframe>';
}

var content = $('<div class="tbwrap">'+element+'</div>'); // wrap html content

$(content).hide();

$('.tbimageholder').append(content).attr('tbindex', options.tbindex);

navi(content, settings, { // add navigation
currentindex: currentindex,
totalimgcount: totalimgcount,
url: true,
attrH: settings.attrH,
attrW: settings.attrW
});
}} else if (url === undefined) { // if item is object or regular text/image based content

var content = $('<div class="tbwrap">'+settings.swfobject+'</div>'); // wrap html content
$(content).hide();
$('.tbimageholder').append(content).attr('tbindex', options.tbindex);

navi(content, settings, { // add navigation
currentindex: currentindex,
totalimgcount: totalimgcount,
url: false
});
}
}


function makeoverlay(){ // FUNCTION TO CREATE overlay ::: STEP 3
var bodyH = $(window).height();
var bodyW = $(window).width();
$('body').append('<div class="tbimageholder"></div><div class="imgnav"></div><div id="tboverlay"></div><div id="tbspinner"></div>'); // append the tboverlay and tbimageholder
$('#tboverlay').css({width: bodyW, height:bodyH}); // adjust size of tboverlay to fit browser window
}




$(window).resize(function(){ // adjust image holder, overlay and navigation when resizing window
var imgH = $('.tbimageholder').outerHeight();
var imgW = $('.tbimageholder').outerWidth();
var rs = true;

resize(imgW, imgH, rs);
});





jQuery(document).ready(function($) {
$('.thickbox').each(function(index) {
$(this).addClass('tb'+index);
});


$(".thickbox").click(function(event){ // WHEN THUMB/LINK IS CLICKED :::: STEP 1

event.preventDefault(); // stop default link behaviour
var totalimgcount = $(".thickbox[tbskip!=true]").length; // Determine total number of images
var content = $(this).html(); // Determine html content of thickbox container

whatkind($(this));

if (tbskip == 'true') { // if item should be displayed by itself and not as part of all items on the page
makeoverlay();
var currentindex = 1;

return displayimage(url, currentindex, totalimgcount, { // run displayimage function.
tbindex: tbindex,
swfobject: content,
clicksource: false,
//tbtitle: '<span class="tbtitle">'+tbtitle+'</span>', //Denne linje bestemmer om billedtitlen skal vises over billedet.
tbskip: tbskip,
kind: kind,
attrH: attrH,
attrW: attrW,
params: paramset
});
}


$('.thickbox[tbskip!=true]').each(function(index) { // Count images and find the one which was clicked

var currentclass = $(this).attr('class');

if (tbindex == currentclass) {
var currentindex = index+1; // Determine position of clicked image in index

makeoverlay(); // run makeoverlay function.

displayimage(url, currentindex, totalimgcount, { // run displayimage function.
tbindex: tbindex,
swfobject: content,
clicksource: false,
//tbtitle: '<span class="tbtitle">'+tbtitle+'</span>', //Denne linje bestemmer om billedtitlen skal vises over billedet.
tbskip: tbskip,
kind: kind,
attrH: attrH,
attrW: attrW,
params: paramset
// prevtxt: '<span id="prev" class="prevbutton navbut">INSERT ALTERNATE PREV TEXT HERE AND UNCOMMENT</span>',
// nexttxt: '<span id="next" class="nextbutton navbut">INSERT ALTERNATE NEXT TEXT HERE AND UNCOMMENT</span>',
// close: '<span class="closebutton">INSERT ALTERNATE CLOSE TEXT HERE AND UNCOMMENT</span>'
});
}});
});
});

