// set this variable to "y" in pages to fix top margin of the footer to make it stick to the browser window bottom part
//var _shallChangeFooterTopMargin = "n";

// function to set the style class of the Top main navigation

function setTopNavClass(){
	var _docURL = document.location.href;	
	if(_docURL.indexOf('default.aspx') != -1){	
		document.getElementById('link1').className = "navHomeSel";
	}
	else if(_docURL.indexOf('theaward.aspx') != -1){
		document.getElementById('link2').className = "navAwardSel";
	}
	else if(_docURL.indexOf('alstandings.aspx') != -1){
		document.getElementById('link3').className = "navALSel";
	}
	else if(_docURL.indexOf('nlstandings.aspx') != -1){
		document.getElementById('link4').className = "navNLSel";
	}
	else if(_docURL.indexOf('pastwinners.aspx') != -1){	
		document.getElementById('link5').className = "navPWinnersSel";
	}
	else if(_docURL.indexOf('rmnews.aspx') != -1 || _docURL.indexOf('archivenews.aspx') != -1){	
		document.getElementById('link6').className = "navInNewsSel";
	}
	else if(_docURL.indexOf('sitemap.aspx') != -1 || _docURL.indexOf('privacy.aspx') != -1 || _docURL.indexOf('legal.aspx') != -1 || _docURL.indexOf('pitcherdetail.aspx') != -1){
	}
	else{
		document.getElementById('link1').className = "navHomeSel";
	}
	chkBreadCrumb();
	
	// sets the background Image of the page basing on the postion of the footer
	
	var _footerPos = "";
	var whichFooter = "";
	if(document.getElementById('privacyLeftBottomPanelcontent')){
		whichFooter = document.getElementById('privacyLeftBottomPanelcontent');
	}
	else if(document.getElementById('leftBottomPanelcontent')){
		whichFooter = document.getElementById('leftBottomPanelcontent');
	}
	/*else if(document.getElementById('leftBottomPanelcontent1')){
		whichFooter = document.getElementById('leftBottomPanelcontent1');
	}*/
	if(whichFooter != ""){
		if(navigator.userAgent.indexOf("Safari") != -1 || navigator.userAgent.indexOf("Macintosh") != -1){
			var ltCoords = getCoords(whichFooter);
			_footerPos = ltCoords.y;
		}
		else{
			var _XQdrs = whichFooter.getBoundingClientRect();
			_footerPos = _XQdrs.bottom;
		}
	}
	
	if(parseInt(_footerPos) < 1200 && _footerPos != ""){
		setBgExtension();
	}
	
	/*if(_shallChangeFooterTopMargin == "y"){
		var _scrnHt = parseInt(screen.availHeight)-60;
		var _docHt = parseInt(document.documentElement.offsetHeight);
		alert(_docHt+' - '+_scrnHt);
		if(_docHt < _scrnHt){
			var _chg = _scrnHt - _docHt;			
			whichFooter.style.margin = _chg+"px 0px 0px 0px"; 
		}
	}*/
	
}


function setBgExtension(){ //sets DIV "bgExtension" height
	document.getElementById('bgExtension').style.background = "none";		
	document.body.style.background = "url(images/site_bg.jpg) no-repeat center top";
	document.body.style.backgroundColor = "#1E0041";
}

//Function to set the style class of the topnav on hover events

var prevClass = "";

function chgNavClass(_obj,str){   
	var chkClass = _obj.className;
	if(str == "n" && prevClass != "selected"){
		if(chkClass.indexOf("Sel") != -1){
			var ind = chkClass.indexOf("Sel");
			var classStr = chkClass.substring(0, ind);
			_obj.className = classStr;
			prevClass = "";
		}
	}
	else{
		if(str == "y"){
			if(chkClass.indexOf("Sel") != -1){
						prevClass = "selected";
						return false;
			}
			else{_obj.className = chkClass+"Sel"; prevClass = "";}
		}
	}
}

//function to restructure Dynamic data into a Table in the HOMEPAGE

