$(document).ready(function() {

	// age check
	$('#age_check .content .text .age_check .button').click(function() {
		var value = $(this).attr('rel');
		if ($('#age_check .content .text .age_check .checkbox').hasClass('checked')) {
			$.cookie('age_check', value, { expires: 365 });
		}
	});

	$('#age_check .content .text .age_check .checkbox').click(function() {
		$(this).toggleClass('checked');
	});
  
  //////////////////////////////
  
  // candidates - default filters	
	var can_page = 1;
  var can_category = 1;
  var can_from_page = 1;
  var can_to_page = 1;
  var can_pages = 1;
  var can_category = 1;
  
  // candidates - function
  function candidates() {
    $.ajax({
      url: 'ajax/candidates.php',
      type: 'post',
      data: 'page='+can_page+'&category='+can_category,
      dataType: 'html',
      success: function(data) {
        $('#heroes-2 .content .popup .pagination ul').empty();
        $('#heroes-2 .content .popup .ajax').html(data);
        // create pagination
        can_pages = Math.ceil($('#heroes-2 .content .popup .profiles').attr('rel')/4);
        if (can_page < 3) {
            can_from_page = 1;
            if (can_pages < 5) {
              can_to_page = can_pages;
            } else {
              can_to_page = 5;
            }
        } else {
          if (can_page < (can_pages - 2)) {
            can_from_page = can_page - 2;
            can_to_page = can_page + 2;
          } else {
            can_from_page = can_pages - 4;
            can_to_page = can_pages;
          }
        }
        
        for (var i = can_from_page; i <= can_to_page; i++){
          if (i < can_to_page){
            if(can_from_page > 1 && i == can_from_page) {
              $('#heroes-2 .content .popup .pagination ul').append('<li>... <a href="javascript:void(0)">'+i+'</a> - </li>');
            } else {
              $('#heroes-2 .content .popup .pagination ul').append('<li><a href="javascript:void(0)">'+i+'</a> - </li>');
            }
          } else {
            if(can_to_page < can_pages && i == can_to_page) {
              $('#heroes-2 .content .popup .pagination ul').append('<li><a href="javascript:void(0)">'+i+'</a> ...</li>');
            } else {
              $('#heroes-2 .content .popup .pagination ul').append('<li><a href="javascript:void(0)">'+i+'</a></li>');
            }
          }
        }
        $('#heroes-2 .content .popup .pagination ul li:nth-child('+ (can_page - can_from_page + 1) +') a').addClass('active');
      }
    });
  }
  
  candidates();
  
  // candidates - pagination
  $('#heroes-2 .content .popup .pagination ul li a').live('click', function() {
		if (!$(this).hasClass('active'))
		{
			$('#heroes-2 .content .popup .ajax').empty();
			$('#heroes-2 .content .popup .pagination ul li a.active').removeClass('active');
			$(this).addClass('active');
			can_page = parseInt($(this).text());
			candidates();
		}
	});
	
	$('#heroes-2 .content .popup .previous, #heroes-2 .content .popup .next').live('click', function() {
		if ($(this).hasClass('previous')) {
			if (can_page-1 > 0) {
        $('#heroes-2 .content .popup .ajax').empty();
				can_page = can_page-1;
			} else {			
				return false;
			}
		} else {
			if (can_page+1 <= can_pages) {
        $('#heroes-2 .content .popup .ajax').empty();
				can_page = can_page+1;	
			} else {
				return false;
			}
		}
		$('#heroes-2 .content .popup .pagination ul li a.active').removeClass('active');
		candidates();
	});
  
  //candidates category
	$('#heroes-2 .content .popup .nav a').click(function() {
		if (!$(this).hasClass('active')) {
      $('#heroes-2 .content .popup .ajax').empty();
      can_page = 1;
			$('#heroes-2 .content .popup .nav a.active').removeClass('active');
			$(this).addClass('active');
			can_category = parseInt($(this).index()) + 1;
			candidates();
		}
	});
  
	//////////////////////////////

	// events - default filters	
	var eve_page = 1;
  var eve_pages = 1;
  var eve_from_page = 1;
  var eve_to_page = 1;
	var eve_year = 2011;
	var eve_month = 'all';
	var eve_location = 'all';

	// events - function
	function events() {
		$.ajax({
			url: 'ajax/events.php',
			type: 'post',
			data: 'page='+eve_page+'&year='+eve_year+'&month='+eve_month+'&location='+eve_location,
			dataType: 'html',
			success: function(data) {
				$('#ceres_night-2 .content .pagination ul').empty();
				$('#ceres_night-2 .content .events .ajax').html(data);
				// create pagination
				eve_pages = Math.ceil($('#ceres_night-2 .content .events .list').attr('rel')/5);
        if (eve_page < 5){
          eve_from_page = 1;
          if (eve_pages < 9) {
            eve_to_page = eve_pages;
          } else {
            eve_to_page = 9;
          }
        } else {
          if (eve_page < (eve_pages - 4)) {
            eve_from_page = eve_page - 4;
            eve_to_page = eve_page + 4;
          } else {
            eve_from_page = eve_pages - 8;
            eve_to_page = eve_pages;
          }
        }
        
        for (var i = eve_from_page; i <= eve_to_page; i++){
          if (i < eve_to_page){
            if(eve_from_page > 1 && i == eve_from_page) {
              $('#ceres_night-2 .content .pagination ul').append('<li>... <a href="javascript:void(0)">'+i+'</a> - </li>');
            } else {
              $('#ceres_night-2 .content .pagination ul').append('<li><a href="javascript:void(0)">'+i+'</a> - </li>');
            }
          } else {
            if(eve_to_page < eve_pages && i == eve_to_page) {
              $('#ceres_night-2 .content .pagination ul').append('<li><a href="javascript:void(0)">'+i+'</a> ...</li>');
            } else {
              $('#ceres_night-2 .content .pagination ul').append('<li><a href="javascript:void(0)">'+i+'</a></li>');
            }
          }
        }
				$('#ceres_night-2 .content .pagination ul li:nth-child(' + (eve_page - eve_from_page + 1) + ') a').addClass('active');
			}
		});
	}

	events();
	
	// events - pagination
	$('#ceres_night-2 .content .pagination ul li a').live('click', function() {
		if (!$(this).hasClass('active'))
		{
			$('#ceres_night-2 .content .events .ajax').empty();
			$('#ceres_night-2 .content .pagination ul li a.active').removeClass('active');
			$(this).addClass('active');
			eve_page = parseInt($(this).text());
			events();
		}
	});
	
	$('#ceres_night-2 .content .pagination .previous, #ceres_night-2 .content .pagination .next').live('click', function() {
		if ($(this).hasClass('previous')) {
			if (eve_page-1 > 0) {
        $('#ceres_night-2 .content .events .ajax').empty();
				eve_page = eve_page-1;
			} else {			
				return false;
			}
		} else {
			if (eve_page+1 <= eve_pages) {
        $('#ceres_night-2 .content .events .ajax').empty();
				eve_page = eve_page+1;	
			} else {
				return false;	
			}
		}
		$('#ceres_night-2 .content .pagination ul li a.active').removeClass('active');
		events();
	});
	
	// events - year
	$('#ceres_night-2 .content .events .years li a').click(function() {
		if (!$(this).hasClass('active')) {
      $('#ceres_night-2 .content .events .ajax').empty();
      eve_page = 1;
			$('#ceres_night-2 .content .events .years li a.active').removeClass('active');
			$(this).addClass('active');
			eve_year = parseInt($(this).text());
			events();
		}
	});

	// events - month
	$('#ceres_night-2 .content .events .months li a').click(function() {
		if (!$(this).hasClass('active')) {
      $('#ceres_night-2 .content .events .ajax').empty();
      eve_page = 1;
			$('#ceres_night-2 .content .events .months li a.active').removeClass('active');
			$(this).addClass('active');
			if ($(this).parent('li').hasClass('all')) {
				eve_month = 'all';
			} else {
				eve_month = $(this).text();
			}
			events();
		}
	});

	// events - location
	$('#ceres_night-2 .content .events .location .select .choose').click(function() {
		$('#ceres_night-2 .content .events .location .select ul').slideToggle(300);
	});
	
	$('#ceres_night-2 .content .events .location .select ul li a').click(function() {
		$('#ceres_night-2 .content .events .ajax').empty();
		eve_page = 1;
		$('#ceres_night-2 .content .events .location .select span').html($(this).text());
		$('#ceres_night-2 .content .events .location .select ul').slideUp(300);
			eve_location = $(this).text();
			events();
	});	

	//////////////////////////////

	// the_beer slider
	var slides = $('#the_beer .content .slider img').size();
	var show = 1;
	$('#the_beer .content .slider-nav a').click(function() {
		if ($(this).hasClass('previous')) {
			show = show-1;
		} else {
			show = show+1;
		}
		if (show > slides) {
			show = 1;
		} else if (show < 1) {
			show = slides;
		}
		$('#the_beer .content .slider img:visible').hide();
		$('#the_beer .content .slider-nav ul li:visible').hide();
		$('#the_beer .content .details div:visible').hide();
		$('#the_beer .content .slider img:nth-child('+show+')').fadeIn(500);
		$('#the_beer .content .slider-nav ul li:nth-child('+show+')').show();
		$('#the_beer .content .details div:nth-child('+show+')').fadeIn(500);
	});
	
	//////////////////////////////
	
	// heroes-2 slider
	$('#heroes-2 .content nav .button-video').click(function() {
		$('#heroes-2 .content nav .button-classification').removeClass('active');
		$('#heroes-2 .content .classification').hide();
		$(this).addClass('active');
		$('#heroes-2 .content .video').fadeIn(500);
	});
	
	$('#heroes-2 .content nav .button-classification').click(function() {
		$('#heroes-2 .content nav .button-video').removeClass('active');
		$('#heroes-2 .content .video').hide();
		$(this).addClass('active');
		$('#heroes-2 .content .classification').fadeIn(500);
	});
	
	$('#heroes-2 .content nav .arrow').click(function() {
		if ($('#heroes-2 .content nav .button-video').hasClass('active')) {
			$('#heroes-2 .content nav .button-video').removeClass('active');
			$('#heroes-2 .content .video').hide();
			$('#heroes-2 .content nav .button-classification').addClass('active');
			$('#heroes-2 .content .classification').fadeIn(500);
		} else {
			$('#heroes-2 .content nav .button-classification').removeClass('active');
			$('#heroes-2 .content .classification').hide();
			$('#heroes-2 .content nav .button-video').addClass('active');
			$('#heroes-2 .content .video').fadeIn(500);		
		}
	});
	
	// heroes-2 popup
	$('#heroes-2 .content .classification .more').click(function() {
		$('#popup-overlay').fadeIn(500);
		$('#heroes-2 .popup').fadeIn(500);
	});
	
	$('#heroes-2 .popup > .close').click(function() {
		$('#popup-overlay').fadeOut(500);
		$('#heroes-2 .popup').fadeOut(500);
	});
	
	// heroes-2 popup nav
	$('#heroes-2 .popup .nav a').click(function() {
		if (!$(this).hasClass('active')) {
			$('#heroes-2 .popup .nav a.active').removeClass('active');
			$(this).addClass('active');
		}
	});
	
	// heroes-2 popup details
	$('#heroes-2 .popup .profiles li a[rel="details"]').click(function() {
		$(this).parents('li').children('.details').fadeIn(500);
	});
	
	$('#heroes-2 .popup .profiles .details .close').click(function() {
		$(this).parent('.details').fadeOut(500);
	});
	
	// heroes-2 popup pagination
	$('#heroes-2 .popup .pagination ul li a').click(function() {
		if (!$(this).hasClass('active')) {
			$('#heroes-2 .popup .pagination ul li a.active').removeClass('active');
			$(this).addClass('active');
		}
	});
	
	//////////////////////////////
	
	// heroes-3 slider
	$('#heroes-3 .content > nav .button').click(function() {
		var clicked = $(this).index();
		if (!$(this).hasClass('active')) {
			$('#heroes-3 .content > nav .button.active').removeClass('active');
			$('#heroes-3 .content > .text > div:visible').hide();
			$('#heroes-3 .content > .quotes:visible').hide();
			$('#heroes-3 .content > img.hero:visible').hide();
			$(this).addClass('active');	
			$('#heroes-3 .content > .text > div:nth-child('+clicked+')').fadeIn(500);
			$('#heroes-3 .content > .quotation-'+clicked+'').fadeIn(500);
			$('#heroes-3 .content > img.hero-'+clicked+'').fadeIn(500);
		}	
	});
	
	$('#heroes-3 .content > nav .arrow').click(function() {
		var active = $('#heroes-3 .content > nav .button.active').index();
		if ($(this).hasClass('previous')) {
			if (active-1 != 0) {
				var next = active-1;			
			} else {
				var next = 4;	
			}	
		} else {
			if (active+1 < 5) {
				var next = active+1;			
			} else {
				var next = 1;	
			}	
		}
		$('#heroes-3 .content > nav .button.active').removeClass('active');
		$('#heroes-3 .content > .text > div:visible').hide();
		$('#heroes-3 .content > .quotes:visible').hide();
		$('#heroes-3 .content > img.hero:visible').hide();
		$('#heroes-3 .content > nav .button-hero-'+next+'').addClass('active');	
		$('#heroes-3 .content > .text > div:nth-child('+next+')').fadeIn(500);
		$('#heroes-3 .content > .quotation-'+next+'').fadeIn(500);
		$('#heroes-3 .content > img.hero-'+next+'').fadeIn(500);
	});
	
	// heroes-3 video
	$('#heroes-3 .content .text .play a').click(function() {
		$(this).parents('div').children('.video').fadeIn(500);
	});
	
	$('#heroes-3 .content .text .video .close').click(function() {
		$(this).parent('div').fadeOut(500);
	});

	// heroes-3 popup
	$('#heroes-3 .content > .candidate a').click(function() {
    $('#heroes-3 .popup-3').hide();
    $('#heroes-3 .popup-2').hide();
    $('#heroes-3 .popup-1').show();
		$('#popup-overlay').fadeIn(500);
		$('#heroes-3 .popup').fadeIn(500);
	});
	
	$('#heroes-3 .popup > .close').click(function() {
		$('#popup-overlay').fadeOut(500);
		$('#heroes-3 .popup').fadeOut(500);
	});

	// heroes-3 popup nav
	$('#heroes-3 .popup-1 > nav a').click(function() {
		var clicked = $(this).index()+1;
		if (!$(this).hasClass('active')) {
			$('#heroes-3 .popup-1 > nav a.active').removeClass('active');
			$('#heroes-3 .popup-1 .hide li:visible').hide();
			$(this).addClass('active');
			$('#heroes-3 .popup-1 .hide li.hero-'+clicked+'').fadeIn(500);
		}
	});
	
	// heroes-3 popup file input
	$('#heroes-3 .popup-1 form .file').click(function() {
		if (!$(this).hasClass('active')) {
			$('#heroes-3 .popup-1 form input[type="file"]').trigger('click');
		}
	});

  $('#heroes-3 .popup-1 form input[type="file"]').change(function() {
  	$('#heroes-3 .popup-1 form .file').addClass('active');
  	$('#heroes-3 .popup-1 form .success').fadeIn(500);
  });
  
	// heroes-3 popup custom inputs  
	$('#heroes-3 .popup-2 .register .main .radio input[type="radio"]').ezMark();
	$('#heroes-3 .popup-2 .register .terms .radio input[type="radio"]').ezMark();
	
	//////////////////////////////
	
	// heroes-4 nav
	$('#heroes-4 .content nav a').click(function() {
		if (!$(this).hasClass('active')) {
			$('#heroes-4 .content nav a.active').removeClass('active');
			$(this).addClass('active');
		}
	});
	
	// heroes-4 buttons
	$('#heroes-4 .content .button a').click(function() {
		var clicked = $(this).attr('rel');
		if (!$(this).parent('span').hasClass('active')) {
			$('#heroes-4 .content .button.active').removeClass('active');
			$('#heroes-4 .content .hide:visible').hide();
			$(this).parent('span').addClass('active');
			$('#heroes-4 .content .hide.'+clicked+'').fadeIn(500);
		}
	});
	
	// heroes-4 video	
	$('#heroes-4 .content .hide .play').click(function() {
		$('#heroes-4 .content .video').fadeIn(500);
	});
	
	$('#heroes-4 .content .video .embed .close').click(function() {
		$('#heroes-4 .content .video').fadeOut(500);
	});
  
  //////////////////////////////
  
  // Candidati - register candidates form
  $('#heroes-3 .popup-1 form').ajaxForm(function(returnedData) {
    $('.popup-1').hide();
    if(returnedData == 4){
      $('.popup-3').show();
    } else {
      $('.popup-2').show();
    }
  });
  
  // Candidati - register user
  $('#heroes-3 .popup-2 form').ajaxForm(function(returnedData) {
      $('.popup-2').hide();
      if(returnedData == 2) {
        $('.popup-1').show();
      } else {
        $('.popup-3').show();
      }
  });

	//////////////////////////////
	
	// contatti-1 custom select
	$('#contatti-1 .content form select').selectbox();
	
	//////////////////////////////

	// #contatti-2 file input
	$('#contatti-2 .content form .upload .file').click(function() {
		if (!$(this).hasClass('active')) {
			$('#contatti-2 .content form .upload input[type="file"]').trigger('click');
		}
	});

  $('#contatti-2 .content form .upload input[type="file"]').change(function() {
  	$('#contatti-2 .content form .upload .file').addClass('active');
  	$('#contatti-2 .content form .upload .success span').html($('#contatti-2 .content form .upload input[type="file"]').val().split('\\').pop());
  	$('#contatti-2 .content form .upload .success').fadeIn(500);
  });
  
	//////////////////////////////
  
  // #contatti-1 ajax submit
  $('#contatti-1 form').ajaxForm(function() { 
    $('#contatti-1 .popup').show();
  });
  
  // #contatti-2 ajax submit
  $('#contatti-2 form').ajaxForm(function() {
    $('#contatti-2 .popup').show();
  });
  
  //////////////////////////////
  
	// default input/textarea value
  $('input[type="text"], textarea').focus(function() { 
    if ($(this).val() == $(this).prop('defaultValue')) {
      $(this).val('');
    }
  });
  $('input[type="text"], textarea').blur(function() {
    if ($(this).val() == '') {
      $(this).val($(this).prop('defaultValue'));
    } 
  });
});
