var contest_id = null;
check_confirmation = false;

$(document).ready( function() {

	// frontpage feed
	if($('#homefeed').length>0) {
		//
		$.post(base + '/twitterfeed/?location=http://twitter.com/statuses/mentions.json',
			  {},function(response) {

				  $('#homefeed').html('No Recent Tweets');
				  if(response.nodata) return;
				  if(response.length>0) {
					  
					  var count = response.length;
					  if(count>11) count=11;
					  $('#homefeed').html('');
					  for (var x=0;x<count;x++) {
						  var note = '<div class="feeditem"><img class="icon" src="' + response[x].user.profile_image_url + '">';
						  note += '<strong><a href="http://www.twitter.com/' + response[x].user.screen_name + '">';
						  note += response[x].user.screen_name + '</a>:</strong>&nbsp;';
						  note += response[x].text + '<p class="anchor"></p></div>';
						  $('#homefeed').append(note);
					  }
					  
				  }
				  
			  },'json');
		
	}
	
	// ie sux!
	if(jQuery.browser.msie) {
		$('.selected span').css('padding-top','0px');
	}
});	

var hidestatlink = false;
function getResults() {

	if (document.location.href.indexOf('winners')==-1 && document.location.href.indexOf('/results/')==-1) {
	$('#maintimer').html('This contest has ended, and the winners are...');
	setTimeout(function() { document.location.href =  document.location.href + '?winners';  },1000);
	}
        return;

	$.get(base + '/confirmwin/?id=' + contest_id,{},function(response) {
		
		if(typeof response.results != 'undefined') {
		  var note = '<p>Contest has ended, and the winner(s) are...</p>';
		  for(var index in response.results) {
			  var winner = response.results[index];
			  note += '<div class="feeditem"><img class="icon" src="' + winner.photo + '">';
			  note += '<strong><a href="http://www.twitter.com/' + winner.user + '" target="_NEW">';
			  note += winner.name + '</a>! Congrats @' + winner.user + ', you have won this contest.</strong>&nbsp;</div>';
		  }
		  note += '<p>Please contact the contest owner for details about collecting your prize(s).</p>';
		  $('#winner').html(note);
		  
		} else {
			$('#winner').html('<h2>Whale Attack!</h2><p>Contest winner is pending. We could not verify the winner at this time, please check back later.</p><p>If this problem persists your contest may not have a winner. Please check the contest stats page to see how many valid entries there were.</p>');
		}
		
		if(hidestatlink!=true) {
		$('#winner').append('<div id="statlink" class="center"><a class="btn" href="' + base + '/results/' + contest_id + '/"><span><span>view contest statistics</span></span></a></div>')
		}
		$('#winner').show();
		$('#keyinfo').hide();
		$('#ticketbox').fadeOut();
		
		},'json');
	
}

function number_format(number, decimals, dec_point, thousands_sep) {

    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }    return s.join(dec);
}

