/*
 * NEWS FEEDS
 * 
 */
var loadNewsFeed = function(numOfItems) {
	var feedUrl = 'news_feed.jsp';
    jQuery.getFeed({
        url: feedUrl,
        success: function(feed) {
			//build up html
			var html = '';
			for (var i = 0; i < (numOfItems || feed.items.length); i++) {
				var item = feed.items[i];
				//set date
				var _date = item.updated.slice(5,16);
				//replace month
				switch (_date.slice(3,6)) {
					case 'Jan': _date = _date.replace('Jan','January');
					break;
					case 'Feb': _date = _date.replace('Feb','February');
					break;
					case 'Mar': _date = _date.replace('Mar','March');
					break;
					case 'Apr': _date = _date.replace('Apr','April');
					break;
					case 'Jun': _date = _date.replace('Jun','June');
					break;
					case 'Jul': _date = _date.replace('Jul','July');
					break;
					case 'Aug': _date = _date.replace('Aug','August');
					break;
					case 'Sep': _date = _date.replace('Sep','September');
					break;
					case 'Oct': _date = _date.replace('Oct','October');
					break;
					case 'Nov': _date = _date.replace('Nov','November');
					break;
					case 'Dec': _date = _date.replace('Dec','December');
					break;
				}
				//remove any leading zero
				if(_date.charAt(0) == '0') {
					_date = _date.slice(1);
				}
				//pull it together
				html += '<p>';
				html += _date + '<br />';
				html += '<a class="news-link" href="'+ item.link +'">'+ item.title +'</a>';
				html += '</p>';
			}		
			$('#news-articles').html(html);
			$('#news-articles a.news-link').click(function() {
				window.open(this.href);
				return false;
			});
	    }
    });
}

/*
 * VIDEO
 * 
 */
var suppressBlock = $.browser.msie && (parseInt($.browser.version, 10) >= 7);
function openVideo(videoUrl, fragUrl) {
	if(document.getElementById('video-loader') != null) {
		return;
	}
//set size			
var wi = 746;
var he = 426;
if(document.body.className.indexOf('news_features') != -1) {
	wi = 480;
	he = 476;
}
if(videoUrl.indexOf("hip_hop") != -1) {
	wi = 380;
	he = 320;
}
if(videoUrl.indexOf("think_h20") != -1) {
	wi = 300;
	he = 285;
}
	var mt = -(wi/2);
	if(document.body.className.indexOf('news_features') == -1 && jQuery.browser.msie && jQuery.browser.version < 7.0) {
		mt = mt - 2;
	}
	if(!suppressBlock) {
		$.blockUI({message:null});
	}
	var loaderDiv = $('<div id="video-loader"/>').height(100).width(100).css({
		marginLeft: -50,
		marginTop: -50
	}).appendTo($('#body')).vCenter();
	$.get(fragUrl, function(data, responseText){
		loaderDiv.animate({
			width:wi,
			marginLeft: mt,
			height:he,
			marginTop:-(he/2 - 40)
		},500,function(){
			loaderDiv.css('background-image','none');
			loaderDiv.html(data);
			$('img.close-link').click(function(){
				closeVideo();
			});
		$('#video-content a').not('a.transcript-link').bind('click', function () {
			if (!suppressBlock) {
				$.unblockUI();
			}
			return true;
		});	
		$('#video-content a.transcript-link').bind('click', function () {
			if (!suppressBlock) {
				$.unblockUI();
			}
			window.open($(this).attr('href'));
			if (!suppressBlock) {
				$.blockUI();
			}
			return false;
		});			
			//fill the container
			var vUrl = videoUrl;
//set size			
var vW = "420";
var vH = "366";
if(videoUrl.indexOf("hip_hop") != -1) {
	vW = "316";
	vH = "238";
}
if(videoUrl.indexOf("think_h20") != -1) {
	vW = "240";
	vH = "205"; //add 25 for bottom bar
}
			var so = new SWFObject(basePath + "../files_citizenship/flash/GE_Video_Player.swf", "GE_Video_Player", vW, vH, "8", "#000000");
			so.addVariable('videoPath', vUrl);
			var isFF2Mac = navigator.userAgent.toLowerCase().indexOf('mac') != -1 && jQuery.browser.mozilla && !document.getElementsByClassName;
			if(!isFF2Mac) {
				//ff2 mac does not like this
				so.addParam('wmode', 'opaque');
			} 
			so.write('video-holder');	
		});
	});
	$('#video-holder').css('z-index','5000');
}
function closeVideo() {
	if(!suppressBlock) {
		$.unblockUI();
	}
	$('#video-loader').remove();
}

