/** Handle Post Formats Custom Meta boxes **/
function postformat_meta() {
	//show metat box based on selection of post format
	var postformat_type_grp = jQuery('#post-formats-select input');

	jQuery('#post-formats-select input').change(function () {
		//hide all the post format meta boxes
		if(jQuery(this).is(':checked') === true) { //alert(jQuery(this).val());
			jQuery('div[class*="postformatmetabox-"]').hide();
			jQuery('.postformatmetabox-'+jQuery(this).val()).show();
		}
	}).change();
}
styleSelect = {
	init: function () {
		jQuery('.select_wrapper').each(function () {
			jQuery(this).prepend('<span>' + jQuery(this).find('.select option:selected').text() + '</span>');
		});
		jQuery('.select').live('change', function () {
			jQuery(this).prev('span').replaceWith('<span>' + jQuery(this).find('option:selected').text() + '</span>');
		});
		jQuery('.select').bind(jQuery.browser.msie ? 'click' : 'change', function(event) {
			jQuery(this).prev('span').replaceWith('<span>' + jQuery(this).find('option:selected').text() + '</span>');
		});
	}
};
(function($){
	    "use strict";
		jQuery(document).ready(function() {
			jQuery('.iva-radio-option').click(function(){
			var iva_option_id = jQuery(this).attr('data-id');
			jQuery( '#'+iva_option_id ).attr('checked', 'checked');
			jQuery(this).parent().parent().find('.iva-radio-option').removeClass('iva-radio-option-selected');
			jQuery(this).addClass('iva-radio-option-selected');
		});

		jQuery('.iva-radio-option').show();
		jQuery('.iva-radio-img-label').hide();
		jQuery('.iva-radio-img-radio').hide();

		postformat_meta();
		styleSelect.init();

		/*-- postlinkurl selection--*/
		jQuery("#storeup_postlinktype_options").change(function () {
			jQuery(".linkoption").hide();
			var selected_ptoption = jQuery("#storeup_postlinktype_options option:selected").val();
			jQuery("."+selected_ptoption).show();
		}).change();

		/*-- custom Testimonial uploadimage/gravatar selection--*/
		jQuery("#testimonial_image_option").change(function () {
			jQuery(".testimonialoption").hide();
			var testimonialoption = jQuery("#testimonial_image_option option:selected").val();
			jQuery("."+testimonialoption).show();
		}).change();

		/*-- custom Logo option selection--*/
		jQuery("#storeup_logo").change(function () {
			jQuery(".title").slideUp();
			jQuery(".logo").slideUp();
			var selected_teaser = jQuery("#storeup_logo option:selected").val();
			jQuery("."+selected_teaser).slideDown();
		}).change();

		/*-- custom teaser option selection--*/
		jQuery("#storeup_subheader_teaser_options").change(function () {
			jQuery(".sub_teaser_option").slideUp();
			var subheader_teaser_select = jQuery("#storeup_subheader_teaser_options option:selected").val();
			jQuery("."+subheader_teaser_select).slideDown();
		}).change();

		/*-- Page Layout Meta --*/
		jQuery("#storeup_page_layout").change(function(){
			if(jQuery('#storeup_page_layout').is(':checked') != true) {
				jQuery(".storeup_page_bg").slideUp();
			}else{
				jQuery(".storeup_page_bg").slideDown();
			}
		}).change();

		/*-- Slider selection--*/
		jQuery("#storeup_page_slider").change(function () {
		jQuery(".page_slider").hide();
			var chooseslider = jQuery("#storeup_page_slider option:selected").val();
			if ( chooseslider != "" ){
				jQuery("."+chooseslider).show();
			}
		}).change();

		/*-- Custom Slider Selection--*/
		jQuery("#storeup_slider").change(function () {
			jQuery(".iva_of_sliders").hide();
			jQuery(".subtoggle").hide();
			var selected_slider = jQuery("#storeup_slider option:selected").val();
			if(selected_slider != "") {
				jQuery("."+selected_slider).show();
			}
		}).change();

		//Header
		jQuery("#storeup_headerstyle").change(function () {
			jQuery(".iva_of_headerstyle").hide();
			var selected_headerstyle = jQuery("#storeup_headerstyle option:selected").val();
			if( selected_headerstyle !== '' ) {
				jQuery("."+selected_headerstyle).show();
			}
		}).change();

		jQuery('#media-items').bind('DOMNodeInserted',function(){
			jQuery('input[value="Insert into Post"]').each(function(){
				jQuery(this).attr('value','Use This Image');
			});
		});

		/*-- Upload Image Remove-*/
		 jQuery('.cimage_remove').live('click', function(event) {
		   var defaultImage = jQuery(this).parent().siblings('.custom_default_image').text();
		   jQuery(this).parent().siblings('.custom_upload_image').val('');
		   jQuery(this).parent('.iva-screenshot').slideUp();
		   return false;
		});

		var custom_uploader,clickedID ,formfield = '';
		jQuery('.custom_upload_image_button').click(function() {
			clickedID = jQuery(this).attr('id');
			formfield = jQuery(this).prev( 'input').attr( 'id' );
			if (custom_uploader) {
				custom_uploader.open();
				return;
			}
			//Extend the wp.media object
			custom_uploader = wp.media.frames.file_frame = wp.media({
				title: 'Choose This',
				button: {
					text: 'Choose This'
				},
				multiple: false
			});
			custom_uploader.on('select', function() {
				var attachment = custom_uploader.state().get('selection').first().toJSON();
				var image = /(^.*\.jpg|jpeg|png|gif*)/gi;
				if(typeof(attachment.sizes.thumbnail)  === "undefined") {
					if (attachment.url.match(image)) {
					  var  btnContent = '<img src="'+attachment.url+'" alt="" />';
					  btnContent += '<a href="javascript:(void);" class="cimage_remove button button-primary">x</a>';
					}
				}else if (attachment.sizes.thumbnail.url.match(image)) {
					var  btnContent = '<img src="'+attachment.sizes.thumbnail.url+'" alt="" />';
					btnContent += '<a href="javascript:(void);" class="cimage_remove button button-primary">x</a>'
				}

				jQuery( '#' + formfield).val(attachment.url);
				jQuery( '#' + formfield).siblings( '#iva_imagepreview-'+clickedID).slideDown().html(btnContent);
			});
			//Open the uploader dialog
			custom_uploader.open();
			return false;

		});

		jQuery(window).load(function() {
    		jQuery('.page_load').fadeOut(800);
		});

		jQuery('.repeatable-add').click(function() {
			field = jQuery(this).closest('td').find('.custom_repeatable li:last').clone(true);
			fieldLocation = jQuery(this).closest('td').find('.custom_repeatable li:last');
			jQuery('input', field).val('').attr('name', function(index, name) {
				return name.replace(/(\d+)/, function(fullMatch, n) {
					return Number(n) + 1;
				});
			})
			field.insertAfter(fieldLocation, jQuery(this).closest('td'))
			return false;
		});

		jQuery('.repeatable-remove').click(function(){
			jQuery(this).parent().remove();
			return false;
		});
		// Color Picker
		jQuery('.wpcolorSelector').each(function(){
			var Othis = this; //cache a copy of the this variable for use inside nested function
			var initialColor = jQuery(Othis).prev('input').attr('value');
			var initialColorid = jQuery(Othis).prev('input').attr('id');
			jQuery(Othis).children('div').css('backgroundColor', initialColor);
			jQuery('#' + initialColorid).wpColorPicker({
				color: initialColor,
				onShow: function (colpkr) {
					jQuery(colpkr).fadeIn(500);
					return false;
				},
				onHide: function (colpkr) {
					jQuery(colpkr).fadeOut(500);
					return false;
				},
				onChange: function (hsb, hex, rgb) {
					jQuery(Othis).children('div').css('backgroundColor', '#' + hex);
					jQuery(Othis).prev('input').attr('value','#' + hex);
				}
			});
		});
		//end color picker

		function GoogleFontSelect( slctr, mainID ){

			var _selected = $(slctr).val(); 						//get current value - selected and saved
			var _linkclass = 'style_link_'+ mainID;
			var _previewer = mainID +'_ggf_previewer';

			if( _selected ){ //if var exists and isset

				$('.'+ _previewer ).fadeIn();

				//Check if selected is not equal with "Select a font" and execute the script.
				if ( _selected !== ' ' ) {

					//remove other elements crested in <head>
					$( '.'+ _linkclass ).remove();
					var arr = [ "Arial", "Verdana", "Tahoma", "Sans-serif", "Lucida Grande" , "Georgia,serif", "Trebuchet MS, Tahoma, sans-serif", "Times New Roman, Geneva, sans-serif", "Palatino,Palatino Linotyp,serif","Helvetica Neue, Helvetica, sans-serif" ];
					if ($.inArray(_selected, arr) !== -1){

					}else{
						//replace spaces with "+" sign
						var the_font = _selected.replace(/\s+/g, '+');

						//add reference to google font family
						$('head').append('<link href="http://fonts.googleapis.com/css?family='+ the_font +'" rel="stylesheet" type="text/css" class="'+ _linkclass +'">');
					}
					//show in the preview box the font
					$('.'+ _previewer ).css('font-family', _selected +', sans-serif' );
				}else{
					//if selected is not a font remove style "font-family" at preview box
					$('.'+ _previewer ).css('font-family', '' );
					$('.'+ _previewer ).fadeOut();
				}
			}
		}

		//init for each element
		jQuery( '.google_font_select' ).each(function(){
			var mainID = jQuery(this).attr('id');
			GoogleFontSelect( this, mainID );
		});

		//init when value is changed
		jQuery( '.google_font_select' ).change(function(){
			var mainID = jQuery(this).attr('id');
			GoogleFontSelect( this, mainID );
		});

	jQuery('.iva_select_all').toggle( function(){
		  var iva_attr_id = jQuery(this).attr( 'data-id' );
		  jQuery('.' +iva_attr_id+ ' option').prop( 'selected',true );
		},function(){
			var iva_attr_id = jQuery( this ).attr( 'data-id' );
		    jQuery('.' +iva_attr_id+ ' option' ).prop( 'selected',false);
	});
});

})(jQuery);

	// rgba color picker
	jQuery(document).ready(function($) {

	Color.prototype.toString = function(remove_alpha) {
		if (remove_alpha == 'no-alpha') {
			return this.toCSS('rgba', '1').replace(/\s+/g, '');
		}
		if (this._alpha < 1) {
			return this.toCSS('rgba', this._alpha).replace(/\s+/g, '');
		}
		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;
	};

	  $('.iva-color-control').each(function() {

		var $control = $(this),
			value = $control.val().replace(/\s+/g, '');
		// Manage Palettes
		var palette_input = $control.attr('data-palette');
		if (palette_input == 'false' || palette_input == false) {
			var palette = false;
		} else if (palette_input == 'true' || palette_input == true) {
			var palette = true;
		} else {
			var palette = $control.attr('data-palette').split(",");
		}
		$control.wpColorPicker({ // change some things with the color picker
			 clear: function(event, ui) {
			// TODO reset Alpha Slider to 100
			 },
			change: function(event, ui) {

				// send ajax request to wp.customizer to enable Save & Publish button
				var _new_value = $control.val();

				// change the background color of our transparency container whenever a color is updated
				var $transparency = $control.parents('.wp-picker-container:first').find('.transparency');

				// we only want to show the color at 100% alpha
				$transparency.css('backgroundColor', ui.color.toString('no-alpha'));
			},
			palettes: palette // remove the color palettes
		});
		$('<div class="iva-alpha-container"><div class="slider-alpha"></div><div class="transparency"></div></div>').appendTo($control.parents('.wp-picker-container'));
		var $alpha_slider = $control.parents('.wp-picker-container:first').find('.slider-alpha');

		// if in format RGBA - grab A channel value
		if (value.match(/rgba\(\d+\,\d+\,\d+\,([^\)]+)\)/)) {
			var alpha_val = parseFloat(value.match(/rgba\(\d+\,\d+\,\d+\,([^\)]+)\)/)[1]) * 100;
			var alpha_val = parseInt(alpha_val);
		} else {
			var alpha_val = 100;
		}

		$alpha_slider.slider({
			slide: function(event, ui) {
				$(this).find('.ui-slider-handle').text(ui.value); // show value on slider handle
				// send ajax request to wp.customizer to enable Save & Publish button
				var _new_value = $control.val();
			},
			create: function(event, ui) {
				var v = $(this).slider('value');
				$(this).find('.ui-slider-handle').text(v);
			},
			value: alpha_val,
			range: "max",
			step: 1,
			min: 1,
			max: 100
		}); // slider

		$alpha_slider.slider().on('slidechange', function(event, ui) {
			var new_alpha_val = parseFloat(ui.value),
				iris = $control.data('a8cIris'),
				color_picker = $control.data('wpWpColorPicker');
				iris._color._alpha = new_alpha_val / 100.0;
				$control.val(iris._color.toString());
				color_picker.toggler.css({
				backgroundColor: $control.val()
			});
			// fix relationship between alpha slider and the 'side slider not updating.
			var get_val = $control.val();
			$($control).wpColorPicker('color', get_val);
		});
	}); // each

	// Icon Box Widget
	jQuery('span.font-icon-name').click( function(){
		var icon_name =jQuery(this).attr('data-icon');
		jQuery('.icon-input').val(icon_name);
	});

	// Add Units in Meta Fields
	jQuery("#add_units_pricing").click(function($) {
		if ( jQuery('.at_units_pricing_count at_units_table tr').length>1 ) {
			jQuery('.at_units_pricing_count table tbody>tr:first').clone().insertAfter('.at_units_pricing_count table tbody>tr:last');
		} else {
			jQuery('.at_units_pricing_count table tbody>tr:first').clone().insertAfter('.at_units_pricing_count table tbody>tr:last');
		}

		jQuery('.at_units_pricing_count table tbody>tr:last').find('.at-height').val('');
		jQuery('.at_units_pricing_count table tbody>tr:last').find('.at-length').val('');
		jQuery('.at_units_pricing_count table tbody>tr:last').find('.at-features').val('');
		jQuery('.at_units_pricing_count table tbody>tr:last').find('.at-rate').val('');
		jQuery('.at_units_pricing_count table tbody>tr:last').find('.at-ofr-rate').val('');

		return false;
	});
	jQuery(".at_units_pricing_wrap").on('click', '.button-primary', function(){
		addrow = jQuery(this).parent().parent();
		if(jQuery('.at_units_pricing_count table tbody>tr').length>1){
			addrow.remove();
		}else{
			alert('Insert at-least One Field');
		}
		return false;
	});
	// drag drop
	jQuery('table#at_units_table tbody').sortable({
		  //helper: fixHelper
	});
});
