
// load-up funtions - binds click functions
$j(function () {
	$j('#close_contest, #contest_header .b_close').click(function (e) {
		this.blur();
		e.preventDefault();
		if (confirm('Are you sure you want to close the contest. The contest tab will remain hidden until a new questions is released or you choose to view it again.')) {
			$j('#contest_cont').hide();
			set_cookie('contest_state', 'off', 14);
			$j.ajax({
				type: 'post',
				url: '/contest.php?c=off',
				dataType: 'html'
			});
		}
	});
	$j('#contest_tab div, #contest_header .b_minimize').click(function (e) {
		this.blur();
		e.preventDefault();
		if ($j('#contest_content').hasClass('show')) {
			$j('#contest_content').removeClass('show').hide();
			$j('#contest_cont').css('left', 0);
			$j('#contest_tab').show();
		}
		else {
			$j('#contest_tab').hide();
			$j('#contest_content').addClass('show').show();
			var centered_left = (size[0] - $j('#contest_cont').outerWidth())/2;
			$j('#contest_cont').css('left', centered_left);
		}
		set_tab_status();
	});
	$j('#contest_header .b_help').click(function (e) {
		this.blur();
		e.preventDefault();
		$j(this).blur();
		var pos = $j('#contest_cont').position(); 
		$j('#contest_help').show().css({
			'left': pos.left + 25,
			'top': pos.top + 35
		}).addClass('modal');		
		var size = getPageSize();
		$j('#overlay').css('height', size[1]).show();
	});
	$j('#contest_help a.close').click(function (e) {
		e.preventDefault();
		$j('#contest_help').removeClass('modal').hide();
		$j('#overlay').hide();
	});
	
	if ($j('#contest_cont').css('display') != 'none') {
		set_tab_status();
		if (get_cookie('contest_id')) show_contest(get_cookie('contest_id'));
		else show_questions();
		update_points();
	}
	
	$j('#contest_login .register a.b_main').click(function () {
		this.blur();
		set_cookie('contest_state', 'hide');
	});
	
	$j('#right_column .content_cta a').click(function (e) {
		$j('#contest_cont').show();
		$j('#contest_content').removeClass('show');
		$j('#contest_tab div').click();
	});
	
	var size = getPageSize();
	if ($j('#contest_content').hasClass('show')) {
		$j('#contest_tab').hide();
		$j('#contest_content').show();
		var centered_left = (size[0] - $j('#contest_cont').outerWidth())/2;
		$j('#contest_cont').css('left', centered_left);
	}
});

// set tab arrow and cookie state based on the main content's visibility
function set_tab_status() {
	var state = ($j('#contest_content').hasClass('show')) ? 'show' : 'hide';
	if (state == 'show') {
		var src = '/images/contest/hide.gif';
		var lang = $j('#hide_lang').val();
	}
	else {
		var src = '/images/contest/show.gif';
		var lang = $j('#show_lang').val();
	}
	$j('#contest_tab .toggle_contest').attr({'src': src, 'alt': lang, 'title': lang});
	set_cookie('contest_state', state);	
}

// grab the info for a contest
function show_contest(contest_id) {
	set_cookie('contest_id', contest_id, 14);
	$j.ajax({
		type: 'post',
		url: '/contest.php?c=question',
		dataType: 'json',
		data: 'contest_id='+ contest_id,
		success: function (data) {
			if (data.content != '') {
				$j('#contest_questions').hide();
				$j('#contest_question').html(data.content);
				$j('#contest_question').show();
				if ($j('#contest_question #contest_can_answer').length) {
					if ($j('#contest_question #contest_can_answer').val() == 0) {
						$j('#contest_question .grey_box .content').click(function () {
							$j('#contest_question .black_box a').click();
						}).css('cursor', 'pointer');
					}
				}
			}
			else {
				set_cookie('contest_id', 0, -1);
				show_questions();
			}
		}
	});
}

