var startTotal = 0;

function countItem()
{
return $('.paginazione div.box').length;
}

function initPaginazione(nipp)
{
	$('.paginazione div.box').hide();
	viewPage(1,nipp);
	//createNav(1,nipp);
}

function viewPage(p,nipp)
{

	$('.paginazione div.box').hide();
	for(i=(((p-1)*nipp)+1);i<((p*nipp)+1);i++)
	{	
		$('.paginazione div.box:nth-child('+i+')').show();
	}
	createNav(p,nipp);
	return void(0);
}
function createNav(p,nipp)
{
	if($('.paginazione div.nav').length > 0)
	{
		$('.paginazione div.nav').remove();
	}
	var html='';	
	var ni=countItem();		
	var np=Math.ceil(ni/nipp);	
	//alert(np);
	var nippp=8;	
	var maxpagnav=Math.ceil(np/nippp);
	//alert(maxpagnav);
	var pagnavatt=Math.ceil(p/nippp);	
	var min=(pagnavatt-1)*nippp+1;	
	var max=(pagnavatt*nippp);
	
	
	
	
	var htmlprev='<a href="javascript:viewPage('+((pagnavatt-1)*nippp)+','+nipp+')" class="navprev"><img src="img/prev.png" alt="" /></a>';
	var htmlnext='<a href="javascript:viewPage('+(pagnavatt*nippp+1)+','+nipp+')" class="navnext"><img src="img/next.png" alt="" /></a>';
	
	
	if(maxpagnav == pagnavatt)
	{	
		max-=(maxpagnav*nippp*nipp-ni)/nipp;
		max=Math.ceil(max);
	}
	
	
	if(pagnavatt > 1)
	{
		html+=htmlprev;
	}
	//alert($('.paginazione div.box:nth-child(17)').length);
	for(i=min;i<=max;i++)	
	{		
			
			if(i==p)
			{
				html+='<a href="javascript:viewPage('+i+','+nipp+')" class="pag selected">'+i+'</a>';
			}
			else
			{
				html+='<a href="javascript:viewPage('+i+','+nipp+')" class="pag">'+i+'</a>';
			}
		
	}

	if(maxpagnav > pagnavatt)
	{
		html+=htmlnext;
	}
	
	$('.paginazione').append('<div class="nav"><p><span>'+html+'<br class="clear" /></span></p></div>');
	

}

function linkBox(name)
{
if($('.page-'+name).length>0)
	{		
		$('.page-'+name+' .white-inner .box').each(function(index) {
			//alert(index + ': ' + $(this).text());
			if($(this).find('a').length==1 && $(this).hasClass('exclude') == false)
			{
				MyElem = $(this);
				MyElem.data('href',$(this).find('a').attr('href'));
				
				if($(this).find('a').hasClass('blank'))
					{
						MyElem.data('class','blank');
						$(this).find('a').removeClass('blank');
						$(this).find('a').removeAttr('target');
						$(this).find('a').removeAttr('href');
					}				
				//alert($(this).data('href'));
				$(this).css('cursor','pointer');
				$(this).find('a').click(function(){return void(0);});
				$(this).click(function() {
					if($(this).data('class')=='blank')
					{					
					window.open($(this).data('href'));
					}
					else
					{				
					 window.location.href = $(this).data('href');
					 return void(0);
					}});
				
			}
		  });

	}
}




