// F. Permadi 2005.
// Highlights table row
// Copyright (C) F. Permadi
// This code is provided "as is" and without warranty of any kind.  Use at your own risk.



// These variables are for saving the original background colors
var savedStates=new Array();
var savedStateCount=0;

/////////////////////////////////////////////////////
// This function takes an element as a parameter and 
//   returns an object which contain the saved state
//   of the element's background color.
/////////////////////////////////////////////////////
function saveBackgroundStyle(myElement)
{
  saved=new Object();
  saved.element=myElement;
  saved.className=myElement.className;
  saved.backgroundColor=myElement.style["backgroundColor"];
  return saved;   
}

/////////////////////////////////////////////////////
// This function takes an element as a parameter and 
//   returns an object which contain the saved state
//   of the element's background color.
/////////////////////////////////////////////////////
function restoreBackgroundStyle(savedState)
{
  savedState.element.style["backgroundColor"]=savedState.backgroundColor;
  if (savedState.className)
  {
    savedState.element.className=savedState.className;    
  }
}

/////////////////////////////////////////////////////
// This function is used by highlightTableRow() to find table cells (TD) node
/////////////////////////////////////////////////////
function findNode(startingNode, tagName)
{
  // on Firefox, the TD node might not be the firstChild node of the TR node
  myElement=startingNode;
  var i=0;
  while (myElement && (!myElement.tagName || (myElement.tagName && myElement.tagName!=tagName)))
  {
    myElement=startingNode.childNodes[i];
    i++;
  }  
  if (myElement && myElement.tagName && myElement.tagName==tagName)
  {
    return myElement;
  }
  // on IE, the TD node might be the firstChild node of the TR node  
  else if (startingNode.firstChild)
    return findNode(startingNode.firstChild, tagName);
  return 0;
}

/////////////////////////////////////////////////////
// Highlight table row.
// newElement could be any element nested inside the table
// highlightColor is the color of the highlight
/////////////////////////////////////////////////////
function highlightTableRow(myElement, highlightColor)
{
  var i=0;
  // Restore color of the previously highlighted row
  for (i; i<savedStateCount; i++)
  {
    restoreBackgroundStyle(savedStates[i]);          
  }
  savedStateCount=0;

  // To get the node to the row (ie: the <TR> element), 
  // we need to traverse the parent nodes until we get a row element (TR)
  // Netscape has a weird node (if the mouse is over a text object, then there's no tagName
  while (myElement && ((myElement.tagName && myElement.tagName!="TR") || !myElement.tagName))
  {
    myElement=myElement.parentNode;
  }

  // If you don't want a particular row to be highlighted, set it's id to "header"
  // If you don't want a particular row to be highlighted, set it's id to "header"
  if (!myElement || (myElement && myElement.id && myElement.id=="header") )
    return;
		  
  // Highlight every cell on the row
  if (myElement)
  {
    var tableRow=myElement;
    
    // Save the backgroundColor style OR the style class of the row (if defined)
    if (tableRow)
    {
	  savedStates[savedStateCount]=saveBackgroundStyle(tableRow);
      savedStateCount++;
    }

    // myElement is a <TR>, then find the first TD
    var tableCell=findNode(myElement, "TD");    

    var i=0;
    // Loop through every sibling (a sibling of a cell should be a cell)
    // We then highlight every siblings
    while (tableCell)
    {
      // Make sure it's actually a cell (a TD)
      if (tableCell.tagName=="TD")
      {
        // If no style has been assigned, assign it, otherwise Netscape will 
        // behave weird.
        if (!tableCell.style)
        {
          tableCell.style={};
        }
        else
        {
          savedStates[savedStateCount]=saveBackgroundStyle(tableCell);        
          savedStateCount++;
        }
        // Assign the highlight color
        tableCell.style["backgroundColor"]=highlightColor;

        // Optional: alter cursor
        tableCell.style.cursor='default';
        i++;
      }
      // Go to the next cell in the row
      tableCell=tableCell.nextSibling;
    }
  }
}

/////////////////////////////////////////////////////
// This function is to be assigned to a <table> mouse event handler.
// If the element that fired the event is within a table row,
//   this function will highlight the row.
/////////////////////////////////////////////////////
function trackTableHighlight(mEvent, highlightColor)
{
  if (!mEvent)
    mEvent=window.event;
		
  // Internet Explorer
  if (mEvent.srcElement)
  {
    highlightTableRow( mEvent.srcElement, highlightColor);
  }
  // Netscape and Firefox
  else if (mEvent.target)
  {
    highlightTableRow( mEvent.target, highlightColor);		
  }
}

