//++
//	Cufon replacement calls
//++
Cufon.replace('.splash_content .page_title, .splash_content p, .spotlight_title, .spotlight_subtitle, .button_link, .bento h3.container_title');


//++ 
// print links
//++
GE_Main.mapCSSToFn('print_link',
	function(el) {
      $(el).observe("click", function(e) {
         window.print();
		 e.stop();
      });
	}
);


//++ 
// external linking functionality
//++ 
function GE_makeExternalLink(link) {
	if (link.readAttribute('title')) {
		if ( link.readAttribute('title').indexOf('(opens in new window)') < 0 ) { 
			link.writeAttribute('title',link.readAttribute('title')+' (opens in new window)');
		}
	}
	link.observe('click', function(e){
		if (/MSIE/i.test(navigator.userAgent)) {
			this.writeAttribute('target','_blank');
		} else {
			window.open(this.readAttribute('href'), 'ge_external_link');
			e.stop();
		}
	});
}
//open external links in new window 90% of the size of current window
GE_Main.mapCSSToFn('external_link', GE_makeExternalLink);
// (ryan.rowe@frogdesign.com) class the appropriate external links with the bhv_external class for the function above
GE_Main.addInitFn(inspectLinks);
function inspectLinks(elements) {

	//if (document.location.href.match("https://")) return;

	var exclude_list = new Array();
	//add to the exclude list below by pushing new strings onto the array
	exclude_list.push("://www.genewscenter.com/");
	exclude_list.push("://ir.10kwizard.com");
	exclude_list.push("://genewscenter.com/");
	exclude_list.push("://www.ge.com/");
	exclude_list.push("://video.google.com/");

	//also part of the exclude list: download links for videos, audio and pdfs
	//exclude_list.push(".pdf");
	exclude_list.push(".m4v");
	exclude_list.push(".flv");
	exclude_list.push(".mp3");
	exclude_list.push(".mp4");
	exclude_list.push("mailto:");
	var elen = exclude_list.length;
	
	var exception_list = new Array();
	//List to add those cases that we want external inside of the exclude list elements
	exception_list.push(".pdf");
	exception_list.push("ar2007/");
	exception_list.push("ar2008/");
	exception_list.push("ar2009/");
	exception_list.push("ecoreport/");
	exception_list.push("fusion.google.com");
	exception_list.push("add.my.yahoo.com");
	exception_list.push("bloglines.com");
	exception_list.push("citizenship/");
	exception_list.push("sustainablecities/");

	var exlen = exception_list.length;

	var links = elements || document.links;
	var i = links.length;
	while(i--) {
		var link = $(links[i]);

		var url = link.readAttribute("href");
		if( url.match("://") ){ //only if we match the http(s):// absolute link indicator
			var externalFlag = true; //guilty until proven innocent

			var j = elen;
			var k = exlen;
			while(j--) {// Loop looking for excluded links
				if(url.match(exclude_list[j])) {
					var externalFlag = false; //innocent!
					break;
				}

			}

			while(k--) {// Loop looking for exceptions
				if(url.match(exception_list[k])) {
					if (exception_list[k]=="citizenship/" || exception_list[k]=="foundation/") {
							if (url.match(".jsp")){
								var externalFlag = true; //guilty!
								break;
							}
						}
					else {
					var externalFlag = true; //guilty!
					break;
					}
				}
			}
			
			if(externalFlag) {
				GE_makeExternalLink(link);
			}
		}
		else
			{
				if(url.match(".pdf") || url.match("ar2007/") || url.match("/research/") || url.match("ar2008/") || url.match("ar2009/"))
					{
					GE_makeExternalLink(link);
					}
				if (url.match("citizenship/") || url.match("foundation/")) {
						if (url.match(".jsp")){
							GE_makeExternalLink(link);
						}
					}				
			}
	}
}


