web/wp-admin/js/customize-controls.js
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
     1 (function(a,c){var b=wp.customize;b.Setting=b.Value.extend({initialize:function(g,f,d){var e;b.Value.prototype.initialize.call(this,f,d);this.id=g;this.transport=this.transport||"refresh";this.bind(this.preview)},preview:function(){switch(this.transport){case"refresh":return this.previewer.refresh();case"postMessage":return this.previewer.send("setting",[this.id,this()])}}});b.Control=b.Class.extend({initialize:function(i,e){var g=this,d,h,f;this.params={};c.extend(this,e||{});this.id=i;this.selector="#customize-control-"+i.replace("]","").replace("[","-");this.container=c(this.selector);f=c.map(this.params.settings,function(j){return j});b.apply(b,f.concat(function(){var j;g.settings={};for(j in g.params.settings){g.settings[j]=b(g.params.settings[j])}g.setting=g.settings["default"]||null;g.ready()}));g.elements=[];d=this.container.find("[data-customize-setting-link]");h={};d.each(function(){var k=c(this),j;if(k.is(":radio")){j=k.prop("name");if(h[j]){return}h[j]=true;k=d.filter('[name="'+j+'"]')}b(k.data("customizeSettingLink"),function(m){var l=new b.Element(k);g.elements.push(l);l.sync(m);l.set(m())})})},ready:function(){},dropdownInit:function(){var e=this,d=this.container.find(".dropdown-status"),f=this.params,g=function(h){if(typeof h==="string"&&f.statuses&&f.statuses[h]){d.html(f.statuses[h]).show()}else{d.hide()}};this.container.on("click",".dropdown",function(h){h.preventDefault();e.container.toggleClass("open")});this.setting.bind(g);g(this.setting())}});b.ColorControl=b.Control.extend({ready:function(){var g=this,f,e,d,h,i;f=/^#([A-Fa-f0-9]{3}){0,2}$/;e=this.container.find(".dropdown-content");d=new b.Element(this.container.find(".color-picker-hex"));i=function(j){e.css("background",j);g.farbtastic.setColor(j)};this.farbtastic=c.farbtastic(this.container.find(".farbtastic-placeholder"),g.setting.set);d.sync(this.setting).validate=function(j){return f.test(j)?j:null};this.setting.bind(i);i(this.setting());this.dropdownInit()}});b.UploadControl=b.Control.extend({ready:function(){var d=this;this.params.removed=this.params.removed||"";this.success=c.proxy(this.success,this);this.uploader=c.extend({container:this.container,browser:this.container.find(".upload"),dropzone:this.container.find(".upload-dropzone"),success:this.success},this.uploader||{});if(this.uploader.supported){if(d.params.context){d.uploader.param("post_data[context]",this.params.context)}d.uploader.param("post_data[theme]",b.settings.theme.stylesheet)}this.uploader=new wp.Uploader(this.uploader);this.remover=this.container.find(".remove");this.remover.click(function(e){d.setting.set(d.params.removed);e.preventDefault()});this.removerVisibility=c.proxy(this.removerVisibility,this);this.setting.bind(this.removerVisibility);this.removerVisibility(this.setting.get())},success:function(d){this.setting.set(d.url)},removerVisibility:function(d){this.remover.toggle(d!=this.params.removed)}});b.ImageControl=b.UploadControl.extend({ready:function(){var e=this,d;this.uploader={init:function(f){var h,g;if(this.supports.dragdrop){return}h=e.container.find(".upload-fallback");g=h.children().detach();this.browser.detach().empty().append(g);h.append(this.browser).show()}};b.UploadControl.prototype.ready.call(this);this.thumbnail=this.container.find(".preview-thumbnail img");this.thumbnailSrc=c.proxy(this.thumbnailSrc,this);this.setting.bind(this.thumbnailSrc);this.library=this.container.find(".library");this.tabs={};d=this.library.find(".library-content");this.library.children("ul").children("li").each(function(){var g=c(this),h=g.data("customizeTab"),f=d.filter('[data-customize-tab="'+h+'"]');e.tabs[h]={both:g.add(f),link:g,panel:f}});this.library.children("ul").on("click","li",function(g){var h=c(this).data("customizeTab"),f=e.tabs[h];g.preventDefault();if(f.link.hasClass("library-selected")){return}e.selected.both.removeClass("library-selected");e.selected=f;e.selected.both.addClass("library-selected")});this.library.on("click","a",function(f){var g=c(this).data("customizeImageValue");if(g){e.setting.set(g);f.preventDefault()}});if(this.tabs.uploaded){this.tabs.uploaded.target=this.library.find(".uploaded-target");if(!this.tabs.uploaded.panel.find(".thumbnail").length){this.tabs.uploaded.both.addClass("hidden")}}d.each(function(){var f=e.tabs[c(this).data("customizeTab")];if(!f.link.hasClass("hidden")){e.selected=f;f.both.addClass("library-selected");return false}});this.dropdownInit()},success:function(d){b.UploadControl.prototype.success.call(this,d);if(this.tabs.uploaded&&this.tabs.uploaded.target.length){this.tabs.uploaded.both.removeClass("hidden");d.element=c('<a href="#" class="thumbnail"></a>').data("customizeImageValue",d.url).append('<img src="'+d.url+'" />').appendTo(this.tabs.uploaded.target)}},thumbnailSrc:function(d){if(/^(https?:)?\/\//.test(d)){this.thumbnail.prop("src",d).show()}else{this.thumbnail.hide()}}});b.defaultConstructor=b.Setting;b.control=new b.Values({defaultConstructor:b.Control});b.PreviewFrame=b.Messenger.extend({sensitivity:2000,initialize:function(g,f){var e=c.Deferred(),d=this;e.promise(this);this.container=g.container;this.signature=g.signature;c.extend(g,{channel:b.PreviewFrame.uuid()});b.Messenger.prototype.initialize.call(this,g,f);this.add("previewUrl",g.previewUrl);this.query=c.extend(g.query||{},{customize_messenger_channel:this.channel()});this.run(e)},run:function(e){var d=this,f=false,g=false;if(this._ready){this.unbind("ready",this._ready)}this._ready=function(){g=true;if(f){e.resolveWith(d)}};this.bind("ready",this._ready);this.request=c.ajax(this.previewUrl(),{type:"POST",data:this.query,xhrFields:{withCredentials:true}});this.request.fail(function(){e.rejectWith(d,["request failure"])});this.request.done(function(j){var i=d.request.getResponseHeader("Location"),h=d.signature,k;if(i&&i!=d.previewUrl()){e.rejectWith(d,["redirect",i]);return}if("0"===j){d.login(e);return}if("-1"===j){e.rejectWith(d,["cheatin"]);return}k=j.lastIndexOf(h);if(-1===k||k<j.lastIndexOf("</html>")){e.rejectWith(d,["unsigned"]);return}j=j.slice(0,k)+j.slice(k+h.length);d.iframe=c("<iframe />").appendTo(d.container);d.iframe.one("load",function(){f=true;if(g){e.resolveWith(d)}else{setTimeout(function(){e.rejectWith(d,["ready timeout"])},d.sensitivity)}});d.targetWindow(d.iframe[0].contentWindow);d.targetWindow().document.open();d.targetWindow().document.write(j);d.targetWindow().document.close()})},login:function(e){var d=this,f;f=function(){e.rejectWith(d,["logged out"])};if(this.triedLogin){return f()}c.get(b.settings.url.ajax,{action:"logged-in"}).fail(f).done(function(g){var h;if("1"!==g){f()}h=c('<iframe src="'+d.previewUrl()+'" />').hide();h.appendTo(d.container);h.load(function(){d.triedLogin=true;h.remove();d.run(e)})})},destroy:function(){b.Messenger.prototype.destroy.call(this);this.request.abort();if(this.iframe){this.iframe.remove()}delete this.request;delete this.iframe;delete this.targetWindow}});(function(){var d=0;b.PreviewFrame.uuid=function(){return"preview-"+d++}}());b.Previewer=b.Messenger.extend({refreshBuffer:250,initialize:function(h,f){var d=this,g=/^https?/,e;c.extend(this,f||{});this.refresh=(function(i){var j=i.refresh,l=function(){k=null;j.call(i)},k;return function(){if(typeof k!=="number"){if(i.loading){i.abort()}else{return l()}}clearTimeout(k);k=setTimeout(l,i.refreshBuffer)}})(this);this.container=b.ensure(h.container);this.allowedUrls=h.allowedUrls;this.signature=h.signature;h.url=window.location.href;b.Messenger.prototype.initialize.call(this,h);this.add("scheme",this.origin()).link(this.origin).setter(function(j){var i=j.match(g);return i?i[0]:""});this.add("previewUrl",h.previewUrl).setter(function(j){var i;if(/\/wp-admin(\/|$)/.test(j.replace(/[#?].*$/,""))){return null}c.each([j.replace(g,d.scheme()),j],function(l,k){c.each(d.allowedUrls,function(m,n){if(0===k.indexOf(n)){i=k;return false}});if(i){return false}});return i?i:null});this.previewUrl.bind(this.refresh);this.scroll=0;this.bind("scroll",function(i){this.scroll=i});this.bind("url",this.previewUrl)},query:function(){},abort:function(){if(this.loading){this.loading.destroy();delete this.loading}},refresh:function(){var d=this;this.abort();this.loading=new b.PreviewFrame({url:this.url(),previewUrl:this.previewUrl(),query:this.query()||{},container:this.container,signature:this.signature});this.loading.done(function(){this.bind("synced",function(){if(d.preview){d.preview.destroy()}d.preview=this;delete d.loading;d.targetWindow(this.targetWindow());d.channel(this.channel());d.send("active")});this.send("sync",{scroll:d.scroll,settings:b.get()})});this.loading.fail(function(f,e){if("redirect"===f&&e){d.previewUrl(e)}if("logged out"===f){if(d.preview){d.preview.destroy();delete d.preview}d.login().done(d.refresh)}if("cheatin"===f){d.cheatin()}})},login:function(){var g=this,d,f,e;if(this._login){return this._login}d=c.Deferred();this._login=d.promise();f=new b.Messenger({channel:"login",url:b.settings.url.login});e=c('<iframe src="'+b.settings.url.login+'" />').appendTo(this.container);f.targetWindow(e[0].contentWindow);f.bind("login",function(){e.remove();f.destroy();delete g._login;d.resolve()});return this._login},cheatin:function(){c(document.body).empty().addClass("cheatin").append("<p>"+b.l10n.cheatin+"</p>")}});b.controlConstructor={color:b.ColorControl,upload:b.UploadControl,image:b.ImageControl};c(function(){b.settings=window._wpCustomizeSettings;b.l10n=window._wpCustomizeControlsL10n;if(!b.settings){return}if(!c.support.postMessage||(!c.support.cors&&b.settings.isCrossDomain)){return window.location=b.settings.url.fallback}var d=c(document.body),e=d.children(".wp-full-overlay"),g,h,f;c("#customize-controls").on("keydown",function(i){if(c(i.target).is("textarea")){return}if(13===i.which){i.preventDefault()}});h=new b.Previewer({container:"#customize-preview",form:"#customize-controls",previewUrl:b.settings.url.preview,allowedUrls:b.settings.url.allowed,signature:"WP_CUSTOMIZER_SIGNATURE"},{nonce:b.settings.nonce,query:function(){return{wp_customize:"on",theme:b.settings.theme.stylesheet,customized:JSON.stringify(b.get()),nonce:this.nonce.preview}},save:function(){var i=this,k=c.extend(this.query(),{action:"customize_save",nonce:this.nonce.save}),j=c.post(b.settings.url.ajax,k);b.trigger("save",j);d.addClass("saving");j.always(function(){d.removeClass("saving")});j.done(function(l){if("0"===l){i.preview.iframe.hide();i.login().done(function(){i.save();i.preview.iframe.show()});return}if("-1"===l){i.cheatin();return}b.trigger("saved")})}});h.bind("nonce",function(i){c.extend(this.nonce,i)});c.each(b.settings.settings,function(j,i){b.create(j,j,i.value,{transport:i.transport,previewer:h})});c.each(b.settings.controls,function(l,j){var i=b.controlConstructor[j.type]||b.Control,k;k=b.control.add(l,new i(l,{params:j,previewer:h}))});if(h.previewUrl()){h.refresh()}else{h.previewUrl(b.settings.url.home)}(function(){var k=new b.Values(),j=k.create("saved"),i=k.create("activated");k.bind("change",function(){var m=c("#save"),l=c(".back");if(!i()){m.val(b.l10n.activate).prop("disabled",false);l.text(b.l10n.cancel)}else{if(j()){m.val(b.l10n.saved).prop("disabled",true);l.text(b.l10n.close)}else{m.val(b.l10n.save).prop("disabled",false);l.text(b.l10n.cancel)}}});j(true);i(b.settings.theme.active);b.bind("change",function(){k("saved").set(false)});b.bind("saved",function(){k("saved").set(true);k("activated").set(true)});i.bind(function(l){if(l){b.trigger("activated")}});b.state=k}());c(".customize-section-title").click(function(j){var i=c(this).parents(".customize-section");if(i.hasClass("cannot-expand")){return}c(".customize-section").not(i).removeClass("open");i.toggleClass("open");j.preventDefault()});c("#save").click(function(i){h.save();i.preventDefault()});c(".collapse-sidebar").click(function(i){e.toggleClass("collapsed").toggleClass("expanded");i.preventDefault()});f=new b.Messenger({url:b.settings.url.parent,channel:"loader"});f.bind("back",function(){c(".back").on("click.back",function(i){i.preventDefault();f.send("close")})});b.bind("saved",function(){f.send("saved")});b.bind("activated",function(){if(f.targetWindow()){f.send("activated",b.settings.url.activated)}else{if(b.settings.url.activated){window.location=b.settings.url.activated}}});f.send("ready");c.each({background_image:{controls:["background_repeat","background_position_x","background_attachment"],callback:function(i){return !!i}},show_on_front:{controls:["page_on_front","page_for_posts"],callback:function(i){return"page"===i}},header_textcolor:{controls:["header_textcolor"],callback:function(i){return"blank"!==i}}},function(i,j){b(i,function(k){c.each(j.controls,function(l,m){b.control(m,function(o){var n=function(p){o.container.toggle(j.callback(p))};n(k.get());k.bind(n)})})})});b.control("display_header_text",function(j){var i="";j.elements[0].unsync(b("header_textcolor"));j.element=new b.Element(j.container.find("input"));j.element.set("blank"!==j.setting());j.element.bind(function(k){if(!k){i=b("header_textcolor").get()}j.setting.set(k?i:"blank")});j.setting.bind(function(k){j.element.set("blank"!==k)})});b.control("header_image",function(i){i.setting.bind(function(j){if(j===i.params.removed){i.settings.data.set(false)}});i.library.on("click","a",function(j){i.settings.data.set(c(this).data("customizeHeaderImageData"))});i.uploader.success=function(k){var j;b.ImageControl.prototype.success.call(i,k);j={attachment_id:k.id,url:k.url,thumbnail_url:k.url,height:k.meta.height,width:k.meta.width};k.element.data("customizeHeaderImageData",j);i.settings.data.set(j)}});b.trigger("ready")})})(wp,jQuery);
     1 (function( exports, $ ){
       
     2 	var api = wp.customize;
       
     3 
       
     4 	/*
       
     5 	 * @param options
       
     6 	 * - previewer - The Previewer instance to sync with.
       
     7 	 * - transport - The transport to use for previewing. Supports 'refresh' and 'postMessage'.
       
     8 	 */
       
     9 	api.Setting = api.Value.extend({
       
    10 		initialize: function( id, value, options ) {
       
    11 			var element;
       
    12 
       
    13 			api.Value.prototype.initialize.call( this, value, options );
       
    14 
       
    15 			this.id = id;
       
    16 			this.transport = this.transport || 'refresh';
       
    17 
       
    18 			this.bind( this.preview );
       
    19 		},
       
    20 		preview: function() {
       
    21 			switch ( this.transport ) {
       
    22 				case 'refresh':
       
    23 					return this.previewer.refresh();
       
    24 				case 'postMessage':
       
    25 					return this.previewer.send( 'setting', [ this.id, this() ] );
       
    26 			}
       
    27 		}
       
    28 	});
       
    29 
       
    30 	api.Control = api.Class.extend({
       
    31 		initialize: function( id, options ) {
       
    32 			var control = this,
       
    33 				nodes, radios, settings;
       
    34 
       
    35 			this.params = {};
       
    36 			$.extend( this, options || {} );
       
    37 
       
    38 			this.id = id;
       
    39 			this.selector = '#customize-control-' + id.replace( ']', '' ).replace( '[', '-' );
       
    40 			this.container = $( this.selector );
       
    41 
       
    42 			settings = $.map( this.params.settings, function( value ) {
       
    43 				return value;
       
    44 			});
       
    45 
       
    46 			api.apply( api, settings.concat( function() {
       
    47 				var key;
       
    48 
       
    49 				control.settings = {};
       
    50 				for ( key in control.params.settings ) {
       
    51 					control.settings[ key ] = api( control.params.settings[ key ] );
       
    52 				}
       
    53 
       
    54 				control.setting = control.settings['default'] || null;
       
    55 				control.ready();
       
    56 			}) );
       
    57 
       
    58 			control.elements = [];
       
    59 
       
    60 			nodes  = this.container.find('[data-customize-setting-link]');
       
    61 			radios = {};
       
    62 
       
    63 			nodes.each( function() {
       
    64 				var node = $(this),
       
    65 					name;
       
    66 
       
    67 				if ( node.is(':radio') ) {
       
    68 					name = node.prop('name');
       
    69 					if ( radios[ name ] )
       
    70 						return;
       
    71 
       
    72 					radios[ name ] = true;
       
    73 					node = nodes.filter( '[name="' + name + '"]' );
       
    74 				}
       
    75 
       
    76 				api( node.data('customizeSettingLink'), function( setting ) {
       
    77 					var element = new api.Element( node );
       
    78 					control.elements.push( element );
       
    79 					element.sync( setting );
       
    80 					element.set( setting() );
       
    81 				});
       
    82 			});
       
    83 		},
       
    84 
       
    85 		ready: function() {},
       
    86 
       
    87 		dropdownInit: function() {
       
    88 			var control  = this,
       
    89 				statuses = this.container.find('.dropdown-status'),
       
    90 				params   = this.params,
       
    91 				update   = function( to ) {
       
    92 					if ( typeof	to === 'string' && params.statuses && params.statuses[ to ] )
       
    93 						statuses.html( params.statuses[ to ] ).show();
       
    94 					else
       
    95 						statuses.hide();
       
    96 				};
       
    97 
       
    98 			var toggleFreeze = false;
       
    99 
       
   100 			// Support the .dropdown class to open/close complex elements
       
   101 			this.container.on( 'click keydown', '.dropdown', function( event ) {
       
   102 				if ( event.type === 'keydown' &&  13 !== event.which ) // enter
       
   103 					return;
       
   104 
       
   105 				event.preventDefault();
       
   106 
       
   107 				if (!toggleFreeze)
       
   108 					control.container.toggleClass('open');
       
   109 
       
   110 				if ( control.container.hasClass('open') )
       
   111 					control.container.parent().parent().find('li.library-selected').focus();
       
   112 
       
   113 				// Don't want to fire focus and click at same time
       
   114 				toggleFreeze = true;
       
   115 				setTimeout(function () {
       
   116 					toggleFreeze = false;
       
   117 				}, 400);
       
   118 			});
       
   119 
       
   120 			this.setting.bind( update );
       
   121 			update( this.setting() );
       
   122 		}
       
   123 	});
       
   124 
       
   125 	api.ColorControl = api.Control.extend({
       
   126 		ready: function() {
       
   127 			var control = this,
       
   128 				picker = this.container.find('.color-picker-hex');
       
   129 
       
   130 			picker.val( control.setting() ).wpColorPicker({
       
   131 				change: function( event, options ) {
       
   132 					control.setting.set( picker.wpColorPicker('color') );
       
   133  				},
       
   134  				clear: function() {
       
   135  					control.setting.set( false );
       
   136  				}
       
   137 			});
       
   138 		}
       
   139 	});
       
   140 
       
   141 	api.UploadControl = api.Control.extend({
       
   142 		ready: function() {
       
   143 			var control = this;
       
   144 
       
   145 			this.params.removed = this.params.removed || '';
       
   146 
       
   147 			this.success = $.proxy( this.success, this );
       
   148 
       
   149 			this.uploader = $.extend({
       
   150 				container: this.container,
       
   151 				browser:   this.container.find('.upload'),
       
   152 				dropzone:  this.container.find('.upload-dropzone'),
       
   153 				success:   this.success,
       
   154 				plupload:  {},
       
   155 				params:    {}
       
   156 			}, this.uploader || {} );
       
   157 
       
   158 			if ( control.params.extensions ) {
       
   159 				control.uploader.plupload.filters = [{
       
   160 					title:      api.l10n.allowedFiles,
       
   161 					extensions: control.params.extensions
       
   162 				}];
       
   163 			}
       
   164 
       
   165 			if ( control.params.context )
       
   166 				control.uploader.params['post_data[context]'] = this.params.context;
       
   167 
       
   168 			if ( api.settings.theme.stylesheet )
       
   169 				control.uploader.params['post_data[theme]'] = api.settings.theme.stylesheet;
       
   170 
       
   171 			this.uploader = new wp.Uploader( this.uploader );
       
   172 
       
   173 			this.remover = this.container.find('.remove');
       
   174 			this.remover.on( 'click keydown', function( event ) {
       
   175 				if ( event.type === 'keydown' &&  13 !== event.which ) // enter
       
   176 					return;
       
   177 
       
   178 				control.setting.set( control.params.removed );
       
   179 				event.preventDefault();
       
   180 			});
       
   181 
       
   182 			this.removerVisibility = $.proxy( this.removerVisibility, this );
       
   183 			this.setting.bind( this.removerVisibility );
       
   184 			this.removerVisibility( this.setting.get() );
       
   185 		},
       
   186 		success: function( attachment ) {
       
   187 			this.setting.set( attachment.get('url') );
       
   188 		},
       
   189 		removerVisibility: function( to ) {
       
   190 			this.remover.toggle( to != this.params.removed );
       
   191 		}
       
   192 	});
       
   193 
       
   194 	api.ImageControl = api.UploadControl.extend({
       
   195 		ready: function() {
       
   196 			var control = this,
       
   197 				panels;
       
   198 
       
   199 			this.uploader = {
       
   200 				init: function( up ) {
       
   201 					var fallback, button;
       
   202 
       
   203 					if ( this.supports.dragdrop )
       
   204 						return;
       
   205 
       
   206 					// Maintain references while wrapping the fallback button.
       
   207 					fallback = control.container.find( '.upload-fallback' );
       
   208 					button   = fallback.children().detach();
       
   209 
       
   210 					this.browser.detach().empty().append( button );
       
   211 					fallback.append( this.browser ).show();
       
   212 				}
       
   213 			};
       
   214 
       
   215 			api.UploadControl.prototype.ready.call( this );
       
   216 
       
   217 			this.thumbnail    = this.container.find('.preview-thumbnail img');
       
   218 			this.thumbnailSrc = $.proxy( this.thumbnailSrc, this );
       
   219 			this.setting.bind( this.thumbnailSrc );
       
   220 
       
   221 			this.library = this.container.find('.library');
       
   222 
       
   223 			// Generate tab objects
       
   224 			this.tabs = {};
       
   225 			panels    = this.library.find('.library-content');
       
   226 
       
   227 			this.library.children('ul').children('li').each( function() {
       
   228 				var link  = $(this),
       
   229 					id    = link.data('customizeTab'),
       
   230 					panel = panels.filter('[data-customize-tab="' + id + '"]');
       
   231 
       
   232 				control.tabs[ id ] = {
       
   233 					both:  link.add( panel ),
       
   234 					link:  link,
       
   235 					panel: panel
       
   236 				};
       
   237 			});
       
   238 
       
   239 			// Bind tab switch events
       
   240 			this.library.children('ul').on( 'click keydown', 'li', function( event ) {
       
   241 				if ( event.type === 'keydown' &&  13 !== event.which ) // enter
       
   242 					return;
       
   243 
       
   244 				var id  = $(this).data('customizeTab'),
       
   245 					tab = control.tabs[ id ];
       
   246 
       
   247 				event.preventDefault();
       
   248 
       
   249 				if ( tab.link.hasClass('library-selected') )
       
   250 					return;
       
   251 
       
   252 				control.selected.both.removeClass('library-selected');
       
   253 				control.selected = tab;
       
   254 				control.selected.both.addClass('library-selected');
       
   255 			});
       
   256 
       
   257 			// Bind events to switch image urls.
       
   258 			this.library.on( 'click keydown', 'a', function( event ) {
       
   259 				if ( event.type === 'keydown' && 13 !== event.which ) // enter
       
   260 					return;
       
   261 
       
   262 				var value = $(this).data('customizeImageValue');
       
   263 
       
   264 				if ( value ) {
       
   265 					control.setting.set( value );
       
   266 					event.preventDefault();
       
   267 				}
       
   268 			});
       
   269 
       
   270 			if ( this.tabs.uploaded ) {
       
   271 				this.tabs.uploaded.target = this.library.find('.uploaded-target');
       
   272 				if ( ! this.tabs.uploaded.panel.find('.thumbnail').length )
       
   273 					this.tabs.uploaded.both.addClass('hidden');
       
   274 			}
       
   275 
       
   276 			// Select a tab
       
   277 			panels.each( function() {
       
   278 				var tab = control.tabs[ $(this).data('customizeTab') ];
       
   279 
       
   280 				// Select the first visible tab.
       
   281 				if ( ! tab.link.hasClass('hidden') ) {
       
   282 					control.selected = tab;
       
   283 					tab.both.addClass('library-selected');
       
   284 					return false;
       
   285 				}
       
   286 			});
       
   287 
       
   288 			this.dropdownInit();
       
   289 		},
       
   290 		success: function( attachment ) {
       
   291 			api.UploadControl.prototype.success.call( this, attachment );
       
   292 
       
   293 			// Add the uploaded image to the uploaded tab.
       
   294 			if ( this.tabs.uploaded && this.tabs.uploaded.target.length ) {
       
   295 				this.tabs.uploaded.both.removeClass('hidden');
       
   296 
       
   297 				// @todo: Do NOT store this on the attachment model. That is bad.
       
   298 				attachment.element = $( '<a href="#" class="thumbnail"></a>' )
       
   299 					.data( 'customizeImageValue', attachment.get('url') )
       
   300 					.append( '<img src="' +  attachment.get('url')+ '" />' )
       
   301 					.appendTo( this.tabs.uploaded.target );
       
   302 			}
       
   303 		},
       
   304 		thumbnailSrc: function( to ) {
       
   305 			if ( /^(https?:)?\/\//.test( to ) )
       
   306 				this.thumbnail.prop( 'src', to ).show();
       
   307 			else
       
   308 				this.thumbnail.hide();
       
   309 		}
       
   310 	});
       
   311 
       
   312 	// Change objects contained within the main customize object to Settings.
       
   313 	api.defaultConstructor = api.Setting;
       
   314 
       
   315 	// Create the collection of Control objects.
       
   316 	api.control = new api.Values({ defaultConstructor: api.Control });
       
   317 
       
   318 	api.PreviewFrame = api.Messenger.extend({
       
   319 		sensitivity: 2000,
       
   320 
       
   321 		initialize: function( params, options ) {
       
   322 			var deferred = $.Deferred(),
       
   323 				self     = this;
       
   324 
       
   325 			// This is the promise object.
       
   326 			deferred.promise( this );
       
   327 
       
   328 			this.container = params.container;
       
   329 			this.signature = params.signature;
       
   330 
       
   331 			$.extend( params, { channel: api.PreviewFrame.uuid() });
       
   332 
       
   333 			api.Messenger.prototype.initialize.call( this, params, options );
       
   334 
       
   335 			this.add( 'previewUrl', params.previewUrl );
       
   336 
       
   337 			this.query = $.extend( params.query || {}, { customize_messenger_channel: this.channel() });
       
   338 
       
   339 			this.run( deferred );
       
   340 		},
       
   341 
       
   342 		run: function( deferred ) {
       
   343 			var self   = this,
       
   344 				loaded = false,
       
   345 				ready  = false;
       
   346 
       
   347 			if ( this._ready )
       
   348 				this.unbind( 'ready', this._ready );
       
   349 
       
   350 			this._ready = function() {
       
   351 				ready = true;
       
   352 
       
   353 				if ( loaded )
       
   354 					deferred.resolveWith( self );
       
   355 			};
       
   356 
       
   357 			this.bind( 'ready', this._ready );
       
   358 
       
   359 			this.request = $.ajax( this.previewUrl(), {
       
   360 				type: 'POST',
       
   361 				data: this.query,
       
   362 				xhrFields: {
       
   363 					withCredentials: true
       
   364 				}
       
   365 			} );
       
   366 
       
   367 			this.request.fail( function() {
       
   368 				deferred.rejectWith( self, [ 'request failure' ] );
       
   369 			});
       
   370 
       
   371 			this.request.done( function( response ) {
       
   372 				var location = self.request.getResponseHeader('Location'),
       
   373 					signature = self.signature,
       
   374 					index;
       
   375 
       
   376 				// Check if the location response header differs from the current URL.
       
   377 				// If so, the request was redirected; try loading the requested page.
       
   378 				if ( location && location != self.previewUrl() ) {
       
   379 					deferred.rejectWith( self, [ 'redirect', location ] );
       
   380 					return;
       
   381 				}
       
   382 
       
   383 				// Check if the user is not logged in.
       
   384 				if ( '0' === response ) {
       
   385 					self.login( deferred );
       
   386 					return;
       
   387 				}
       
   388 
       
   389 				// Check for cheaters.
       
   390 				if ( '-1' === response ) {
       
   391 					deferred.rejectWith( self, [ 'cheatin' ] );
       
   392 					return;
       
   393 				}
       
   394 
       
   395 				// Check for a signature in the request.
       
   396 				index = response.lastIndexOf( signature );
       
   397 				if ( -1 === index || index < response.lastIndexOf('</html>') ) {
       
   398 					deferred.rejectWith( self, [ 'unsigned' ] );
       
   399 					return;
       
   400 				}
       
   401 
       
   402 				// Strip the signature from the request.
       
   403 				response = response.slice( 0, index ) + response.slice( index + signature.length );
       
   404 
       
   405 				// Create the iframe and inject the html content.
       
   406 				self.iframe = $('<iframe />').appendTo( self.container );
       
   407 
       
   408 				// Bind load event after the iframe has been added to the page;
       
   409 				// otherwise it will fire when injected into the DOM.
       
   410 				self.iframe.one( 'load', function() {
       
   411 					loaded = true;
       
   412 
       
   413 					if ( ready ) {
       
   414 						deferred.resolveWith( self );
       
   415 					} else {
       
   416 						setTimeout( function() {
       
   417 							deferred.rejectWith( self, [ 'ready timeout' ] );
       
   418 						}, self.sensitivity );
       
   419 					}
       
   420 				});
       
   421 
       
   422 				self.targetWindow( self.iframe[0].contentWindow );
       
   423 
       
   424 				self.targetWindow().document.open();
       
   425 				self.targetWindow().document.write( response );
       
   426 				self.targetWindow().document.close();
       
   427 			});
       
   428 		},
       
   429 
       
   430 		login: function( deferred ) {
       
   431 			var self = this,
       
   432 				reject;
       
   433 
       
   434 			reject = function() {
       
   435 				deferred.rejectWith( self, [ 'logged out' ] );
       
   436 			};
       
   437 
       
   438 			if ( this.triedLogin )
       
   439 				return reject();
       
   440 
       
   441 			// Check if we have an admin cookie.
       
   442 			$.get( api.settings.url.ajax, {
       
   443 				action: 'logged-in'
       
   444 			}).fail( reject ).done( function( response ) {
       
   445 				var iframe;
       
   446 
       
   447 				if ( '1' !== response )
       
   448 					reject();
       
   449 
       
   450 				iframe = $('<iframe src="' + self.previewUrl() + '" />').hide();
       
   451 				iframe.appendTo( self.container );
       
   452 				iframe.load( function() {
       
   453 					self.triedLogin = true;
       
   454 
       
   455 					iframe.remove();
       
   456 					self.run( deferred );
       
   457 				});
       
   458 			});
       
   459 		},
       
   460 
       
   461 		destroy: function() {
       
   462 			api.Messenger.prototype.destroy.call( this );
       
   463 			this.request.abort();
       
   464 
       
   465 			if ( this.iframe )
       
   466 				this.iframe.remove();
       
   467 
       
   468 			delete this.request;
       
   469 			delete this.iframe;
       
   470 			delete this.targetWindow;
       
   471 		}
       
   472 	});
       
   473 
       
   474 	(function(){
       
   475 		var uuid = 0;
       
   476 		api.PreviewFrame.uuid = function() {
       
   477 			return 'preview-' + uuid++;
       
   478 		};
       
   479 	}());
       
   480 
       
   481 	api.Previewer = api.Messenger.extend({
       
   482 		refreshBuffer: 250,
       
   483 
       
   484 		/**
       
   485 		 * Requires params:
       
   486 		 *  - container  - a selector or jQuery element
       
   487 		 *  - previewUrl - the URL of preview frame
       
   488 		 */
       
   489 		initialize: function( params, options ) {
       
   490 			var self = this,
       
   491 				rscheme = /^https?/,
       
   492 				url;
       
   493 
       
   494 			$.extend( this, options || {} );
       
   495 
       
   496 			/*
       
   497 			 * Wrap this.refresh to prevent it from hammering the servers:
       
   498 			 *
       
   499 			 * If refresh is called once and no other refresh requests are
       
   500 			 * loading, trigger the request immediately.
       
   501 			 *
       
   502 			 * If refresh is called while another refresh request is loading,
       
   503 			 * debounce the refresh requests:
       
   504 			 * 1. Stop the loading request (as it is instantly outdated).
       
   505 			 * 2. Trigger the new request once refresh hasn't been called for
       
   506 			 *    self.refreshBuffer milliseconds.
       
   507 			 */
       
   508 			this.refresh = (function( self ) {
       
   509 				var refresh  = self.refresh,
       
   510 					callback = function() {
       
   511 						timeout = null;
       
   512 						refresh.call( self );
       
   513 					},
       
   514 					timeout;
       
   515 
       
   516 				return function() {
       
   517 					if ( typeof timeout !== 'number' ) {
       
   518 						if ( self.loading ) {
       
   519 							self.abort();
       
   520 						} else {
       
   521 							return callback();
       
   522 						}
       
   523 					}
       
   524 
       
   525 					clearTimeout( timeout );
       
   526 					timeout = setTimeout( callback, self.refreshBuffer );
       
   527 				};
       
   528 			})( this );
       
   529 
       
   530 			this.container   = api.ensure( params.container );
       
   531 			this.allowedUrls = params.allowedUrls;
       
   532 			this.signature   = params.signature;
       
   533 
       
   534 			params.url = window.location.href;
       
   535 
       
   536 			api.Messenger.prototype.initialize.call( this, params );
       
   537 
       
   538 			this.add( 'scheme', this.origin() ).link( this.origin ).setter( function( to ) {
       
   539 				var match = to.match( rscheme );
       
   540 				return match ? match[0] : '';
       
   541 			});
       
   542 
       
   543 			// Limit the URL to internal, front-end links.
       
   544 			//
       
   545 			// If the frontend and the admin are served from the same domain, load the
       
   546 			// preview over ssl if the customizer is being loaded over ssl. This avoids
       
   547 			// insecure content warnings. This is not attempted if the admin and frontend
       
   548 			// are on different domains to avoid the case where the frontend doesn't have
       
   549 			// ssl certs.
       
   550 
       
   551 			this.add( 'previewUrl', params.previewUrl ).setter( function( to ) {
       
   552 				var result;
       
   553 
       
   554 				// Check for URLs that include "/wp-admin/" or end in "/wp-admin".
       
   555 				// Strip hashes and query strings before testing.
       
   556 				if ( /\/wp-admin(\/|$)/.test( to.replace(/[#?].*$/, '') ) )
       
   557 					return null;
       
   558 
       
   559 				// Attempt to match the URL to the control frame's scheme
       
   560 				// and check if it's allowed. If not, try the original URL.
       
   561 				$.each([ to.replace( rscheme, self.scheme() ), to ], function( i, url ) {
       
   562 					$.each( self.allowedUrls, function( i, allowed ) {
       
   563 						if ( 0 === url.indexOf( allowed ) ) {
       
   564 							result = url;
       
   565 							return false;
       
   566 						}
       
   567 					});
       
   568 					if ( result )
       
   569 						return false;
       
   570 				});
       
   571 
       
   572 				// If we found a matching result, return it. If not, bail.
       
   573 				return result ? result : null;
       
   574 			});
       
   575 
       
   576 			// Refresh the preview when the URL is changed (but not yet).
       
   577 			this.previewUrl.bind( this.refresh );
       
   578 
       
   579 			this.scroll = 0;
       
   580 			this.bind( 'scroll', function( distance ) {
       
   581 				this.scroll = distance;
       
   582 			});
       
   583 
       
   584 			// Update the URL when the iframe sends a URL message.
       
   585 			this.bind( 'url', this.previewUrl );
       
   586 		},
       
   587 
       
   588 		query: function() {},
       
   589 
       
   590 		abort: function() {
       
   591 			if ( this.loading ) {
       
   592 				this.loading.destroy();
       
   593 				delete this.loading;
       
   594 			}
       
   595 		},
       
   596 
       
   597 		refresh: function() {
       
   598 			var self = this;
       
   599 
       
   600 			this.abort();
       
   601 
       
   602 			this.loading = new api.PreviewFrame({
       
   603 				url:        this.url(),
       
   604 				previewUrl: this.previewUrl(),
       
   605 				query:      this.query() || {},
       
   606 				container:  this.container,
       
   607 				signature:  this.signature
       
   608 			});
       
   609 
       
   610 			this.loading.done( function() {
       
   611 				// 'this' is the loading frame
       
   612 				this.bind( 'synced', function() {
       
   613 					if ( self.preview )
       
   614 						self.preview.destroy();
       
   615 					self.preview = this;
       
   616 					delete self.loading;
       
   617 
       
   618 					self.targetWindow( this.targetWindow() );
       
   619 					self.channel( this.channel() );
       
   620 
       
   621 					self.send( 'active' );
       
   622 				});
       
   623 
       
   624 				this.send( 'sync', {
       
   625 					scroll:   self.scroll,
       
   626 					settings: api.get()
       
   627 				});
       
   628 			});
       
   629 
       
   630 			this.loading.fail( function( reason, location ) {
       
   631 				if ( 'redirect' === reason && location )
       
   632 					self.previewUrl( location );
       
   633 
       
   634 				if ( 'logged out' === reason ) {
       
   635 					if ( self.preview ) {
       
   636 						self.preview.destroy();
       
   637 						delete self.preview;
       
   638 					}
       
   639 
       
   640 					self.login().done( self.refresh );
       
   641 				}
       
   642 
       
   643 				if ( 'cheatin' === reason )
       
   644 					self.cheatin();
       
   645 			});
       
   646 		},
       
   647 
       
   648 		login: function() {
       
   649 			var previewer = this,
       
   650 				deferred, messenger, iframe;
       
   651 
       
   652 			if ( this._login )
       
   653 				return this._login;
       
   654 
       
   655 			deferred = $.Deferred();
       
   656 			this._login = deferred.promise();
       
   657 
       
   658 			messenger = new api.Messenger({
       
   659 				channel: 'login',
       
   660 				url:     api.settings.url.login
       
   661 			});
       
   662 
       
   663 			iframe = $('<iframe src="' + api.settings.url.login + '" />').appendTo( this.container );
       
   664 
       
   665 			messenger.targetWindow( iframe[0].contentWindow );
       
   666 
       
   667 			messenger.bind( 'login', function() {
       
   668 				iframe.remove();
       
   669 				messenger.destroy();
       
   670 				delete previewer._login;
       
   671 				deferred.resolve();
       
   672 			});
       
   673 
       
   674 			return this._login;
       
   675 		},
       
   676 
       
   677 		cheatin: function() {
       
   678 			$( document.body ).empty().addClass('cheatin').append( '<p>' + api.l10n.cheatin + '</p>' );
       
   679 		}
       
   680 	});
       
   681 
       
   682 	/* =====================================================================
       
   683 	 * Ready.
       
   684 	 * ===================================================================== */
       
   685 
       
   686 	api.controlConstructor = {
       
   687 		color:  api.ColorControl,
       
   688 		upload: api.UploadControl,
       
   689 		image:  api.ImageControl
       
   690 	};
       
   691 
       
   692 	$( function() {
       
   693 		api.settings = window._wpCustomizeSettings;
       
   694 		api.l10n = window._wpCustomizeControlsL10n;
       
   695 
       
   696 		// Check if we can run the customizer.
       
   697 		if ( ! api.settings )
       
   698 			return;
       
   699 
       
   700 		// Redirect to the fallback preview if any incompatibilities are found.
       
   701 		if ( ! $.support.postMessage || ( ! $.support.cors && api.settings.isCrossDomain ) )
       
   702 			return window.location = api.settings.url.fallback;
       
   703 
       
   704 		var body = $( document.body ),
       
   705 			overlay = body.children('.wp-full-overlay'),
       
   706 			query, previewer, parent;
       
   707 
       
   708 		// Prevent the form from saving when enter is pressed.
       
   709 		$('#customize-controls').on( 'keydown', function( e ) {
       
   710 			if ( $( e.target ).is('textarea') )
       
   711 				return;
       
   712 
       
   713 			if ( 13 === e.which ) // Enter
       
   714 				e.preventDefault();
       
   715 		});
       
   716 
       
   717 		// Initialize Previewer
       
   718 		previewer = new api.Previewer({
       
   719 			container:   '#customize-preview',
       
   720 			form:        '#customize-controls',
       
   721 			previewUrl:  api.settings.url.preview,
       
   722 			allowedUrls: api.settings.url.allowed,
       
   723 			signature:   'WP_CUSTOMIZER_SIGNATURE'
       
   724 		}, {
       
   725 
       
   726 			nonce: api.settings.nonce,
       
   727 
       
   728 			query: function() {
       
   729 				return {
       
   730 					wp_customize: 'on',
       
   731 					theme:        api.settings.theme.stylesheet,
       
   732 					customized:   JSON.stringify( api.get() ),
       
   733 					nonce:        this.nonce.preview
       
   734 				};
       
   735 			},
       
   736 
       
   737 			save: function() {
       
   738 				var self  = this,
       
   739 					query = $.extend( this.query(), {
       
   740 						action: 'customize_save',
       
   741 						nonce:  this.nonce.save
       
   742 					}),
       
   743 					request = $.post( api.settings.url.ajax, query );
       
   744 
       
   745 				api.trigger( 'save', request );
       
   746 
       
   747 				body.addClass('saving');
       
   748 
       
   749 				request.always( function() {
       
   750 					body.removeClass('saving');
       
   751 				});
       
   752 
       
   753 				request.done( function( response ) {
       
   754 					// Check if the user is logged out.
       
   755 					if ( '0' === response ) {
       
   756 						self.preview.iframe.hide();
       
   757 						self.login().done( function() {
       
   758 							self.save();
       
   759 							self.preview.iframe.show();
       
   760 						});
       
   761 						return;
       
   762 					}
       
   763 
       
   764 					// Check for cheaters.
       
   765 					if ( '-1' === response ) {
       
   766 						self.cheatin();
       
   767 						return;
       
   768 					}
       
   769 
       
   770 					api.trigger( 'saved' );
       
   771 				});
       
   772 			}
       
   773 		});
       
   774 
       
   775 		// Refresh the nonces if the preview sends updated nonces over.
       
   776  		previewer.bind( 'nonce', function( nonce ) {
       
   777  			$.extend( this.nonce, nonce );
       
   778  		});
       
   779 
       
   780 		$.each( api.settings.settings, function( id, data ) {
       
   781 			api.create( id, id, data.value, {
       
   782 				transport: data.transport,
       
   783 				previewer: previewer
       
   784 			} );
       
   785 		});
       
   786 
       
   787 		$.each( api.settings.controls, function( id, data ) {
       
   788 			var constructor = api.controlConstructor[ data.type ] || api.Control,
       
   789 				control;
       
   790 
       
   791 			control = api.control.add( id, new constructor( id, {
       
   792 				params: data,
       
   793 				previewer: previewer
       
   794 			} ) );
       
   795 		});
       
   796 
       
   797 		// Check if preview url is valid and load the preview frame.
       
   798 		if ( previewer.previewUrl() )
       
   799 			previewer.refresh();
       
   800 		else
       
   801 			previewer.previewUrl( api.settings.url.home );
       
   802 
       
   803 		// Save and activated states
       
   804 		(function() {
       
   805 			var state = new api.Values(),
       
   806 				saved = state.create('saved'),
       
   807 				activated = state.create('activated');
       
   808 
       
   809 			state.bind( 'change', function() {
       
   810 				var save = $('#save'),
       
   811 					back = $('.back');
       
   812 
       
   813 				if ( ! activated() ) {
       
   814 					save.val( api.l10n.activate ).prop( 'disabled', false );
       
   815 					back.text( api.l10n.cancel );
       
   816 
       
   817 				} else if ( saved() ) {
       
   818 					save.val( api.l10n.saved ).prop( 'disabled', true );
       
   819 					back.text( api.l10n.close );
       
   820 
       
   821 				} else {
       
   822 					save.val( api.l10n.save ).prop( 'disabled', false );
       
   823 					back.text( api.l10n.cancel );
       
   824 				}
       
   825 			});
       
   826 
       
   827 			// Set default states.
       
   828 			saved( true );
       
   829 			activated( api.settings.theme.active );
       
   830 
       
   831 			api.bind( 'change', function() {
       
   832 				state('saved').set( false );
       
   833 			});
       
   834 
       
   835 			api.bind( 'saved', function() {
       
   836 				state('saved').set( true );
       
   837 				state('activated').set( true );
       
   838 			});
       
   839 
       
   840 			activated.bind( function( to ) {
       
   841 				if ( to )
       
   842 					api.trigger( 'activated' );
       
   843 			});
       
   844 
       
   845 			// Expose states to the API.
       
   846 			api.state = state;
       
   847 		}());
       
   848 
       
   849 		// Temporary accordion code.
       
   850 		$('.customize-section-title').bind('click keydown', function( event ) {
       
   851 
       
   852 			if ( event.type === 'keydown' &&  13 !== event.which ) // enter
       
   853 					return;
       
   854 
       
   855 			var clicked = $( this ).parents( '.customize-section' );
       
   856 
       
   857 			if ( clicked.hasClass('cannot-expand') )
       
   858 				return;
       
   859 
       
   860 			// Scroll up if on #customize-section-title_tagline
       
   861 			if ('customize-section-title_tagline' === clicked.attr('id'))
       
   862 				$('.wp-full-overlay-sidebar-content').scrollTop(0);
       
   863 
       
   864 			$( '.customize-section' ).not( clicked ).removeClass( 'open' );
       
   865 			clicked.toggleClass( 'open' );
       
   866 			event.preventDefault();
       
   867 		});
       
   868 
       
   869 		// Button bindings.
       
   870 		$('#save').click( function( event ) {
       
   871 			previewer.save();
       
   872 			event.preventDefault();
       
   873 		}).keydown( function( event ) {
       
   874 			if ( 9 === event.which ) // tab
       
   875 				return;
       
   876 			if ( 13 === event.which ) // enter
       
   877 				previewer.save();
       
   878 			event.preventDefault();
       
   879 		});
       
   880 
       
   881 		$('.back').keydown( function( event ) {
       
   882 			if ( 9 === event.which ) // tab
       
   883 				return;
       
   884 			if ( 13 === event.which ) // enter
       
   885 				parent.send( 'close' );
       
   886 			event.preventDefault();
       
   887 		});
       
   888 
       
   889 		$('.collapse-sidebar').on( 'click keydown', function( event ) {
       
   890 			if ( event.type === 'keydown' &&  13 !== event.which ) // enter
       
   891 				return;
       
   892 
       
   893 			overlay.toggleClass( 'collapsed' ).toggleClass( 'expanded' );
       
   894 			event.preventDefault();
       
   895 		});
       
   896 
       
   897 		// Create a potential postMessage connection with the parent frame.
       
   898 		parent = new api.Messenger({
       
   899 			url: api.settings.url.parent,
       
   900 			channel: 'loader'
       
   901 		});
       
   902 
       
   903 		// If we receive a 'back' event, we're inside an iframe.
       
   904 		// Send any clicks to the 'Return' link to the parent page.
       
   905 		parent.bind( 'back', function() {
       
   906 			$('.back').on( 'click.back', function( event ) {
       
   907 				event.preventDefault();
       
   908 				parent.send( 'close' );
       
   909 			});
       
   910 		});
       
   911 
       
   912 		// Pass events through to the parent.
       
   913 		api.bind( 'saved', function() {
       
   914 			parent.send( 'saved' );
       
   915 		});
       
   916 
       
   917 		// When activated, let the loader handle redirecting the page.
       
   918 		// If no loader exists, redirect the page ourselves (if a url exists).
       
   919 		api.bind( 'activated', function() {
       
   920 			if ( parent.targetWindow() )
       
   921 				parent.send( 'activated', api.settings.url.activated );
       
   922 			else if ( api.settings.url.activated )
       
   923 				window.location = api.settings.url.activated;
       
   924 		});
       
   925 
       
   926 		// Initialize the connection with the parent frame.
       
   927 		parent.send( 'ready' );
       
   928 
       
   929 		// Control visibility for default controls
       
   930 		$.each({
       
   931 			'background_image': {
       
   932 				controls: [ 'background_repeat', 'background_position_x', 'background_attachment' ],
       
   933 				callback: function( to ) { return !! to }
       
   934 			},
       
   935 			'show_on_front': {
       
   936 				controls: [ 'page_on_front', 'page_for_posts' ],
       
   937 				callback: function( to ) { return 'page' === to }
       
   938 			},
       
   939 			'header_textcolor': {
       
   940 				controls: [ 'header_textcolor' ],
       
   941 				callback: function( to ) { return 'blank' !== to }
       
   942 			}
       
   943 		}, function( settingId, o ) {
       
   944 			api( settingId, function( setting ) {
       
   945 				$.each( o.controls, function( i, controlId ) {
       
   946 					api.control( controlId, function( control ) {
       
   947 						var visibility = function( to ) {
       
   948 							control.container.toggle( o.callback( to ) );
       
   949 						};
       
   950 
       
   951 						visibility( setting.get() );
       
   952 						setting.bind( visibility );
       
   953 					});
       
   954 				});
       
   955 			});
       
   956 		});
       
   957 
       
   958 		// Juggle the two controls that use header_textcolor
       
   959 		api.control( 'display_header_text', function( control ) {
       
   960 			var last = '';
       
   961 
       
   962 			control.elements[0].unsync( api( 'header_textcolor' ) );
       
   963 
       
   964 			control.element = new api.Element( control.container.find('input') );
       
   965 			control.element.set( 'blank' !== control.setting() );
       
   966 
       
   967 			control.element.bind( function( to ) {
       
   968 				if ( ! to )
       
   969 					last = api( 'header_textcolor' ).get();
       
   970 
       
   971 				control.setting.set( to ? last : 'blank' );
       
   972 			});
       
   973 
       
   974 			control.setting.bind( function( to ) {
       
   975 				control.element.set( 'blank' !== to );
       
   976 			});
       
   977 		});
       
   978 
       
   979 		// Handle header image data
       
   980 		api.control( 'header_image', function( control ) {
       
   981 			control.setting.bind( function( to ) {
       
   982 				if ( to === control.params.removed )
       
   983 					control.settings.data.set( false );
       
   984 			});
       
   985 
       
   986 			control.library.on( 'click', 'a', function( event ) {
       
   987 				control.settings.data.set( $(this).data('customizeHeaderImageData') );
       
   988 			});
       
   989 
       
   990 			control.uploader.success = function( attachment ) {
       
   991 				var data;
       
   992 
       
   993 				api.ImageControl.prototype.success.call( control, attachment );
       
   994 
       
   995 				data = {
       
   996 					attachment_id: attachment.get('id'),
       
   997 					url:           attachment.get('url'),
       
   998 					thumbnail_url: attachment.get('url'),
       
   999 					height:        attachment.get('height'),
       
  1000 					width:         attachment.get('width')
       
  1001 				};
       
  1002 
       
  1003 				attachment.element.data( 'customizeHeaderImageData', data );
       
  1004 				control.settings.data.set( data );
       
  1005 			};
       
  1006 		});
       
  1007 
       
  1008 		api.trigger( 'ready' );
       
  1009 
       
  1010 		// Make sure left column gets focus
       
  1011 		var topFocus = $('.back');
       
  1012 		topFocus.focus();
       
  1013 		setTimeout(function () {
       
  1014 			topFocus.focus();
       
  1015 		}, 200);
       
  1016 
       
  1017 	});
       
  1018 
       
  1019 })( wp, jQuery );