function restructureTable(tableID){
	var sRow = document.getElementById(tableID).rows;
	document.write("<table border='0' cellpadding='8' cellspacing='0' class='leagueTable'>");
	document.write("<tr><th width='75'>POINTS</th>");
	document.write("<th align='left'>NAME</th></tr>");
	
	for(i=1; i<sRow.length; i++){
		var sTD = sRow[i].getElementsByTagName('TD');
		if(i==1){
			document.write("<tr><td><div class='pts1'>"+sTD[0].innerHTML+"</div></td><td class='plyrName1'>"+sTD[1].innerHTML+"<div class='teamName'>"+sTD[2].innerHTML+"</div></td></tr>");
		}
		else{
			document.write("<tr><td><div class='pts'>"+sTD[0].innerHTML+"</div></td><td class='plyrName'>"+sTD[1].innerHTML+"<div class='teamName'>"+sTD[2].innerHTML+"</div></td></tr>");
        }
	}
	
	document.write("</table>");
}



// popup window

function popWindow(){
	window.open('email.aspx','NewsSignup','width=490,height=480,left=100,top=100,scrollbars=0');
}

// sample function to display the thankyou page in the Signup page (for news)

function submitEmailForm(){
	if(document.getElementById('formSubmission')){
		document.getElementById('formSubmission').style.display = "none";
	}
	if(document.getElementById('thanksMesg')){
		document.getElementById('thanksMesg').style.display = "block";
	}
}

// function for Homepage Quiz section

function getCorrectAnswer(){
	var radioGrp = document.forms[0].rdoQuizOptionList;	
	var _correctAnswer = document.getElementById('lblanswer').innerHTML;
	var _fldset = document.getElementById('weeklyQuiz');
	var _successMesg = document.getElementById('quizSuccessMesg');
	var y = _fldset.getElementsByTagName("LABEL");

	var radioSelected;

	for(i=0; i<radioGrp.length; i++){
		if(radioGrp[i].checked == true){
			radioSelected = i;
			break;
		}
		else{radioSelected = "noSelection";}
	}
	
	if(radioSelected == "noSelection"){
		alert("Please select an option");
		return;
	}
	else{
		if(y[radioSelected].innerHTML == _correctAnswer){
			_fldset.style.display = "none";
			_successMesg.style.display = "block";
			_successMesg.innerHTML = "<strong>Correct: </strong>"+_correctAnswer;
			//alert('Correct:'+_correctAnswer);
		}
		else{
			_fldset.style.display = "none";
			_successMesg.style.display = "block";
			_successMesg.innerHTML = "<strong>Incorrect: </strong>"+_correctAnswer;
			//alert('Incorrect:'+_correctAnswer);
		}
		radioGrp[radioSelected].checked = false; // uncheck the selected option
	}
}


// used in AL and NL standings' pages to reformat/style the first 2 rows

function setALStndgResultRowStyles(whichTable){
	_whichTable = document.getElementById(whichTable);
	var newRow = _whichTable.tBodies[0].rows[1].cloneNode(true);
	var _firstRow = _whichTable.tBodies[0].rows[1];	
	_firstRow.parentNode.insertBefore(newRow,_firstRow);
	var _firstRowTD = newRow.getElementsByTagName("TD");
	
	var gridResultsRow = document.getElementById(whichTable).rows;
	var firstResultRow = gridResultsRow[2].getElementsByTagName("TD");
	
	var _TDCount = _firstRowTD.length;
	for(i=0; i<_TDCount; i++){
		_firstRowTD[i].innerHTML = "";
		_firstRowTD[i].style.height="5px";
		_firstRowTD[i].style.padding="0px";
		
		if(whichTable != "dgGames"){
			firstResultRow[i].style.padding = "0px";
			var wBgColor = firstResultRow[i].innerHTML;
			firstResultRow[i].innerHTML = "<div class='resultsFirstRow'>"+wBgColor+"</div>";
			if(i == 2){
				firstResultRow[i].innerHTML = "<div class='resultsFirstRowCell2'>"+wBgColor+"</div>";
			}
			// removes all the content in the last cell of the table
			if(i == _TDCount-1){
				firstResultRow[i].innerHTML = "";
			}
		}
	}
}

