$(document).ready( function() {

	// Vid första sidladdning komma om det finns öppna fejder
	updateChallenges();

	$('.quick_cat').click( function() {

		id = $(this).attr('id');

		var val = $('input#cat_'+id).val();

		if(val == 1){
			$('input#cat_'+id).attr('value', 0);
		}else{
			$('input#cat_'+id).attr('value', 1);
		}

		$(this).toggleClass('on');
		
	});

	$('.avatar').click( function() {
		var id = $(this).attr('id');

		$('.active').removeClass('active');

		$('#'+id).addClass('active');3

		$('#bon_avatar').attr('value', id);
	
	});

	$('#quickplay').submit( function() {

		var cat_1 = $('input#cat_1').val();
		var cat_2 = $('input#cat_2').val();
		var cat_3 = $('input#cat_3').val();
		var cat_4 = $('input#cat_4').val();
		var cat_5 = $('input#cat_5').val();
		var cat_6 = $('input#cat_6').val();
		
		if(cat_1 == 0 && cat_2 == 0 && cat_3 == 0 && cat_4 == 0 && cat_5 == 0 && cat_6 == 0){
			alert('Ni måste välja en eller flera kategorier');
			return false;
		}

		return true;
	});

	$('.addAdress').click( function() {
		var email = $(this).attr('id');

		for(var i = 1; i <= 10; i++){

			if($('#mail_'+i).val() == email){
				alert('Adressen finns redan');
				break;
			}

			if($('#mail_'+i).val().length <= 0){
				$('#mail_'+i).attr('value', email);
				break;
			}
		}
	});

	var auto_refresh = setInterval( function() {

		var reload = $('.reload_challenges').val();

		if(reload == 1){
			$.post('xmlhttp/checkMyActiveChallenges.php', function(data){
				$('#my_active_challenges').html(data);
			});
		}

	}, 7654);

	var auto_refresh = setInterval( function() { 
		
		updateChallenges();
		
	}, 10000);

	var auto_refresh = setInterval( function() {

		$.post('xmlhttp/countChallengeUsers.php', function(data){
			var list = data.split('||');

			for ( var i in list ) {
				var data = list[i].split('__');

				$('#users_'+data[0]).html(data[1]+' / 10');
			} 
		});

	}, 8870);


	$('.reportquestion').click( function() {

		var id = $(this).attr('id');

		return popup ('report.php?questionID='+id, 440, 540, 'no', 'no', 'auto');
	});
});


function updateChallenges(){

	var height = 90;

	$.post('xmlhttp/checkActiveChallenges.php', function(data){
		if(data.length > 10){	
			$('#active_challenges').show();
			var list = data.split('||||');
			$('#active_challenges').html(list[0]);

			height += (list[1] * 20);
			$('#active_challenges').css('height', height+'px');
		}else{
			$('#active_challenges').hide();
		}
	});

}


function killChallenge(id){

	$.post('xmlhttp/denyChallenge.php', { id: id }, function(data){
		if(data == 'ok'){
			$.post('xmlhttp/checkMyActiveChallenges.php', function(data){
				$('#my_active_challenges').html(data);
			});
		}
	});

	$('.reload_challenges').attr('value', 1);
}


function selectAmount(amount){
	$('#quick_amount').attr('value', amount);
}

// Kolla svaret på true or false
function answer(answer, no){
	var correct = $('#grade_'+no).val();

	var inrow = parseInt($('.correct_row').html());
	var rekord = parseInt($('.rekord_row').html());

	if(answer == correct){
		$('.correct').fadeIn("slow");
		setTimeout('$(\'.correct\').fadeOut("slow");', 1500);
		inrow += 1;
	}else{
		$('.wrong').fadeIn("slow");
		setTimeout('$(\'.wrong\').fadeOut("slow");', 1500);

		if(inrow > rekord){
			$('.rekord_row').html(inrow);

			//Sätt cookie
			document.cookie = "true_or_false="+inrow+"; expires=20350910";
		}

		inrow = '0';
	}

	$('.correct_row').html(inrow);

	var num = parseInt(no);

	$('div.statement_'+no).hide();
	$('div.statement_'+(num+1)).show();
}

function popup (url, width, height, status, menu, resizable) {
	var time = new Date();
	var name = Math.floor((Math.random() * Math.round(time.getTime())));

	var prop = 'toolbar=no,location=no,directories=no,scrollbars=yes,copyhistory=no,';
	prop += 'status='+ (status ? 'yes' : 'no') +',';
	prop += 'resizable='+ (resizable ? resizable : 'yes') + ',';
	prop += 'menu='+ (menu ? 'yes' : 'no') +',';
	prop += 'width='+ (width ? width : 500) +',';
	prop += 'height='+ (height ? height : 400);

	var win = window.open(typeof(url) == 'string' ? url : url.href, name, prop);
	win.focus();
	
	return false;
}


// Rapportera frågor

function reportQuestion(id){

	$('.reportquestion').attr('id', id);
	//alert(id);
}

