// JavaScript Document
var subTextArray = new Array();
var x = 0;
function setUpMenu(){

	/*$.ajax({
		type: "GET",
		url: "/gbm/control/gbm-sub.xml",
		dataType: ($.browser.msie) ? "text" : "xml",
		success: function(data) {
			if (typeof data == "string") {
				xml = new ActiveXObject("Microsoft.XMLDOM");
				xml.async = false;
				xml.loadXML(data);
			} 
			else {
				xml = data;
			}
			x = 0;
			$(xml).find('item').each(function(){
				$(this).find('subtext').each(function(){
					var subtxt = $(this).text();
					subTextArray[x] = subtxt;
					//alert(subtxt);
					x++;
				});
			});						
		},
		complete: function(){
			var liCounter = 0;
			$('#navigation li a').each(function(){
				$(this).attr('href', '#');
				$(this).addClass('subNav');
				oldText = $(this).html();
				$(this).html(oldText+'<br /><span>'+subTextArray[liCounter]+'</span>');
				//alert($(this).html());
				liCounter++;
			});		
		}
	});*/
	$('#navigation li a').each(function(){
		//$(this).attr('href', '#');
		$(this).removeAttr('href');
		$(this).addClass('subNav');		
		//oldText = $(this).html();
		//$(this).html(oldText+'<br /><span>'+subTextArray[liCounter]+'</span>');
		//alert($(this).html());
		//liCounter++;
	});
}

function setUp(){
	crumbVal = document.getElementById('breadcrumbs').innerHTML;
	liCount = document.getElementById('breadcrumbs').getElementsByTagName('li');
	crumbVal = '<div id="crumbs">' +crumbVal+ '</div>'
	if (liCount.length > 1){
		newCrumbs = document.getElementById('r1c1r1c2').innerHTML;
		document.getElementById('r1c1r1c2').innerHTML = crumbVal + newCrumbs;
	}
	//Breadcrumb slicing
	if ($('#crumbs ul li').length > 2){
	 //if ($('#crumbs ul li.last').text().length > 30){
		 $('#crumbs ul li a').each(function(index) {
			bcLimit = $(this).text();
			if ($(this).text().length > 15){
				$(this).html(bcLimit.substr(0, 10)+'...')
			}
		 });
		 //if ($('#crumbs ul li.last').text().length > 40){
			//$('#crumbs ul li.last').each(function(index) {
				//$(this).html($(this).text().substr(0, 40)+'...');
			//});
		 //}
	  //}
	}
	//Tab listing
	if ($('ul#tabslist').length > 0){
		$('ul#tabslist li').each(function(index) {
			charCount = $(this).find('a span').text();
			wordCount = $(this).find('a span').text().split(' ').length;
			if (charCount.length > 30){
				tabLine1 = '';
				tabLine2 = '';
				x = 0;
				yCount = 0;
				tempCount = 0;
				counter = charCount.length/2;
				for (y=0;x<1;y++){
					charCount = charCount.toString();
					tempCount = (charCount.length/2)+yCount;
					tempTab = charCount.substr(tempCount);
					if (charCount.substr(tempCount,1) != " "){
						yCount++;
					} else {
						x = 1;
						tabLine2 = '<br />'+tempTab;
						tabLine1 = charCount.substr(0, tempCount);
					}
				}
				tabLine1 = tabLine1+tabLine2;
				$(this).find('a span').html(tabLine1);
				$(this).find('a').addClass('twoLines');
			}
		});
	}
}

function createCSSJS(cssJSName, ntype){
 var headID = document.getElementsByTagName("head")[0];         
 var cssNode = document.createElement('link');
 var jsNode = document.createElement('script');
 if (ntype == 'css'){
	 cssNode.type = 'text/css';
	 cssNode.rel = 'stylesheet';
	 cssNode.href = cssJSName;
	 cssNode.media = 'screen';
	 headID.appendChild(cssNode);
 } else if (ntype == 'js'){
	 jsNode.language = 'javaScript';
	 jsNode.type = 'text/javascript';
	 jsNode.src = cssJSName;
	 headID.appendChild(jsNode);
 }
}
function clearjscssfile(filename, filetype){
 var targetelement1=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
 var targetattr1=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
 var allsuspects1=document.getElementsByTagName(targetelement1)
 for (var i=allsuspects1.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
  if (allsuspects1[i] && allsuspects1[i].getAttribute(targetattr1)!=null)
   allsuspects1[i].parentNode.removeChild(allsuspects1[i]) //remove element by calling parentNode.removeChild()
 }
}

