$(document).ready(function(){  
	//LOADING POPUP  
	//Click the button event!  
	$("a#report_form").click(function(){  
		//centering with css  
		centerPopup();  
		//load popup  
		loadPopup('report');
		resetPopup();
		resetValues();
    // return false;
	});
	
	$("a#toolkit_form").click(function(){  
		//centering with css  
		centerPopup();  
		//load popup  
		loadPopup('toolkit');
		resetPopup();
		resetValues();
    // return false;
	});

	$("#popupThankyouClose").click(function(){  
		disableThankyou();  
	});

	//CLOSING POPUP  
	//Click the x event!  
	$(".popupContactClose").click(function(){  
		disablePopup();  
	});  
	//Click out event!  
	$("#backgroundPopup").click(function(){  
		disablePopup();  
	});  
	
	$("#backgroundPopup2").click(function(){  
		disablePO();  
	});
	//Press Escape event!  
	$(document).keypress(function(e){  
		if(e.keyCode==27 && popupStatus==1){  
			disablePopup();  
		}  
	});
	
	
	/*
	========================
	
	TOOLKIT
	
	========================
	*/
	
	$("form#toolkit_form2 .id_payment_method_paypal").click(function(){
		$('form#toolkit_form2 .id_payment_method').val(1);
	});
	
	$("form#toolkit_form2 .id_payment_method_po").click(function(){
		centerPO();  
		//load popup  
		loadPO();
		
		// attach click event
		$('#po_number_field').val('');
		$('#submit_po').unbind('click');
		$('#submit_po').bind('click', function(){
			// make sure po number present
			if($('#po_number_field').val().length > 0){
				$('form#toolkit_form2 .id_po_number').val($('#po_number_field').val());
				$('form#toolkit_form2 .id_payment_method').val(2);
				disablePO();
				$('form#toolkit_form2').submit();
			}
			else{
				// error
			}
		});
		
		return false;
	});
	
	// hijack the form submit	
	$('form#toolkit_form2').bind('submit',function(e){
		resetPopup();
		$.post('/about_mindfulness/process_form/surgery-tool-kit',$('form#toolkit_form2').serialize(),function(data){

			var result = eval(data);

			// if the post was a success then display thank you popover
			if(result.success){
				
				if(result.payment_method == 1){
					$('#paypal_quantity').val($('form#toolkit_form2 div.quantity select').val());
					$('#paypal_custom').val(result.id);
					$('#paypal_button_id').val('10692399');
					$('#paypal_form').submit();
				}
				else {
				    $(".popupContact").fadeOut("slow");  
    				popupStatus = 0;
    				$('#change_text').html('MINDFULNESS TOOLKIT');
    				Cufon.replace('#popupThankyou h3');
    				centerThankyou();
    				loadThankyou();
				}

			}
			else {
				jQuery.each(result.errors, function(i, val) {
					if (val.length) {
						$('form#toolkit_form2 input[name="' + i +'"]').css('background-color','#ffd200');
						$('form#toolkit_form2 input[name="' + i +'"]').after(val);
						
						if(i == 'quantity'){
							$('form#toolkit_form2 div.quantity select').css('background-color','#ffd200');
							$('form#toolkit_form2 div.quantity select').after(val);
						}
					};					
				});
				centerPopup();
			}

			// if not mark incorrect fields on the form
			},'json');
		
			return false;
	});
	
	$('form#toolkit_form2 div.quantity select').bind('change',function(e){
		if(e.target.value == ''){
			$('form#toolkit_form2 .order_details .price span').html('0');
		}
		else {
			$('form#toolkit_form2 .order_details .price span').html(20*e.target.value);
		}
	});
	/*
	==============================
	
	REPORT
	
	==============================
	*/
	
	$("form#report_form2 .id_payment_method_po").click(function(){
		centerPO();  
		//load popup  
		loadPO();

		// attach click event
		$('#po_number_field').val('');
		$('#submit_po').unbind('click');
		$('#submit_po').bind('click', function(){
			// make sure po number present
			if($('#po_number_field').val().length > 0){
				$('form#report_form2 .id_po_number').val($('#po_number_field').val());
				$('form#report_form2 .id_payment_method').val(2);
				disablePO();
				$('form#report_form2').submit();
			}
			else{
				// error
			}
		});

		return false;
	});
	
	$("form#report_form2 .id_payment_method_paypal").click(function(){
		$('form#report_form2 .id_payment_method').val(1);
	});
		
	// hijack the form submit
	$('form#report_form2').bind('submit',function(e){
		resetPopup();
		$.post('/about_mindfulness/process_form/mindfulness-report',$('form#report_form2').serialize(),function(data){
	
			var result = eval(data);
	
			// if the post was a success then display thank you popover
			if(result.success){
				
				if(result.payment_method == 1){
					$('#paypal_quantity').val($('form#report_form2 div.quantity select').val());
					$('#paypal_custom').val(result.id);
					$('#paypal_button_id').val('10932836');
					$('#paypal_form').submit();
				}
				else{
				    $(".popupContact").fadeOut("slow");  
    				popupStatus = 0;
    				$('#change_text').html('MINDFULNESS REPORT');
    				Cufon.replace('#popupThankyou h3');
    				centerThankyou();
    				loadThankyou();
				}

			}
			else {
				jQuery.each(result.errors, function(i, val) {
					if (val.length) {
						$('form#report_form2 input[name="' + i +'"]').css('background-color','#ffd200');
						$('form#report_form2 input[name="' + i +'"]').after(val);
						
						if(i == 'quantity'){
							$('form#report_form2 div.quantity select').css('background-color','#ffd200');
							$('form#report_form2 div.quantity select').after(val);
						}
					};					
				});
			}
			// if not mark incorrect fields on the form
			},'json');
	
			return false;
		});
		
		$('form#report_form2 div.quantity select').bind('change',function(e){
			if(e.target.value == ''){
				$('form#report_form2 .order_details .price span').html('0');
			}
			else {
				$('form#report_form2 .order_details .price span').html(15*e.target.value);
			}
		});
});


