var theme;
(function($){
	var api,s;

	// Initialize the shortcode/api object
	theme = api = {};
	
	api.init = function(options){
		s = api.settings = $.extend({}, api.settings, options);
		
		api.option.init();
	};
	
	// theme options init functions
	api.option = {};
	
	api.option.init = function(){
		api.option.select();
		api.option.multiDropdown();
		api.option.ddMultiSelect();
		api.option.superLink();
		api.option.uploader();
		api.option.range();
		api.option.measurement();
		api.option.validator();
		api.option.color();
		api.option.toggle();
		api.option.triToggle();
		api.option.switchDesc();
	};
	
	api.fontUpdate = function(){
		/*var files = new Array();*/
		jQuery("select.include").each(function(){
			var fontName = jQuery(this).attr("id");
			if(jQuery(this).html()!=''){
				/*files.push(jQuery("#font_item option:contains("+fontName+")").attr("id"));*/
				var option = new Array();
				jQuery(this).children("option").each(function(){
					option.push(jQuery(this).val());
				});
				jQuery("input[name='fonts[include]["+fontName+"]']").val(option.join(','));
			}else{
				jQuery("input[name='fonts[include]["+fontName+"]']").val('');
			}
		});
		var exclude = new Array();
		jQuery("#exclude option").each(function(){
			exclude.push(jQuery(this).val());
		});
		/*jQuery("#fontFile").val(files.join(','));*/
		jQuery("#fontExclude").val(exclude.join(','));

	},


		api.option.select = function(){
		var initChosen = function(element){
			var $placeholder = $(element).attr('data-placeholder');
			if($placeholder!=undefined){
				$(element).data("placeholder", $placeholder);
			}
			
			if($(element).data("order")==true){
				$(element).val('');
				$(element).chosen();
				var $ordered = $('[name="_'+$(element).attr('id')+'"]');
				var selected = $ordered.val().split(',');
				var chosen_object = $(element).data('chosen');
				$.each(selected, function(index, item){
					$.each(chosen_object.results_data, function(i, data){
						if(data.value == item){
							$("#"+data.dom_id).trigger('mouseup');
						}
					});
				});
				$(element).data("backupVal",$(element).val());
				$(element).change(function(){
					var backup = $(this).data("backupVal");
					var current = $(this).val();
					if(backup == null){
						backup = [];
					}
					if(current == null){
						current = [];
					}
					if(backup.length > current.length){							
						$.each(backup, function(index, item) { 
							if($.inArray(item, current) < 0){
								for(var i=0; i<selected.length; i++){
									if(selected[i] == item){
										selected.splice(i,1);
									}
								}
							}
						});
					}else if(backup.length < current.length){
						$.each(current, function(index, item) { 
							if($.inArray(item, backup) < 0){
								selected.push(item);
							}
						});
					}
					$ordered.val(selected.join(','));
					$(this).data("backupVal",current);
				});
			}else{
				$(element).chosen();
			}
		}
		$("select.chosen").each(function(){
			if($(this).parents('.postbox').is('.closed')){
				var chosen = $(this);
					
				chosen.parents('.postbox').children('.hndle,.handlediv').bind('clickoutside',function(e){
					initChosen(chosen);
				});
			}else{
				initChosen(this);
			}
		});
		
	};
	api.option.multiDropdown = function(){
		var wrap = $(".multidropdown-wrap");

		wrap.each(function() {
			var selects = $(this).children('select');
			var field = $(this).siblings('input:hidden');
			field.val("");
			var name = field.attr("name");
			selects.each(function(i) {
				if ($(this).val()) {
					if (field.val()) {
						field.val(field.val() + ',' + $(this).val());
					} else {
						field.val($(this).val());
					}
				}
				$(this).attr('id', name + '_' + i);
				$(this).attr('name', name + '_' + i);

				$(this).unbind('change').bind('change',function() {
					if ($(this).val() && selects.length == i + 1) {
						$(this).clone().val("").appendTo(wrap);
					} else if (!($(this).val())
							&& !(selects.length == i + 1)) {
						$(this).remove();
					}
					api.option.multiDropdown();
				});
			})
		});
	};
	
	api.option.superLink = function() {
		var wrap = $(".superlink-wrap");
		wrap.each(function(){
			var field = $(this).siblings('input:hidden');
			var selector = $(this).siblings('select');
			var name = field.attr('name');
			var items = $(this).children();
			selector.change(function(){
				items.hide();
				$("#"+name+"_"+$(this).val()).show();
				field.val('');
			});
			items.change(function(){
				field.val(selector.val()+'||'+$(this).val());
			})
		});
	};
	
	api.option.uploader = function(){
		if($('.theme-add-media-button').length > 0){
			// thank to http://mikejolley.com/2012/12/using-the-new-wordpress-3-5-media-uploader-in-plugins/
			var file_frame;

			jQuery('.theme-add-media-button').on('click', function( event ){
				var button = $(this);
    			var target = button.data('target');

    			button.blur();
				event.preventDefault();

				// If the media frame already exists, reopen it.
				if ( file_frame ) {
				  file_frame.open();
				  return;
				}

				// Create the media frame.
				file_frame = wp.media.frames.file_frame = wp.media({
				  title: jQuery( this ).data( 'uploader_title' ),
				  button: {
				    text: jQuery( this ).data( 'uploader_button_text' ),
				  },
				  multiple: false  // Set to true to allow multiple files to be selected
				});

				file_frame.on( 'select', function() {
					attachment = file_frame.state().get('selection').first().toJSON();
					var imagewidth = $('#'+target+'_preview').attr('data-imagewidth');
					if($("#"+target).size()>0){
						$("#"+target).val('{"method":"form","media":"'+attachment.id+'","type":"image","title":"'+attachment.title+'"}');
						$("#"+target+"_preview").html('<a class="thickbox" href="'+attachment.url+'?"><img style="max-width:'+imagewidth+'px" src="'+attachment.url+'"/></a>');
					}
				});

				file_frame.open();
			});
		}
		
	
		$(".theme-upload-remove").click(function(){
			$content = $(this).parent().parent();
			$content.find('.upload-value').val('');
			$content.find('.theme-option-image-preview').html('');
		});
	};
	api.option.uploader.getMedia = function(media,title,type,method,target){
		imagewidth = $('#'+target+'_preview').attr('data-imagewidth');
		imageheight = $('#'+target+'_preview').attr('data-imageheight');
		$.post(ajaxurl, {
			action:'theme-option-get-media',
			media: media,
			title:title,
			type:type,
			method:method.toString(),
			imagewidth:imagewidth,
			imageheight:imageheight,
			cookie: encodeURIComponent(document.cookie)
		}, function(html){
			if ( html == '0' ) {
				alert( 'Could not use this Media. Try a different attachment.' );
			} else {
				if($("#"+target).size()>0){
					$("#"+target).val('{"media":"'+media+'","method":"'+method+'","type":"'+type+'","title":"'+title+'"}');
					$("#"+target+"_preview").html(html);
				}else{
					$(".shortcode_generator_iframe")[0].contentWindow.shortcode.getUploadedImage(target,media,type,method);
				}
			}
		});
		
	};
	
	api.option.range = function(){
		$(".range-input-wrap :range").rangeinput();
	};
	api.option.measurement = function(){
		var wrap = $(".measurement-wrap");
		wrap.each(function(){
			var field = $(this).find('input:hidden');
			var range = $(this).find('input:range');
			var unit = $(this).find('select');
			var name = field.attr('name');
			var items = $(this).children();
			range.change(function(){
				if($(this).val() != 0){
					field.val($(this).val()+unit.val());
				}else{
					field.val('');
				}
			});
			unit.change(function(){
				if(range.val() != 0){
					field.val(range.val()+$(this).val());
				}else{
					field.val('');
				}
			})
		});
	};
	
	
	api.option.validator = function(){
		$.tools.validator.addEffect("option", function(errors, event) {
			// add new ones
			$.each(errors, function(index, error) {
				var input = error.input;
				input.addClass("invalid");
				var msg = input.next('.validator-error').empty();
				$.each(error.messages, function(i, m) {
					$("<span/>").html(m).appendTo(msg);			
				});
			});
			
		// the effect does nothing when all inputs are valid	
		}, function(inputs)  {
			inputs.removeClass("invalid").each(function() {
				$(this).next('.validator-error').empty();
			});
		});
		$(".validator-wrap :input").validator({effect:'option'});
	};
	
	api.option.color = function(){
		$.fn.mColorPicker.init.showLogo = false;
		$.fn.mColorPicker.defaults.imageFolder = theme_admin_assets_uri + "/images/mColorPicker/";
	};
	
	api.option.toggle = function(){
		$('.toggle-button:checkbox').each(function(){
			if($(this).parents('.postbox').is('.closed')){
				var button = $(this);
					
				button.parents('.postbox').children('.hndle,.handlediv').bind('clickoutside',function(e){
					button.iphoneStyle();
				});
			}else{
				$(this).iphoneStyle();
			}
		});
	};
	
	api.option.triToggle = function(){
		$('select.tri-toggle-button').each(function(){
			if($(this).parents('.postbox').is('.closed')){
				var button = $(this);
				button.parents('.postbox').children('.hndle,.handlediv').bind('clickoutside',function(e){
					button.iphoneStyleTriToggle();
				});
			}else{
				$(this).iphoneStyleTriToggle();
			}
		});
	};
	
	api.option.switchDesc = function(){
		$('.meta-box-item a.switch').click(function(event){
			$(this).parent().siblings('.description').toggle();
			event.preventDefault();
		});
	};
	api.option.ddMultiSelect = function(){
		var wrap = $(".ddmultiselect-wrap");

		wrap.each(function() {
			var field = $(this).siblings('input:hidden');
			var enabled = $(this).find('.ddmultiselect-enabled-holder ul');
			var disabled = $(this).find('.ddmultiselect-disabled-holder ul');
			enabled.sortable({
				connectWith: disabled,
				dropOnEmpty: true,
				placeholder: 'ddmultiselect-placeholder',
				items: 'li',
				update: function(event, ui){
					var $values = jQuery.map(enabled.find('li'),function(item){
						return $(item).data('value');
					});
					field.val($values.join(','));
					console.info($values);
				}
			}).disableSelection();
			disabled.sortable({
				connectWith: enabled,
				dropOnEmpty: true,
				placeholder: 'ddmultiselect-placeholder',
				items: 'li'
			}).disableSelection();
		});
	};
	api.option.getVal = function(name){
		var target = $('[name="'+name+'"]');
		if(target.size() == 0){
			target = $('[name="'+name+'[]"]');
		}
		if(target.is('.toggle-button')){
			if(target.is(':checked')){
				return true;
			}else{
				return false;
			}
		}
		if(target.is('.tri-toggle-button')){
			switch(target.val()){
				case 'true':
					return true;
				case 'false':
					return false;
				case 'default':
					return '';
			}
		}
		return target.val();
	};
})(jQuery);

