var requestpopupStatus = 0;

function loadRequestPopup(){
	if(requestpopupStatus==0){
		$("#backgroundPopup").css({   
			"opacity": "0.7"  
		});   
		$("#step_1").show();
		$("#wait_1").hide();
		$("#step_2").hide();
		$("#step_3_ok").hide();
		$("#step_3_error").hide();
		$("#step_4").hide();
	
		$("#backgroundPopup").fadeIn("slow");   
		$("#popupRequest").fadeIn("slow");
		requestpopupStatus = 1;
	}
}

function disableRequestPopup(){
	if(requestpopupStatus==1){
		$("#backgroundPopup").fadeOut("slow");  
		$("#popupRequest").fadeOut("slow");
		requestpopupStatus = 0;
	}
}

function centerRequestPopup(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupRequest").height();
	var popupWidth = $("#popupRequest").width();
	$("#popupRequest").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	$("#backgroundPopup").css({   
		"height": windowHeight
	}); 
}

$(document).ready(function() {

	$("#email_form").validate({
		submitHandler: function(form) {
			
			$("#wait_1").show('slow');
			$("#step_1").hide('slow');
		}
	});
	
	function showEmailFormRequest(formData, jqForm, options){
		return true; 
	}

	function showEmailFormResponse(responseText, statusText, xhr, $form){
//		alert(responseText);
		$("#email_address_1").val($("#email_address").val());
		$("#email_address_2").val($("#email_address").val());
		if(responseText=='ok'){
			$("#wait_1").hide('slow');
			$("#step_3_ok").hide('slow');
			$("#step_3_error").hide('slow');
			$("#step_2").hide('slow');
			$("#step_4").show('slow');
			setTimeout("disableRequestPopup()",2000);
		}else{
			$("#wait_1").hide('slow');
			$("#step_3_ok").hide('slow');
			$("#step_3_error").hide('slow');
			$("#step_4").hide('slow');
			$("#step_2").show('slow');
		}
	}

	var options_email = { 
        beforeSubmit:  showEmailFormRequest,   
        success:       showEmailFormResponse  
    }; 
		
	$('#email_form').ajaxForm(options_email); 

	var options_validate = { 
        beforeSubmit:  showValidationFormRequest,   
        success:       showValidationFormResponse  
    };
	
	$('#validate_form').ajaxForm(options_validate); 

	var options_validate = { 
        beforeSubmit:  showValidationRetryFormRequest,   
        success:       showValidationRetryFormResponse  
    };
	
	$('#validate_form_retry').ajaxForm(options_validate); 

	function showValidationFormRequest(formData, jqForm, options){
		return true; 
	}

	function showValidationFormResponse(responseText, statusText, xhr, $form){
//		alert(responseText);
		if(responseText=='ok'){
			$("#wait_1").hide('slow');
			$("#step_2").hide('slow');
			$("#step_4").hide('slow');
			$("#step_3_ok").show('slow');
			setTimeout("disableRequestPopup()",2000);
		}else{
			$("#wait_1").hide('slow');
			$("#step_2").hide('slow');
			$("#step_3_ok").hide('slow');
			$("#step_4").hide('slow');
			$("#step_3_error").show('slow');
		}
	}	


	function showValidationRetryFormRequest(formData, jqForm, options){
		$("#wait_1").hide('slow');
		$("#step_2").hide('slow');
		$("#step_3_ok").hide('slow');
		$("#step_3_error").hide('slow');
		$("#step_4").hide('slow');
		$("#wait_1").show('slow');
		return true; 
	}

	function showValidationRetryFormResponse(responseText, statusText, xhr, $form){
		if(responseText=='ok'){
			$("#wait_1").hide('slow');
			$("#step_2").hide('slow');
			$("#step_3_error").hide('slow');
			$("#step_4").hide('slow');
			$("#step_3_ok").show('slow');
			setTimeout("disableRequestPopup()",2000);
		}else{
			$("#wait_1").hide('slow');
			$("#step_2").hide('slow');
			$("#step_3_ok").hide('slow');
			$("#step_4").hide('slow');
			$("#step_3_error").show('slow');
		}
	}	

	jQuery('#cropbox').Jcrop();
	$('#uploadPDF').uploadify({
	'uploader': '/swf/uploadify.swf',
	'script': '/include/classes/uploadify/uploadify.php',
	'folder': '/articles/pdf/',
	'cancelImg': '/images/cancel.png',
	'buttonImg': '/images/select.png',
	'auto'           : true,
	'wmode': 'transparent',
	'onSelectOnce' : function(){ 
		$('#uploadPDF').uploadifySettings('scriptData',{'id':$("#id").val(),'type':'article_pdf'}); 
	},
	'onComplete':function(event, queueID, fileObj, response, data){
		id=response;
		pdf='<tr class="article_pdf" id="article_pdf_'+id+'"><td width="22"><img src="/images/small_pdf_icon.jpg"><td>'+fileObj.name+'</td></td><td width="20"><a title="remove" href="#" onClick="removeFile('+id+')"><img src="/images/delete.png"></a></td></tr>';
		$('#pdf_files').append(pdf);
	}
	});

	$("#receive_article").click(function(event){
		centerRequestPopup();
		loadRequestPopup();
		event.preventDefault()
	});

	$("#popupRequestClose").click(function(){
		disableRequestPopup();
	});
})

function removeFile(id_file){
	$.ajax({ url: "/include/Article/removeFile.php?id="+id_file, context: document.body, success: function(){
		$('#article_pdf_'+id_file).remove();
	}});	
}

function selectImage(path,img){
	p=document.getElementById('weblogImage');
	p.src=path+'_thumbs/'+img;
	document.forms.articleForm.image_url.value=path+img;
	disablePopup();
}


var popupStatus = 0;


function loadPopup(){
	if(popupStatus==0){
		$("#backgroundPopup").css({   
			"opacity": "0.7"  
		});   
		$("#backgroundPopup").fadeIn("slow");   
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopup(){
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");  
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}


function centerPopup(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	$("#backgroundPopup").css({   
		"height": windowHeight
	}); 
}


$(document).ready(function(){
	$("#button").click(function(event){
		centerPopup();
		loadPopup();
		event.preventDefault()
	});
				
	$("#popupContactClose").click(function(){
		disablePopup();
	});

	$("#backgroundPopup").click(function(){
		disablePopup();
		disableRequestPopup();
	});
	
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
		if(e.keyCode==27 && requestpopupStatus==1){
			disableRequestPopup();
		}
	});

});



