/*
--------------------------------------------------------------------------
'The Longbox'
A lightbox designed and built by Phillip Long
Free for all to use and modify, with credit and
link to Phillip Long at humanbagel.com

contact: human.bagel@gmail.com
--------------------------------------------------------------------------
*/

// SET UP DEFAULT VALUES
//////////////////////////
//
// Default Height
default_height = '375px';

// Default Width
default_width = '450px';

// Default Distance From Top (Don't add quotes or 'px' to this variable)
default_top = 35;

// Default Distance From Left
default_left = '250px';

// Default file extensions to render for the auto paramater
// Seperate extensions with a '|'
// Don't remove the leading and trailing '/' 

// Default extensions to render as plain text
default_text = "txt|js|css";

// Default extensions to render as HTML
default_html = "html|htm|php|asp|aspx|cgi|pl";

// Default extensions to render as an image are hardcoded later on
// in the script. I see no need for the average user to modify these.

// END DEFAULT VALUES
//////////////////////////

function lightbox(opt){
// Set variables to the associative array,
// modify them to be useful,
// set default values

loc = opt['location'];

extra = opt['extra'];

param = opt['type'];

if(opt['type']) { }
else { param = "auto"; }

source = opt['source'];

if(source) { }
else { source = 'file'; }

caption = opt['caption'];

if(opt['height']) {
heightvar = opt['height']+'px'; }
else {
heightvar = default_height; } //'450px'; }

if(opt['width']) {
widthvar = opt['width']+'px'; }
else {
widthvar = default_width; } //'450px'; }


if(opt['top']) {
topvar = opt['top']; }
else {
topvar = default_top; } //35; }

if(opt['left']) {
leftvar = opt['left']+'px'; }
else {
leftvar = default_left; } //'250px'; }


// set up the auto paramater, based on file extension
if(param == "auto") {

if (typeof(loc.length)=="undefined") { param = "gallery"; } 
else { 
loci = loc.split(".");
loci = loci[loci.length -1];

txt = loci.search("/"+default_text+"/i");
img = loci.search(/jpg|jpeg|gif|png|bmp|tiff/i);
html = loci.search("/"+default_html+"/i");

if(txt == 0) { param = "text"; }
if(img == 0) { param = "image"; }
if(html == 0) { param = "html"; } } 
}


// AJAX request function
function ajaxreq(url) {
var myaj = new Ajax.Request(
url, {
method: 'get',
onComplete: complete
}
);
}


// when AJAX request completes...
function complete(oReq) {
if(!oReq.responseText) { oReq.responseText = "<b>No Contents</b>"; }

if(oReq.responseText) { rt = oReq.responseText; }
else if(param == 'image') { }
else { rt = $(loc).innerHTML; }


switch(param){

// if type is image
case 'image':
x=new Image;
x.src=loc;
// if height and width set by user...
if(opt['height'] && opt['width']){
$('lightdiv').style.height = heightvar;
$('lightdiv').style.width = widthvar;
rt = "<img src='"+loc+"' height='100%' width='100%'><br>";  }

// ...if not
else {
$('lightdiv').style.height = 'auto';
$('lightdiv').style.width = 'auto'; 
rt = "<img src='"+loc+"'><br>"; }
break;

// if type is text
case 'text':
rt = rt.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")
rt = rt.replace(/\n/g,'<br>');
if(opt['height']) { $('lightdiv').style.height = heightvar; }
else {
$('lightdiv').style.height = heightvar; 
$('lightdiv').style.width = widthvar; }
break;

// if type is gallery
case 'gallery':
gd = $('gallerydata').innerHTML;
gd = gd.split('|||');
gdlinks = '| ';
for(i=1; i<=gd.length-1; i++) {
gdlinks += "<a href='javascript:switchimg(\""+gd[i]+"\")'>"+i+"</a> | ";  }

// if height and width set by user...
if(opt['height'] && opt['width']){
$('lightdiv').style.height = heightvar;
$('lightdiv').style.width = widthvar;
rt = gdlinks + "<br><img src='"+gd[1]+"' height='100%' width='100%' id='boximg'><br>"; }

// ...if not
else {
$('lightdiv').style.height = 'auto';
$('lightdiv').style.width = 'auto'; 
rt = gdlinks + "<br><img src='"+gd[1]+"' id='boximg'><br>"; }
break;

// default (html)
default:
if(opt['height']) { $('lightdiv').style.height = heightvar; }
else {
$('lightdiv').style.height = heightvar; }
$('lightdiv').style.width = widthvar; }

// if there is a caption...
if(caption) {
$('lightdiv').innerHTML = rt + "<br><div class=\"caption\">" + caption + "<br></div>";
 }

// ...if not
else { $('lightdiv').innerHTML = rt; }

if(extra) { $('lightdiv').innerHTML += extra; }

// set the position of the exit button dynamically
$('exitbutton').style.width = $('lightdiv').offsetWidth+'px';
$('exitbutton').style.position = "absolute";
$('exitbutton').style.top = document.documentElement.scrollTop+topvar-20+'px';
$('exitbutton').style.left = leftvar;
}


function lightboxaction(cont) {
// create elements
lightdiv = document.createElement("div");
coverdiv = document.createElement("img");
topexit = document.createElement("div");

// give them ID's
lightdiv.setAttribute('id', 'lightdiv');
coverdiv.setAttribute('id', 'coverdiv');
topexit.setAttribute('id', 'exitbutton');

// postion elements
coverdiv.src = 'longbox/cover.jpg';
lightdiv.style.top = document.documentElement.scrollTop+topvar+'px';
lightdiv.style.left = leftvar;

// insert elements
my_div = document.getElementById("top");
document.body.insertBefore(lightdiv, my_div);
document.body.insertBefore(coverdiv, lightdiv);
document.body.insertBefore(topexit, lightdiv);

// fill the exit button
topexit.innerHTML = "<div class='offbutton' onclick='kill()'>CLICK HERE TO CLOSE</div>";
// deal with the gallery
if (param == 'gallery') {
gallerydata = document.createElement("div");
gallerydata.setAttribute('id', 'gallerydata');
gallerydata.setAttribute('class', 'hiddenelement');
document.body.insertBefore(gallerydata, lightdiv);
for (i in loc) {
gd = gallerydata.innerHTML;
gallerydata.innerHTML = gd + '|||' + loc[i];
i++; }
}

// if type is not image, make AJAX request
if(param != "image" && source == "file" && param != "gallery") { ajaxreq(cont); }

// if not, skip AJAX request
else { complete(cont); }
}

// execute the function
lightboxaction(loc);
}

// remove elements
function kill() {
$("lightdiv").remove();
$("coverdiv").remove();
$("exitbutton").remove();
}

function switchimg(img) {
x=new Image;
x.src=img;
exitbox(img);
}

function exitbox(imga) {
$('boximg').src = imga; 
// set the position of the exit button dynamically
$('exitbutton').style.width = "250px"; //$('lightdiv').offsetWidth+'px';
$('exitbutton').style.position = "absolute";
$('exitbutton').style.top = document.documentElement.scrollTop+topvar-20+'px';
$('exitbutton').style.left = leftvar; }

// When the page loads, add the lightbox function to onclick on all links 
// with the rel attribute of "lightbox"
// The HREF attribute becomes the location.
function setLinks() {
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++){
var anchor = anchors[i];
if(anchor.getAttribute("rel") == "lightbox"){
linkHREF = anchor.getAttribute("href");
anchor.setAttribute("href", "javascript:lightbox({location: '"+linkHREF+"'})"); } } }

window.onload = function() {
setLinks(); }