﻿//++
//	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");
	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("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/"))
					{
					GE_makeExternalLink(link);
					}
				if (url.match("citizenship/") || url.match("foundation/")) {
						if (url.match(".jsp")){
							GE_makeExternalLink(link);
						}
					}				
			}
	}
}


//++
//	Navigation dropdowns
//++
GE_Nav = function(el) {
	this.el = $(el);
	this.a = null;
	this.subNav = null;
	this.index = null;
	this.init();
}
GE_Nav.itemIndex = 0;;
GE_Nav.shimEl = null;
GE_Nav.searchedCurrentLiEl = false;
GE_Nav.prototype.init = function() {

	var hasChildren = this.el.hasClassName("hasChildren");

	if (hasChildren) {

		this.index = GE_Nav.itemIndex++;

//		if (!/WebKit/i.test(navigator.userAgent)) { // sniff out Safari and disable drop-down menus
			this.el.observe("mouseover", this.doOver.bindAsEventListener(this) );
			this.el.observe("mouseout", this.doOut.bindAsEventListener(this) );
			this.el.observe("focus", this.show.bind(this) );
			this.el.observe("blur", this.hide.bind(this) );
//		} else {
			// with drop-downs disabled, safari has -1px top margin shift because of lack of border.
			// these observes will remove that style on hover.
//			this.el.observe("mouseover",function(){this.el.down().setStyle({backgroundPosition:'50% -24px'});}.bind(this));
//			this.el.observe("mouseout",function(){this.el.down().setStyle({backgroundPosition:'50% 1px'});}.bind(this));
//		}

		this.subNav = this.el.getElementsBySelector(".navSubItem")[0];

		if (this.subNav) {

			this.a = this.el.down().cloneNode(true);
			var li = $(document.createElement('li'));
			li.appendChild(this.a);
			if (this.el.hasClassName('currentItem') && !this.el.hasClassName('currentParent')) { li.addClassName('currentItem') }
			if(this.a.innerHTML != 'Submit an Idea' && this.a.innerHTML != 'Contact Information'){	//don't do this for submit an idea and contact info
				this.subNav.insertBefore(li,this.subNav.down());
			}
			var subNavWrapper = $(document.createElement('div'));
			subNavWrapper.addClassName('currentItem');
			if ($('ge_skipToContent')) subNavWrapper.appendChild($('ge_skipToContent'));
			subNavWrapper.appendChild(this.subNav);

			$("ge_navigation").appendChild(subNavWrapper);
		}
	}
}
GE_Nav.prototype.doOver = function(e) {
	if (GE_detectMouseEnter(this.el,e)) {
		this.show();
	}
	Event.stop(e);
	return false;
}
GE_Nav.prototype.doOut = function(e) {
	if (GE_detectMouseLeave(this.el,e)) {
		this.hide();
	}
	Event.stop(e);
	return false;
}
GE_Nav.prototype.show = function() {
	this.el.addClassName('activeItem');

	var html = ddcontent[this.index];

	new Insertion.After(this.el.down(), html);

	var newEl = this.el.down().next();

	if (this.el.id=="nav_products_services") {
		var elGroupLI = newEl.immediateDescendants();
		// elGroupLI.pop().remove(); //remove this line to add last element (A-Z index) back
		var numLI = elGroupLI.length;
		var rowCount = 8; // Math.floor(numLI/3);
		// var remainder = numLI%3;
		var index = 0;
		for (var r=0;r<3;r++) {
			var elLI = new Element('li',{className:'navGroup'});
			var elGroupUL = new Element('ul');
			// var c = (0<remainder--)?rowCount+1:rowCount;
			for (var n=0;n<rowCount;n++) {
				if (index >= numLI) break;
				elGroupUL.appendChild(elGroupLI[index++]);
			}
			elLI.appendChild(elGroupUL);
			newEl.appendChild(elLI);
		}
	}
	
	// find images in innovation dropdowns and append baselink to them
	if (this.el.id=='nav_innovation') {
		// var imgEls = newEl.select('img');
		// imgEls.each(function(el){
		// 	el.writeAttribute('src',GE_baselink+el.readAttribute('src'));
		// });
		Cufon.replace('#subnav_innovation .button_link');
	}	

	if (/MSIE/i.test(navigator.userAgent)) {
		var shimEl = this.getShimEl();
		Position.clone(newEl, shimEl);
		shimEl.setStyle({zIndex:"1", visibility:"inherit" });
	}
}
GE_Nav.prototype.hide = function() {
	this.el.removeClassName('activeItem');

	var tmp = this.el.down().next();
	if (tmp) { tmp.remove(); }
	// this.el.down().next().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_Nav.shimEl.src=GE_baselink+"html_view/blank.htm";
		//GE_Nav.shimEl.setOpacity(.01);
		document.body.appendChild(GE_Nav.shimEl);
	}
	return GE_Nav.shimEl;
}
GE_Main.mapFnToCSS("navItem",GE_Nav);


//++
//	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));
		}
	}
);