/*-- Global Research functions --*/
function emailAdd(globResForm){
	var blank_pattern=/^[ \t\n]*$/;
	if (blank_pattern.test(globResForm.rs.value) || (globResForm.rs.value == 'External e-mail address') ){
		alert("Please input your e-mail address");
		globResForm.rs.focus();
		//return false;
	} else {
		globResForm.submit();
	}

	//return true;
}
function fillSearch(a){

}
function clearSearch(a){

}
function clearSearchField(sq){
	if(sq.value == 'External e-mail address'){
	   sq.value='';
	}
}

function fillSearchField(sq){
	if(sq.value == ''){
	   sq.value='External e-mail address';
	}
}

var docTitle = '';
$(document).ready(function() {
	docTitle = document.title;

	/*Sitemap functions*/
	$('li.open ul').hide();
	$('li.open span a.open').attr('class','close');
	$('li.open').attr('class','close');
	$('div#siteMap ul li').first().attr('class','open');
	$('div#siteMap ul li span a.close').first().attr('class','open');
	$('div#siteMap ul li ul').first().show();
	
	
	/*Share button*/
	currentText = $('.shareLink').parent().html();
	shareDiv = '<div class="shareIcons">'+
	'<a href="'+'http://www.facebook.com/share.php?u='+gotoURL+'" class="fbook" onmouseover="stop_Int()" onmouseout="addthis_close()" target="_blank" title="Share on Facebook">Facebook</a>'+
	'<a href="'+'http://www.linkedin.com/shareArticle?mini=true&url='+gotoURL+'&title='+gotoTitle+'" class="linked" onmouseover="stop_Int()" onmouseout="addthis_close()" target="_blank" title="Share on LinkedIn">LinkedIn</a>'+
	'<a href="'+'http://www.twitter.com/home/?status='+gotoTitle+'+'+gotoURL+'" class="tweet" onmouseover="stop_Int()" onmouseout="addthis_close()" target="_blank" title="Tweet this">Twitter</a>'+
	'<a href="'+'https://m.google.com/app/plus/x/?v=compose&content='+gotoTitle+' - '+gotoURL+'" class="gplus" onmouseover="stop_Int()" onmouseout="addthis_close()" target="_blank" title="Share on Google+">Google</a>'+
	'<a href="/gbm/rss-insights" class="rss" onmouseover="stop_Int()" onmouseout="addthis_close()" target="_blank" title="Subscribe to RSS">RSS 2.0</a>'+
	'<a href="javascript:void(0)" class="mail" onmouseover="stop_Int()" onmouseout="addthis_close()" target="_self" title="E-mail this link">E-mail</a>'+
	'</div>';
	$('.shareLink').parent().append(shareDiv);
	$('.mail').attr('target','_self');
	$('.sendEmail').attr('onmouseover','stop_Int()');
	$('.sendEmail').attr('onmouseout','addthis_close()');
	emailDiv = '<div class="sendEmail">'+
	'<input name="mailbox"  value="e-mail address" class="mailbox" onmouseover="stop_Int()" onmouseout="addthis_close()" onkeyup="stop_Int()" onkeydown="stop_Int()" onkeypress="stop_Int()" maxlength="150" />'+
	'<a href="javascript:void(0)" onmouseover="stop_Int()" onmouseout="addthis_close()" title="Click to open your default mail client">Send</a>'+
	'<p>* This will open your default e-mail client</p>'+
	'<p class="errorNote">&nbsp;</p>'+
	'</div>';
	$('.mail').parent().append(emailDiv)
	$('.mail').click(function() {
		$('.sendEmail').show();
		$(this).blur();
		return false;
	});
	$('.mailbox').click(function() {
		$(this).attr('value','');
	});
	$('.sendEmail a').click(function() {
		if ( $.browser.msie ) {
			mailAdd = $(this).prev().prev('input.mailbox').val();
		} else {
			mailAdd = $(this).prev('input.mailbox').val();
		}
		if ((mailAdd=="") || (mailAdd=="e-mail address") || (mailAdd==null)){
			$('.errorNote').html('Please enter a valid address');
		} else {
			if (echeck(mailAdd)!=false){
				//$(this).attr('href','mailto:'+mailAdd+'?subject='+gotoTitle+'&body='+gotoURL);
				document.location.href='mailto:'+mailAdd+'?subject='+encodeURIComponent(gotoTitle)+'&body='+encodeURIComponent(gotoURL);
			}
		}
		return false;
	});
	$('.shareIcons').hide();
	$('.sendEmail').hide();
	
	/*Overlay fix for IE*/
	if ($.browser.msie) {
		$('.overlaylink').each(function(){
			if(($.trim(($(this).html()))) != "&nbsp;"){
				$(this).attr('style','background:none;');
				$(this).append('<span class="overlaylinkFix">&nbsp;</span>');
			}
		});
		$('.downloadlink').each(function(){
			if(($.trim(($(this).html()))) != "&nbsp;"){
				$(this).attr('style','background:none;');
				$(this).append('<span class="downloadlink">&nbsp;</span>');
			}
		});
	}
	
	if($(".pagewithembedvideo").length > 0){
		$('div.shareIcons').css('bottom','32px');
		$('.sendEmail').css({'margin-top':'0','left':'-213px','top':'102px'});
	}
});
function siteMapToggle(target){
	if (document.getElementById(target+'Toggle').style.display == 'none'){
		document.getElementById(target+'Toggle').style.display = 'block';
		document.getElementById(target).className = 'open';
	} else {
		document.getElementById(target+'Toggle').style.display = 'none';
		document.getElementById(target).className = 'close';
	}
}
function switchTab(tab, loc){
	tabNames = document.getElementById('tabslist');
	tabClass = tabNames.getElementsByTagName('li');
	tabLinkCount = tabNames.getElementsByTagName('a');
	for (x=0;x<=tabLinkCount.length;x++){
		if (tabLinkCount[x].id==tab){
			tabIndex = x;
			break
		}
	}
	$('.tabcontentarea').hide();
	$('#tabslist li').removeClass('current');
	$('.tabcontentarea').eq(tabIndex).show();
	$('#tabslist li').eq(tabIndex).addClass('current');
	window.location.hash = '#'+loc;
	document.title = docTitle;
}
var gotoURL = '';
var gotoTitle = '';
var intval= '';