/*
 * AUDIO
 * 
 */
function loadAudio(audioLink) {
	//set path to audio file from link href
	var audioPath = audioLink.href;
	//create and insert container to hold the audio
	var contId = $(audioLink).attr('rel');
	//fill the container
	var so = new SWFObject(basePath + "../files_citizenship/flash/GE_Audio_Player.swf", "GE_Audio_Player", "275", "18", "8", "#ffffff");
	so.addVariable("sndPath", audioPath);
	so.write(contId);
}
function toggleAudio(el) {
	var _blkq = $(el).parents('blockquote')[0];
	var _cla = _blkq.className;
		_cla = _cla.indexOf('hide-audio_') != -1 ? _cla.replace('hide-audio_','show-audio_') : _cla.replace('show-audio_','hide-audio_');
		_blkq.className = _cla;
}

/*
 * PERSPECTIVE boxes
 * 
 */
//hide audio clips and attach toggle behaviors
var enhancePerspectives = function() {
	if($('body').hasClass('news_features')) {
		return;
	}
	//show audio link
	var showAudioLink = '<a href="#" class="show-audio-link">Audio</a>';
	//hide audio link
	var hideAudioLink = '<a href="#" class="hide-audio-link">Hide audio</a>';
	//add links	
	$('.perspective .audio').prepend(hideAudioLink).prev('p').append(showAudioLink);
	//set default view
	$('.perspective').addClass('hide-audio_');
	$('a.show-audio-link').bind('click', function() {
		toggleAudio(this);
		return false;		
	});
	$('a.hide-audio-link').bind('click', function() {
		toggleAudio(this);
		return false;		
	});
}

/*
 * GLOBAL enhancements
 * 
 */
//replace all reset buttons with a image
var replaceFormResets = function(){
	var _newReset = '<input type="image" src="'+ basePath + '../files_citizenship/_images/button-reset.gif" class="image" onclick="this.form.reset();return false;" />';
	$('input.reset-input').replaceWith(_newReset);
}
//enable tab toggles and set default state
var enhanceTabs = function() {
	//set display defaults
	$('#body').addClass("tab1Current_");
	$('.tab-content').not('#tab1-content').hide();
	$('.tab-content h3').addClass('move_');
	//attach event handlers to links
	$('#business-themes-tabs a').click(function() {
		var _currentClass = this.id+'Current_';
		if(!$('#body').hasClass(_currentClass)) {
			//show the target content
			var _contentToShow = '#'+this.id+'-content';
			$(_contentToShow).show();
			//hide other tab content
			$('.tab-content').not(_contentToShow).hide();
			//add class name to body
			$('#body').attr('class','').addClass(_currentClass);
		}
		return false;
	})
}


 
/*
 * PERFORMANCE METRICS
 * 
 */
