var currentSlide = 1;
var canClick = true;
function galleryChange(modo) {
	if (modo == 'prev') {
		currentSlide--;
		sliderMargin = '+=89';
	}
	
	if (modo == 'next') {
		currentSlide++;
		sliderMargin = '-=89';
	}

	if (currentSlide < 1) {
		currentSlide = 1;
		sliderMargin = '';
	}
	if (currentSlide > ($('#slider').children('li').length - 2)) {
		currentSlide = $('#slider').children('li').length - 2;
		sliderMargin = '';
	}
	$('#slider li').removeClass('current');
	$('#slider li:eq(' + currentSlide + ')').addClass('current');
	if (sliderMargin != '')
		$('#slider').animate({marginLeft: sliderMargin}, 100);
}

function rollSlider(mode, obj) {
	if (canClick == true) {
		var $myContainer = $('#' + obj);
		var $currPos = $myContainer.css('margin-top');
		$currPos = $currPos.substr(0, $currPos.length - 2);
		var $howMuch = $('#' + obj).find(':first').height();
		
		var $animatedMargin;
		var $listMax = ($('#' + obj).height() - $('#' + obj).parent().height()) * -1;
	
		if (mode == 'next') {
			$animatedMargin = '-=' + $howMuch;
		}
	
		if (mode == 'prev') {
			$animatedMargin = '+=' + $howMuch;
		}
	
		if ($currPos == 0 && mode == 'prev') {
			$animatedMargin = $listMax;
		}
	
		if ($currPos <= $listMax && mode == 'next') {
			$animatedMargin = 0;
		}
		canClick = false;
		$myContainer.animate({marginTop: $animatedMargin}, '250', function() { canClick = true; });
	}
}

function showClock() {
	var myWatch = $.cookie('myWatch');
	if (myWatch == '' || myWatch == null) myWatch = 'BR';
	
	var now = new Date();
	var h = now.getHours();
	var m = now.getMinutes();
	var s = now.getSeconds();
	
	var spOffset = 180;
	
	offSets = new Array();
	offSets['BR'] = 180;
	offSets['US'] = 300;
	offSets['FR'] = -60;
	offSets['JP'] = -540;
	
	offNames = new Array();
	offNames['BR'] = 'S&atilde;o Paulo';
	offNames['US'] = 'New York';
	offNames['FR'] = 'Paris';
	offNames['JP'] = 'Tokio';
	
	var hDif = spOffset - offSets[myWatch];
	hDif = hDif / 60;
	
	h = h + hDif;
	
	if (h == 24) h = '0';
	if (h > 24) h = h - 24;
	
	if (h < 10) h = '0' + h;
	if (m < 10) m = '0' + m;
	if (s < 10) s = '0' + s;
	
	var time = h + ':' + m + ':' + s;	
	
	$('#clockShow').html(time);
	
	$('#clockSelect .myWatch').html(offNames[myWatch]);
	
	setTimeout('showClock()', 500);
}

function setClock(obj) {
	$.cookie('myWatch', obj.attr('rel'), {path: '/'});
}

