//	window open
function winOpen(URL, windowName, width, height, left, top, resizable, location, menubar, scrollbars, status, toolbar, directories){
	var windowFeatures;
	windowFeatures = '';
	if (width != '')
		windowFeatures = windowFeatures+'width='+width+',';
	if (height != '')
		windowFeatures = windowFeatures+'height='+height+',';
	if (left != '')
		windowFeatures = windowFeatures+'height='+left+',';
	if (top != '')
		windowFeatures = windowFeatures+'height='+top+',';
	if (resizable)
		windowFeatures = windowFeatures+'resizable,';
	if (location)
		windowFeatures = windowFeatures+'location,';
	if (menubar)
		windowFeatures = windowFeatures+'menubar,';
	if (scrollbars)
		windowFeatures = windowFeatures+'scrollbars,';
	if (status)
		windowFeatures = windowFeatures+'status,';
	if (toolbar)
		windowFeatures = windowFeatures+'toolbar,';
	if (directories)
		windowFeatures = windowFeatures+'directories,';
	window.open(URL, windowName, windowFeatures);
}

$(document).ready(function(){
	//	visualizza il fumetto
	function visualizzaFumetto(testo, ubC, iId, pCl, pHr) {
		var unbindClick = ubC || true,
			inputId = iId || '',
			pClass = pCl || '',
			goTo = pHr || '';
		$('<div id="bloccatuttoIscrizione"></div>').appendTo('body');
		$('<div id="fumettoReg"></div>').appendTo('body');
		$('<p class="' + pClass + '">' + testo + '</p>').appendTo('#fumettoReg').show();
		var wi = $(window).width(),
			he = $(window).height(),
			b_wi = document.documentElement.scrollWidth || document.body.scrollWidth,
			b_he = document.documentElement.scrollHeight || document.body.scrollHeight,
			sh = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop,
			bti = $('#bloccatuttoIscrizione');
		if (b_wi < wi) b_wi = wi;
		if (b_he < he) b_he = he;
		bti.css({'opacity': '0.7', height: b_he + 'px', width: b_wi + 'px'});
		$('#fumettoReg').css({left: wi/2-557/2, top: he/2-100+sh, width: '557px', height: '200px'});
		$(document).click(function(e){
			if (goTo != '') {
				window.location = goTo;
			} else {
				var $tgt = $(e.target);
				if (!$tgt.is('a')) $('#bloccatuttoIscrizione, #fumettoReg').remove();
				if (unbindClick) $(document).unbind('click');
				if (inputId != '') $('#' + inputId).val('');
			}
		});
	}
	
	//	carica ranking utente
	$("#rank").load("/rank-cmd.php");

	//	recupera password
	$('#insert').click(function(){
		var o = {};
		o._em = $('#inputSup').val();
		o._a = 'r';
			$.post('../recuperapassword-cmd.php', o, function(data){
				visualizzaFumetto(data, false, 'inputSup');
			}, "text");
		return false;
	});

	//	invia amico
	$('#inputLog').click(function(){
  		var $this = $(this);
		if ($this.val() == 'Inserisci email') $this.val('');
	});
	$('#submitLog').click(function(){
		var o = {};
		o._em = $('#inputLog').val();
		o._a = 'r';
			$.post('../inviaamico-cmd.php', o, function(data){
				visualizzaFumetto(data, false, 'inputLog')
			}, "text");
		return false;
	});

	//	sistema il div oscurante
	var bti = $('#bloccatuttoIscrizione');
	if (bti.length > 0) {
		var $p = $('#fumettoReg p'),
			testo = $p.html(),
			classe = $p.attr('class'),
			$a = $p.children('a'),
			href = '';
		$('#bloccatuttoIscrizione, #fumettoReg').remove();
		if ($a.length > 0) {
			href = $a.attr('href');
		}
		visualizzaFumetto(testo, false, '', classe, href);
	}
	
	//	paginazione sessioni 1
	$('#listaSessioni li, #sessionMenu img').click(function(e){
		e.stopPropagation();
		var $this = $(this);
		if ($this.is('.click_ok')) {
			var i = + $this.attr('id').slice(-1);
			$('div[id^=session]').hide();
			$('#session' + i.toString()).show();
			//	destra
			$('div[id^=right_sess]').hide();
			$('#right_sess_' + i.toString()).show();
			$('.on').removeClass('on');
			$('#listItem' + i.toString()).addClass('on');
			window.location = '#sessione';
		} else if ($this.is('.no_click')) {
			visualizzaFumetto('La sessione non è ancora aperta, controlla il calendario delle lezioni <a href="calendario.php">qui</a>.', false);
		}
	});
	
	
	//	paginazione test
	$('div[id^=test_page_]').not(':first').hide();
	$(document).click(function(e){
		var $tgt = $(e.target);
		if ($tgt.is('#paginazione a')) {
			var n = $tgt.text(),
				vis = $('div[id^=test_page_]:visible').attr('id').split('_')[2];
			$('div[id^=test_page_]').hide();
			$('#test_page_' + n).show();
			$tgt.parent().text(n);
			$('#link_pag_' + vis).html('<a href="#">' + vis + '</a>');
			return false;
		}
	});
	$('.test_continua').click(function(){
		var curPage = + $('div[id^=test_page_]:visible').attr('id').split('_')[2],
			as = $('#paginazione span');
		$('div[id^=test_page_]').hide();
		$('#test_page_' + (curPage+1)).show();
		as.eq(curPage).text(curPage+1);
		$('#link_pag_' + curPage).html('<a href="#">' + curPage + '</a>');
		return false;
	});
	
	//	torna il 31 ottobre
	//	n.b.: la funzione fa riferimento a "seiPreparatoLink1".
	//	per usarla sui test seguenti cambiare in seiPreparatoLink2, seiPreparatoLink3 o seiPreparatoLink4
	/*$('#seiPreparatoLink1').click(function(){
		if (!$(this).is('.testNoLog')) {
			visualizzaFumetto('Torna il 31 ottobre per fare il test, intanto continua a seguire le lezioni della Durex Academy!', false);
			return false;
		}
   	});
	$('#seiPreparatoLink2').click(function(){
		if (!$(this).is('.testNoLog')) {
			visualizzaFumetto('Torna il 16 novembre per fare il test, intanto continua a seguire le lezioni della Durex Academy!', false);
			return false;
		}
   	});
	$('#seiPreparatoLink3').click(function(){
		if (!$(this).is('.testNoLog')) {
			visualizzaFumetto('Torna il 30 novembre per fare il test, intanto continua a seguire le lezioni della Durex Academy!', false);
			return false;
		}
   	});
	$('#seiPreparatoLink4').click(function(){
		if (!$(this).is('.testNoLog')) {
			visualizzaFumetto('Torna il 15 dicembre per fare il test, intanto continua a seguire le lezioni della Durex Academy!', false);
			return false;
		}
   	});*/
	
	//	stessa cosa, ma per la videolezione
	//	quando servirà, decommentare winOpen e commentare visualizzaFumetto :)
	$('a[id^=guardavideo]').click(function(){
		var $this = $(this),
			hrf = $this.attr('href');
		if (hrf == 'videolezione1.html') {
			//visualizzaFumetto('Torna il 31 ottobre per vedere la videolezione della Professoressa Clara!', false);
			winOpen(hrf, 'videolezione', '580', '350', '', '', true, false, false, true, false, false, false);
		} else if (hrf == 'videolezione2.html') {
			//visualizzaFumetto('Torna il 16 novembre per vedere la videolezione della Professoressa Clara!', false);
			winOpen(hrf, 'videolezione', '580', '350', '', '', true, false, false, true, false, false, false);
		} else if (hrf == 'videolezione3.html') {
			//visualizzaFumetto('Torna il 30 novembre per vedere la videolezione della Professoressa Clara!', false);
			winOpen(hrf, 'videolezione', '580', '350', '', '', true, false, false, true, false, false, false);
		} else if (hrf == 'videolezione4.html') {
			//visualizzaFumetto('Torna il 15 dicembre per vedere la videolezione della Professoressa Clara!', false);
			winOpen(hrf, 'videolezione', '580', '350', '', '', true, false, false, true, false, false, false);
		}
		$this.blur();
		return false;
   	});
	
	//	click sul test utente non loggato
	$('.testNoLog').click(function(){
		visualizzaFumetto('Per effettuare il test devi essere loggato, se non hai una user ID e una password iscriviti <a href="iscrizionecorsi.php">qui</a>.', false);
		return false;
	});
	
	
	//	modifiche per chiusura concorso 20091217
	$('#iscriviti, #iscrivitiLink').click(function(){
		visualizzaFumetto('Le iscrizioni sono chiuse, torna a visitarci per scoprire il Collaudatore e Collaudatrice Durex selezionati.');
		return false;
	});
	$('#frmLogin').submit(function(){
		visualizzaFumetto('I test sono chiusi torna a visitarci per scoprire il Collaudatore e Collaudatrice Durex selezionati.');
		return false;
	});
});