/////////////////////////////////////////////////////
// Highlight table row.
// newElement could be any element nested inside the table
// highlightColor is the color of the highlight
/////////////////////////////////////////////////////
function highlightTableRowVersionA(myElement, highlightColor)
{
  var i=0;
  // Restore color of the previously highlighted row
  for (i; i<savedStateCount; i++)
  {
    restoreBackgroundStyle(savedStates[i]);          
  }
  savedStateCount=0;

  // If you don't want a particular row to be highlighted, set it's id to "header"
  if (!myElement || (myElement && myElement.id && myElement.id=="header") )
    return;
		  
  // Highlight every cell on the row
  if (myElement)
  {
    var tableRow=myElement;
    
    // Save the backgroundColor style OR the style class of the row (if defined)
    if (tableRow)
    {
	  savedStates[savedStateCount]=saveBackgroundStyle(tableRow);
      savedStateCount++;
    }

    // myElement is a <TR>, then find the first TD
    var tableCell=findNode(myElement, "TD");    

    var i=0;
    // Loop through every sibling (a sibling of a cell should be a cell)
    // We then highlight every siblings
    while (tableCell)
    {
      // Make sure it's actually a cell (a TD)
      if (tableCell.tagName=="TD")
      {
        // If no style has been assigned, assign it, otherwise Netscape will 
        // behave weird.
        if (!tableCell.style)
        {
          tableCell.style={};
        }
        else
        {
          savedStates[savedStateCount]=saveBackgroundStyle(tableCell);        
          savedStateCount++;
        }
        // Assign the highlight color
        tableCell.style["backgroundColor"]=highlightColor;

        // Optional: alter cursor
        tableCell.style.cursor='default';
        i++;
      }
      // Go to the next cell in the row
      tableCell=tableCell.nextSibling;
    }
  }
}

function DoNav(theUrl) {
	document.location.href = theUrl;
}


function reload(form)
{
var val=form.sportID.options[form.sportID.options.selectedIndex].value;
self.location='editprofile.php?option=favteam&sportID=' + val ;
}


function calcPayout(gameType, line, wager) {
	
	// calculate payout for US odds
	if ((Math.abs(line) > 50) & (gameType == 0)) {

		if (line > 0) {
			credits_won = ((line / 100) * wager);
		} // end if
		else
		if (line < 0) {
			line = line * -1;
			credits_won = ((100 / line) * wager);
		} // end if
		else
		if (line == 0) {
			credits_won = wager * 2;
		} // end if

	} // end if
	else {

		// calculate odds for European odds
		credits_won = (line * wager);

	} // end else

	return Math.round(credits_won);

} // end calcPayout


function updatePayout() {
	
	var fieldVal = document.wagerForm;
	if (fieldVal != null) {
		if (document.wagerForm.wager != null) {
			fieldVal = document.wagerForm.wager.value;
			var gameType = wagerForm.gameType.value;
		} // end if
	} // end if

	// 'update_area' is the id of the element to update.
	var elementMoneyVisitor = document.getElementById('moneyVisitor');
	var elementMoneyHome = document.getElementById('moneyHome');

	var elementLineVisitor = document.getElementById('lineVisitor');
	var elementLineHome = document.getElementById('lineHome');

	var elementTieVisitor = document.getElementById('tieVisitor');
	var elementTieHome = document.getElementById('tieHome');

	var elementTotalVisitor = document.getElementById('totalVisitor');
	var elementTotalHome = document.getElementById('totalHome');


	// get odds
	var elementMoneyPayVisitor = document.getElementById("moneyPayVisitor");
	if (elementMoneyPayVisitor != null) {
		elementMoneyPayVisitor = document.getElementById("moneyPayVisitor").innerHTML;
	} // end if
	
	var elementMoneyPayHome = document.getElementById("moneyPayHome");
	if (elementMoneyPayHome != null) {
		elementMoneyPayHome = document.getElementById("moneyPayHome").innerHTML;
	} // end if

	var elementLinePayVisitor = document.getElementById("linePayVisitor");
	if (elementLinePayVisitor != null) {
		elementLinePayVisitor = document.getElementById("linePayVisitor").innerHTML;	
	} // end if

	var elementLinePayHome = document.getElementById("linePayHome");
	if (elementLinePayHome != null) {
		elementLinePayHome = document.getElementById("linePayHome").innerHTML;
	} // end if

	var elementTiePayVisitor = document.getElementById("tiePayVisitor");
	if (elementTiePayVisitor != null) {
		elementTiePayVisitor = document.getElementById("tiePayVisitor").innerHTML;
	} // end if

	var elementTiePayHome = document.getElementById("tiePayHome");
	if (elementTiePayHome != null) {
		elementTiePayHome = document.getElementById("tiePayHome").innerHTML;
	} // end if

	var elementTotalPayVisitor = document.getElementById("totalPayVisitor");
	if (elementTotalPayVisitor != null)	{
		elementTotalPayVisitor = document.getElementById("totalPayVisitor").innerHTML;
	} // end if

	var elementTotalPayHome = document.getElementById("totalPayHome");
	if (elementTotalPayHome) {
		elementTotalPayHome = document.getElementById("totalPayHome").innerHTML;
	} // end if


	if (!isNaN(fieldVal)) {

		if (elementMoneyVisitor != null) {
			elementMoneyVisitor.innerHTML = '[' + calcPayout(gameType, elementMoneyPayVisitor, fieldVal) + 'cr]';
		} // end if
		
		if (elementMoneyHome != null) {
			elementMoneyHome.innerHTML = '[' + calcPayout(gameType, elementMoneyPayHome, fieldVal) + 'cr]';
		}

		if (elementLineVisitor != null) {
			elementLineVisitor.innerHTML = '[' + calcPayout(gameType, elementLinePayVisitor, fieldVal) + 'cr]';
		}

		if (elementLineHome != null) {
			elementLineHome.innerHTML = '[' + calcPayout(gameType, elementLinePayHome, fieldVal) + 'cr]';
		}

		if (elementTieVisitor != null) {
			elementTieVisitor.innerHTML = '[' + calcPayout(gameType, elementTiePayVisitor, fieldVal) + 'cr]';
		}
		
		if (elementTieHome != null) {
			elementTieHome.innerHTML = '[' + calcPayout(gameType, elementTiePayHome, fieldVal) + 'cr]';
		}
		
		if (elementTotalVisitor != null) {
			elementTotalVisitor.innerHTML = '[' + calcPayout(gameType, elementTotalPayVisitor, fieldVal) + 'cr]';
		}

		if (elementTotalHome != null) {
			elementTotalHome.innerHTML = '[' + calcPayout(gameType, elementTotalPayHome, fieldVal) + 'cr]';
		}

	} // end if

} // end updatePayout

	function toggleRows(thisname) {
	
		tr=document.getElementsByTagName('tr')
		for (i=0;i<tr.length;i++){
		  if (tr[i].getAttribute(thisname)){
		   if ( tr[i].style.display=='none' ){
			 tr[i].style.display = '';
		   }
		   else {
			tr[i].style.display = 'none';
		   }
		  }
		}
	}

