/**
 * Created with Visual Form Builder by 23rd and Walnut
 * www.visualformbuilder.com
 * www.23andwalnut.com
 */


jQuery(document).ready(function()
{

	 
	
    //Style selects, checkboxes, etc
	jQuery("select, input:radio, input:file ,input:checkbox").uniform();

    //Date and Range Inputs
	jQuery("#field1zz , #field5zzz , #field5zzzg,#field698").rangeinput();

    /**
     * Get the jQuery Tools Validator to validate checkbox and
     * radio groups rather than each individual input
     */

	jQuery('[type=checkbox]').bind('change', function(){
        clearCheckboxError(jQuery(this));
    });
	
	
	//update frequency box
    function slider(id,slide) {
    	 
		  
		if(jQuery(id).attr("checked"))
        {
            //call the function to be fired
            //when your box changes from
            //unchecked to checked
			jQuery(slide).slideDown();
        }
        else
        {
            //call the function to be fired
            //when your box changes from
            //checked to unchecked
        	jQuery(slide).slideUp();
        }
		jQuery.uniform.update();
	}


    slider('#field-wp_pinterest_to_wp-update','#update_slide');

  
    jQuery("#field-wp_pinterest_to_wp-update").change(function(){ 

		slider('#field-wp_pinterest_to_wp-update','#update_slide');
		 
	});
    
    
    //campaign type selector
    function showHide(){
        if(jQuery('#camp_type').val() == 'board'){
            jQuery('#field-camp_type-board-container').slideDown();
        }else{
             jQuery('#field-camp_type-board-container').slideUp();
        }
    }
    
    showHide();
    
    jQuery('#update_boards').click(function(){
    
    	jQuery.ajax({
    	    url: jQuery(this).attr('href'),
    	    type: 'POST',
    	    data: {
    	        action: "fetch_boards",
    	        user: jQuery('#gallery_user').val() 
    	       
    	    },

    	    success: function (data) {
    	    	jQuery('#ajax-loadingimg').addClass('ajax-loading');

    	        var res = jQuery.parseJSON(data);
    	        if (res['status'] == 'success') {
    	            //console.log(data);


    	            jQuery('#camp_board option').remove();
    	            jQuery.uniform.update();

    	            var ids = res['ids'];
    	            var titles = res['titles'];

    	            for (var i = 0; i < ids.length; i++) {

    	            	jQuery('#camp_board').append('<option value="' + ids[i] + '">' + titles[i] + '</option>');
    	                jQuery.uniform.update();


    	            }

    	        } else if (res['status'] == 'fail') {

    	            alert('problem fetching boards please try again .');
    	            return false;
    	        		

    	        }

    	    },

    	    beforeSend: function () {
    	    	
    	    	jQuery('#ajax-loadingimg').removeClass('ajax-loading');

    	    }


    	});
    	return false;
    }); 
    
    function updateBoards(){
    	if(jQuery('#camp_board').val() == null){
    		jQuery('#update_boards').trigger('click');
    	}
    }
    
    jQuery('#camp_type').change(function(){
       
        showHide();
        updateBoards();
    });
    
     

    
    //validate checkbox and radio groups
    function validateCheckRadio(){
        var err = {};

        jQuery('.radio-group, .checkbox-group').each(function(){
             if(jQuery(this).hasClass('required'))
                if (!jQuery(this).find('input:checked').length)
                    err[jQuery(this).find('input:first').attr('name')] = 'Please complete this mandatory field.';
        });

        if (!jQuery.isEmptyObject(err)){
            validator.invalidate(err);
            return false
        }
        else return true;

    }





    //clear any checkbox errors
    function clearCheckboxError(input){
        var parentDiv = input.parents('.field');

        if (parentDiv.hasClass('required'))
            if (parentDiv.find('input:checked').length > 0){
                validator.reset(parentDiv.find('input:first'));
                parentDiv.find('.error').remove();
            }
    }




    //Position the error messages next to input labels
    jQuery.tools.validator.addEffect("labelMate", function(errors, event){
        jQuery.each(errors, function(index, error){
        	
            error.input.first().parents('.field').find('.error').remove().end().find('label').after('<span class="error">' + error.messages[0] + '</span>');
        });

    }, function(inputs){
        inputs.each(function(){
            jQuery(this).parents('.field').find('.error').remove();
        });

    });


    /**
     * Handle the form submission, display success message if
     * no errors are returned by the server. Call validator.invalidate
     * otherwise.
     */

  // jQuery("#post").validator({effect:'labelMate'});
   
   jQuery("#publish").click(function(event){
	   
	  
	   //validating
	   

	 
		   var inputs = jQuery(".TTWForm :input").validator({effect:'labelMate'});
       
	   
	   //var inputs = jQuery(".TTWForm-container :input").validator({effect:'labelMate'});
	   
	   //check if not valid stop loading ajax icons
	   if( ! inputs.data("validator").checkValidity()){
		   
		   event.stopImmediatePropagation();
		   
	   }
	   
	   
	   
   });
     
     

});
