// 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++;
			});		
		}
	});*/
	$(window).load(function(){
	$('#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 > 3){
		newCrumbs = document.getElementById('r1c1r1c2').innerHTML;
		document.getElementById('r1c1r1c2').innerHTML = crumbVal + newCrumbs;
	}
}

function setUpNew(){
	//crumbVal = document.getElementById('breadcrumbs').innerHTML;
//	liCount = document.getElementById('breadcrumbs').getElementsByTagName('li');
//	crumbVal = '<div id="crumbs">' +crumbVal+ '</div>'
//	if (liCount.length > 2){
//		newCrumbs = document.getElementById('r1c1r1c2').innerHTML;
//		document.getElementById('r1c1r1c2').innerHTML = crumbVal + newCrumbs;
//	}
}


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()
 }
}

$(document).ready(function() {
	$('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="Email 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();
	});
	$('.mailbox').click(function() {
		$(this).attr('value','');
	});
	$('.sendEmail a').click(function() {
		mailAdd = $(this).prev().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);
			}
		}
	});
	$('.shareIcons').hide();
	$('.sendEmail').hide();
	
	/*Overlay fix for IE*/
	
});
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';
	}
}

/*Share link functions*/
var gotoURL = '';
var gotoTitle = '';
var intval= '';

/*hover function*/
function addthis_open(target, a, toGo, title){
	stop_Int();
	$('.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='+gotoURL);
	$('.linked').attr('href', 'http://www.linkedin.com/shareArticle?mini=true&url='+gotoURL+'&title='+gotoTitle);
	$('.tweet').attr('href', 'http://www.twitter.com/home/?status='+gotoTitle+'+'+gotoURL);
	$('.gplus').attr('href', 'https://m.google.com/app/plus/x/?v=compose&content='+gotoTitle+' - '+gotoURL);
}

function addthis_close(){
	if(intval==""){
		intval=window.setInterval("hideShare()",1000);
	} else {
		stop_Int();
	}
}
/*click function*/
function addthis_sendto(){
	
}
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					
}
/*Share link functions*/