/*hover function*/
function addthis_open(target, a, toGo, title){
	stop_Int();
	var rowCount = $('tr.entry').length;
	if(rowCount > 0){
		if($('tr.entry').index($(target).parents('tr.entry')) == rowCount-1){
			$(target).siblings('div.shareIcons').css('bottom','32px');
		}
	}
	
	$('.shareIcons').show();
	if (toGo == "[URL]"){
		gotoURL = document.location;
	} else {
		gotoURL = toGo;
	}
	if (title == "[TITLE]"){
		gotoTitle = document.title;
	} else {
		gotoTitle = title;
	}
	//$('.shareIcons a').attr('target','_blank');
	$('.fbook').attr('href', 'http://www.facebook.com/share.php?u='+encodeURIComponent(gotoURL));
	$('.linked').attr('href', 'http://www.linkedin.com/shareArticle?mini=true&url='+encodeURIComponent(gotoURL)+'&title='+encodeURIComponent(gotoTitle));
	$('.tweet').attr('href', 'http://www.twitter.com/home/?status='+encodeURIComponent(gotoTitle)+'+'+encodeURIComponent(gotoURL));
	$('.gplus').attr('href', 'https://m.google.com/app/plus/x/?v=compose&content='+encodeURIComponent(gotoTitle)+' - '+encodeURIComponent(gotoURL));
}

function addthis_close(){
	if(intval==""){
		intval=window.setInterval("hideShare()",1000);
	} else {
		stop_Int();
	}
}
/*click function*/
function addthis_sendto(){
	return false;
}
function hideShare(){
	$('.sendEmail input').val('e-mail address');
	$('.errorNote').html('');
	$('.sendEmail').hide();
	$('.shareIcons').hide();
}
function stop_Int(){
	if(intval!=""){
		window.clearInterval(intval);
		intval="";
	}
}
function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   $('.errorNote').html('Please enter a valid address');
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   $('.errorNote').html('Please enter a valid address');
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		$('.errorNote').html('Please enter a valid address');
		return false
	}
	if (str.indexOf(at,(lat+1))!=-1){
		$('.errorNote').html('Please enter a valid address');
		return false
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		$('.errorNote').html('Please enter a valid address');
		return false
	}
	if (str.indexOf(dot,(lat+2))==-1){
		$('.errorNote').html('Please enter a valid address');
		return false
	}
	if (str.indexOf(" ")!=-1){
		$('.errorNote').html('Please enter a valid address');
		return false
	}
	 return true					
}

$(window).load(function(){	
	if(top != self || $(".pagewithembedvideo").length > 0){
		$('div.shareIcons').css('bottom','32px');
		$('.sendEmail').css({'margin-top':'0','left':'-213px','top':'102px'});
	}
});

function setTags(a, id, hitPath, campPath){
	hostVal = window.location.hostname;
	if ($(a).parent().hasClass('current') != true){
		dcsMultiTrack('DCS.dcssip', hostVal,'DCS.dcsuri', hitPath,'WT.ac', id);
	}
}