// function to display the NUMBER on the helmet in Pitcher Details page
function setHelmetNum(){
	var _span = document.getElementById("helmetNum").getElementsByTagName("SPAN");
	var _num = _span[0].innerHTML;
	document.getElementById("helmetNum").style.backgroundImage = "url(images/numbers/"+_num+".gif)";							
}

//link navigation in the breadcrumb (alstandings and nlstandings pages)
var _ifLeague = "";
function chkBreadCrumb(){	
	if(document.getElementById('lblLeagueType')){
		var anchorObj = document.getElementById('lblLeagueType').innerHTML;	
		anchorObj = anchorObj.toLowerCase();
		if(anchorObj.indexOf("american league")!= -1){
			_ifLeague = 'a';
			document.getElementById('link3').className = "navALSel";
		}
		if(anchorObj.indexOf("national league")!= -1){
			_ifLeague = 'n';
			document.getElementById('link4').className = "navNLSel";
		}
	}
}

function chkLeague(){
	chkBreadCrumb();
	if(_ifLeague == 'a'){
		document.location.href = "alstandings.aspx";
	}
	else if(_ifLeague == 'n'){
		document.location.href = "nlstandings.aspx";
	}
}


// function to set the News articles flow in the priority order with the Right Panel, latest articles - used in rmnews.aspx page

function setNewsArticlesFlow(articleNum,isArchive){
	if(isArchive == "n"){
		document.getElementById('newsArticleHead0').innerHTML = document.getElementById('dlLatest__ctl'+articleNum+'_lblHead').innerHTML;
		document.getElementById('newsArticleContent0').innerHTML = document.getElementById('dlLatest__ctl'+articleNum+'_lblBody').innerHTML;
		document.getElementById('newsArticleDate').innerHTML = document.getElementById('dlLatest__ctl'+articleNum+'_lblPubDate').innerHTML;		
	}else{
		document.getElementById('newsArticleHead0').innerHTML = document.getElementById('lblHeadA').innerHTML;
		document.getElementById('newsArticleContent0').innerHTML = document.getElementById('lblBodyA').innerHTML;
		document.getElementById('newsArticleDate').innerHTML = document.getElementById('lblPubDateA').innerHTML;
	}
	
	var _spanItems = document.getElementById('latestNewsArticles').getElementsByTagName('span');
	
	var rightPanelArticles = "";
	for(i=0; i<4; i++){
		if(i != parseInt(articleNum)){
			if(document.getElementById('dlLatest__ctl'+i+'_lblHead')){
				rightPanelArticles += "<p class='newsHeadLine'>"+document.getElementById('dlLatest__ctl'+i+'_lblHead').innerHTML+"</p>"
				var _article = document.getElementById('dlLatest__ctl'+i+'_lblBody').innerHTML;
				_article = _article.substring(0, 100);
				rightPanelArticles += "<p class='newsMesg'>"+_article+" ... <a href='#' onclick=\"setNewsArticlesFlow('"+i+"','n')\" class='whiteLink'>Continue Reading</a></p>";
			}
		}
	}
	if (rightPanelArticles != ""){
		//document.getElementById('rightPanelNewsArticles').innerHTML = rightPanelArticles;
		document.getElementById('rightPanelNewsArticles').innerHTML = "<strong style='color:#FFFFFF;'>Coming Soon...</strong>";
	}
}
//used in rmnews.apx page for popup
function exitPopup(strURL){
	window.open('exitpopup.aspx?newURL='+strURL,'LeavingRRM','height=250, width=400, top=200, left=200, scrollbars=0, resize=0');
}

