/******************************************************************************************************************/
// Tabs Handle
/******************************************************************************************************************/

function handleTabs(){
	$('.tabs .tab').hide();
	
	$('.tabs .sel').each(function(){
		initid = $(this).find('A').attr('href');
		$(initid).show();
	});
	
	$('.tabs .labels A').click(function(){
		$(this).parents('.tabs').find('.labels LI').removeClass('sel');
		$(this).parent().addClass('sel');
		
		myid = $(this).attr('href');
		
		$(this).parents('.tabs').find('.tab').hide();
		//$(myid).fadeIn(300);
		$(myid).show();
		
		return false;
	});
}


/******************************************************************************************************************/
// Top3 tooltip function
/******************************************************************************************************************/

function handleTop3(){
	$('#top3 .info').hide();
	
	$('#top3 .info').css({ opacity: 0.9, top: '0px', left: '0px' });
	
	$('#top3 LI').hover(function(){
		pos = $(this).position();
		t = pos.top - 75;
		l = pos.left - 36;
		$(this).find('.info').css({ top: t, left: l });
		$(this).find('.info').fadeIn(200);
	}, function(){
		$(this).find('.info').hide();
	});
}

/******************************************************************************************************************/
// Spots slide functions
/******************************************************************************************************************/

function SlideRun(i){
	if (i == 0) {
		$('.slide LI:eq('+ i +')').animate({marginTop: '50px', opacity: 0}, 500, function(){
			$('.slide LI').css('marginTop','0px');
			$('.slide LI').animate({opacity: 1}, 500);
		});
	}
	else $('.slide LI:eq('+ i +')').animate({marginTop: '50px', opacity: 0}, 500);
		
	i--;
	if (i < 0) { i = $('.slide LI').size() - 1 };
	SlideTimerId = window.setTimeout('SlideRun('+ i +')', 4000);
}

function SlideInit(){
	snum = $('.slide LI').size();
	if (snum > 1) SlideTimerId = window.setTimeout('SlideRun('+ (snum - 1) +')', 3000);
}

/******************************************************************************************************************/
// Forms validation function
/******************************************************************************************************************/

function validateForms(){
	// login form validation
	$('#form-login').validate({
		errorElement: 'div',
		rules: {
			log: 'required',
			pwd: 'required',
			user_login: 'required'
		},
		messages: {
			log: '&laquo; campo obbligatorio',
			pwd: '&laquo; campo obbligatorio',
			user_login: '&laquo; campo obbligatorio'
		}
	});
	
	// registration form validation
	$('#form-register, #form-register2').validate({
		errorElement: 'div',
		rules: {
			nome: 'required',
			cognome: 'required',
			email: 'required',
			log: 'required',
			pwd: 'required',
			cap: 'required',
			privacy: 'required'
		},
		messages: {
			nome: '&laquo; campo obbligatorio',
			cognome: '&laquo; campo obbligatorio',
			email: '&laquo; campo obbligatorio',
			log: '&laquo; campo obbligatorio',
			pwd: '&laquo; campo obbligatorio',
			cap: '&laquo; campo obbligatorio',
			privacy: '&laquo; campo obbligatorio'
		}
	});

	// profile form validation
	$('#form-profile').validate({
		errorElement: 'div',
		rules: {
			nome: 'required',
			cognome: 'required',
			email: 'required',
			cap: 'required'
		},
		messages: {
			nome: '&laquo; campo obbligatorio',
			cognome: '&laquo; campo obbligatorio',
			email: '&laquo; campo obbligatorio',
			cap: '&laquo; campo obbligatorio'
		}
	});
	
	// contact form validation
	$('#form-contatti').validate({
		errorElement: 'div',
		rules: {
			email: 'required',
			testo: 'required'
		},
		messages: {
			email: '&laquo; campo obbligatorio',
			testo: '&laquo; campo obbligatorio'
		}
	});
}

/******************************************************************************************************************/
// Lotteria function
/******************************************************************************************************************/

function handleLotteria(){
	selected = 0;
	
	// card handle
	$('#card TD').hover(function(){
		if (!$(this).hasClass('sel') && !$(this).parents('TABLE').hasClass('disabled')) $(this).css('background','#BAEAD0');
	}, function(){
		if (!$(this).hasClass('sel') && !$(this).parents('TABLE').hasClass('disabled')) $(this).css('background','transparent');
	});
	
	
	$('#card TD').click(function(){
		if (!$(this).hasClass('sel')){
			selected++;
			if (selected <= 6) {
				$(this).addClass('sel');
				selector = '.n'+selected;
				$(selector).find('STRONG').text($(this).text());
				$(selector).find('INPUT').attr('value', $(this).text());
			}
			
			if (selected >= 6) $('#card').addClass('disabled');
		}
	});
		
	// delete selection
	$('#numbers .delete A').click(function(){
		$('#card TD').removeClass('sel');
		$('#card TD').css('background','transparent');
		$('#numbers LI STRONG').text('?');
		
		$('#card').removeClass('disabled');
		
		selected = 0;
		
		return false;
	});
	
	// banners handle
	$('#banners A').click(function(){
		if (selected >= 6) {
		
			// link pupunder
			var width = '800';
			var height = '600';
			var p = 'scrollbars=yes,resizable=yes,toolbar=yes,' +
			'menubar=yes,status=yes,location=yes,left=85,top=20,height=' +
			height + ',width=' + width;
			
			myurl = $(this).attr('href');
			
			win2=window.open(myurl,'',p);
			win2.blur();
			window.focus();
		
			// form submit
			$('#form-lotteria').submit();
			
			return false;
		}
		else {
			alert('Devi scegliere 6 NUMERI prima di convalidare la schedina!');
			return false;
		}
	});
}