$(document).ready(function() {

	$('body').addClass('os' + $.whichos.OS());					   
						   
	$('#menu ul').hide();
	$('a#showMenu').hide();						   
	$('#menu ul').fadeIn('slow');

	$('a#hideMenu').click(function() {
		$('#menu ul').fadeOut('slow');
		$('a#hideMenu').fadeOut('slow');
		$('a#showMenu').fadeIn('slow');
		return false;
	});
	$('a#showMenu').click(function() {
		$('#menu ul').fadeIn('slow');
		$('a#showMenu').fadeOut('slow');
		$('a#hideMenu').fadeIn('slow');
		return false;
	});
	
	$('.br').hide();
	$('.en').hide();
	$('.br').fadeIn('slow');

	$('a#showBr').click(function() {
		$('.br').fadeIn('slow');
		$('.en').fadeOut('slow');
		return false;
	});

	$('a#showEn').click(function() {
		$('.en').fadeIn('slow');
		$('.br').fadeOut('slow');
		return false;
	});

	$("#listModels li").hover(function() {
		$(this).find('img:first').stop().fadeTo("fast", 1);
	}, function() {
		$(this).find('img:first').stop().fadeTo("slow", 0.5);
	});
	$("#listModels li img").fadeTo("fast", 0.5);
	
	$("#slider li").hover(function() {
		$(this).find('img:first').stop().fadeTo("fast", 1);
	}, function() {
		$(this).find('img:first').stop().fadeTo("slow", 0.5);
	});
	$("#slider li img").fadeTo("fast", 0.5);

	$("#list-tooltip img").hover(function() {
		$(this).stop().fadeTo("fast", 1);
	}, function() {
		$(this).stop().fadeTo("slow", 0.5);
	});
	$("#list-tooltip img").fadeTo("fast", 0.5);

	$('a.jt').cluetip({local:true, hideLocal:true, dropShadow: false, width: 140, height:'auto', topOffset:0, leftOffset:0, showTitle: true, positionBy: 'auto', tracking: false, clickThrough: true });   

	$('a.sr').cluetip({local:true, hideLocal:true, dropShadow: false, width: 200, height:'auto!important' , topOffset:20, leftOffset:0, showTitle: false, positionBy: 'auto', tracking: false, mouseOutClose:    false});   


	$(".setaesquerda a").click(function(e) {
		e.preventDefault();
		galleryChange('prev');
	});
	
	$(".setadireita a").click(function(e) {
		e.preventDefault();
		galleryChange('next');
	});
	
	$('a.next').click(function(e) {
		if ($(this).attr('rel') != '' && $('#' + $(this).attr('rel'))) {
			e.preventDefault();
			rollSlider('next', $(this).attr('rel'));
		}
	});
	
	$('a.prev').click(function(e) {
		if ($(this).attr('rel') != '' && $('#' + $(this).attr('rel'))) {
			e.preventDefault();
			rollSlider('prev', $(this).attr('rel'));
		}
	});
	
	$('a.printComposite').click(function(e) {
		e.preventDefault();
		window.open($(this).attr('rel') + '&image=' + $('#slider li.current .picture img').attr('src'));
	});
	
	$('a.tabLinks').click(function(e) {
		e.preventDefault();
		if ($('div.' + $(this).attr('rel')).is(':hidden')) {
			$('a.tabLinks').removeClass('current');
			$(this).addClass("current");
			$('div.tabContent:visible').hide();
			$('div.' + $(this).attr('rel')).fadeIn('slow');
		}
	});
	
	if ($('#formNewFace')) {
		$('#formNewFace .etapa1 #sendRound').click(function() {
				$('a.tabLinks').removeClass('current');
				$("a.tabLinks[rel='etapa2']").addClass('current');
				$('div.tabContent:visible').hide();
				$('div.etapa2').fadeIn('slow');
				return false;
		});
		$('#formNewFace .etapa2 #sendRound').click(function() {
				$('a.tabLinks').removeClass('current');
				$("a.tabLinks[rel='etapa3']").addClass('current');
				$('div.tabContent:visible').hide();
				$('div.etapa3').fadeIn('slow');
				return false;
		});
		$('#formNewFace .etapa3 #sendRound').click(function() {
				$('a.tabLinks').removeClass('current');
				$("a.tabLinks[rel='etapa4']").addClass('current');
				$('div.tabContent:visible').hide();
				$('div.etapa4').fadeIn('slow');
				return false;
		});
		
		var now = new Date();
		var maxYear = now.getFullYear();
		var minYear = now.getFullYear() - 120;
	
		$("#formNewFace").validate({
			invalidHandler: function(e, validator) {
				var errors = validator.numberOfInvalids();
				if (errors) {
					alert('Existem erros em seu formulário.\nPor favor, verifique os erros nas 4 etapas e tente novamente.');
				}
			},
			onkeyup: true,
			rules: {
				dna: {required: true, min: minYear, max: maxYear},
				tatuagem: {required: true},
				piercing: {required: true},
				cicatriz: {required: true},
				estrias: {required: true},
				sinais: {required: true},
				tatuagem_onde: {required: '#nffts:checked'},
				piercing_onde: {required: '#nffps:checked'},
				cicatriz_onde: {required: '#nffcs:checked'},
				estrias_onde: {required: '#nffes:checked'},
				sinais_onde: {required: '#nffss:checked'}
			}
		});
		
		$("#formNewFace #fone").mask('99 9999-9999');
		$("#formNewFace #celular").mask('99 9999-9999');
		$("#formNewFace #fone_pai").mask('99 9999-9999');
		$("#formNewFace #fone_mae").mask('99 9999-9999');
		$("#formNewFace #cpf").mask('999.999.999-99');
		$("#formNewFace #cep").mask('99999-999');
		$("#formNewFace #altura").mask('9,99');
	}
	
	if ($('#formInscricao')) {
		$("#formInscricao").validate({
			invalidHandler: function(e, validator) {
				var errors = validator.numberOfInvalids();
				if (errors) {
					alert('Existem erros em seu formulário.\nPor favor, verifique os erros e tente novamente.');
				}
			},
			onkeyup: true,
			rules: {
				dna: {required: true, min: minYear, max: maxYear}
			}
		});
		
		$("#formInscricao #altura").mask('9,99');
	}
	
	if ($('#contactform')) {
		$("#contactform").validate({
			invalidHandler: function(e, validator) {
				var errors = validator.numberOfInvalids();
				if (errors) {
					alert('Existem erros em seu formulário.\nPor favor, verifique os erros e tente novamente.');
				}
			},
			onkeyup: true,
			rules: {
				codimg: {required: true, remote: basePath + "checkRec.php"}
			}
		});
		
		$("#contactform #codimg").mask('****');
	}
	
	$('#clockSelect li').click(function() {
		setClock($(this));
	});
	
	$('#clockSelect').hover(function() {
		$('#clockSelect .theSelect').css('overflow', 'show');
		$('#clockSelect ul').animate({top: '-=67'}, 250);
	}, function() {
		$('#clockSelect .theSelect').css('overflow', 'hidden');
		$('#clockSelect ul').animate({top: '+=67'}, 250);
	});
	
	showClock();
	
	jQuery.validator.addMethod("campoCPF", function( value, element ) {
	if (value != '' && value != '___.___.___-__') {
		cpf = value.split(".");
		if (cpf.length == 3) {
			n1 = cpf[0];
			n2 = cpf[1];
			n3 = cpf[2];
			cpf = n3.split('-');
			n3 = cpf[0];
			dv = cpf[1];
			
			cpf = n1.toString();
			cpf = cpf + n2.toString();
			cpf = cpf + n3.toString();
			
			var d1 = 0;
			for (i = 0; i < 9; i++) { 
				d1 += cpf.charAt(i)*(10-i); 
			}
			if (d1 == 0) {
				return false;
			}
			
			d1 = 11 - (d1 % 11);
			if (d1 > 9) d1 = 0; 
			if (dv.charAt(0) != d1) {
				return false;
			}
			
			d1 *= 2;
			for (i = 0; i < 9; i++) { 
				d1 += cpf.charAt(i)*(11-i);
			}
			
			d1 = 11 - (d1 % 11);
			if (d1 > 9) d1 = 0;
			
			if (dv.charAt(1) != d1) {
				return false; 
			}				
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
	}, "");
	
	jQuery.validator.messages.required = "";
	
	$('#goAdvBtn').click(function() {
		document.location.href = basePath + 'search';
	});
});

Cufon.replace('.cufon');
Cufon.replace('.sidebar-info h4');
Cufon.replace('#newface-steps');