//used in archivenews.aspx page 
function setNewsArchiveFlow(articleNum){	
	var _spanItems = document.getElementById('latestNewsArticles').getElementsByTagName('span');
	
	var rightPanelArticles = "";
	for(i=0; i<3; i++){
		if(document.getElementById('dlLatest__ctl'+i+'_lblHead')){
			rightPanelArticles += "<p class='newsHeadLine'>"+document.getElementById('dlLatest__ctl'+i+'_lblHead').innerHTML+"</p>"
			var _article = document.getElementById('dlLatest__ctl'+i+'_lblBody').innerHTML;
			var _newsLink = document.getElementById('dlLatest__ctl'+i+'_lblNewsId').innerHTML;
			_article = _article.substring(0, 100);
			rightPanelArticles += "<p class='newsMesg'>"+_article+" ... <a href='rmnews.aspx?id="+_newsLink+"&aID="+i+"' class='whiteLink'>Continue Reading</a></p>";
		}
	}
	if (rightPanelArticles != ""){
		//document.getElementById('rightPanelNewsArticles').innerHTML = rightPanelArticles;
		document.getElementById('rightPanelNewsArticles').innerHTML = "<strong style='color:#FFFFFF;'>Coming Soon...</strong>";
	}
}

//used in rmnews.apx page for popup
function chgHrefs(){
	var sHrefs = document.getElementById('latestNewsArticles').getElementsByTagName('A');
	for(i=0; i<sHrefs.length; i++){
		var preHref = sHrefs[i].href;
		sHrefs[i].href = "javascript:exitPopup('"+preHref+"')";
	}
}


function getCoords (element) {
	var coords = { x: 0, y: 0, width: element.offsetWidth, height: element.offsetHeight };
	while (element) {
		coords.x += element.offsetLeft;
		coords.y += element.offsetTop;
		element = element.offsetParent;
	}
	return coords;
}

function setFooterAtBottom(setWhichTable){
	if(navigator.userAgent.indexOf("Mac") != -1 || navigator.userAgent.indexOf("Safari") != -1){		
		var ltCoords = getCoords(document.getElementById(setWhichTable));
		var rtCoords = getCoords(document.getElementById('rightPanelBot'));
		var _setHeight = document.getElementById(setWhichTable).offsetHeight;
		
		if(_setHeight > rtCoords.y){			
			document.getElementById('leftBottomPanelcontent1').style.top = _setHeight+ltCoords.y;
		}
		else{
			var _diff = rtCoords.y - ltCoords.y;
			if(_diff > 100){
				document.getElementById('leftBottomPanelcontent1').style.top = rtCoords.y - 100;
			}
			else{
				document.getElementById('leftBottomPanelcontent1').style.top = rtCoords.y - _diff;
			}
			
		}		
		document.getElementById('leftBottomPanelcontent1').style.left = ltCoords.x;
	}
	else{
		var ltContent = document.getElementById(setWhichTable).getBoundingClientRect();
		var rtContent = document.getElementById('rightPanelBot').getBoundingClientRect();
		//set the 'top' parameter for the footer
		if(ltContent.bottom > rtContent.bottom){
			// check for IE
			if(navigator.appName.indexOf("Microsoft") != -1){
				document.getElementById('leftBottomPanelcontent1').style.top = ltContent.bottom-2+"px";
			}
			else{
				document.getElementById('leftBottomPanelcontent1').style.top = ltContent.bottom+"px";
			}
		}
		else{
			var __x = rtContent.bottom - ltContent.bottom;
			if(__x > 120){
				document.getElementById('leftBottomPanelcontent1').style.top = rtContent.bottom-116+"px";
			}
			else{
				document.getElementById('leftBottomPanelcontent1').style.top = rtContent.bottom-__x+"px";
			}
		}
		//set the 'left' parameter for the footer
		// check for IE
		if(navigator.appName.indexOf("Microsoft") != -1){
			document.getElementById('leftBottomPanelcontent1').style.left = ltContent.left-2+"px";
		}
		else{
			document.getElementById('leftBottomPanelcontent1').style.left = ltContent.left+"px";
		}
	}
	document.getElementById('leftBottomPanelcontent1').style.display = "block";
}