/* global wp, jQuery */
/**
 * File customizer.js.
 *
 * Theme Customizer enhancements for a better user experience.
 *
 * Contains handlers to make Theme Customizer preview reload changes asynchronously.
 */

 /**
 * Dynamic Internal/Embedded Style for a Control
 */
function portfolio_one_dynamic_css( control, style ) {
	control = control.replace( '[', '-' );
	control = control.replace( ']', '' );
	jQuery( 'style#' + control ).remove();

	jQuery( 'head' ).append(
		'<style id="' + control + '">' + style + '</style>'
	);
}



( function( $ ) {
	// Site title and description.
	wp.customize( 'blogname', function( value ) {
		value.bind( function( to ) {
			$( '.site-title a' ).text( to );
		} );
	} );
	wp.customize( 'blogdescription', function( value ) {
		value.bind( function( to ) {
			$( '.site-description' ).text( to );
		} );
	} );

	// Header text color.
	wp.customize( 'header_textcolor', function( value ) {
		value.bind( function( to ) {
			if ( 'blank' === to ) {
				$( '.site-title, .site-description' ).css( {
					clip: 'rect(1px, 1px, 1px, 1px)',
					position: 'absolute',
				} );
			} else {
				$( '.site-title, .site-description' ).css( {
					clip: 'auto',
					position: 'relative',
				} );
				$( '.site-title a, .site-description' ).css( {
					color: to,
				} );
			}
		} );
	} );

	/**
	* Header mail address
	*
	*/
	wp.customize( 'max_portfolio_email_input_field', function( value ) {
		value.bind( function( to ) {
			$( '.max-left-header .site-email a' ).text( to );
		} );
	} );


	/**
	* Header cta button
	*
	*/
	wp.customize( 'cww_header_cta_text', function( value ) {
		value.bind( function( to ) {
			$( '.header-cta-wrapp a span' ).text( to );
		} );
	} );

	wp.customize( 'cww_header_cta_url', function( value ) {
		value.bind( function( to ) {
			$( '.header-cta-wrapp a' ).attr('href', to );
		} );
	} );

	wp.customize( 'cww_header_cta_bg', function( value ) {
		value.bind( function( color ) {
			if (color == '') {
				wp.customize.preview.send( 'refresh' );
			}
			if ( color ) {
				var	dynamicStyle = '.header-cta-wrapp.layout-one a,.header-cta-wrapp.layout-two span.icon-wrap,.header-cta-wrapp.layout-two a:hover .inner-btn-wrapp { background-color: ' + color + '; } ';                
				portfolio_one_dynamic_css( 'cww_header_cta_bg', dynamicStyle );
			}

		} );
	} );
	
	

	//header bg color
	wp.customize( 'cww_header_bg', function( value ) {
		value.bind( function( to ) {
			$( 'header.site-header' ).css({
				'background': to ,
			});
		} );
	} );

	//Menu link color
	wp.customize( 'cww_menu_link_color', function( value ) {
		value.bind( function( to ) {
			$( '.main-navigation a' ).css({
				'color': to ,
			});
		} );
	} );

	
	wp.customize( 'cww_menu_link_color_hover', function( value ) {
		value.bind( function( color ) {
			if (color == '') {
				wp.customize.preview.send( 'refresh' );
			}
			if ( color ) {
				var	dynamicStyle = '.main-navigation a:hover { color: ' + color + '; } ';                
				portfolio_one_dynamic_css( 'cww_menu_link_color_hover', dynamicStyle );
			}

		} );
	} );
	

}( jQuery ) );
