/*
This is a sitewide javascript file which contains functions to:
- open popups
- open external sites in a new window
- clear the search box on focus
- display title text for: pdf links, popups, external links
- add google analytics tracking for external links and multimedia files

enter any global js functions into this file
=============================================================*/
var cb = {
	addEvent: function(elm, evType, fn, useCapture) {
	// cross browser event handling
	if (elm.addEventListener) {
	elm.addEventListener(evType, fn, useCapture);
	return true;
	} else if (elm.attachEvent) {
	var r = elm.attachEvent('on' + evType, fn);
	return r;
	} else {
	elm['on' + evType] = fn;
	}
},


stopLink: function(e) {
	//prevent the default, which is the link opening in the current window
	if (window.event) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}	
	if (e && e.stopPropagation && e.preventDefault) {
		e.stopPropagation();
		e.preventDefault();
	}
},
	/* as above but for Safari */
	cancelClick: function(e) {
		if (document.getElementById) {
			return false;	
		}
		return true;
	},
	
init: function() {
		if (!document.getElementsByTagName || !document.getElementById) return;
		var textfield = document.getElementById('searchBox');
		cb.addEvent(textfield, 'focus', cb.clearText, false);
		//cb.addEvent(textfield, 'blur', cb.clearText, false);		
		/* listen for anchors with rel="external" attribute */
		var anchor_list = document.getElementsByTagName("a");
		for (var i=0; i<anchor_list.length; i++) { // loop through and find all anchor tags 
			var anchor = anchor_list[i];
			// below: check the a tag has an href attribute AND the rel="external" attribute 
			if (anchor.getAttribute("href")) {

                // register with google analytics event tracking
				cb.addEvent(anchor, 'click', cb.registerWithGoogleAnalytics, false);
				
				if (anchor.getAttribute("rel") == "external") {
					cb.addEvent(anchor, 'click', cb.openBlankWindow, false);			

					cb.addEvent(anchor, 'click', cb.stopLink, false);
					
				}

				if (anchor.className && (' ' + anchor.className + ' ').indexOf(' popup ') != -1) {
					cb.addEvent(anchor, 'click', cb.popup, false);
					cb.addEvent(anchor, 'click', cb.stopLink, false);
					anchor.onclick = cb.cancelClick;//for Safari only
				}
			}
			cb.addEvent(anchor, 'mouseover', cb.linkTitle, false);
			
		}


},
displayPageTitle: function() {
	var pageTitle = document.getElementById('pageTitle');
	pageTitle.innerHTML = " > " + document.title;
},
popup: function(e) {
	var el = window.event ? window.event.srcElement : e ? e.target : null;
		if (el.nodeType==3) {
		el = el.parentNode;	// Safari fix
		
	}

	/*
	some hyperlinks have a child element inside them - either a span or img tag.
	In this case the var el, passed into this fuction, relates to that child node and NOT the hyperlink itself.
	The following lines check for this and if necessary set the element to its parent so as to ensure the popup 
	will activate on the hyperlink not any other element.
	*/
	parentEl = el.parentNode;
	if (parentEl.className && (' ' + parentEl.className + ' ').indexOf(' popup ') != -1) {
		el = parentEl;
	} 
	
	var _POPUP_FEATURES = 'location=0,statusbar=0,menubar=0,width=680,height=450,resizable=yes, status=yes, scrollbars=yes';
	
	window.open(el.href, '', _POPUP_FEATURES);
},

/* function to clear the search box when clicked */
clearText: function(e) {
	var el = window.event ? window.event.srcElement : e ? e.target : null;
	
	
	if (el.value != '') {
		el.value = '';
	}
	else {
		el.value = 'Enter query';
	}
},

/* register anchor events with google analytics */
registerWithGoogleAnalytics:  function(e) {

	var el = window.event ? window.event.srcElement : e ? e.target : null;


    // ensure the anchor has a href
	if (el.href != '')
	{
        // track external links using the extlink prefix
		if (el.rel == "external") {
            
			urchinTracker('/extlink/' + el.href);
			//alert('/extlink/' + el.href);

        
		} 
		// track multimedia using the intlink prefix
		else if   (el.href.match(".pdf") || el.href.match(".mp3") || el.href.match(".swf") || el.href.match(".flv")  || el.href.match(".doc")  || el.href.match(".xls")  ) {
			
			urchinTracker('/intlink/' + el.href.replace( 'http://www.organdonation.nhs.uk/',''));
			//alert('/intlink/' + el.href.replace( 'http://www.uktransplant.org.uk/',''));
		}	

	}


},

linkTitle: function(e) {
	var el = window.event ? window.event.srcElement : e ? e.target : null;
	
	if (el.rel == "external") {
	el.title = "external site; opens in new browser window";
	} 
	

}, 

/* function to open a blank window when an external hyperlink is clicked */
openBlankWindow: function(e) {
	var el = window.event ? window.event.srcElement : e ? e.target : null;
	
	window.open(el.href);
},

anchor_list: [] /* for IE??*/
};

cb.addEvent(window, 'load', cb.init, false);
cb.addEvent(window, 'load', cb.displayPageTitle, false);