// show the main questions
function show_questions() {
	set_cookie('contest_id', 0, -1);
	$j.ajax({
		type: 'post',
		url: '/contest.php?c=question_list',
		dataType: 'json',
		success: function (data) {
			$j('#contest_questions').html(data.content);
			$j('#contest_questions').show();
			$j('#contest_question').hide();
			$j('#contest_help .content').html(data.description);
		}
	});	
}

// set the current question as 'answerable'
function set_can_answer(anchor, contest_id, position) {
	set_cookie('contest_state', 'hide');
	$j.ajax({
		type: 'post',
		url: '/contest.php?c=can_answer',
		dataType: 'html',
		data: 'contest_id='+ contest_id +'&position='+ position,
		success: function () {
			window.location.href = $j(anchor).attr('href');
		}
	});
}

function answer_question(contest_id, position, points, correct_answer) {
	$j('#select_answer').hide();
	if ($j('#contest_question input:checked').length == 0) {
		$j('#select_answer').show();
		$j('#contest_question input:radio').eq(0).focus();
		return;
	}
	var answer = $j('#contest_question input:checked').eq(0).val();
	var answer_id = $j('#contest_question input:checked').eq(0).attr('id');
	answer_id = answer_id.substr(answer_id.indexOf('_') + 1);
	var correct = (answer_id == correct_answer) ? 1 : 0;
	$j.ajax({
		type: 'post',
		url: '/contest.php?c=answer',
		dataType: 'json',
		data: 'contest_id='+ contest_id +'&position='+ position +'&points='+ points +'&correct='+ correct +'&answer='+ answer,
		success: function (data) {
			if (data.content != '') {
				$j('#contest_question .answer').hide();
				$j('#contest_question .answer_response div').html(data.content);
				$j('#contest_question .answer_response').show();
				if (correct) {
					set_cookie('contest_id', 0);
					update_points();
				}
			}
		}
	});
}

// update the total points box
function update_points() {
	$j.ajax({
		type: 'post',
		url: '/contest.php?c=get_points',
		dataType: 'json',
		success: function (data) {
			if (data.points != '') {
				$j('#current_points strong').text(data.points);
			}
		}
	});
}

// give bonus points for sharing about the contest
function give_bonus(anchor, type, contest_id, position) {
	var src = $j(anchor).find('img').attr('src');
	if (src.indexOf('_off') == -1) {
		$j(anchor).find('img').attr('src', src.replace('.jpg', '_off.jpg'));
		$j(anchor).css('cursor', 'default');
		$j.ajax({
			type: 'post',
			url: '/contest.php?c=bonus_points',
			data: 'contest_id='+ contest_id +'&position='+ position +'&type='+ type,
			dataType: 'html',
			success: function () {
				update_points();
			}
		});
		return true;
	}
	return false;
}

// reset the page to answer a question again
function try_again() {
	$j('#contest_question input:checked').attr('checked', '');
	$j('#contest_question .answer').show();
	$j('#contest_question .answer_response').hide();
}

// set the offset to get to the previous questions
function prev_questions() {
	set_cookie('contest_next_set', 1);
	show_questions();
}

//set the offset to get to the next questions
function next_questions() {
	set_cookie('contest_next_set', -1);
	show_questions();
}

//set a cookie
function set_cookie(name, value, expires) {
	// convert expires to milliseconds
	if (expires != null) {
		expires = expires * 1000 * 86400;
		var expiry = new Date();
		expiry.setTime(expiry.getTime() + expires);
	}
	else {
		expires = 0;
	}
	document.cookie=name +"="+ escape(value) + ((expires) ? ";expires="+expiry.toGMTString() : "") + ";path=/";
}

//get a cookie's value
function get_cookie(name) {
	if (document.cookie.length > 0) {
	  start = document.cookie.indexOf(name + "=");
	  if (start != -1) {
	    start = start + name.length+1;
	    end = document.cookie.indexOf(";", start);
	    if (end == -1) end=document.cookie.length;
	    return unescape(document.cookie.substring(start, end));
		}
	}
	return "";
}

