$j = jQuery.noConflict();

var docWidth, docHeight, docScrollTop;

var currentScr = "div.ctn-report-abuse";

phpUrl = phpUrl + "_php/report.php";

function validateForm(){
		$j('div.ctn-report-abuse>form').validate({
			rules: {
				name: 'required',			
				email: {
				  required: true,
				  email: true
				}
			},
			resetForm: true,
			focusCleanup: true,
			errorContainer: 'div.msg-error',
			errorPlacement: function(error, element) {
				var elementName = element.attr('name');
				highlight(elementName);
				$j('div.msg-error').html("Campo é obrigatório.");
			},
			success: function(elementSuccess){				
				unhighlight(elementSuccess.attr('for'));
			},
			submitHandler: function() {
			var iRobot="yes";
			iRobot = $j("div.spambotTrap>input:radio:checked").length > 0 ? "yes" : "no";
				if(iRobot=="no"){
				var formContent = $j('div.ctn-report-abuse>form').serialize();
					$j.ajax({
						type: "post",
					    url: phpUrl,
					    dataType: "json",
						data: formContent,
					    success: function(data){
							thankYouScreen();
							return false;
						},
						beforeSend: function(){
							loading();
						}
					 });
				}
			}
		});
		 

}

function runOnce(){
	$j(".lens").click(function(){ closeAllScreens(); });
	$j(".btn-close").click(function(){ closeAllScreens(); });
	bindChangesOnScr();
	restoreScr();
	showLens();
	$j("#name").focus();
	validateForm();
}

function closeAllScreens(){
	unbindChangesOnScr();
	$j("div.ctn-report-abuse").hide();
	$j("div.ctn-thank-you").hide();
	$j(".lens").hide();
}

function highlight(e){
	var highlightIt = 'input[name='+e+']';
	$j(highlightIt).css("border","1px solid #ff0000");
}

function unhighlight(e){
	var unhighlightIt = 'input[name='+e+']';
	$j(unhighlightIt).css("border","1px solid #BEB7AF");
}

function thankYouScreen(){
	currentScr = "div.ctn-thank-you";
	restoreScr();
	$j("div.ctn-report-abuse").hide();
	$j("div.ctn-thank-you").show();
}

function loading(){
	$j('div.ctn-report-abuse>form').hide();
	$j('div.loading').show();
}

function findCenter(){
	docWidth = $j(window).width();
	docHeight = $j(window).height();
	docScrollTop = $j(window).scrollTop();
}

function showLens(){
	$j(".lens").css("display","block").fadeTo("normal", 0.75);
}

function restoreScr(){
    
	findCenter();
	
	winHeight = (docHeight-$j(currentScr).height())/2+docScrollTop;
	
	$j(currentScr).css("display","block");
	$j(currentScr).css({top: winHeight});

}

function bindChangesOnScr(){
	$j(window).bind('resize scroll', function() { restoreScr(); });
}

function unbindChangesOnScr(){
	$j(window).unbind('resize');
	$j(window).unbind('scroll');
}


