// JavaScript Document
$(document).ready(function() {
	putback('null');
	
	$(document).pngFix();
	//if(!ie6()) $(".chicbox").chicbox();
	$(".aModal").click(function() {
		var action=$(this).attr('rel');
		switch(action) {
			case 'addKid':
				PXL_addKid($(this));
				break;
		}
	});
	
	// rix
	//$('#tooltip_big').hide().html($('#'+$('.rix').attr('rel')).html());
	$('.rix').each(function() {
		var t=$(this);
		$('#tooltip_mid').hide().html($(t).attr('title'));
		if (!ie6()) {
			$(this).tooltip('#tooltip_mid');
		}
		else {
			$(t).click(function() {
				alert($(this).attr('rev')+'\n\n'+$(this).attr('title'));
			});
		}
	});
	$('.hide').hide();
    // bind to the form's submit event 
    $('#newsletter_form, #sendtofriend_form').submit(function() {
		$(this).find('input').removeClass('ui-1px-form-input-error');
		var options = { 
	        //target:        '#text_content_full',   // target element(s) to be updated with server response 
	        beforeSubmit:  showRequest,  // pre-submit callback 
	        success:       showResponse,  // post-submit callback 
	        // other available options: 
	        type:      "post"        // 'get' or 'post', override for form's 'method' attribute 
	        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
	        //clearForm: true        // clear all form fields after successful submit 
	        //resetForm: true        // reset the form after successful submit 
	 
	        // $.ajax options can be used here too, for example: 
	        //timeout:   3000
	    }; 
		var act="";
		options.url=(act=$(this).attr('action'))=="" ? "/inc/form/register.php" : act;
		//alert(options.url);
        $(this).ajaxSubmit(options);
        return false; 
    });
    $('#preferences_form').submit(function() {
		var options = {
	        beforeSubmit:  showRequest,
	        success:       showResponseUnsub,
	        type:			"post"
	    }; 
		options.url="/inc/form/unsubscribe-form-process.php";
        $(this).ajaxSubmit(options);
        return false;
    });
	$("input:submit").button();
});
// pre-submit callback 
function showRequest(formData, jqForm, options) {
    var queryString = $.param(formData);
    //alert('About to submit: \n\n' + queryString);
    return true;
}
function error(xml) {
	var error = $(xml).find('error').text();
	if (error != "") {
		var field = $(xml).find('field').text();
		if (field) {
			putback(field);
			$("#" + field).addClass('form-input-error').focus();
		}
		var modify = $(xml).find('modify').text();
		var modal = $(xml).find('modal').text();
		var parcels = $(xml).find('parcels').text().split('|');
		
		var sParcels_yes=parcels[0];
		var sParcels_no=parcels[1];
		var sParcels_close=parcels[2];
			
		var buttons = {};
		
		if (parseInt(modify)==1) {
			
			buttons[sParcels_no] = function() { 
				$(this).dialog("close"); 
			};
			
			buttons[sParcels_yes] = function() { 
				$(this).dialog("close");
				var options = {
					beforeSubmit: showRequest,
					success: showResponseMod,
					type: "post"
				};
				options.url = "/inc/form/unsubscribe-form-process.php";
				$('#newsletter_form').ajaxSubmit(options);
				return true;
			};
				
			modlog({
				resizable: false,
				modal: true,
				width: 310,
				height: 200,
				title: '<img src="/images/icons/question.gif" />',
				buttons: buttons
				},
				$('#dialog').html(error),
				true
			);
			/*
			if (window.confirm(error + '\nVoulez-vous modifier vos informations personelles>')) {
				var options = {
					beforeSubmit: showRequest,
					success: showResponseUnsub,
					type: "post"
				};
				options.url = "/inc/form/unsubscribe-form-process.php";
				$('#newsletter_form').ajaxSubmit(options);
				return true;
			}*/
		}
		else {
			if(parseInt(modal)!=1) {
				buttons[sParcels_close] = function() { 
					$(this).dialog("close"); 
				};
				modlog({
					resizable: false,
					modal: true,
					width: 310,
					height: 200,
					title: '<img src="/images/icons/x.gif" />',
					buttons: buttons
					},
					$('#dialog').html(error),
					true
				);
			}
			else alert(error);
		}
		return true;
	}
	return false;
}
// post-submit callback 
function showResponse(xml, statusText, options)  {
	if (!error(xml)) {
		var modal=parseInt($(xml).find('modal').text());
		var load_callback=$(xml).find('load_callback').text();
		if(load_callback=="") load_callback="/inc/form/register_thx.php?lang="+(typeof $('#lang').val()!='undefined' ? $('#lang').val() : 'en');
		var load_method=$(xml).find('load_method').text();
		if(load_method=='href') document.location.href=load_callback;
		else (modal==1 ? applyTextNoMod(load_callback) : applyText(load_callback));
	}
}
function showResponseMod(xml, statusText, options)  {
	//alert(xml); 
	if (!error(xml)) {
		var modal=parseInt($(xml).find('modal').text());
		var load_callback=$(xml).find('load_callback').text();
		if(load_callback=="") load_callback="/inc/form/unsub_thx.php?lang=" + (typeof $('#lang').val() != 'undefined' ? $('#lang').val() : 'en');
		var load_method=$(xml).find('load_method').text();
		if(load_method=='href') document.location.href=load_callback;
		else (modal==1 ? applyTextNoMod(load_callback) : applyText(load_callback));
	}
}
// post-submit callback 
function showResponseUnsub(xml, statusText)  {
	if (!error(xml)) {
		applyTextNoMod("/inc/form/unsub_thx.php?lang=" + (typeof $('#lang').val() != 'undefined' ? $('#lang').val() : 'en'));
	}
}
function applyTextNoMod(c) {
	//alert('noMod: '+c);
	$(".ajax_content").fadeOut("fast",function() {
		$(this).load(c).fadeIn("slow");
	});
}
function applyText(c) {
	//alert('mod: '+c);
	var buttons = {};
		
	buttons["OK"] = function() { 
		$(this).dialog("close");
		document.location.reload();
	};
	modlog({
		resizable: false,
		modal: true,
		width: 600,
		height: 370,
		title: '<img src="/images/icons/check.gif" />',
		buttons: buttons
		},
		$('#dialog').load(c),
		true
	);
}
function putback(field) {
	$("input").each(function() {
		if($(this).attr('id')!=field) $(this).removeClass('form-input-error');
	});
}
// add/rem kids
function PXL_addKid() {
	var h=$('#kids_content_fields').html();
	var num=$('#kids_num').val();
	var ids=$('#kids_fields_id').val();
	num++;
	ids+='{'+num+'},';
	$('#kids_fields_id').val(ids);
	$('#kids_num').val(num);
	h=h.replace(/{num}/g,num);
	$('#kids_content').append(h);
	//alert(num);
	//$.scrollTo("#friend_cont_"+num,800);
}
function PXL_delKid(id) {
	$("#kids_cont_"+id).slideUp("slow",function() {
		$(this).remove();
	});
	var num=$('#kids_num').val();
	var ids=$('#kids_fields_id').val();
	var rem_id='{'+id+'}';
	ids=ids.replace(rem_id,'');
	$('#kids_fields_id').val(ids);
}