/******************************************************************************************************************/
// Tombola function
/******************************************************************************************************************/

function handleTombola( ){
	selected = 0;

	if (typeof(acquistate) != 'undefined')	{	return;	}

	max = $('.active').size();
	cost = 150;
	tot = 0;
	
	$('#cards .active').toggle(function(){
		if (!$(this).hasClass('sel')) {
			selected++;
			if (selected <= max) {
				tot = selected * cost;
				$('#play .points STRONG').text(tot);
				$('#play .points INPUT').attr('value', tot);
				
				$(this).removeClass('active');
				$(this).addClass('sel');
				
				$(this).find('.selected').attr('value','true');
			}
			else selected = max;
		}
	}, function(){
		selected--;
		if (selected >= 0) {
			tot = selected * cost;
			$('#play .points STRONG').text(tot);
			$('#play .points INPUT').attr('value', tot);
				
			$(this).removeClass('sel');
			$(this).addClass('active');
			
			$(this).find('.selected').attr('value','false');
		}
		else selected = 0;
	});
}
function getFlashMovieObject(movieName) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;   
    return (isIE) ? window[movieName] : document[movieName];
}

function userID( adv ) {
    var swf = getFlashMovieObject("advcocc");
    swf.setCookie( adv );
}
function chkAdv()	{
	var swf = getFlashMovieObject("advcocc");
	var myAdvCook = swf.getCookie( );
	return myAdvCook;
}
/******************************************************************************************************************/
// ON DOCUMENT LOAD...
/******************************************************************************************************************/

$(function(){
	
	// Validate forms
	validateForms();
	
	// Top3 tooltip function
	handleTop3();
	
	// Spots slide functions
	SlideInit();
	
	// Tabs handle
	handleTabs();
	
	// Lotteria
	handleLotteria();
	
	// Tombola
	handleTombola();
	
	// Manage profile
	$('#form-profile').hide();
	
	$('#profile .manage H2').toggle(function(){
		$('#form-profile').fadeIn(300);
		$(this).css({ backgroundPosition: '0px -50px' });
	}, function(){
		$('#form-profile').hide();
		$(this).css({ backgroundPosition: '0px 0px' });
	});
	
	$('#form-schedine SELECT').change(function(){ $('#form-schedine').submit(); });

	$('#lstpsw').click(function() {
		$('#loginbox').hide().find('INPUT').attr("disabled",true);
		$('#resetbox').show().find('INPUT').removeAttr("disabled");

		return false;
	});

	$('#bklogin').click(function() {
		$('#loginbox').show().find('INPUT').removeAttr("disabled");
		$('#resetbox').hide().find('INPUT').attr("disabled",true);

		return false;
	});
	
	$('#delete').click(function(){ 
		answer = confirm('Sei veramente sicuro di voler rimuovere il tuo account?')
		if (answer){
			$('#del').attr('value','true');
			$('#form-profile').submit(); 
		}
		else {
			return false;		
		}
	});
	
	// Invita amici
	$('#form-invita .friendlist').append('<ul></ul>');
	
	$('#form-invita .add').click(function(){
		femail = $('#femail').attr('value');
		if (femail != "") {
			$('#femail').attr('value','');
			$('#form-invita .friendlist UL').append('<li>'+ femail +' <span class="remove">RIMUOVI</span> <input type="hidden" name="friendmail[]" value="'+ femail +'"/></li>');
			$('#form-invita .remove').click(function(){ $(this).parent().remove(); });
		}
	});
	
	$('#form-invita').submit(function(){
		if ($(this).find('.friendlist LI').size() == 0) {
			alert('Inserisci almeno l\'email di un amico!');
			return false;
		}
	});
});

// If the element's string matches the regular expression it is numbers and letters
function isAlphaNumeric( ID , formID )	{
	var elem	= document.getElementById( ID );
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if (elem.value == '')	{	return true;	}
	if(elem.value.match(alphaExp)){
		var myForm	= document.getElementById( formID );
		//document.form-register2.submit();
		myForm.submit();
		return true;
	} else {
		alert( 'Inserisci una username valida, solo numeri e lettere!' );
		elem.focus();
		return false;
	}
}

