var disclaimer = getCookie('diochan_disc');
var embedjs = getCookie('embedjs');
var updateposts = getCookie('updateposts');
var lastvisit = getCookie('lastvisit_'+ku_board);
var laststring = '';

$(function() {
	var checkcaptcha = getCookie('captcha');
	if (checkcaptcha != '1') {
		$.get("/inc/func/captcha.php", function (data) {
			if (data == '1') {
				set_cookie('captcha',1,1);
				$('.captcha').remove();
			}
		});
	}
	
	$(window).scroll(function(){
		var activeload = getCookie('activeload');
		if (activeload == '1' && p4g3 < 11 &&
		document.getElementById('loading') == null &&
		$(window).scrollTop() == $(document).height() - $(window).height()) {
			$('#page'+p4g3).append('<img src="'+ku_boardspath+'/images/ajax-loader.gif" alt="loading" id="loading" />');
			$('<div id="n3wp4g3" />').load(ku_boardspath+'/'+ku_board+'/'+(p4g3+1)+'.html #page'+(p4g3+1), function() {
				p4g3 += 1;
				$(this).appendTo('#page'+(p4g3-1));
				$('#loading').fadeOut(500, function(){
					$(this).remove();
				});
				addpreviewevents();
				delandbanlinks();
			});
		}
	});
	
	$('#postform_comment').change(function() {
		if ($('#postform_comment').val() != $('#fakearea').val())
			$('#fakearea').val($('#postform_comment').val());
	});
	
	$('#fakearea').change(function() {
		if ($('#fakearea').val() != $('#postform_comment').val())
			$('#postform_comment').val($('#fakearea').val());
	});
	
	$('#fake_sage').change(function() {
		if($('#fake_sage:checked').val() == 'on') {
			$("[name=em]").val('salvia');
		} else {
			$("[name=em]").val('');
		}
	});
	
	var timestamp = Math.round(new Date().getTime() / 1000);
	var pass = 0;
	if (lastvisit != '') {
		if (updateposts != '0') {
			set_cookie('lastvisit_'+ku_board,timestamp,30);
			if (updateposts > 5)
				updateposts = 1;
			updateboards();
		}
	} else {
		set_cookie('lastvisit_'+ku_board,timestamp,30);
	}

	
	$('#recaptcha_logo').remove();
	$('#recaptcha_tagline').remove();
	
	var giornalino = getCookie('giornalino');
	if (giornalino == '1')
		$('blockquote').addClass('giornale');
		
	// var replythread = $('#postform [name=replythread]').val();
	// if (replythread != 0)
		// quickreply(replythread);
});

function check_captcha() {
	var checkcaptcha = getCookie('captcha');
	if (checkcaptcha != '1') {
		$.get("/inc/func/captcha.php", function (data) {
			if (data == '1') {
				set_cookie('captcha',1,1);
				$('.captcha').remove();
			}
		});
	} else
		$('.captcha').remove();
}

function updateboards() {
	var merda = '';
	$.getJSON("/lastlist.json",	function(data) {
		$.each(data.boards, function(i,board) {
			if (updateposts == 2 || updateposts == 4)
				merda = board.t;
			else
				merda = board.p;
			var boardlast = getCookie('lastvisit_'+board.name);
			if (boardlast != '' && boardlast < merda) {
				if (updateposts > 2) {
					//show linguetta
				}
				$('a[href$="/'+board.name+'/"]').addClass('newposts');
			}
		});
	});
}

function espandithread(pagina, divid) {		
	$('<div id="container" />').load(ku_boardspath + '/read.php?'+pagina, function () {
		$(this).hide()
		.appendTo('#'+divid+' > blockquote')
		.slideDown(1000);
	});
	
	$('#'+divid+' .abbrev').hide()
}

function removevideo(videoid) {
	if (embedjs == '1') {
		$('object .video'+videoid).replaceWith('<a href="http://www.youtube.com/watch?v='+videoid+'"><img src="http://img.youtube.com/vi/'+videoid+'/default.jpg" /></a>');
	}
}

function resizevideo(object) {
	if ( $('.'+object).attr('width') == '560' ) {
		//ingrandisce se piccolo
		$('.'+object).attr({ width: "300",height: "243" });
	} else {
		//rimpicciolisce se grande
		$('.'+object).attr({ width: "560",height: "340" });
	}
}

function hidethread(numero) {
	if ($.browser.safari || ku_board == '*') {
		document.getElementById('unhidethread' + numero).style.display = 'block';
		document.getElementById('thread' + numero).style.display = 'none';
	} else {
		$("#thread"+numero).slideUp(1000, function() {
			$('#unhidethread'+numero).css('display', 'block');
		});
	}
	hiddenthreads.push(numero);
	set_cookie('hiddenthreads',hiddenthreads.join('!'),30);
}

