// Wheel page functions
function thisMovie(movieName)
{
    if (navigator.appName.indexOf("Microsoft") != -1)
    {
        return window[movieName];
    }
    else
    {
        return document[movieName];
    }
} 

function setFlashProps(spins, jackpot, cash)
{
    thisMovie("wheel").setFlashProps(spins, jackpot, cash);
} 

// Called after wheel has finished spinning
function postSpin(spins, cash, jackpot)
{
	if ($('welcome_box'))
	{
		$('welcome_box').style.display = 'none';
		Effect.BlindDown('winner_box');
	}
	else
	{
		updateContestantStats(spins, cash);
	}
	$('jackpot').innerHTML = addCommas(parseFloat(jackpot).toFixed(2));
}

function siteRedirect()
{
	var cTicks = parseInt($('redirect_timer').innerHTML);
	var timer = setInterval(function()
	{
		if (cTicks > 1)
		{
			$('redirect_timer').innerHTML = --cTicks;
		}
		else
		{
			clearInterval(timer);
			window.location.href = '/offers';
		}
	
	}, 1000);
}

function refreshWheel(points,spins)
{
	var flash = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="708" height="548" id="wheel" align="middle">';
	flash += '<param name="allowScriptAccess" value="sameDomain" />';
	flash += '<param name="allowFullScreen" value="false" />';
	flash += '<param name="wmode" value="transparent" />';
	flash += '<param name="movie" value="/flash/wheel.swf?points=' + points + '&mySpin=' + spins +'" />';
	flash += '<param name="quality" value="high" /><param name="bgcolor" value="#000000" />';
	flash += '<embed src="/flash/wheel.swf?points=' + points + '&mySpin=' + spins +'" quality="high" bgcolor="#000000" width="708" height="548" wmode="transparent" name="wheel" align="middle" allowscriptaccess="sameDomain" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
	flash += '</object>';
	
	$('wheel_flash').innerHTML = flash;
}

function nonincentivizedPostback(offerId) 
{
	var url = '/methoduser/nonincentivized/' + offerId;
	// notice the use of a proxy to circumvent the Same Origin Policy.
	
	new Ajax.Request(url, {
		method: 'get',
		requestTimeout: 10,
		onFailure: function(transport) {
		},
		onSuccess: function(transport) {
			var json = transport.responseText.evalJSON();
			if (json.result.success == 'true')
			{
				updateContestantStats(json.result.spins, json.result.cash);
				$('jackpot').innerHTML = addCommas(parseFloat(json.result.jackpot).toFixed(2));
				setFlashProps(json.result.spins, json.result.cash, json.result.jackpot);
			}
		}
	});
}

function skipOffer(offerId) 
{
	var url = '/methoduser/skipoffer/' + offerId;
	// notice the use of a proxy to circumvent the Same Origin Policy.
	
	new Ajax.Request(url, {
		method: 'get',
		requestTimeout: 10,
		onFailure: function(transport) {
		},
		onSuccess: function(transport) {
			var json = transport.responseText.evalJSON();
			if (json.result.success == 'true')
			{
				updateContestantStats(json.result.spins, json.result.cash);
				$('jackpot').innerHTML = addCommas(parseFloat(json.result.jackpot).toFixed(2));
				setFlashProps(json.result.spins, json.result.cash, json.result.jackpot);
			}
		}
	});
}

Event.observe(window, 'load', function() 
	{
		if ($('redirect_timer') && ($('more_offers_box').style.display != 'none'))
		{
			siteRedirect();
		}
	}
);