//++
//	Navigation dropdowns
//++
var GE_Nav = function(item) {
	if (!$('nav_'+item.id)) return;
	this.el = $('nav_'+item.id);
	this.subNav = null;
	this.init(item);
};
GE_Nav.shimEl = null;
GE_Nav.prototype.init = function(item) {

	if (item.children || item.content) {

		//create dropdown sub-navigation items
		this.subNav = new Element('ul').addClassName('navDropdown');
		var self = this;
		if (item.children) {
			//insert current section in to secondary nav tree
			if (this.el.hasClassName('currentItem') && item.name != item.children[0].name) {
				$('ge_secondaryNav').insert({top: new Element('li').addClassName(!this.el.hasClassName('currentParent')?'currentItem':'').insert( new Element('a',{href:item.url}).insert(item.name) ) });
			}
			if (item.children.length > 9) {
				var len = item.children.length;
				var cols = 3;
				var rows = Math.floor(len/cols) + (len%cols?1:0);
				var index = 0;
				for (var r=0; r < cols; r++) {
					var li = new Element('li').addClassName('navGroup');
					var ul = new Element('ul');
					for (var n=0; n < rows; n++) {
						if (index >= len) break;
						ul.insert( new Element('li').insert( new Element('a',{href:(item.children[index].url.substr(0,1)=='/')?GE_baselink+item.children[index].url.substr(1,item.children[index].url.length):item.children[index].url}).insert(item.children[index].name) ) );
						index++;
					}
					li.insert(ul);
					this.subNav.insert(li);
				}
			} else {
				item.children.each(function(subItem){ self.subNav.insert( new Element('li').insert( new Element('a',{href:(subItem.url.substr(0,1)=='/')?GE_baselink+subItem.url.substr(1,subItem.url.length):subItem.url}).insert(subItem.name) ) ); });
			}
		} else if (item.content) {
			this.subNav.insert(item.content);
			if (/MSIE/i.test(navigator.userAgent) && !/MSIE 8.0/i.test(navigator.userAgent)) {
				this.subNav.select('img').each(function(el){ 
					var src = el.readAttribute('src');
					src = GE_baselink + src.substr(src.indexOf('ge_images'),src.length);
					el.writeAttribute('src',src);
				});
				this.subNav.select('a').each(function(el){
					var loc = document.location.href;
					loc = loc.substr(0,loc.lastIndexOf('/')+1);
					var href = el.readAttribute('href');
					if (href.indexOf(loc)<0) return false;
					href = GE_baselink + href.substr(loc.length,href.length);
					el.writeAttribute('href',href);
				});
			} else { 
				this.subNav.select('img').each(function(el){ el.writeAttribute('src',GE_baselink+el.readAttribute('src')); });
				this.subNav.select('a').each(function(el){ if (el.readAttribute('href').indexOf('http://')>=0) return false; el.writeAttribute('href',GE_baselink+el.readAttribute('href')); });
			}
		}
		this.subNav.writeAttribute('id','subnav_'+item.id);
		
		//move left navigation and skip out of hovering LI
		if ($('ge_secondaryNav')) { var leftNav = $('ge_secondaryNav').remove(); $('ge_navigation').insert(leftNav); }
		if ($('ge_skipToContent')) { var skipLink = $('ge_skipToContent').remove(); $('ge_navigation').insert(skipLink); }
		
		//attach event listeners for opening/closing dropdowns
		this.el.observe('mouseover',this.doOver.bindAsEventListener(this) );
		this.el.observe('focus',this.show.bind(this) );
		this.el.observe('mouseout',this.doOut.bindAsEventListener(this) );
		this.el.observe('blur',this.hide.bind(this) );
	}
};
GE_Nav.prototype.doOver = function(e) {
	if (GE_detectMouseEnter(this.el,e)) { this.show(); }
	e.stop();
	return false;
};
GE_Nav.prototype.doOut = function(e) {
	if (GE_detectMouseLeave(this.el,e)) { this.hide(); }
	e.stop();
	return false;
};
GE_Nav.prototype.show = function() {
	this.el.addClassName('activeItem').insert(this.subNav);
	if (this.el.id=='nav_innovation') { Cufon.replace('#subnav_innovation .button_link'); }	
	if (/MSIE/i.test(navigator.userAgent)) {
		var shimEl = this.getShimEl();
		Position.clone(this.subNav, shimEl);
		shimEl.setStyle({zIndex:"1", visibility:"inherit" });
	}
};
GE_Nav.prototype.hide = function() {
	this.el.removeClassName('activeItem');
	this.subNav.remove();
	if (/MSIE/i.test(navigator.userAgent)) { this.getShimEl().setStyle({visibility:"hidden"}); }
};
GE_Nav.prototype.getShimEl = function() {
	if (GE_Nav.shimEl==null) {
		GE_Nav.shimEl = new Element( "iframe", {style:"visibility:hidden;background:white;position:absolute; top:0px; left:0px;", frameBorder:"0", scrolling:"no"});
		GE_Nav.shimEl.src=GE_baselink+"html_view/blank.htm";
		document.body.appendChild(GE_Nav.shimEl);
	}
	return GE_Nav.shimEl;
}
GE_Main.addInitFn(function(){ GE_NavTree.items.each(function(item){new GE_Nav(item);}); });


