var $ = jQuery.noConflict();
$(document).ready(function() {

    $( "#tn-review-metabox" ).hide();
	$( "#trendingnews-video-link" ).hide();
	
	
    if( $( "input#post-format-aside" ).is(':checked') ){
        $( "#tn-review-metabox" ).show();
    } else 
	if( $( "input#post-format-video" ).is(':checked') ){
		$( "#trendingnews-video-link" ).show();
	}
	
	
	    // If post format is selected
	
	
    $( "input#post-format-aside" ).click( function() {
        if( $( "input#post-format-aside" ).is(':checked') ){
            $( "#tn-review-metabox" ).show(500);
			$( "#trendingnews-video-link" ).hide();
        }});

    $( "input#post-format-video" ).click( function() {
        if( $( "input#post-format-video" ).is(':checked') ){
			$( "#trendingnews-video-link" ).show(500);
            $( "#tn-review-metabox" ).hide();
        }});		

    $( "input#post-format-0" ).click( function() {
        if( $( "input#post-format-0" ).is(':checked') ){
			$( "#trendingnews-video-link" ).hide();
            $( "#tn-review-metabox" ).hide();
        }});			


	/**
	 * Repeatable field
	 */
	$('#add-row').on('click', function(e) {
		e.preventDefault();
		var row = $('.empty-row.screen-reader-text.scores').clone(true);
		row.removeClass('empty-row screen-reader-text scores');
		row.insertBefore('#tn-review-item tbody>tr:last');
		row.find("[name='tn_review_item_score[]']").addClass('review-score');
		$.review_total();
	});
	
	$('#add-row-good').on('click', function(e) {
		e.preventDefault();
		var row = $('.empty-row.screen-reader-text.good').clone(true);
		row.removeClass('empty-row screen-reader-text good');
		row.insertBefore('#tn-review-good tbody>tr:last');
	});

	$('#add-row-bad').on('click', function(e) {
		e.preventDefault();
		var row = $('.empty-row.screen-reader-text.bad').clone(true);
		row.removeClass('empty-row screen-reader-text bad');
		row.insertBefore('#tn-review-bad tbody>tr:last');
	});




	$('.remove-row').on('click', function(e) {
		e.preventDefault();
		$(this).parents('tr').remove();
		$.review_total();
	});

	/**
	 * Review total
	 */
	$.extend({
		
		review_total: function(){
			$('.review-score').on( 'change', function () {
				
				var sum   = 0,
					value = 0,
					input = $('.review-score').length;
					
				$('.review-score').each(function () {
					value = Number($(this).val());
					if (!isNaN(value)) sum += value / input;
				});
				
				$('.tn-review-total').val( Math.round(sum * 10) / 10 );
				
				$.validate_review_value();
				
			});
			
		},
			
	});
	
	$.review_total();
	
	
	
	

	var custom_uploader;
 
 
    $('#image_button').click(function(e) {
 
        e.preventDefault();
 
        //If the uploader object has already been created, reopen the dialog
        if (custom_uploader) {
            custom_uploader.open();
            return;
        }
 
        //Extend the wp.media object
        custom_uploader = wp.media.frames.file_frame = wp.media({
            title: 'Choose Image',
            button: {
                text: 'Choose Image'
            },
            multiple: false
        });
 
        //When a file is selected, grab the URL and set it as the text field's value
        custom_uploader.on('select', function() {
            attachment = custom_uploader.state().get('selection').first().toJSON();
            $('#tn_review_image').val(attachment.url);
			$(".tn-image-preview").attr('src',attachment.url);
			
        });
        custom_uploader.open();
 
    });
});