
//Title: 	Global Javascript Functions for nac-cna.ca 
//Author:	jay.west
//Contact:	jaywest@nac-cna.ca
//Updated:	May 26, 2010   


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




// use jQuery to open external links in a new window


$(document).ready(function() {
						   
	// open external links in a new browser window
	$("a[href^='http']").attr('target','_blank');

	// open pdf links in a new browser window, add pdf icon
	$("a[href$='.pdf']").attr('target','_blank').attr('class','pdf');

	// open external links in a new browser window
	$("a[href^='mailto']").attr('class','email');




	// clears the earch field contents and appends a class on focus
	$("input.txtKeywords").focus(function() {
		$(this).addClass("fieldFocus");
		$(this).val("")
	});
	$("input.txtKeywords").blur(function() {
		$(this).removeClass("fieldFocus");
		$(this).val("Search")
	});



	// open acordion hover for #nav_section
	$("#nav_section>ul").accordion({
		collapsible: true,
		active: false,
		header: '.menu',
		autoHeight: false,
		navigation: true
	});


	// Add .last class to all lists
	//$('#oitp ul li:last').addClass('last');	
	
	
});





// tipsy

(function($) {
    $.fn.tipsy = function(opts) {

        opts = $.extend({fade: false, gravity: 'n'}, opts || {});
        var tip = null, cancelHide = false;

        this.hover(function() {
            
            $.data(this, 'cancel.tipsy', true);

            var tip = $.data(this, 'active.tipsy');
            if (!tip) {
                tip = $('<div class="tipsy"><div class="tipsy-inner">' + $(this).attr('title') + '</div></div>');
                tip.css({position: 'absolute', zIndex: 100000});
                $(this).attr('title', '');
                $.data(this, 'active.tipsy', tip);
            }
            
            var pos = $.extend({}, $(this).offset(), {width: this.offsetWidth, height: this.offsetHeight});
            tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).appendTo(document.body);
            var actualWidth = tip[0].offsetWidth, actualHeight = tip[0].offsetHeight;
            
            switch (opts.gravity.charAt(0)) {
                case 'n':
                    tip.css({top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tipsy-north');
                    break;
                case 's':
                    tip.css({top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tipsy-south');
                    break;
                case 'e':
                    tip.css({top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}).addClass('tipsy-east');
                    break;
                case 'w':
                    tip.css({top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}).addClass('tipsy-west');
                    break;
            }

            if (opts.fade) {
                tip.css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: 1});
            } else {
                tip.css({visibility: 'visible'});
            }

        }, function() {
            $.data(this, 'cancel.tipsy', false);
            var self = this;
            setTimeout(function() {
                if ($.data(this, 'cancel.tipsy')) return;
                var tip = $.data(self, 'active.tipsy');
                if (opts.fade) {
                    tip.stop().fadeOut(function() { $(this).remove(); });
                } else {
                    tip.remove();
                }
            }, 100);
            
        });

    };
})(jQuery);