function unhidethread(numero) {
	if ($.browser.safari || ku_board == '*') {
		document.getElementById('unhidethread' + numero).style.display = 'none';
		document.getElementById('thread' + numero).style.display = 'block';
	} else {
		$('#unhidethread'+numero).css('display', 'none');
		$('div#thread'+numero).slideDown(1000);
	}
	hiddenthreads.splice(hiddenthreads.indexOf(numero),1);
	set_cookie('hiddenthreads',hiddenthreads.join('!'),30);
}

function exptoggle() {
	$('table.userdelete').toggle("medium");
}

function togglefunz(funzione) {
	var func = getCookie(funzione);
	var testo = "Abilitato";
	if (func == '1') {
		set_cookie(funzione, '0', 30);
		alert('La funzione e\' ora disabilitata.');
		testo = "Disabilitato";
	} else {
		set_cookie(funzione, '1', 30);
		alert('La funzione e\' ora abilitata.');
		testo = "Abilitato";
	}
	document.getElementById(funzione).innerHTML = testo;
}

function toggleTextareaSize(textareaId, cols1, rows1, cols2, rows2) {
	if (($('#'+textareaId).attr('rows') == rows1) && ($('#'+textareaId).attr('cols') == cols1)) {
		$('#'+textareaId).attr({
		rows: rows2,
		cols: cols2
		});
	} else {
		$('#'+textareaId).attr({
		rows: rows1,
		cols: cols1
		});
	}
}

var menu_current = '';
function menu_show(id) {
	if(menu_current != '') {
		document.getElementById(menu_current).style.display = 'none';
	}
	if(id != '' && document.getElementById(id)) {
		document.getElementById(id).style.display = 'block';
		menu_current = id;
	}
}

jQuery.fn.center = function (absolute) {
	return this.each(function () {
		var t = jQuery(this);

		t.css({
			position:	absolute ? 'absolute' : 'fixed', 
			left:		'50%', 
			top:		'50%', 
			zIndex:		'99'
		}).css({
			marginLeft:	'-' + (t.outerWidth() / 2) + 'px', 
			marginTop:	'-' + (t.outerHeight() / 2) + 'px'
		});

		if (absolute) {
			t.css({
				marginTop:	parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(), 
				marginLeft:	parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
			});
		}
	});
};

function espandipic(id, orig_w, orig_h, thumb_w, thumb_h) {
	var src = $('#thumb'+id+' > img').attr('src');
	var info = src.split('/');
	var pic_name = info[5].split('.');
	//se siamo nella thumb
	if(isNaN(pic_name[0])) {
		var pic_length = pic_name[0].length -1;
		var pic_id = pic_name[0].substring(0, pic_length);
		$('#thumb'+id+' > img').attr({
			src: ku_boardspath+ '/' + ku_board + '/src/' + pic_id + '.' + pic_name[1],
			width: orig_w,
			height: orig_h
		});
	} else {
		var pic_length = pic_name[0].length;
		var pic_id = pic_name[0].substring(0, pic_length);
		$('#thumb'+id+' > img').attr({
			src: ku_boardspath+ '/' + ku_board + '/thumb/' + pic_id + 's.' + pic_name[1],
			width: thumb_w,
			height: thumb_h
		});
	}
}

function activatedisclaimer() {
	if (ku_board == 'int')
		var discla = 'disclaimer_en.htm';
	else
		var discla = 'disclaimer.htm';
	$('<div id="di5c141m3r" />').load('http://www.diochan.com/'+discla, function() {
		$(this).appendTo('#asdisclaimer');
		$(".disclaimer_screen").fadeTo('slow', '0.9');
		$(".disclaimer_screen").center();
		$(".disclaimer").center();					
	});
}

function attivanavigatore() {
	if (ku_board == 'int')
		var naviga = 'navigator.htm';
	else
		var naviga = 'navigatore.htm';
	$('<div id="n4v1g4t0r3" />').load('http://www.diochan.com/'+naviga, function() {
		$(this).appendTo('#adminbar_2');
		$('#adminbar_2').css('display', 'block');
		$('.adminbar').css('display', 'none');
		$('.navbar').css('display', 'none');
	});
	document.write('<br /><br />');
}

function input_check(field, isfake) {
	if (isfake)
		$('#'+field).val($('#fake_'+field).val());
	if (ispage && ($('#postform [name=replythread]').val() == '0')) {
		$('#file_2').val('');
		$('#file_3').val('');
		$('#fileurl_2').val('');
		$('#fileurl_3').val('');
	}
	if ( ($('#file_1').val() != '' || $('#file_2').val() != '' || $('#file_3').val() != '' ) && 
	($('#fileurl_1').val() != '' || $('#fileurl_2').val() != '' || $('#fileurl_3').val() != '')) {
		$('#fileurl_1').val('');
		$('#fileurl_2').val('');
		$('#fileurl_3').val('');
		$('.fileurl2').hide();
		$('.fileurl3').hide();
	}
	var file_field = field.split('_');
	if ($('#'+field).val() != '' && parseFloat(file_field[1]) < 3 && $('#postform [name=replythread]').val() != '0') {
		$('.'+file_field[0]+(parseFloat(file_field[1])+1)).show();
	}
}
 
