﻿var lastEl;
var lastEn;
var scrlFlag = false;

function isiPhone() {
	var agent = navigator.userAgent.toLowerCase();
	return agent.match(/iPhone/i);
} 

function bc(q, clr) {
	var zindex = 0;
	if (clr == '') {
		zindex = 1;
	}
	q.css('background-color', clr);
	q.css('zIndex', zindex);
}
function s(elID, entID) {
	var el = document.getElementById(elID);
	if (el) {
		var offsetBuffer = 20;
		var theTop = el.offsetTop - offsetBuffer;
		var theLeft = el.offsetLeft - offsetBuffer;
		if (isiPhone()) {
			// uncomment this if the new jquery scroll doesn't work.
			window.scrollTo(theLeft, theTop);
			$("div#" + elID).fadeIn(100).fadeOut(150).fadeIn(150).fadeOut(150).fadeIn(150).fadeOut(150).fadeIn(150);
			if (entID != null && entID.length > 0) {
				sh(entID);
			}
			return false;
		}
		$.scrollTo({ top: theTop, left: theLeft }, 800, {
			onAfter: function() {
				if (!$.browser.msie || !jQuery.support.boxModel) {
					$("div#" + elID).fadeIn(100).fadeOut(150).fadeIn(150).fadeOut(150).fadeIn(150).fadeOut(150).fadeIn(150);
				}
				else {
					$('div', $("div#" + elID).get(0)).fadeOut(150).fadeIn(150).fadeOut(150).fadeIn(150).fadeOut(150).fadeIn(150);
				}

				if (entID != null && entID.length > 0) {
					sh(entID);
				}
			}
		});
	}
	return false;
}
function scrollHome()
{
	var offsetBuffer = 20;
	var defaultMatch = "W1-1";
	var defaultMatch2 = "W2-1";
	var el = document.getElementById(defaultMatch);
	if (!el)
	{
		//el = document.getElementById(defaultMatch2);
	}
	if (lastEl) lastEl.style.backgroundColor = 'White';
	var el = document.getElementById(defaultMatch);
	if (el)
	{
		window.scrollTo(el.offsetLeft - offsetBuffer, el.offsetTop - offsetBuffer);
	}
	return false;
}
function sm(entrantID) {
	var target = "EntrantMatches";
	if (isiPhone()) {
		target = "_self";
	}
	window.open("EntrantMatches.aspx?EntrantID=" + entrantID
		, target, "location=0,address=0,status=0,toolbar=0,resizable=1,width=650,height=600");
}
function sh(entrantID) {
	var s = "a[onclick*='" + entrantID + "']";
	if (lastEn != entrantID) {
		$(lastEn).each(function() {
			bc($(this).parent(), '');
			$(this).css('color', '');
		});
	}
	$(s).each(function() {
		bc($(this).parent(), '#CC4400');
		$(this).css('color', 'White');
	});
	lastEn = s;
}
function sb(bracketID, matchLabel, entrantID, closeThis)
{
	var navWin;
	// need to detect if window.opener is really parent from playbca.com
	if (window.opener && closeThis)
	{
		navWin = window.opener;
	}
	else
	{
		navWin = window;
	}
	var newURL = "BracketViewer.aspx?BracketID="+bracketID;

	if (matchLabel.length > 0)
	{
		newURL = newURL + "&MatchLabel="+matchLabel;
	}

	if (entrantID.length > 0) {
		newURL = newURL + "&EntrantID=" + entrantID;
	}

	navWin.location.href = newURL;
	if (window.opener && closeThis)
	{
		if (window.opener) this.close();
	}
}
function scm(poolEventID, room, closeThis) {
	var navWin;
	// need to detect if window.opener is really parent from playbca.com
	if (window.opener && closeThis) {
		navWin = window.opener;
	}
	else {
		navWin = window;
	}
	var newURL = "CurrentMatches.aspx?PoolEventID=" + poolEventID;

	if (room.length > 0) {
		newURL = newURL + "&Room=" + room;
	}

	navWin.location.href = newURL;
	if (window.opener && closeThis) {
		if (window.opener) this.close();
	}
}
function st(tournamentID)
{
	window.location.href = "BracketViewer.aspx?TournamentID="+tournamentID;
}
function se(eventID)
{
	window.location.href = "BracketViewer.aspx?EventID="+eventID;
}

function getParam( name )
{  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	if( results == null )
	{
		return "";  
	}
	else
	{
		return results[1];
	}
}

