
// Title: 	Global Javascript Functions for BCScene.ca 
// Author:	jay.west
// Contact:	creative@jaywest.com
// Updated:	March 13, 2009 


// automatically create photo captions and credits from img TITLE and CAPTION attributes
// works in Windows IE5.01, IE5.5 IE6, NN6, NN7, Firefox
// works in Mac IE 5, Safari
// also requires CSS to style captions

// will only be applied to images in the ID=contentColumn
//<div class="photo"><img src="photo.jpg" width="160" height="160" alt="caption [credit]" /></div>


// Create valid photo captions and credits from img attributes


function extractImageTitles() {

	var images = document.getElementById("content_sub").getElementsByTagName('img'); // just get images in the content_main div
	for (var i = 0; i < images.length; i++) {
	
		// check to see if this image has a parent with a class attribute
		var strImage = images[i].parentNode.getAttribute('class');  // for Mozilla
		var strImage = images[i].parentNode.className;  // for IE
		// only act on images inside p with a class of "photo"
		if (strImage == 'photo') {

			// get the width attribute of the image and add it to the parent class
			//var strWidth = images[i].getAttribute('width');
			//images[i].parentNode.style.width = strWidth+"px";
			//images[i].parentNode.style.width = "468px";

			// separate the caption from the credit within the images ALT tag
			// assumes the caption comes first followed by the credit in square brackets
			var strAltTag = images[i].getAttribute('alt');

			// only act on images with an alt tag
			if (strAltTag != null) {
				var intCreditStart = (strAltTag.indexOf("["));
					if (intCreditStart < 0) {    //if there is no credit start
						var intCreditStart = 9999;  //extend the string selection to include nothing
					}
				var intCreditEnd = (strAltTag.lastIndexOf("]"));
					if (intCreditEnd < 0) {       //if there is no caption end
						var intCreditEnd = 9999;  //extend the string selection to include everything
					}
				var strCredit = strAltTag.substring(intCreditStart+1, intCreditEnd);
				var strCaption = strAltTag.substring(0,intCreditStart);
				
				// create photo credit div
				if ((strCredit) && (strCredit != '')) {
					var divCredit = document.createElement('div');
					divCredit.className = 'credit';
					divCredit.appendChild(document.createTextNode(strCredit));
				}		
		
				// create photo caption div
				if ((strCaption) && (strCaption != '')) {
					var divCaption = document.createElement('div');
					divCaption.className = 'caption';
					divCaption.appendChild(document.createTextNode(strCaption));
				}
				
				// wrap caption/credit for transparent background
				var capWrapper = document.createElement('span');
				images[i].parentNode.appendChild(capWrapper); // create a wrapper for the caption and credit
				if (strCredit != '') {  // check to see if a credit exists
					capWrapper.appendChild(divCredit);
				}
				if (strCaption != '') { // check to see if a caption exists
					capWrapper.appendChild(divCaption);	
				}
			}
		}
	}
}
//window.onload = extractImageTitles;



// Run the functions once the page has loaded: 

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