/**
 * Live-update changed settings in real time in the Customizer preview.
 */

( function( $ ) {
	
	'use strict';
		
	var api = wp.customize,
	cacheColor = '';
	
	$.fn.removeClassWild = function(mask) {
        return this.removeClass(function(index, cls) {
            var re = mask.replace(/\*/g, '\\S+');
            return (cls.match(new RegExp('\\b' + re + '', 'g')) || []).join(' ');
        });
    };
			
	_.each( kktfwp_jsvars, function( jsVars, setting ) {		
		
		if ( jsVars !== undefined ) {
									
				// Make sure element is defined.
				if ( jsVars.element === undefined ) {
					jsVars.element = '';
				}
				
				// Make sure property is defined.
				if ( jsVars.property === undefined ) {
					jsVars.property = '';
				}
				
				// Use css if method is undefined
				if ( jsVars['function'] === undefined ) {
					jsVars['function'] = 'css';
				}
				
				// Use empty prefix if undefined
				if ( jsVars.prefix === undefined ) {
					jsVars.prefix = '';
				}

				// Use empty suffix if undefined
				if ( jsVars.suffix === undefined ) {
					jsVars.suffix = '';
				}
				
				// Use empty suffix if undefined
				if ( jsVars.reload === undefined ) {
					jsVars.reload = 0;
				}
				
				// Cache value
				if ( jsVars.cache === undefined ) {
					jsVars.cache = false;
				}
		}	
		
		api( setting, function( value ) {

			value.bind( function( newval ) {
				
				var val = jsVars.prefix + (( jsVars.property === 'opacity' ) ? newval/100 : newval ) + jsVars.suffix;
				
				if ( jsVars['function'] === 'css' ) {
					
					$( jsVars.element ).css( jsVars.property, val );
						
						if ( jsVars.cache ) {
							cacheColor = val;
						}
				} else if ( jsVars['function'] === 'html' ) {
					$( jsVars.element ).html( val );
				} else {
					$( jsVars.element ).text( val );
				}
				
				if ( jsVars.reload ) {
					$('.kktfwp-projects').isotope('layout');					
				}
				
				if ( jsVars.property === 'fontFamily' ) {
					WebFont.load({
						google: {
							families: [ newval + ':100,200,300,400,500,600,700,800,900:latin,latin-ext' ]
						}
					});					
				}
					
			})
			
		})
		
	});
	
	// Content Opcacity
	
	api( 'kktfwp_content_opacity', function( value ) {
		value.bind( function( newval ) {
			if ( api( 'kktfwp_color_scheme' )._value === 'dark' ) {
				$( '.content, .container' ).css( 'background-color', 'rgba(0,0,0,'+ newval / 100 );
			} else {
				$( '.content, .container' ).css( 'background-color', 'rgba(255,255,255,'+ newval / 100 );
			}
		} );
	} );
	
	// Blog sidebar position
	
	api( 'kktfwp_blog_sidebar', function( value ) {
		value.bind( function( newval ) {
			
			if ( newval === 'left' ) {
				$('body').addClass( 'left-sidebar' );
			} else {
				$('body').removeClass( 'left-sidebar' );
			}
			
		} );
	} );
	
	
	// Logo Height

	api( 'blogname', function( value ) {
		value.bind( function( newval ) {
			$( '.kktfwp-logo a' ).html( newval );
		} );
	} );
	
} )( jQuery );