var enhanceMetricsForm = function(){
	//this is total hack
	var selects = [];
		selects[0] = '<select id="metrics_view_altselect" class="metrics_view_select" name="view_main"><option value="intro">Introduction</option><option value="gen">GE General data</option><option value="ehs">Environment, Health &#38; Safety data</option><option value="eco">Ecomagination data</option><option value="emp">Employee data</option><option value="cp">Communities &#38; Philanthropy data</option><option value="gov">Governance data</option><option value="suppliers">Suppliers</option></select>'
		selects[1] = '<label for="gen_select">Then narrow by:</label><br /><select id="gen_select" class="metrics_view_subselect" name="view_sub"><option value="">- Select -</option><option value="gen_revreg">Revenue by Country/Region</option><option value="gen_revbus">Revenue by Business</option><option value="gen_prof">Profit</option><option value="gen_emp">Employees</option></select>'
		selects[2] = '<label for="emp_select">Then narrow by:</label><br /><select id="emp_select" class="metrics_view_subselect" name="view_sub"><option value="">- Select -</option><option value="emp_all">GE Employees</option><option value="emp_reg">GE Employees by Region</option><option value="emp_femreg">GE Female Employees by Region</option><option value="emp_exec">Executive (U.S. Race/Ethnicity Representation)</option><option value="emp_prof">Professional (U.S. Race/Ethnicity Representation)</option><option value="emp_other">All Other (U.S. Race/Ethnicity Representation)</option><option value="emp_divrace">Total Diverse Representation (Race/Ethnicity)</option></select>'
		selects[3] = '<label for="cp_select">Then narrow by:</label><br /><select id="cp_select" class="metrics_view_subselect" name="view_sub"><option value="">- Select -</option><option value="cp_allcont">All Contributions</option><option value="cp_foundgrants">GE Foundation Grants Breakdown</option><option value="cp_corpcont">GE Corporate &#38; Business Contributions</option><option value="cp_empcont">GE Employee Contributions</option><option value="cp_contpercent">Contributions as % of GE Net Income</option><option value="cp_volunteerism">Volunteer Breakdown</option></select>'
	var input = '<input type="hidden" id="view" name="view" value="" />'
	$('#metrics_view_select').replaceWith(selects[0]);
	var _genel = $('<p />').attr('id','gen_wrap').addClass('sub-select');
		_genel.html(selects[1]);
	var _cpel = $('<p />').attr('id','cp_wrap').addClass('sub-select');
		_cpel.html(selects[3]);
	var _empel = $('<p />').attr('id','emp_wrap').addClass('sub-select');
		_empel.html(selects[2]);
	$('#performance_metrics_form').append(input);
	$('#performance_metrics_form').append(_genel);
	$('#performance_metrics_form').append(_cpel);
	$('#performance_metrics_form').append(_empel);
	//show correct selects on load
	var _sel = $('#metrics_selected').get(0).value;
	if(_sel.indexOf('gen') != -1) {
		$('#metrics_view_altselect option[value=gen]').attr('selected','selected');
		$('#gen_wrap').show();
		$('#gen_select option[value='+_sel+']').attr('selected','selected');
	}  else if (_sel.indexOf('cp') != -1) {
		$('#metrics_view_altselect option[value=cp]').attr('selected','selected');
		$('#cp_wrap').show();
		$('#cp_select option[value='+_sel+']').attr('selected','selected');
	} else if (_sel.indexOf('emp') != -1) {
		$('#metrics_view_altselect option[value=emp]').attr('selected','selected');
		$('#emp_wrap').show();
		$('#emp_select option[value='+_sel+']').attr('selected','selected');
	} else if (_sel.indexOf('ehs') != -1) {
		//do no more
		$('#metrics_view_altselect option[value=ehs]').attr('selected','selected');
	} else if (_sel.indexOf('eco') != -1) {
		//do no more
		$('#metrics_view_altselect option[value=eco]').attr('selected','selected');
		//do no more
	} else if (_sel.indexOf('gov') != -1) {
		//do no more
		$('#metrics_view_altselect option[value=gov]').attr('selected','selected');	
	} else if (_sel.indexOf('intro') != -1) {
		$('#metrics_view_altselect option[value=intro]').attr('selected','selected');
	} else if (_sel.indexOf('suppliers') != -1) {
		$('#metrics_view_altselect option[value=suppliers]').attr('selected','selected');
	}
	//attach events
	$('#metrics_view_altselect').change(function() {
		$('.sub-select').hide();
		if(this.value == 'gen') {
			$('#gen_wrap').show();
		} else if (this.value == 'cp') {
			$('#cp_wrap').show();
		} else if (this.value == 'emp') {
			$('#emp_wrap').show();
		} else {
			this.form.view.value = this.value;
			this.form.submit();
		}
	});
	$('.metrics_view_subselect').change(function() {
		if (this.value == '') {
			return;
		}
		this.form.view.value = this.value;
		this.form.submit();
	});	
	//just to be safe for safari
	$('#performance_metrics_submit').remove();
	$('h3.metric-header').hide();
}

