// Created by jaywest - creative@jaywest.com
// December 19, 2009
// http://woorkup.com/2009/11/19/jquery-lesson-series-how-to-implement-your-first-plugin/

// 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>



(function($){
    $.fn.captions = function(){
	return this.each(function(){
		var $e = $(this);
		
		//alert(this);
		
		    


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

			// get the width attribute of the image and add it to the parent class
			var strHeight = $e.attr('height');
			//$e[i].parentNode.style.height = strHeight+"px";
			$e.parent().css("height", strHeight+"px");
			var strWidth = $e.attr('width');
			//$e[i].parentNode.style.width = strWidth+"px";
			//$e.parent.width = strWidth+"px";
			$e.parent().css("width", strWidth+"px");
			//images[i].parentNode.style.width = "468px";
			
			//var divPhotoWrap = document.createElement('div');
			//divPhotoWrap.className = 'photo_wrap';
			//divPhotoWrap.style.width = strWidth+"px";
			//images[i].parentNode.insertBefore(images,divPhotoWrap)

			//divPhoto.appendChild(divPhoto);

			// 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 = $e.attr('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');
				$e[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);	
				}
			}
		//}
	}

		    
		    
		});        
	};
})(jQuery);