jQuery.fn.fadeToggle = function(speed, easing, callback) {return this.animate({opacity: 'toggle'}, speed, easing, callback);};

jQuery(document).ready(function(){
	// jQuery('a#ad-toggle').click(function() {jQuery('#formbox').fadeToggle("slow"); return false;});	
	jQuery('a#email-toggle').click(function() {jQuery('#email_form_data').fadeToggle("slow"); return false;});
	jQuery("img.size-thumbnail").parent().fancybox({"hideOnContentClick":true,"overlayShow":true,"overlayOpacity":.5,"zoomSpeedIn":300,"zoomSpeedOut":300});
	jQuery('#gotoStep2').click(function(){
		sendAjaxForm( 'validate' );
    });
	jQuery('#backtoStep1').click(function(){
		jQuery('#modalformstep2').hide();
        jQuery('#modalformstep1').show();
	});
});

/**
 * Send the classified ad to the backend using AJAX
 *
 * @param string action (validate or process)
 */
function sendAjaxForm( action ) {

	var formdata = jQuery('#new_post2').serialize();
	var mysack = new sack( process_form_ajax );
	mysack.execute = 1; // execute the PHP output as javascript
	mysack.setVar( "action", action + '_form_ajax' );
	mysack.setVar( "formdata", formdata );
	mysack.onError = function() { alert('Ajax error processing form' )};
	mysack.runAJAX();
	return false;
	
}
var fileFieldCount = 1;
/**
 * Dynamically add additional form fields.
 *
 * @param int max number of fields to display
 */
function addFileField(max) {
    if (fileFieldCount >= max) return false
    fileFieldCount++;
    var f = document.createElement("input");
    f.type = "file";
    f.name = "images[]";
	f.setAttribute("class", "wwIconified");
    p = document.getElementById("attachments_fields");
    p.appendChild(document.createElement("br"));
    p.appendChild(f);
    p.appendChild(d);
}
/**
 * Text counter for the classified ad form to tell you
 * how many chars you have left in the description field
 */
function textCounter(fieldId,cntfieldId,maxlimit) { 
	if (jQuery("#"+fieldId).val().length > maxlimit) 
		jQuery("#"+fieldId).val( jQuery("#"+fieldId).val().substring(0, maxlimit) ); 
	else 
		jQuery("#"+cntfieldId).val( maxlimit - jQuery("#"+fieldId).val().length );
}
