var ANIMATION_LENGTH = 500;

function openDocumentBox(divId, caller){
	var currentHeight = $("#" + divId).height();
	var heightWithContent = $("#" + divId)[0].scrollHeight;
	var parentHeight = caller.parentNode.scrollHeight;
	if (currentHeight == 0){ // box is closed, open it
		parentHeight = parentHeight + heightWithContent;
		caller.className = 'sub_allesdicht_document_link_selected';
		$("#" + divId).animate({height: heightWithContent + "px"}, ANIMATION_LENGTH);			
	}else{ // box is open, close it
		caller.className = 'sub_allesdicht_document_link';
		parentHeight = parentHeight - heightWithContent;
		$("#" + divId).animate({height: "0px"}, ANIMATION_LENGTH);			
	}
	$("#" + caller.parentNode.id).animate({height: parentHeight + "px"}, ANIMATION_LENGTH);			
}

function openEmailBox(divId, caller){
	var currentHeight = $("#" + divId).height();
	var heightWithContent = $("#" + divId)[0].scrollHeight;
	var parentHeight = caller.parentNode.scrollHeight;
	if (currentHeight == 0){ // box is closed, open it
		parentHeight = parentHeight + heightWithContent;
		caller.className = 'sub_allesdicht_e_mail_link_selected';
		$("#" + divId).animate({height: heightWithContent + "px"}, ANIMATION_LENGTH);
	}else{ // box is open, close it
		caller.className = 'sub_allesdicht_e_mail_link';
		parentHeight = parentHeight - heightWithContent;
		$("#" + divId).animate({height: "0px"}, ANIMATION_LENGTH);			
	}
	$("#" + caller.parentNode.id).animate({height: parentHeight + "px"}, ANIMATION_LENGTH);			
}

function sendContentItemAsEmail(formId, errorMessageBoxId, emailStatusId, itemUniqueId){
	var contentItemUUID = $('#'+ formId + ' .c_contentItemUUID' ).val();
	
	var section = $('#'+ formId + ' .c_section' ).val();
	var category = $('#'+ formId + ' .c_category' ).val();
	var subcategory = $('#'+ formId + ' .c_subcategory' ).val();
	var email = $('#'+ formId + ' .c_email' ).val(); 
	var action = $('#'+ formId).attr('action');
	
	email = jQuery.trim(email);
	if (email == ""){
		$('#' + errorMessageBoxId).attr('innerHTML', '<br/>Vul uw E-mail adres.');
		$('#' + errorMessageBoxId).show();		
	}else if (!isValidEmailAddress(email)){
		$('#' + errorMessageBoxId).attr('innerHTML', '<br/>Het ingevoerde email adres is ongeldig');
		$('#' + errorMessageBoxId).show();
	}else{
				
		 $.ajax({
			   type: "POST",
			   url: action,
			   data: "contentItemUUID=" + contentItemUUID + 
			   		 "&dbfield22=" + section + "&dbfield23=" + category + 
			   		 "&dbfield24=" + subcategory + "&dbfield25=" + email + "&itemUniqueId=" + itemUniqueId,
			   success: function(responseVal){
		    	  var message = null;
		    	  if (responseVal.indexOf("success") > -1){
		    		  message = "De informatie is verstuurd naar";
		    		  $('#' + emailStatusId).attr('innerHTML',message+ "<br/><br/><span><b>"+email+"</b></span>");
		    	  }else if (responseVal.indexOf("already_sent") > -1){
		    		  message = "Dit item is al verstuurd.";
		    		  $('#' + emailStatusId).attr('innerHTML', message);
		    	  }else{
		    		  var message="Fout opgetreden tijdens het verzenden van e-mail. Probeer het later opnieuw.";		    		  
		    		  $('#' + emailStatusId).attr('innerHTML', message);
		    	  }
		    	  $('#' + emailStatusId).show();
		    	  //hide form 
		    	  $('#' + formId).hide();
			   }
		  });
	}	
}

function sendDocumentAsEmail(formId, errorMessageBoxId, emailStatusId, itemUniqueId, documentDownloadURLRepositoryPath){
	var title = $('#'+ formId + ' .c_title' ).val(); 
	var document = $('#'+ formId + ' .c_document' ).val();
	var section = $('#'+ formId + ' .c_section' ).val();
	var category = $('#'+ formId + ' .c_category' ).val();
	var subcategory = $('#'+ formId + ' .c_subcategory' ).val();
	var email = $('#'+ formId + ' .c_email' ).val();
	var action = $('#'+ formId).attr('action');
	
	email = jQuery.trim(email);
	if (email == ""){
		$('#' + errorMessageBoxId).attr('innerHTML', '<br/>Vul uw E-mail adres.');
		$('#' + errorMessageBoxId).show();		
	}else if (!isValidEmailAddress(email)){
		$('#' + errorMessageBoxId).attr('innerHTML', '<br/>Het ingevoerde email adres is ongeldig');
		$('#' + errorMessageBoxId).show();
	}else{
		 $.ajax({
			   type: "POST",
			   url: action,
			   data: "dbfield11=" + title+ "&dbfield12=" + document + "&dbfield13=" + section + 
			   		 "&dbfield14=" + category + "&dbfield15=" + subcategory + "&dbfield16=" + email + "&itemUniqueId=" + itemUniqueId,
			   success: function(responseVal){
		    	  var message = null;
		    	  if (responseVal.indexOf("success") > -1){
		    		  message = "Een e-mail is verstuurd met een link naar het door u opgevraagde document.";
		    		  $('#' + emailStatusId).attr('innerHTML', message + "<br/>");
		    		  trackDocumentDownloadEventWithGoogleAnalytics(documentDownloadURLRepositoryPath);
		    	  } else if (responseVal.indexOf("already_sent") > -1){
		    		  message = "Dit item is al verstuurd.";
		    		  $('#' + emailStatusId).attr('innerHTML', message);
		    	  } else{ // error while sending
		    		  message = "Fout opgetreden tijdens het verzenden van e-mail. Probeer het later opnieuw.";
		    		  $('#' + emailStatusId).attr('innerHTML', message);
		    	  }
		    	  $('#' + emailStatusId).show();
		    	  //hide form 
		    	  $('#' + formId).hide();
			   }
			 });
		
		increaseVisitCounter(itemUniqueId);
	}
}

function documentWithoutRegistrationDownloaded(visitCounterBeanUUID, documentPath){
	increaseVisitCounter(visitCounterBeanUUID);
	trackDocumentDownloadEventWithGoogleAnalytics(documentPath);
}

function trackDocumentDownloadEventWithGoogleAnalytics(documentPath){
	pageTracker._trackEvent("Document download", location.pathname, documentPath);	
}

/* visitCounterURL is set in the jsp page */
function increaseVisitCounter(visitCounterBeanUUID){
	$.post(visitCounterURL, {uuid: visitCounterBeanUUID});
}

function isValidEmailAddress(emailAddress) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailAddress)){
		return (true);
	}
	return (false);	
}
