var msie6 = (window.navigator.userAgent.toUpperCase().toString().indexOf("MSIE 6") != -1);
var webkit = (window.navigator.userAgent.toUpperCase().toString().indexOf("WEBKIT") != -1);
var firefox = (window.navigator.userAgent.toUpperCase().toString().indexOf("FIREFOX") != -1);

var popupTime = 60000*5;
var vidwinVid = null;
var vidwinItem = null;
var screenIdx = 0;
var vidwinshown = false;
function switchScreen()
{
	screenIdx = ((screenIdx + 1) % 6);
	var img = document.getElementById('screen');
	img.src = "images/screen-" + screenIdx + ".png";
	
}

function closeVideo()
{
	var vidwin = document.getElementById('vidwin');
	vidwin.style.display = 'none';
	vidwinshown = false;
	var player = document.getElementById('player');
	if (player && player.exUnloadFLV)
	{
		player.exUnloadFLV(); 
	}
	
	resetGetItPopupInterval();
	if (msie6)
	{
		var coursesTable = document.getElementById('courses-table');
		if (coursesTable != null) coursesTable.style.visibility = 'visible';
	}
}

function videoDone()
{
	var nvid = vids[0];
	for (var i=0;i<vids.length;i++)
	{
		if (vids[i] == vidwinVid) 
		{
			nvid = vids[(i+1)%vids.length];
			break;
		}
	}
	vidwinClick(nvid);
}
var vids = ["overview", "easy-to-use", "easy-fast-fun", "competitive-advantage", "garmin", "waterproof", "distance-of-last-shot", "golfgps-vs-lasers", "survivorman", "speeds-pace-of-play", "testimonials"];


function centerVidwin()
{
	var vidwin = document.getElementById('vidwin');
	var pos = document.body.scrollTop;
	if (pos==null||pos==0) pos = document.documentElement.scrollTop;
	pos += (document.documentElement.clientHeight / 2 - 526 / 2);
	if (pos < 100) pos = 60;
	vidwin.style.top = pos + "px";
}
	
function vidwinClick(vid, center)
{
	closeGetItPopup();

	if (msie6)
	{
		
		var coursesTable = document.getElementById('courses-table');
		if (coursesTable != null) coursesTable.style.visibility = 'hidden';
	}
	if (vid == null || vid == "") vid = "overview";
	var vidwin = document.getElementById('vidwin');
	if (center)
	{
		centerVidwin();
	}
	vidwin.style.display = '';
	if (vidwinItem != null)
	{
		vidwinItem.className = "vidwin-item";
	}
	var item = document.getElementById('vidwin-item-' + vid);
	item.className = "vidwin-item vidwin-item-selected"; 
	var flv = "videos/vid-" + vid + ".flv";
	
	
	if (center || window.fo == null)
	{
		window.fo = new SWFObject("player.swf", "player", "480", "407", "9", "#000000");
		fo.addParam("allowScriptAccess", "always");
		fo.addParam("bgColor", "#000000");
		fo.addVariable("video", flv);
		fo.write("playercontent");
	}
	else
	{
		var player = document.getElementById('player');
		if (player && player.exLoadFLV)
		{
			player.exLoadFLV(flv); 
		}
	}
	vidwinItem = item;
	vidwinVid = vid;
	vidwinshown = true;
}



function searchCourses()
{
	var scrollTop = 1100;
	if (webkit)
	{	
		document.body.scrollTop = scrollTop;
	}
	else
	{
		document.documentElement.scrollTop = scrollTop;
	}
}


var gipitvshown = false;
var gipitvever = false;
var gipitv = null;
var _wol = window.onload;
window.onload = function()
{
	
	if (msie6)
	{
		var pngIds = ["vwsr", "vwst", "vwsb", "vwsl", "vwcb", "hstp", "hsbt", "hslt", "hsrt", "d301", "d302"];
		for (var i=0;i<pngIds.length;i++)
		{
			var ol = document.getElementById(pngIds[i]);
			if (ol != null)
			{
				var img = ol.style.backgroundImage;
				if (img != null && img != '')
				{
					img = img.split("url(").join("").split(")").join("");
					ol.style.backgroundImage = '';
					ol.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\"" + img + "\",sizingMethod=\"crop\")";
				}
				else
				{
					img = ol.src;
					if (img != null)
					{
						ol.src = "images/spacer.png";
						ol.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\"" + img + "\",sizingMethod=\"crop\")";
					}
				}
			}
		}
	}
	window.onmousedown = function()
	{
		bodyScroll();
	}
	document.body.onmousedown = function()
	{
		bodyScroll();
	}
	
	resetGetItPopupInterval();
	
	 if (msie6)
	 {
		window.onscroll = function()
		{
			bodyScroll();
		}
	}			
	
	if (document.location.toString().indexOf('watch=1') != -1)
	{
		
		vidwinClick("overview", true);
	
	}
	
	if (_wol != null) _wol();
}
function bodyScroll()
{
	if (gipitvshown) showGetItPopup();
	resetGetItPopupInterval();
	if (vidwinshown) centerVidwin();
}

function resetGetItPopupInterval()
{
	var hspace = document.getElementById('hspace');
	if (hspace == null) return;
	clearTimeout(gipitv);
	gipitv = setTimeout(showGetItPopup, popupTime);
}

function showGetItPopup()
{
	var hspace = document.getElementById('hspace');
	if (hspace == null) return;
	

	if (vidwinshown || gipitvever == true) return;
	var pos = document.body.scrollTop;
	if (pos==null||pos==0) pos = document.documentElement.scrollTop;
	pos += (document.documentElement.clientHeight / 2 - 239 / 2);
	if (pos < 100) pos = 60;
	hspace.style.top = pos + "px";
	hspace.style.display = 'block';
	gipitvshown = true;
	resetGetItPopupInterval();
}

function closeGetItPopup(ever)
{
	var hspace = document.getElementById('hspace');
	if (hspace == null) return;
	
	hspace.style.display = 'none';
	gipitvshown = false;
	resetGetItPopupInterval();
}


// additional css hacks (minor visual stuff)
if (firefox)
{
	document.write('<style type="text/css">label{position:relative;top:-4px;}</style>');
}




