/***************************************************************************
 *
 * 	----------------------------------------------------------------------
 * 						DO NOT EDIT THIS FILE
 *	----------------------------------------------------------------------
 *
 *  				     Copyright (C) Themify
 *
 *	----------------------------------------------------------------------
 *
 ***************************************************************************/

/**
 * Common shared code between backend and frontend Builder.
 */

;var ThemifyBuilderCommon;
(function($, window, document, undefined) {

    'use strict';

    ThemifyBuilderCommon = {

        /**
         * Function that detects whether localStorage is both supported and available.
         *
         * From MDN.
         *
         * @param type Type of the tested storage. E.g 'localStorage'.
         * @returns {boolean} True on browser having both support and availability, otherwise false.
         */
        storageAvailable: function(type) {
            try {
                var storage = window[type],
                    x = '__storage_test__';
                storage.setItem(x, x);
                storage.removeItem(x);
                return true;
            }
            catch(e) {
                return false;
            }
        },

        /**
         * Clipboard-like functionality. Wraps localStorage with a Themify key. Supports one copied item per time.
         */
        Clipboard: {
            key : 'themify_builder_clipboard_',
            set : function( type, content ) {
                if ( ThemifyBuilderCommon.storageAvailable('localStorage') ) {
                    localStorage.setItem( this.key + 'type', type );
                    localStorage.setItem( this.key + 'content', content );
                } else {
                    alert( themifyBuilderCommon.text_no_localStorage );
                }
            },
            get : function( type ) {
                if ( ThemifyBuilderCommon.storageAvailable('localStorage') ) {
                    var savedType =  localStorage.getItem( this.key + 'type' ),
                        savedContent = localStorage.getItem( this.key + 'content' );

                    if ( typeof type === undefined || type === savedType ) {
                        return savedContent;
                    } else {
                        return false;
                    }
                } else {
                    alert( themifyBuilderCommon.text_no_localStorage );
                }
            }
        },

        confirmDataPaste: function() {
            return confirm(themifyBuilderCommon.text_confirm_data_paste);
        },

        alertWrongPaste: function() {
            alert(themifyBuilderCommon.text_alert_wrong_paste);
        },

        detectBuilderComponent: function($component) {
            if (!$component.attr('data-component')) {
                return false;
            }

            return $component.data('component');
        }
    };

}(jQuery, window, document));