$(function(){
	$("ul#ticker01").liScroll({travelocity: 0.035});
});



$( document ).ready( function () {
	
	updatePayout();
	toggleRows('tRow');
	toggleRows('tRow2');

	// cluetip
	$('a.tips').cluetip({width: '450px', dropShadow: false, hoverIntent: true, tracking: false, sticky: true, closePosition: 'title', closeText: '<img src="js/cluetip/cross.png" alt="" />', mouseOutClose: true, arrows: true});
	$('a.tips2').cluetip({width: '450px', dropShadow: false, hoverIntent: true, tracking: false, arrows: true});

	// Tool Tips
	$( 'a.tipSource' ).hover ( function () {

		var tipId = $(this ).attr ( 'href' );
		var tip = $( tipId );
		var tipSourceOffset = {};
		$( this ).offset ({ margin: true, padding: true,
							border: true, scroll: false }, tipSourceOffset);
		var scroll = {
			left: $( window ).scrollLeft(),
			top: $( window ).scrollTop()
		};
		var viewportWidth = $( window ).width();
		var viewportHeight = $( window ).height();
		var top, left;

		if ( (tipSourceOffset.left - scroll.left) < (viewportWidth / 2) ) {
			left = tipSourceOffset.left;
		} else {
			left = tipSourceOffset.left + $( this ).width() - (tip.width() * 2);
		}
		if ( (tipSourceOffset.top - scroll.top) < (viewportHeight / 2) ) {
			top = tipSourceOffset.top + $( this ).height() - 80;
		} else {
			top = tipSourceOffset.top - tip.height() - 15;
		}

		tip.css ({
			top: top + 'px',
			left: left + 'px'
		}).show( 'fast' );

	}, function () {
		var tipId = $(this ).attr ( 'href' );
		$( tipId ).hide( 'fast' );
	}).click ( function () {
		// Disable the link
		return ( false );});
	$( 'div.tipContent' )
		.css({ position: 'absolute' })
		.removeClass( 'tipContent' )
		.addClass ( 'jq-tipContent' )
		.hide();

		
       // hides the slickbox as soon as the DOM is ready
       // (a little sooner than page load)
        //$('#slickbox').hide();
        $('#slickbox2').hide();

	   // toggles the slickbox on clicking the noted link 
        $('a#slick-toggle').click(function() {
          $('#slickbox').slideToggle(400);
          return false;
        });

	   // toggles the slickbox on clicking the noted link 
        $('a#slick-toggle2').click(function() {
          $('#slickbox2').slideToggle(400);
          return false;
        });
		

});
