/* MP Product Search Widget Ajax JS
------------------------------------------------------------------------------------------------------------------- */

var MPPSW_JS = new function() {

	var t = this;

	/* Search Query function
	---------------------------------------------------------- */
	t.submitSearchQuery = function( input , Main_Modal ) {

		var formElm = input.parents('form.mppsw-product-search-form'),
			tempHtml = formElm.html(),
			serializedForm = formElm.serialize();

		formElm.html( MPPSW_Ajax.loadingText );

		jQuery.post(MPPSW_Ajax.ajaxUrl, serializedForm, function(data) {
			var result = data.split('|SEP|', 3);
			if (result[0] == 'error') {
				formElm.html(result[1]);
				mppsw_search_reset();
			} else {
				formElm.fadeOut(1000, function(){
					formElm.html(result[1]).fadeIn('fast');
					Main_Modal.html(result[2]);
				});
			}
		});
	}

	/* Update Results function
	---------------------------------------------------------- */
	t.updateSearchResults = function() {

		var input = jQuery(this),
			formElm = input.parents('form.mppsw-search-results-listing-form'),
			tempHtml = formElm.html(),
			serializedForm = formElm.serialize();

		formElm.html( MPPSW_Ajax.UpdatingText );
		jQuery.post(MPPSW_Ajax.ajaxUrl, serializedForm, function(data) {
			var result = data.split('|SEP|', 2);
			if (result[0] == 'error') {
				formElm.html(tempHtml);
				formElm.find('tbody.mppsw-table-search-results-body').html(result[1]);
			} else {
				formElm.html(result[1]);
				// update listener
				updateListener();
			}				
		});
		
		return false;
	}

	/* Search Form Reset function
	---------------------------------------------------------- */
	t.searchFormReset = function( input , Main_Modal ) {

		var formElm = input.parents('form.mppsw-product-search-form'),
			tempHtml = formElm.html(),
			serializedForm = formElm.serialize();

		formElm.html( MPPSW_Ajax.loadingText );
		jQuery.post(MPPSW_Ajax.ajaxUrl, serializedForm, function(data) {
			var result = data.split('|SEP|', 2);
			formElm.fadeOut(1000, function(){
				formElm.html(result[1]).fadeIn('fast');
				Main_Modal.html('');
			});
			
		});
	}

	/* Filter By function
	---------------------------------------------------------- */
	t.filterBy = function() {

		var input = jQuery(this),
			val = input.find("option:selected").val(),
			inputParent = input.parent("div.mppsw-select-filterby"),
			filterCat = inputParent.siblings("div.filter-by-category-select"),
			filterTag = inputParent.siblings("div.filter-by-tag-select");

		if ( val == 'filterbycategory' ){
			filterTag.hide();
			filterCat.show('1000');
		} else if ( val == 'filterbytag' ){
			filterCat.hide();
			filterTag.show('1000');
		} else {
			filterCat.hide();
			filterTag.hide();
		}

	}

	/* Modal functions
	---------------------------------------------------------- */
	t.triggerModal = function( Main_Modal ) {
		Main_Modal.removeClass(".mfp-hide");
		jQuery.magnificPopup.open({
		  	items: {
		    	src: Main_Modal,
		    	type: 'inline',
		  	},
          	fixedContentPos: false,
          	fixedBgPos: true,
          	overflowY: "scroll",
          	closeBtnInside: true,
          	preloader: false, 
          	midClick: true,
          	removalDelay: 300,
          	mainClass: "mppsw-mfp-zoom-in"
		});
		// update listener
		updateListener();
	}

	var updateListener = function() {
		jQuery("button.mppsw-search-results-update-btn").on( "click" , MPPSW_JS.updateSearchResults );
	}

	/* Run the init function
	---------------------------------------------------------- */
	jQuery(document).ready(function(){

		var MPPSWMainForm = jQuery("form.mppsw-product-search-form"),
			MPPSWMainModal = jQuery('div#mppsw-results-modal');

		// search query listener
		MPPSWMainForm.on( "click" , "button.mppsw-search-submit-button" , function() { 
			var input = jQuery(this);
			MPPSW_JS.submitSearchQuery( input , MPPSWMainModal );
			return false;
		});

		// filter by listener
		MPPSWMainForm.on( "click" , "select.mppsw-filterbyselect" , MPPSW_JS.filterBy );

		// reset listener
		MPPSWMainForm.on( "click" , "button.mppsw-reset-search-link" , function() { 
			var input = jQuery(this);
			MPPSW_JS.searchFormReset( input , MPPSWMainModal );
			return false;
		});

		// modal trigger listener
		MPPSWMainForm.on( "click" , ".mppsw-modal-trigger" , function(){
			MPPSW_JS.triggerModal( MPPSWMainModal );
			return false;
		});
		
	});

}