/*!
 * Kopatheme uploader.js (http://kopatheme.com)
 * Copyright 2014 Kopasoft.
 * Licensed under GNU General Public License v3
 * 
 */
jQuery(document).ready(function() {

    var file_frame; 

    jQuery('.upload_image_button').on('click', function(evt) {
        evt.preventDefault();

        var clickedID = jQuery(this).attr('alt');   
        image_field = jQuery('#'+clickedID);

        if ( file_frame ) {
            file_frame.open();
            return;
        }

        // create new media
        file_frame = wp.media.frames.file_frame = wp.media({
            title: 'Upload',
            multiple: false
        });

        file_frame.on('select', function () {
            attachment = file_frame.state().get('selection').first().toJSON();
            image_field.val(attachment.url);
        });

        file_frame.open();
    });
});