$(document).ready(function(){

	$('a.blank').attr('target','_blank');
	
	$('input.dynamic').click(function(){gestisci_costo()});
	$('input#username_psi').change(function(){gestisci_costo()});
	$('input.dynamic2').click(function(){gestisci_costo_corso()});
	$('input.dynamic3').change(function(){gestisci_costo_corso()});
	$('a.popup').bind('click', function(e){		
		info=$(this).attr('rel').split('_');
		w=info[0];
		h=info[1];
		s=info[2];
		var l = Math.floor((screen.width-w)/2);
		var t = Math.floor((screen.height-h)/2);
		valori='width='+w+',height='+h+',top='+t+',left='+l+',scrollbars='+s+',toolbar=no,location=no,status=no,menubar=no';
		window.open($(this).attr('href'),'',valori);
		return false;
	});
	/*
	$('#flash_it_home').flashembed({src: 'swf/it_home.swf', version: [9, 0], w3c: true, bgcolor: 'transparent', wmode: 'transparent'});
	$('#flash_en_home').flashembed({src: 'swf/en_home.swf', version: [9, 0], w3c: true, bgcolor: 'transparent', wmode: 'transparent'});
	*/
	/*
	
	// select the thumbnails and make them trigger our overlay
		$("#triggers a").overlay({

			// each trigger uses the same overlay with the id "gallery"
			target: '#gallery',

			// optional exposing effect
			expose: '#f1f1f1'

		// let the gallery plugin do its magic!
		}).gallery({

			// the plugin accepts its own set of configuration options
			speed: 800
		});
	*/
	
	if($('div.scrollable').length > 0)
	{
			$('div.scrollable').scrollable().find('.items a').overlay({
				target: '#gallery',			
				expose: '#0f0f0f'				
			}).gallery({				
				speed: 200,				
				activeClass:'activec',
				disabledClass:'inactive',
				next:'.n',
				prev:'.p',
				template: '<strong>${title}</strong> <span>Immagine ${index} di ${total}</span>'
			});

	}
	
	$('#login #email').focus(function() {
		if($(this).attr('value')=='indirizzo e-mail')
		{
			$(this).attr('value','');
		}
	});
	$('#login #email').blur(function() {
		if($(this).attr('value')=='')
		{
			$(this).attr('value','indirizzo e-mail');
		}
	});
	
	$('#login #pass').focus(function() {
		if($(this).attr('value')=='password')
		{
			$(this).attr('value','');
		}
	});
	$('#login #pass').blur(function() {
		if($(this).attr('value')=='')
		{
			$(this).attr('value','password');
		}
	});
	
	
	nipp=5;
	if(countItem() > nipp)
	{
		initPaginazione(nipp);	
	}
		
	
	linkBox('home');
	linkBox('attivita');
	linkBox('attivita_dettaglio');
	linkBox('seminari_dettaglio');
	linkBox('classipratiche_dettaglio');
	linkBox('photo');
	linkBox('video');
	linkBox('docenti');
	linkBox('seminari');
	linkBox('classipratiche');
	
	
	$('.page-downloads .box .hidden').hide();
	$('.page-downloads .box a.openclose').each(function(index) {
		$(this).click(function() {
		  var display=$(this).parent().parent().children('.hidden').css('display');
		  //alert(display);
		  //alert($(this).text());
		  
		  if(display=='block')
		  {
			$(this).parent().parent().children('.hidden').hide();
			$(this).html('Descrizione completa - Clicca qui  <img src="img/arr3.gif" alt="" />');			
		  }
		  if(display=='none')
		  {
			$(this).parent().parent().children('.hidden').show();
			$(this).html('Chiudi <img src="img/arr3.gif" alt="" />');
			
		  }
		  return false;
		});
		
	
	});
	$("a#intro").fancybox({
				'overlayShow'	: true,
				'transitionIn'	: false,
				'transitionOut'	: true,
				'speedOut'		: 1000,
				'showCloseButton': false,
				'overlayOpacity' : 1,
				'overlayColor' : '#2A2A2A',
				'onComplete'	: function(){autoClose()}
			}).trigger('click');

});
function autoClose()
{
	$('.page-home').show();
	setTimeout('closeFancyShowHome()', 4000);
}
function closeFancyShowHome()
{
	$.fancybox.close();
	//setTimeout('$(\'.page-home_mod\').fadeIn()', 200);
}
function gestisci_costo()
{
	var corso_1 = $('#seminario_1').attr('checked');
	var corso_2 = $('#seminario_2').attr('checked');
	var codice = $('#username_psi').val();
	var have_code = false;
	var nome_promo = '';
	var text = '';
	var postext = '';
	if(codice!='')
	{
		switch(codice.toUpperCase())
		{
			case 'PSITALIA':
				nome_promo = 'Photoshop Italia';
				have_code = true;
			break;
			case 'SCATTODESIGN':
				nome_promo = 'Scatto Design';
				have_code = true;
			break;
			case 'BIGANO':
				nome_promo = 'Bigano Group';
				have_code = true;
			break;
			case 'MACITY':
				nome_promo = 'Macity';
				have_code = true;
			break;
			case 'JUMPER':
				nome_promo = 'Jumper';
				have_code = true;
			break;
			case 'PSDREVOLUTION':
				nome_promo = 'PSDR evolution';
				have_code = true;
			break;
			default:
				postext = ' <b>Il codice sconto inserito non &egrave; valido</b>';
			break;
		}
	}
	if(corso_1)
	{
		if(have_code)
		{
			text = '142,8 &euro; (IVA inclusa)<br />&egrave; stato applicato lo sconto del 20% grazie al codice <b>'+nome_promo+'</b> inserito.';
		} else {
			text = '178,8 &euro; (IVA inclusa)';
		}
	}
	if(corso_2)
	{
		if(have_code)
		{
			text = '285,6 &euro; (IVA inclusa)<br />&egrave; stato applicato lo sconto del 20% grazie al codice <b>'+nome_promo+'</b> inserito.';
		} else {
			text = '321,6 &euro; (IVA inclusa)<br />&egrave; stato applicato lo sconto del 10% per aver scelto di partecipare ad entrambi gli eventi.';
		}
	}
	if(text != '')
	{
		$('#testocosto').val(text+postext);
		$('#rif_costo').html(text+postext);
	}
}
function gestisci_costo_corso()
{
	var corso_1 = $('#corso_1').attr('checked');
	var corso_2 = $('#corso_2').attr('checked');
	var pasti = $('#pasti').val();
	var notti = $('#pernottamenti').val();
	var costo = 0;
	var text = '';
	var postext = '';
	
	if(corso_1)
	{
		costo = 2274;
	}
	if(corso_2)
	{		
		costo = 2382;
	}
	if(costo > 0)
	{
		/*if(parseInt(notti))
		{
			costo = costo+(parseInt(notti)*50);
		}
		if(parseInt(pasti))
		{
			costo = costo+(parseInt(pasti)*25);
		}*/
		text = costo+',00 &euro; (IVA inclusa)';
	
	}
	if(text != '')
	{
		$('#testocosto').val(text+postext);
		$('#rif_costo').html(text+postext);
	}
}
//FUNZIONE UTILIZZATA PER MODIFICARE L'IMMAGINE CAPTCHA
function ricarica()
{
	document.getElementById("imgcaptcha").src = 'captcha/captcha.php?c='+ (new Date()).getTime();
}
