/* Challenge Creation Functions
-----------------------------------------*/
function challenge_type() {
	if ( $('#challenge-type-popular').attr('checked') )
		popular_challenge();
	else if ( $('#challenge-type-record').attr('checked') )
		record_challenge();
}

function popular_challenge() {
	$('#record-info').hide();
}

function record_challenge() {
	$('#record-info').show();
}

function prize_type() {
	if ( $('#challenge-prize-default').attr('checked') )
		default_prize_type();
	else if ( $('#challenge-prize-custom').attr('checked') )
		custom_prize_type();
}

function default_prize_type() {
	$('#custom-prizes').hide();
}

function custom_prize_type() {
	$('#custom-prizes').show();
}


/* Challenge View Page Functions
-----------------------------------------*/
function scroll_to_media(media_id) {
	$('#lbe').attr('scrollTop', $("#media-"+media_id).attr('offsetTop')-90);
}

function show_challenge_media(media_id) {
	scroll_to_media(media_id);
	$('li.lb-item').removeClass('selected');
	$('li#media-'+media_id).addClass('selected');
	$.ajax({type: "POST", url: '/challenges/get_challenge_media/', data: {media_id: media_id}, dataType: 'json', success: show_media_callback});
}

var show_media_callback = function(data) {
	var html = data['html'];
	$('#challenge-content').html(html);
	current_media_id = data['media_id'];
}

function goto_next_media() {
	for ( var i = 0; i < media_ids.length; i++ ) {
		if ( media_ids[i] == current_media_id ) {
			if ( media_ids[i+1] )
				show_challenge_media(media_ids[i+1]);
			else
				show_challenge_media(media_ids[0]);
		}
	}
}

function goto_prev_media() {
	for ( var i = 0; i < media_ids.length; i++ ) {
		if ( media_ids[i] == current_media_id ) {
			if ( media_ids[i-1] )
				show_challenge_media(media_ids[i-1]);
			else
				show_challenge_media(media_ids[media_ids.length-1]);
		}
	}
}

function vote_challenge_media(media_id) {
	$.ajax({type: "POST", url: '/challenges/vote_media/', data: {media_id: media_id}, dataType: 'json'});
	var new_votes = parseInt($('#total-votes').html())+1;
	$('#total-votes').html(new_votes);
	$('#media-'+media_id+'-score').html(new_votes+' Votes');
	document.getElementById('vote-btn').onclick = null;
	$('#vote-btn').removeClass('btn-like-sm');
	$('#vote-btn').addClass('btn-like-sm-voted');
	userbar_message('action', 'vote_challenge');
}

function feature_challenge_toggle(challenge_id, toggle) {
	$.ajax({type: "POST", url: '/challenges/featured/', data: {challenge_id: challenge_id, toggle: toggle}, dataType: 'json'});
	
	if ( toggle == 1 )
		$('#featured-links').html('Featured!');
	else
		$('#featured-links').html('Un-Featured!');
}

/* Challenge Enter Page - Enter Record
-----------------------------------------*/
$(document).ready(function(){
	$(".btn-choose-challenge").click(function() {
		$('.btn-choose-challenge').show();
		$('.enter-record-wrap').hide();
		$(this).hide();
		$(this).next().fadeIn("fast");
		return false;
	});
});