/*
===============================

POPOVER

===============================
*/

//SETTING UP OUR POPUP  
//0 means disabled; 1 means enabled;  
var popupStatus = 0;
var thankyouStatus = 0;

//loading popup with jQuery magic!  
function loadPopup(type){  
	//loads popup only if it is disabled  
	if(popupStatus==0){  
		$("#backgroundPopup").css({  
			"opacity": "0.7"  
		});  
		$("#backgroundPopup").fadeIn("slow");  
		$(".popupContact." + type).fadeIn("slow");  
		popupStatus = 1;  
	}  
}


//disabling popup with jQuery magic!  
function disablePopup(){  
	//disables popup only if it is enabled  
	if(popupStatus==1){  
		$("#backgroundPopup").fadeOut("slow");  
		$(".popupContact").fadeOut("slow");  
		popupStatus = 0;  
	}  
}


//centering popup  
function centerPopup(){  
	//request data for centering  
	var windowWidth = document.documentElement.clientWidth;  
	var windowHeight = document.documentElement.clientHeight;  
	var popupHeight = $(".popupContact").height();  
	var popupWidth = $(".popupContact").width();  
	
	var top = windowHeight/2-popupHeight/2;
	var left = windowWidth/2-popupWidth/2;
	
	if (top < 0) {
		top = 20;
	}
	
	//centering  
	$(".popupContact").css({  
		"position": "absolute",  
		"top": top,  
		"left": left  
	});  
	//only need force for IE6  

	$("#backgroundPopup").css({  
		"height": document.body.clientHeight,
		"width": windowWidth
	});  

}

function resetValues(){
	$('.popupContact input:text').each(function(index,elm){
		$(elm).val('');
	});
	$('.popupContact textarea').val('');
	$('.popupContact select').val('');
	$('.popupContact .id_po_number').val('');
	$('.order_details .price span').html('0');
}

function resetPopup(){
	$('.popupContact input:text').each(function(index,elm){
		$(elm).css('background-color','#ffffff');
	});
	$('.popupContact textarea').css('background-color','#ffffff');
	$('ul.errorlist').remove();
	
	$('div.quantity select').css('background-color','#ffffff');
}

/*
========================

THANKS

=========================
*/

//centering popup  
function centerThankyou(){  
	//request data for centering  
	var windowWidth = document.documentElement.clientWidth;  
	var windowHeight = document.documentElement.clientHeight;  
	var popupHeight = $("#popupThankyou").height();  
	var popupWidth = $("#popupThankyou").width();  
	
	var top = windowHeight/2-popupHeight/2;
	var left = windowWidth/2-popupWidth/2;
	
	if (top < 0) {
		top = 20;
	}
	
	//centering  
	$("#popupThankyou").css({  
		"position": "absolute",  
		"top": top,  
		"left": left  
	});  
	//only need force for IE6  

	$("#backgroundPopup").css({  
		"height": document.body.clientHeight,
		"width": windowWidth
	}); 

}

//loading popup with jQuery magic!  
function loadThankyou(){  
	//loads popup only if it is disabled  
	if(thankyouStatus==0){  
		$("#backgroundPopup").css({  
			"opacity": "0.7"  
		});  
		$("#backgroundPopup").fadeIn("slow");  
		$("#popupThankyou").fadeIn("slow");  
		thankyouStatus = 1;  
	}  
}


//disabling popup with jQuery magic!  
function disableThankyou(){  
	//disables popup only if it is enabled  
	if(thankyouStatus==1){  
		$("#backgroundPopup").fadeOut("slow");  
		$("#popupThankyou").fadeOut("slow");  
		thankyouStatus = 0;  
	}  
}

/*
=================================

PO

=================================
*/

//centering popup  
function centerPO(){  
	//request data for centering  
	var windowWidth = document.documentElement.clientWidth;  
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupPO").height();  
	var popupWidth = $("#popupPO").width();  
	
	var top = windowHeight/2-popupHeight/2;
	var left = windowWidth/2-popupWidth/2;
	
	if (top < 0) {
		top = 20;
	}
	
	//centering  
	$("#popupPO").css({  
		"position": "absolute",  
		"top": top,  
		"left": left  
	});  
	//only need force for IE6  

	$("#backgroundPopup2").css({  
		"height": $("#backgroundPopup").height(),
		"width": $("#backgroundPopup").width()
	}); 

}

//loading popup with jQuery magic!  
function loadPO(){  
	//loads popup only if it is disabled  
	if(thankyouStatus==0){  
		$("#backgroundPopup2").css({  
			"opacity": "0.7"  
		});  
		$("#backgroundPopup2").fadeIn("slow");  
		$("#popupPO").fadeIn("slow");  
		thankyouStatus = 1;  
	}  
}


//disabling popup with jQuery magic!  
function disablePO(){  
	//disables popup only if it is enabled  
	if(thankyouStatus==1){  
		$("#backgroundPopup2").fadeOut("slow");  
		$("#popupPO").fadeOut("slow");  
		thankyouStatus = 0;  
	}  
}