//++
//	site search results
//++
/* Show and Hide the refine_search options */
function sr_toggle_refine(){
	if(document.getElementById("refine_search").style.display == "none"){
		display_refine();
	}else{
		hide_refine();
	}
}
function display_refine(){
	document.getElementById("refine_search").style.display = "block";
	document.getElementById("search_target").style.display = "none";
	document.getElementById("refine_link").style.backgroundImage = "url(icn_up_arrow_blk.gif)";
}
function hide_refine(){
	document.getElementById("refine_search").style.display = "none";
	document.getElementById("search_target").style.display = "block";
	document.getElementById("refine_link").style.backgroundImage = "url(icn_right_arrow_blk.gif)";
}
/*global search functions*/
var GE_okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_ \"-";
var GE_hexchars = "0123456789ABCDEF";

function GE_toHex(n) {
	return GE_hexchars.charAt(n>>4)+GE_hexchars.charAt(n & 0xF);
}
function GE_encodeURIComponentNew(s) {
	if(s==null || s.length<=0)
		return s;
	var s = GE_utf8(s);
	var c;
	var enc = "";
	for (var i= 0; i<s.length; i++) {
		if (GE_okURIchars.indexOf(s.charAt(i))==-1)
			enc += "%"+GE_toHex(s.charCodeAt(i));
		else
			enc += s.charAt(i);
	}
	return enc;
}
function GE_utf8(wide) {
	var c, s;
	var enc = "";
	var i = 0;
	while(i<wide.length) {
		c= wide.charCodeAt(i++);
		if(c>=0xDC00 && c<0xE000)
			continue;
		if(c>=0xD800 && c<0xDC00) {
			if(i>=wide.length)
				continue;
			s= wide.charCodeAt(i++);
			if(s<0xDC00 || c>=0xDE00)
				continue;
			c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
		}
		if (c<0x80)
			enc += String.fromCharCode(c);
		else if (c<0x800)
			enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
		else if (c<0x10000)
			enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
		else
			enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
	}
	return enc;
}
GE_Main.addInitFn(
	function() {
		var form_id = $('search');
		var form_id2 = $('ge_search');
		if (form_id) {
			var formEl = form_id;
			}
		else {
			var formEl = form_id2;
			}
		
		if (formEl) {
	   		Element.observe(formEl,'submit',function(e){
	   			var tEl = formEl.textToSearch;
	   			new Insertion.After( tEl, '<input type="hidden"/>');
	   			var nEl = tEl.next();
	   			tEl.name = "";
	   			nEl.name = "textToSearch";
	   			nEl.value = GE_encodeURIComponentNew(tEl.value);
	   		}.bind(formEl));
		}
	}
);