var activateTableRollovers = function() {
	// display row data on row hover
	var left = -375;
	if(navigator.oscpu && document.getElementsByClassName) left -= -1;
	//set up rollover images
	var contWidth = 375;
	var chartImages = [];
	if(document.getElementById('gov-table') != null) {
		contWidth = 400;
		chartImages[0] = '../../files_citizenship/_images/perf_metrics/gov_data_fair_employ.gif'; 
		chartImages[1] = '../../files_citizenship/_images/perf_metrics/gov_data_conflicts_interest.gif';
		chartImages[2] = '../../files_citizenship/_images/perf_metrics/gov_data_intl_trade_cntrls.gif';
		chartImages[3] = '../../files_citizenship/_images/perf_metrics/gov_data_biz_records.gif';
		chartImages[4] = '../../files_citizenship/_images/perf_metrics/gov_data_security.gif';
		chartImages[5] = '../../files_citizenship/_images/perf_metrics/gov_data_ehs.gif';
		chartImages[6] = '../../files_citizenship/_images/perf_metrics/gov_data_routines.gif';
		chartImages[7] = '../../files_citizenship/_images/perf_metrics/gov_dat_regulatory.gif';
		chartImages[8] = '../../files_citizenship/_images/perf_metrics/gov_data_supplier.gif';
		chartImages[9] = '../../files_citizenship/_images/perf_metrics/gov_data_theft.gif';
		chartImages[10] = '../../files_citizenship/_images/perf_metrics/gov_data_privacy.gif';
		chartImages[11] = '../../files_citizenship/_images/perf_metrics/gov_data_controllership.gif';
		chartImages[12] = '../../files_citizenship/_images/perf_metrics/gov_data_improper_pmts.gif';
		chartImages[13] = '../../files_citizenship/_images/perf_metrics/gov_data_govts.gif';
		chartImages[14] = '../../files_citizenship/_images/perf_metrics/gov_data_intellectual_prop.gif';
		chartImages[15] = '../../files_citizenship/_images/perf_metrics/gov_data_competition.gif';
		chartImages[16] = '../../files_citizenship/_images/perf_metrics/gov_data_integrity_concerns.gif';
		chartImages[17] = '../../files_citizenship/_images/perf_metrics/gov_data_concerns_reported.gif';
		chartImages[18] = '../../files_citizenship/_images/perf_metrics/gov_data_discplinary_actions.gif';
		chartImages[19] = '../../files_citizenship/_images/perf_metrics/gov_geo_breakout_asia.gif';
		chartImages[20] = '../../files_citizenship/_images/perf_metrics/gov_geo_breakout_us_canada.gif';
		chartImages[21] = '../../files_citizenship/_images/perf_metrics/gov_geo_breakout_europe.gif';
		chartImages[22] = '../../files_citizenship/_images/perf_metrics/gov_geo_breakout_latin_america.gif';
	}
	if(document.getElementById('ehs-table') != null) {
		chartImages[0] = '../../files_citizenship/_images/perf_metrics/ehs_data_recordable_rate.gif'; 
		chartImages[1] = '../../files_citizenship/_images/perf_metrics/ehs_data_lost_time_rate.gif';
		chartImages[2] = '../../files_citizenship/_images/perf_metrics/ehs_data_VPP_star_sites.gif';
		chartImages[3] = '../../files_citizenship/_images/perf_metrics/ehs_data_injury_illness.gif';
		chartImages[4] = '../../files_citizenship/_images/perf_metrics/ehs_data_air.gif';
		chartImages[5] = '../../files_citizenship/_images/perf_metrics/ehs_data_wastewater.gif';
		chartImages[6] = '../../files_citizenship/_images/perf_metrics/ehs_data_TRI.gif';
		chartImages[7] = '../../files_citizenship/_images/perf_metrics/ehs_data_total_water_use.gif';
		chartImages[8] = '../../files_citizenship/_images/perf_metrics/ehs_data_NCCW.gif';
		chartImages[9] = '../../files_citizenship/_images/perf_metrics/ehs_data_haz_waste.gif';
		chartImages[10] = '../../files_citizenship/_images/perf_metrics/ehs_data_nonhaz_waste.gif';
		chartImages[11] = '../../files_citizenship/_images/perf_metrics/ehs_data_op_ghg_emissions.gif';
		chartImages[12] = '../../files_citizenship/_images/perf_metrics/ehs_data_op_ghg_intensity.gif';
		chartImages[13] = '../../files_citizenship/_images/perf_metrics/ehs_data_op_energy_intensity.gif';
		chartImages[14] = '../../files_citizenship/_images/perf_metrics/ehs_data_op_energy_use.gif';
		chartImages[15] = '../../files_citizenship/_images/perf_metrics/ehs_data_global_paid_penalties.gif';
		chartImages[16] = '../../files_citizenship/_images/perf_metrics/ehs_data_training_US.gif';
		chartImages[17] = '../../files_citizenship/_images/perf_metrics/ehs_data_training_nonUS.gif';
		chartImages[18] = '../../files_citizenship/_images/perf_metrics/ehs_data_inspections_US.gif';
		chartImages[19] = '../../files_citizenship/_images/perf_metrics/ehs_data_inspections_nonUS.gif';
		chartImages[20] = '../../files_citizenship/_images/perf_metrics/ehs_data_spills_releases.gif';		
	}
	if(document.getElementById('eco-table') != null) {
		chartImages[0] = '../../files_citizenship/_images/perf_metrics/eco_data_RandD.gif'; 
		chartImages[1] = '../../files_citizenship/_images/perf_metrics/eco_data_revenue_growth.gif';
	}
	if(document.getElementById('volunteer-table') != null) {
		contWidth = 400;
		chartImages[0] = '../../files_citizenship/_images/perf_metrics/phil_data_volunteer_country_councils.gif'; 
		chartImages[1] = '../../files_citizenship/_images/perf_metrics/phil_data_volunteer_councils.gif';
		chartImages[2] = '../../files_citizenship/_images/perf_metrics/phil_data_volunteer_portal.gif';
	}
	if(document.getElementById('suppliers-table') != null) {
		contWidth = 400;
		chartImages[0] = '../../files_citizenship/_images/perf_metrics/sup_assess_region.gif'; 
		chartImages[1] = '../../files_citizenship/_images/perf_metrics/supp_findings_region.gif';
		chartImages[2] = '../../files_citizenship/_images/perf_metrics/supp_findings_cat.gif';
	}
	//create container and add images
	if(chartImages.length >= 1) {
		var _html = '<div id="chart-images" class="hide_">';
		for (i = 0; i < chartImages.length; i++) {
			if(chartImages[i] != '') {
				_html += '<img src=' + chartImages[i] + ' alt="" id="i' + (i+1) + '" class="boxer-image" />';
			}
		}			
			_html += '</div>';
		$('#body').append(_html);
	}
	$('table.metric-table tr[class*=pop]').boxer({			
		targetEvent: 'hover',
		targetClass: 'hover',
		positionAlign: 'bottomright',
		boxStyle: {
			left: -contWidth,
			top:-1,
			width: contWidth,
			height:125,
			background: '#e3d0e3',
			textAlign:'center'
			,borderTop: 'solid 1px #e8ebef'
		},
		copyElement:'attr-id',
		copyElementAttributeReplace:['r','i'],
		shadowColor:'#ccc'			
	});
	//for suppliers table (needs to be taller)
	$('table.metric-table tr[class*=pop-chart]').boxer({			
		targetEvent: 'hover',
		targetClass: 'hover',
		positionAlign: 'bottomright',
		boxStyle: {
			left: -contWidth,
			top:-2,
			width: contWidth,
			height:170,
			background: '#e3d0e3',
			textAlign:'center'
		},
		copyElement:'attr-id',
		copyElementAttributeReplace:['r','i'],
		shadowColor:'#ccc'			
	});	
}

/*
 * SEARCH ENCODING
 * 
 */
/* 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;
}	
