function js_scripts_setup()
    {
        //TODO: move javascript to external resource (use wp_localize_script() to pass arguments)
        global $pagenow, $wpdb;
        $default_language = $this->get_default_language();
        $current_language = $this->get_current_language();
        if (isset($_GET['page'])) {
            $page = basename($_GET['page']);
            $page_basename = str_replace('.php', '', $page);
        } else {
            $page_basename = false;
        }
        $icl_ajax_url_root = rtrim(get_site_url(), '/');
        if (defined('FORCE_SSL_ADMIN') && FORCE_SSL_ADMIN) {
            $icl_ajax_url_root = str_replace('http://', 'https://', $icl_ajax_url_root);
        }
        $icl_ajax_url = $icl_ajax_url_root . '/wp-admin/admin.php?page=' . ICL_PLUGIN_FOLDER . '/menu/languages.php';
        ?>
		<script type="text/javascript">
			// <![CDATA[
			var icl_ajx_url;
			icl_ajx_url = '<?php 
        echo $icl_ajax_url;
        ?>
';
			var icl_ajx_saved = '<?php 
        echo icl_js_escape(__('Data saved', 'sitepress'));
        ?>
';
			var icl_ajx_error = '<?php 
        echo icl_js_escape(__('Error: data not saved', 'sitepress'));
        ?>
';
			var icl_default_mark = '<?php 
        echo icl_js_escape(__('default', 'sitepress'));
        ?>
';
			var icl_this_lang = '<?php 
        echo $this->this_lang;
        ?>
';
			var icl_ajxloaderimg_src = '<?php 
        echo ICL_PLUGIN_URL;
        ?>
/res/img/ajax-loader.gif';
			var icl_cat_adder_msg = '<?php 
        echo icl_js_escape(sprintf(__('To add categories that already exist in other languages go to the <a%s>category management page</a>', 'sitepress'), ' href="' . admin_url('edit-tags.php?taxonomy=category') . '"'));
        ?>
';
			// ]]>

			<?php 
        if (!$this->get_setting('ajx_health_checked')) {
            ?>
			addLoadEvent(function () {
				jQuery.ajax({type: "POST", url: icl_ajx_url, data: "icl_ajx_action=health_check", error: function (msg) {
					var icl_initial_language = jQuery('#icl_initial_language');
					if (icl_initial_language.length) {
						icl_initial_language.find('input').attr('disabled', 'disabled');
					}
					jQuery('.wrap').prepend('<div class="error"><p><?php 
            echo icl_js_escape(sprintf(__("WPML can't run normally. There is an installation or server configuration problem. %sShow details%s", 'sitepress'), '<a href="#" onclick="jQuery(this).parent().next().slideToggle()">', '</a>'));
            ?>
</p><p style="display:none"><?php 
            echo icl_js_escape(__('AJAX Error:', 'sitepress'));
            ?>
 ' + msg.statusText + ' [' + msg.status + ']<br />URL:' + icl_ajx_url + '</p></div>');
				}});
			});
			<?php 
        }
        ?>
		</script>
		<?php 
        if ('options-reading.php' == $pagenow) {
            list($warn_home, $warn_posts) = $this->verify_home_and_blog_pages_translations();
            if ($warn_home || $warn_posts) {
                ?>
				<script type="text/javascript">
					addLoadEvent(function () {
						jQuery('input[name="show_on_front"]').parent().parent().parent().parent().append('<?php 
                echo str_replace("'", "\\'", $warn_home . $warn_posts);
                ?>
');
					});
				</script>
			<?php 
            }
        }
        // display correct links on the posts by status break down
        // also fix links to category and tag pages
        if (('edit.php' == $pagenow || 'edit-pages.php' == $pagenow || 'categories.php' == $pagenow || 'edit-tags.php' == $pagenow) && $current_language != $default_language) {
            ?>
			<script type="text/javascript">
				addLoadEvent(function () {
					jQuery('.subsubsub li a').each(function () {
						var h = jQuery(this).attr('href');
						var urlg;
						if (-1 == h.indexOf('?')) urlg = '?'; else urlg = '&';
						jQuery(this).attr('href', h + urlg + 'lang=<?php 
            echo $current_language;
            ?>
');
					});
					jQuery('.column-categories a, .column-tags a, .column-posts a').each(function () {
						jQuery(this).attr('href', jQuery(this).attr('href') + '&lang=<?php 
            echo $current_language;
            ?>
');
					});
				});
			</script>
		<?php 
        }
        if ('edit-tags.php' == $pagenow) {
            ?>
			<script type="text/javascript">
				addLoadEvent(function () {
					var edit_tag = jQuery('#edittag');
					if (edit_tag.find('[name="_wp_original_http_referer"]').length && edit_tag.find('[name="_wp_http_referer"]').length) {
						edit_tag.find('[name="_wp_original_http_referer"]').val('<?php 
            $post_type = isset($_GET['post_type']) ? '&post_type=' . esc_html($_GET['post_type']) : '';
            echo admin_url('edit-tags.php?taxonomy=' . esc_js($_GET['taxonomy']) . '&lang=' . $current_language . '&message=3' . $post_type);
            ?>
');
					}
				});
			</script>
		<?php 
        }
        if ('post-new.php' == $pagenow) {
            if (isset($_GET['trid'])) {
                $translations = $wpdb->get_col($wpdb->prepare("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid=%d", $_GET['trid']));
                remove_filter('option_sticky_posts', array($this, 'option_sticky_posts'));
                // remove filter used to get language relevant stickies. get them all
                $sticky_posts = get_option('sticky_posts');
                add_filter('option_sticky_posts', array($this, 'option_sticky_posts'));
                // add filter back
                $is_sticky = false;
                foreach ($translations as $t) {
                    if (in_array($t, $sticky_posts)) {
                        $is_sticky = true;
                        break;
                    }
                }
                if (isset($_GET['trid']) && ($this->settings['sync_ping_status'] || $this->settings['sync_comment_status'])) {
                    $res = $wpdb->get_row($wpdb->prepare("SELECT comment_status, ping_status FROM {$wpdb->prefix}icl_translations t\r\n\t\t\t\t\tJOIN {$wpdb->posts} p ON t.element_id = p.ID WHERE t.trid=%d", $_GET['trid']));
                    ?>
					<script type="text/javascript">addLoadEvent(function () {
							var comment_status = jQuery('#comment_status');
							var ping_status = jQuery('#ping_status');
							<?php 
                    if ($this->settings['sync_comment_status']) {
                        ?>
							<?php 
                        if ($res->comment_status == 'open') {
                            ?>
							comment_status.attr('checked', 'checked');
							<?php 
                        } else {
                            ?>
							comment_status.removeAttr('checked');
							<?php 
                        }
                        ?>
							<?php 
                    }
                    ?>
							<?php 
                    if ($this->settings['sync_ping_status']) {
                        ?>
							<?php 
                        if ($res->ping_status == 'open') {
                            ?>
							ping_status.attr('checked', 'checked');
							<?php 
                        } else {
                            ?>
							ping_status.removeAttr('checked');
							<?php 
                        }
                        ?>
							<?php 
                    }
                    ?>
						});</script><?php 
                }
                if (isset($_GET['trid']) && $this->settings['sync_private_flag']) {
                    if ('private' == $wpdb->get_var($wpdb->prepare("\r\n\t\t\t\t\t\tSELECT p.post_status FROM {$wpdb->prefix}icl_translations t\r\n\t\t\t\t\t\tJOIN {$wpdb->posts} p ON t.element_id = p.ID\r\n\t\t\t\t\t\tWHERE t.trid=%d AND t.element_type='post_post'\r\n\t\t\t\t\t", $_GET['trid']))) {
                        ?>
						<script type="text/javascript">addLoadEvent(function () {
								jQuery('#visibility-radio-private').attr('checked', 'checked');
								jQuery('#post-visibility-display').html('<?php 
                        echo icl_js_escape(__('Private', 'sitepress'));
                        ?>
');
							});
						</script><?php 
                    }
                }
                if (isset($_GET['trid']) && $this->settings['sync_post_taxonomies']) {
                    $post_type = isset($_GET['post_type']) ? $_GET['post_type'] : 'post';
                    $source_lang = isset($_GET['source_lang']) ? $_GET['source_lang'] : $default_language;
                    $translatable_taxs = $this->get_translatable_taxonomies(true, $post_type);
                    $all_taxs = get_object_taxonomies($post_type);
                    $translations = $this->get_element_translations($_GET['trid'], 'post_' . $post_type);
                    $js = array();
                    if (!empty($all_taxs)) {
                        foreach ($all_taxs as $tax) {
                            $tax_detail = get_taxonomy($tax);
                            $terms = get_the_terms($translations[$source_lang]->element_id, $tax);
                            $term_names = array();
                            if ($terms) {
                                foreach ($terms as $term) {
                                    if ($tax_detail->hierarchical) {
                                        if (in_array($tax, $translatable_taxs)) {
                                            $term_id = icl_object_id($term->term_id, $tax, false);
                                        } else {
                                            $term_id = $term->term_id;
                                        }
                                        $js[] = "jQuery('#in-" . $tax . "-" . $term_id . "').attr('checked', 'checked');";
                                    } else {
                                        if (in_array($tax, $translatable_taxs)) {
                                            $term_id = icl_object_id($term->term_id, $tax, false);
                                            if ($term_id) {
                                                $term = get_term_by('id', $term_id, $tax);
                                                $term_names[] = esc_js($term->name);
                                            }
                                        } else {
                                            $term_names[] = esc_js($term->name);
                                        }
                                    }
                                }
                            }
                            if ($term_names) {
                                $js[] = "jQuery('#{$tax} .taghint').css('visibility','hidden');";
                                $js[] = "jQuery('#new-tag-{$tax}').val('" . join(', ', $term_names) . "');";
                            }
                        }
                    }
                    if ($js) {
                        echo '<script type="text/javascript">';
                        echo PHP_EOL . '// <![CDATA[' . PHP_EOL;
                        echo 'addLoadEvent(function(){' . PHP_EOL;
                        echo join(PHP_EOL, $js);
                        echo PHP_EOL . 'jQuery().ready(function() {
							jQuery(".tagadd").click();
							jQuery(\'html, body\').prop({scrollTop:0});
							jQuery(\'#title\').focus();
							});' . PHP_EOL;
                        echo PHP_EOL . '});' . PHP_EOL;
                        echo PHP_EOL . '// ]]>' . PHP_EOL;
                        echo '</script>';
                    }
                }
                // sync custom fields
                if (!empty($this->settings['translation-management'])) {
                    foreach ((array) $this->settings['translation-management']['custom_fields_translation'] as $key => $sync_opt) {
                        if ($sync_opt == 1) {
                            $copied_cf[] = $key;
                        }
                    }
                }
                if (!empty($copied_cf)) {
                    $source_lang = isset($_GET['source_lang']) ? $_GET['source_lang'] : $default_language;
                    $lang_details = $this->get_language_details($source_lang);
                    $original_custom = get_post_custom($translations[$source_lang]->element_id);
                    $copied_cf = array_intersect($copied_cf, array_keys($original_custom));
                    $copied_cf = apply_filters('icl_custom_fields_to_be_copied', $copied_cf, $translations[$source_lang]->element_id);
                    if (!empty($copied_cf) && (empty($this->user_preferences['notices']) || empty($this->user_preferences['notices']['hide_custom_fields_copy']))) {
                        $ccf_note = '<img src="' . ICL_PLUGIN_URL . '/res/img/alert.png" alt="Notice" width="16" height="16" style="margin-right:8px" />';
                        $ccf_note .= '<a class="icl_user_notice_hide" href="#hide_custom_fields_copy" style="float:right;margin-left:20px;">' . __('Never show this.', 'sitepress') . '</a>';
                        $ccf_note .= wp_nonce_field('save_user_preferences_nonce', '_icl_nonce_sup', false, false);
                        $ccf_note .= sprintf(__('WPML will copy %s from %s when you save this post.', 'sitepress'), '<i><strong>' . join('</strong>, <strong>', $copied_cf) . '</strong></i>', $lang_details['display_name']);
                        $this->admin_notices($ccf_note, 'error');
                    }
                }
            }
            ?>
			<?php 
            if (!empty($is_sticky) && $this->settings['sync_sticky_flag']) {
                ?>
				<script type="text/javascript">
					addLoadEvent(
						function () {
							jQuery('#sticky').attr('checked', 'checked');
							var post_visibility_display = jQuery('#post-visibility-display');
							post_visibility_display.html(post_visibility_display.html() + ', <?php 
                echo icl_js_escape(__('Sticky', 'sitepress'));
                ?>
');
						});
				</script>
			<?php 
            }
            ?>
		<?php 
        }
        if ('page-new.php' == $pagenow || 'post-new.php' == $pagenow && isset($_GET['post_type'])) {
            if (isset($_GET['trid']) && ($this->settings['sync_page_template'] || $this->settings['sync_page_ordering'])) {
                $res = $wpdb->get_row($wpdb->prepare("\r\n\t\t\t\t\tSELECT p.ID, p.menu_order FROM {$wpdb->prefix}icl_translations t\r\n\t\t\t\t\tJOIN {$wpdb->posts} p ON t.element_id = p.ID\r\n\t\t\t\t\tWHERE t.trid=%d AND p.post_type=%s AND t.element_type=%s\r\n\t\t\t\t", $_GET['trid'], $_GET['post_type'], 'post_' . $_GET['post_type']));
                if ($this->settings['sync_page_ordering']) {
                    $menu_order = $res->menu_order;
                } else {
                    $menu_order = false;
                }
                if ($this->settings['sync_page_template']) {
                    $page_template = get_post_meta($res->ID, '_wp_page_template', true);
                } else {
                    $page_template = false;
                }
                if ($menu_order || $page_template) {
                    ?>
					<script type="text/javascript">addLoadEvent(function () { <?php 
                    if ($menu_order) {
                        ?>
							jQuery('#menu_order').val(<?php 
                        echo $menu_order;
                        ?>
);
							<?php 
                    }
                    if ($page_template && 'default' != $page_template) {
                        ?>
							jQuery('#page_template').val('<?php 
                        echo $page_template;
                        ?>
');
							<?php 
                    }
                    ?>
						});</script><?php 
                }
            }
        } elseif ('edit-comments.php' == $pagenow || 'index.php' == $pagenow || 'post.php' == $pagenow) {
            wp_enqueue_script('sitepress-' . $page_basename, ICL_PLUGIN_URL . '/res/js/comments-translation.js', array(), ICL_SITEPRESS_VERSION);
        }
        // sync post dates
        if (icl_is_post_edit()) {
            // @since 3.1.5
            // Enqueing 'wp-jquery-ui-dialog', just in case it doesn't get automatically enqueued
            wp_enqueue_style('wp-jquery-ui-dialog');
            wp_enqueue_style('sitepress-post-edit', ICL_PLUGIN_URL . '/res/css/post-edit.css', array(), ICL_SITEPRESS_VERSION);
            wp_enqueue_script('sitepress-post-edit', ICL_PLUGIN_URL . '/res/js/post-edit.js', array('jquery-ui-dialog', 'jquery-ui-autocomplete', 'autosave'), ICL_SITEPRESS_VERSION);
            if ($this->settings['sync_post_date']) {
                $post_type = isset($_GET['post_type']) ? $_GET['post_type'] : 'post';
                if (isset($_GET['trid'])) {
                    $trid = intval($_GET['trid']);
                } else {
                    $post_id = @intval($_GET['post']);
                    $trid = $this->get_element_trid($post_id, 'post_' . $post_type);
                }
                $translations = $this->get_element_translations($trid, 'post_' . $post_type);
                if (!empty($translations) && isset($translations[$current_language]) && !$translations[$current_language]->original) {
                    $source_lang = isset($_GET['source_lang']) ? $_GET['source_lang'] : $default_language;
                    if (isset($translations[$source_lang])) {
                        $original_date = $wpdb->get_var($wpdb->prepare("SELECT post_date FROM {$wpdb->posts} WHERE ID=%d", $translations[$source_lang]->element_id));
                        $exp = explode(' ', $original_date);
                        list($aa, $mm, $jj) = explode('-', $exp[0]);
                        list($hh, $mn, $ss) = explode(':', $exp[1]);
                        ?>
						<script type="text/javascript">
							addLoadEvent(
								function () {
									jQuery('#aa').val('<?php 
                        echo $aa;
                        ?>
').attr('readonly', 'readonly');
									jQuery('#mm').val('<?php 
                        echo $mm;
                        ?>
').attr('readonly', 'readonly');
									jQuery('#jj').val('<?php 
                        echo $jj;
                        ?>
').attr('readonly', 'readonly');
									jQuery('#hh').val('<?php 
                        echo $hh;
                        ?>
').attr('readonly', 'readonly');
									jQuery('#mn').val('<?php 
                        echo $mn;
                        ?>
').attr('readonly', 'readonly');
									jQuery('#ss').val('<?php 
                        echo $ss;
                        ?>
').attr('readonly', 'readonly');
									var timestamp = jQuery('#timestamp');
									timestamp.find('b').html('<?php 
                        esc_html_e('copy from original', 'sitepress');
                        ?>
');
									timestamp.next().html('<?php 
                        esc_html_e('show', 'sitepress');
                        ?>
');
								});
						</script>
					<?php 
                    }
                }
            }
        }
        if ('post-new.php' == $pagenow && isset($_GET['trid']) && $this->settings['sync_post_format'] && function_exists('get_post_format')) {
            $format = get_post_format($wpdb->get_var($wpdb->prepare("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid=%d and language_code=%s", $_GET['trid'], $_GET['source_lang'])));
            ?>
			<script type="text/javascript">
				addLoadEvent(function () {
					jQuery('#post-format-' + '<?php 
            echo $format;
            ?>
').attr('checked', 'checked');
				});
			</script><?php 
        }
        if (is_admin()) {
            wp_enqueue_script('thickbox');
            wp_enqueue_script('theme-preview');
            wp_enqueue_script('sitepress-icl_reminders', ICL_PLUGIN_URL . '/res/js/icl_reminders.js', array(), ICL_SITEPRESS_VERSION);
        }
        //if('content-translation' == $page_basename) {
        //    wp_enqueue_script('icl-sidebar-scripts', ICL_PLUGIN_URL . '/res/js/icl_sidebar.js', array(), ICL_SITEPRESS_VERSION);
        //}
        if ('languages' == $page_basename || 'string-translation' == $page_basename) {
            wp_enqueue_script('colorpicker');
            wp_enqueue_script('jquery-ui-sortable');
        }
    }
    function js_scripts()
    {
        ?>
        <script type="text/javascript">
            addLoadEvent(function(){                     
                jQuery('#alp_re_scan_but').click(alp_toogle_scan);                
                jQuery('#alp_re_scan_but_all').click(alp_reset_scan_flags);
                jQuery('.alp_use_sug').click(alp_use_suggestion);
                jQuery('#alp_revert_urls').click(alp_do_revert_urls);
                
            });
            var alp_scan_started = false;
            var req_timer = 0;
            function alp_toogle_scan(){                       
                if(!alp_scan_started){  
                    alp_send_request(0); 
                    jQuery('#alp_ajx_ldr_1').fadeIn();
                    jQuery('#alp_re_scan_but').attr('value','<?php 
        echo icl_js_escape(__('Running', 'sitepress'));
        ?>
');    
                }else{
                    jQuery('#alp_re_scan_but').attr('value','<?php 
        echo icl_js_escape(__('Scan', 'sitepress'));
        ?>
');    
                    window.clearTimeout(req_timer);
                    jQuery('#alp_ajx_ldr_1').fadeOut();
                    location.reload();
                }
                alp_scan_started = !alp_scan_started;
                return false;
            }
            
            function alp_send_request(offset){
                jQuery.ajax({
                    type: "POST",
                    url: "<?php 
        echo htmlentities($_SERVER['REQUEST_URI']);
        ?>
",
                    data: "alp_ajx_action=rescan&amp;offset="+offset,
                    success: function(msg){                        
                        if(-1==msg || msg==0){
                            left = '0';
                            alp_toogle_scan();
                        }else{
                            left=msg;
                        }
                        
                        if(left=='0'){
                            jQuery('#alp_re_scan_but').attr('disabled','disabled');    
                        }
                        
                        jQuery('#alp_re_scan_toscan').html(left);
                        if(alp_scan_started){
                            req_timer = window.setTimeout(alp_send_request,3000,offset);
                        }
                    }                                                            
                });
            }
            
            function alp_reset_scan_flags(){
                if(alp_scan_started) return;
                alp_scan_started = false;
                jQuery('#alp_re_scan_but').removeAttr('disabled');    
                jQuery.ajax({
                    type: "POST",
                    url: "<?php 
        echo htmlentities($_SERVER['REQUEST_URI']);
        ?>
",
                    data: "alp_ajx_action=rescan_reset",
                    success: function(msg){    
                        if(msg){
                            alp_toogle_scan()
                        }
                    }                                                            
                });
            }
            function alp_use_suggestion(){
                jqthis = jQuery(this);
                jqthis.parent().parent().css('background-color','#eee');                
                spl = jqthis.attr('id').split('_');
                sug_id = spl[3];
                post_id = spl[4];
                orig_url = jQuery('#alp_bl_'+spl[5]).html();
                jQuery.ajax({
                    type: "POST",
                    url: "<?php 
        echo htmlentities($_SERVER['REQUEST_URI']);
        ?>
",
                    data: "alp_ajx_action=use_suggestion&amp;sug_id="+sug_id+"&amp;post_id="+post_id+"&amp;orig_url="+orig_url,
                    success: function(msg){                                                    
                        spl = msg.split('|');
                        jqthis.parent().html('<?php 
        echo icl_js_escape(__('fixed', 'sitepress'));
        ?>
 - ' + spl[1]);
                    },
                    error: function (msg){
                        alert('Something went wrong');
                        jqthis.parent().parent().css('background-color','#fff');
                    }                                                            
                });
                                
            }
            
            var req_rev_timer = '';
            function alp_do_revert_urls(){
                jQuery('#alp_revert_urls').attr('disabled','disabled');
                jQuery('#alp_revert_urls').attr('value','<?php 
        echo icl_js_escape(__('Running', 'sitepress'));
        ?>
');
                jQuery.ajax({
                    type: "POST",
                    url: "<?php 
        echo htmlentities($_SERVER['REQUEST_URI']);
        ?>
",
                    data: "alp_ajx_action=alp_revert_urls",
                    success: function(msg){                                                    
                        if(-1==msg || msg==0){
                            jQuery('#alp_ajx_ldr_2').fadeOut();
                            jQuery('#alp_rev_items_left').html('');
                            window.clearTimeout(req_rev_timer);
                            jQuery('#alp_revert_urls').removeAttr('disabled');                            
                            jQuery('#alp_revert_urls').attr('value','<?php 
        echo icl_js_escape(__('Start', 'sitepress'));
        ?>
');                            
                            location.reload();
                        }else{
                            jQuery('#alp_rev_items_left').html(msg + ' <?php 
        echo icl_js_escape(__('items left', 'sitepress'));
        ?>
');
                            req_rev_timer = window.setTimeout(alp_do_revert_urls,3000);
                            jQuery('#alp_ajx_ldr_2').fadeIn();
                        }                            
                    },
                    error: function (msg){
                        //alert('Something went wrong');
                    }                                                            
                });
            }
            
        </script>
        <?php 
    }
    function js_scripts()
    {
        global $pagenow;
        if ($pagenow == 'media.php') {
            ?>
			<script type="text/javascript">
				addLoadEvent(function(){                     
					jQuery('#icl_lang_options').insertBefore(jQuery('#post_id'));
					jQuery('#icl_lang_options').fadeIn();
				});
			</script>
			
			<?php 
        }
        if (isset($_GET['page']) && $_GET['page'] == 'wpml-media') {
            ?>
			<script type="text/javascript">
				addLoadEvent(function(){                     
					jQuery('#wpml_media_re_scan_but').click(wpml_media_re_scan);                
					jQuery('#wpml_media_re_scan_all_but').click(wpml_media_re_scan_all);
					jQuery('#wpml_media_feature_image_but').click(wpml_media_feature_image_scan)
					
				});
				var wpml_media_scan_started = false;
				var req_timer = 0;
				function wpml_media_toogle_scan(action){
					action = typeof(action) != 'undefined' ? action : 'rescan';

					if(!wpml_media_scan_started){  
						wpml_media_send_request(action); 
						jQuery('#wpml_media_ajx_ldr_1').fadeIn();
						jQuery('#wpml_media_re_scan_but').attr('value','<?php 
            echo icl_js_escape(__('Running', 'wpml-media'));
            ?>
');    
						jQuery('#wpml_media_re_scan_all_but').attr('value','<?php 
            echo icl_js_escape(__('Running', 'wpml-media'));
            ?>
');    
					}else{
						jQuery('#wpml_media_re_scan_but').attr('value','<?php 
            echo icl_js_escape(__('Scan and duplicate attachments', 'wpml-media'));
            ?>
');    
						jQuery('#wpml_media_re_scan_all_but').attr('value','<?php 
            echo icl_js_escape(__('Scan All', 'wpml-media'));
            ?>
');    
						window.clearTimeout(req_timer);
						jQuery('#wpml_media_ajx_ldr_1').fadeOut();
						location.reload();
					}
					wpml_media_scan_started = !wpml_media_scan_started;
					return false;
				}
				
				function wpml_media_send_request(action){
					jQuery.ajax({
						type: "POST",
						url: "<?php 
            echo htmlentities($_SERVER['REQUEST_URI']);
            ?>
",
						data: "wpml_media_ajx_action=" + action,
						success: function(msg){                        
							if(-1==msg || msg==0){
								left = '0';
								wpml_media_toogle_scan();
							}else{
								left=msg;
							}
							
							
							jQuery('#wpml_media_re_scan_toscan').html(left);
							if(wpml_media_scan_started){
								req_timer = window.setTimeout('wpml_media_send_request("rescan")', 1000);
							}
						}                                                            
					});
					
				}
				function wpml_media_re_scan(){
					wpml_media_toogle_scan("rescan");
				}
				function wpml_media_re_scan_all(){
					wpml_media_toogle_scan("rescan_all");
				}
				
				function wpml_media_feature_image_scan(){
					jQuery('#wpml_media_result').fadeOut();
					jQuery('#wpml_media_ajx_ldr_2').fadeIn();
					jQuery('#wpml_media_feature_image_but').attr('value','<?php 
            echo icl_js_escape(__('Running', 'wpml-media'));
            ?>
');    
					jQuery.ajax({
						type: "POST",
						url: "<?php 
            echo htmlentities($_SERVER['REQUEST_URI']);
            ?>
",
						data: "wpml_media_ajx_action=featured_image_scan",
						success: function(msg){
							jQuery('#wpml_media_ajx_ldr_2').fadeOut();
							jQuery('#wpml_media_feature_image_but').attr('value','<?php 
            echo icl_js_escape(__('Scan and duplicate featured images', 'wpml-media'));
            ?>
');
							jQuery('#wpml_media_result').html(msg);
							jQuery('#wpml_media_result').fadeIn();
						}
					});
				}
				
			</script>
			<?php 
        }
    }
<?php

require_once ICL_PLUGIN_PATH . '/sitepress.php';
$sitepress_settings = $sitepress->get_settings();
$cms_navigation_settings = $sitepress_settings['modules']['cms-navigation'];
$sitepress->noscript_notice();
?>
<script type="text/javascript">        
var icl_ajx_cache_cleared = '<?php 
echo icl_js_escape(__('The cache has been cleared.', 'sitepress'));
?>
';
</script>        
<div class="wrap">
    <div id="icon-options-general" class="icon32 icon32_adv"><br /></div>
    <h2><?php 
echo __('Setup WPML', 'sitepress');
?>
</h2>    
    
    <h3><?php 
echo __('Navigation', 'sitepress');
?>
</h3>    
    
    <p><?php 
echo __('Out-of-the-box support for full CMS navigation in your WordPress site including drop down menus, breadcrumbs trail and sidebar navigation.', 'sitepress');
?>
</p>

    
    public function wpml_js_scripts_setup()
    {
        //TODO: [WPML 3.3] move javascript to external resource (use wp_localize_script() to pass arguments)
        global $pagenow, $sitepress;
        $default_language = $this->sitepress->get_default_language();
        $current_language = $this->sitepress->get_current_language();
        $page_basename = $this->page;
        $this->print_js_globals();
        $wpml_script_setup_args['default_language'] = $default_language;
        $wpml_script_setup_args['current_language'] = $current_language;
        do_action('wpml_scripts_setup', $wpml_script_setup_args);
        if ('options-reading.php' === $pagenow) {
            $this->print_reading_options_js();
        } elseif (in_array($pagenow, array('categories.php', 'edit-tags.php'), true) && $current_language !== $default_language) {
            $this->correct_status_links_js($current_language);
        }
        if ('edit-tags.php' === $pagenow) {
            ?>
			<script type="text/javascript">
				addLoadEvent(function () {
					var edit_tag = jQuery('#edittag');
					if (edit_tag.find('[name="_wp_original_http_referer"]').length && edit_tag.find('[name="_wp_http_referer"]').length) {
						edit_tag.find('[name="_wp_original_http_referer"]').val('<?php 
            $post_type = isset($_GET['post_type']) ? '&post_type=' . esc_html($_GET['post_type']) : '';
            echo admin_url('edit-tags.php?taxonomy=' . esc_js($_GET['taxonomy']) . '&lang=' . $current_language . '&message=3' . $post_type);
            ?>
');
					}
				});
			</script>
		<?php 
        }
        $trid = filter_input(INPUT_GET, 'trid', FILTER_SANITIZE_NUMBER_INT);
        $source_lang = $trid !== null ? filter_input(INPUT_GET, 'source_lang', FILTER_SANITIZE_FULL_SPECIAL_CHARS) : null;
        if ('post-new.php' === $pagenow) {
            if ($trid) {
                $translations = $this->post_translations->get_element_translations(false, $trid);
                remove_filter('pre_option_sticky_posts', array($sitepress, 'option_sticky_posts'));
                // remove filter used to get language relevant stickies. get them all
                $sticky_posts = get_option('sticky_posts');
                add_filter('pre_option_sticky_posts', array($sitepress, 'option_sticky_posts'), 10, 2);
                // add filter back
                $is_sticky = false;
                foreach ($translations as $t) {
                    if (in_array($t, $sticky_posts)) {
                        $is_sticky = true;
                        break;
                    }
                }
                if ($this->sitepress->get_setting('sync_ping_status') || $this->sitepress->get_setting('sync_comment_status')) {
                    $this->print_ping_and_comment_sync_js($trid, $source_lang);
                }
                if ('private' === $this->post_translations->get_original_post_status($trid, $source_lang)) {
                    ?>
					<script type="text/javascript">addLoadEvent(function () {
							jQuery('#visibility-radio-private').attr('checked', 'checked');
							jQuery('#post-visibility-display').html('<?php 
                    echo icl_js_escape(__('Private', 'sitepress'));
                    ?>
');
						});
					</script><?php 
                }
                if ($this->sitepress->get_setting('sync_post_taxonomies')) {
                    $this->print_tax_sync_js();
                }
                $custom_field_note = new WPML_Sync_Custom_Field_Note($this->sitepress);
                $custom_field_note->print_sync_copy_custom_field_note($source_lang, $translations);
            }
            ?>
			<?php 
            if (!empty($is_sticky) && $this->sitepress->get_setting('sync_sticky_flag')) {
                ?>
				<script type="text/javascript">
					addLoadEvent(
						function () {
							jQuery('#sticky').attr('checked', 'checked');
							var post_visibility_display = jQuery('#post-visibility-display');
							post_visibility_display.html(post_visibility_display.html() + ', <?php 
                echo icl_js_escape(__('Sticky', 'sitepress'));
                ?>
');
						});
				</script>
			<?php 
            }
            ?>
		<?php 
        }
        if (('page-new.php' === $pagenow || 'post-new.php' === $pagenow && isset($_GET['post_type'])) && ($trid && ($this->sitepress->get_setting('sync_page_template') || $this->sitepress->get_setting('sync_page_ordering')))) {
            $this->print_mo_sync_js($trid, $source_lang);
        }
        if ($this->sitepress->is_post_edit_screen() && $this->sitepress->get_setting('sync_post_date')) {
            $this->print_sync_date_js();
        }
        if ('post-new.php' === $pagenow && isset($_GET['trid']) && $sitepress->get_setting('sync_post_format') && function_exists('get_post_format')) {
            $format = $this->post_translations->get_original_post_format($trid, $source_lang);
            ?>
			<script type="text/javascript">
				addLoadEvent(function () {
					jQuery('#post-format-' + '<?php 
            echo $format;
            ?>
').attr('checked', 'checked');
				});
			</script><?php 
        }
        wp_enqueue_script('theme-preview');
        if ('languages' === $page_basename || 'string-translation' === $page_basename) {
            wp_enqueue_script('wp-color-picker');
            wp_register_style('wpml-color-picker', ICL_PLUGIN_URL . '/res/css/colorpicker.css', array('wp-color-picker'), ICL_SITEPRESS_VERSION);
            wp_enqueue_style('wpml-color-picker');
            wp_enqueue_script('jquery-ui-sortable');
        }
    }
    function js_scripts_setup()
    {
        global $pagenow, $wpdb;
        if (isset($_GET['page'])) {
            $page = basename($_GET['page']);
            $page_basename = str_replace('.php', '', $page);
        }
        ?>
        <script type="text/javascript">   
        // <![CDATA[    
        <?php 
        if (defined('FORCE_SSL_ADMIN') && FORCE_SSL_ADMIN) {
            ?>
 
        var icl_ajx_url = '<?php 
            echo str_replace('http://', 'https://', rtrim(get_option('siteurl'), '/')) . '/wp-admin/';
            ?>
admin.php?page=<?php 
            echo ICL_PLUGIN_FOLDER;
            ?>
/menu/languages.php';
        <?php 
        } else {
            ?>
        var icl_ajx_url = '<?php 
            echo rtrim(get_option('siteurl'), '/') . '/wp-admin/';
            ?>
admin.php?page=<?php 
            echo ICL_PLUGIN_FOLDER;
            ?>
/menu/languages.php';
        <?php 
        }
        ?>
        var icl_ajx_saved = '<?php 
        echo icl_js_escape(__('Data saved', 'sitepress'));
        ?>
';
        var icl_ajx_error = '<?php 
        echo icl_js_escape(__('Error: data not saved', 'sitepress'));
        ?>
';
        var icl_default_mark = '<?php 
        echo icl_js_escape(__('default', 'sitepress'));
        ?>
';     
        var icl_this_lang = '<?php 
        echo $this->this_lang;
        ?>
';   
        var icl_ajxloaderimg_src = '<?php 
        echo ICL_PLUGIN_URL;
        ?>
/res/img/ajax-loader.gif';
        var icl_cat_adder_msg = '<?php 
        echo icl_js_escape(__('To add categories that already exist in other languages go to the <a href="edit-tags.php?taxonomy=category">category management page<\\/a>', 'sitepress'));
        ?>
';
        // ]]>
        
        <?php 
        if (!$this->settings['ajx_health_checked']) {
            ?>
        addLoadEvent(function(){
            jQuery.ajax({type: "POST",url: icl_ajx_url,data: "icl_ajx_action=health_check", error: function(msg){
                    if(jQuery('#icl_initial_language').length){
                        jQuery('#icl_initial_language input').attr('disabled', 'disabled');
                    }
                    jQuery('.wrap').prepend('<div class="error"><p><?php 
            echo icl_js_escape(sprintf(__("WPML can't run normally. There is an installation or server configuration problem. %sShow details%s", 'sitepress'), '<a href="#" onclick="jQuery(this).parent().next().slideToggle()">', '</a>'));
            ?>
</p><p style="display:none"><?php 
            echo icl_js_escape(__('AJAX Error:', 'sitepress'));
            ?>
 ' + msg.statusText + ' ['+msg.status+']<br />URL:'+ icl_ajx_url +'</p></div>');
            }});
        });
        <?php 
        }
        ?>
        </script>         
        <?php 
        wp_enqueue_script('sitepress-scripts', ICL_PLUGIN_URL . '/res/js/scripts.js', array(), ICL_SITEPRESS_VERSION);
        if (isset($page_basename) && file_exists(ICL_PLUGIN_PATH . '/res/js/' . $page_basename . '.js')) {
            wp_enqueue_script('sitepress-' . $page_basename, ICL_PLUGIN_URL . '/res/js/' . $page_basename . '.js', array(), ICL_SITEPRESS_VERSION);
        }
        if ('options-reading.php' == $pagenow) {
            list($warn_home, $warn_posts) = $this->verify_home_and_blog_pages_translations();
            if ($warn_home || $warn_posts) {
                ?>
                <script type="text/javascript">        
                addLoadEvent(function(){
                jQuery('input[name="show_on_front"]').parent().parent().parent().parent().append('<?php 
                echo str_replace("'", "\\'", $warn_home . $warn_posts);
                ?>
');
                });
                </script>
                <?php 
            }
        }
        // display correct links on the posts by status break down
        // also fix links to category and tag pages
        if (('edit.php' == $pagenow || 'edit-pages.php' == $pagenow || 'categories.php' == $pagenow || 'edit-tags.php' == $pagenow) && $this->get_current_language() != $this->get_default_language()) {
            ?>
                <script type="text/javascript">        
                addLoadEvent(function(){
                    jQuery('.subsubsub li a').each(function(){
                        h = jQuery(this).attr('href');
                        if(-1 == h.indexOf('?')) urlg = '?'; else urlg = '&';
                        jQuery(this).attr('href', h + urlg + 'lang=<?php 
            echo $this->get_current_language();
            ?>
');
                    });
                    jQuery('.column-categories a, .column-tags a, .column-posts a').each(function(){
                        jQuery(this).attr('href', jQuery(this).attr('href') + '&lang=<?php 
            echo $this->get_current_language();
            ?>
');
                    });
                    <?php 
            /*  needs jQuery 1.3
                jQuery('.column-categories a, .column-tags a, .column-posts a').live('mouseover', function(){
                    if(-1 == jQuery(this).attr('href').search('lang='+icl_this_lang)){
                        h = jQuery(this).attr('href');
                        if(-1 == h.indexOf('?')) urlg = '?'; else urlg = '&';                            
                        jQuery(this).attr('href', h + urlg + 'lang='+icl_this_lang);
                    }
                });     
                */
            ?>
           
                });
                </script>
                <?php 
        }
        if ('post-new.php' == $pagenow) {
            if (isset($_GET['trid'])) {
                $translations = $wpdb->get_col("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid='{$_GET['trid']}'");
                remove_filter('option_sticky_posts', array($this, 'option_sticky_posts'));
                // remove filter used to get language relevant stickies. get them all
                $sticky_posts = get_option('sticky_posts');
                add_filter('option_sticky_posts', array($this, 'option_sticky_posts'));
                // add filter back
                $is_sticky = false;
                foreach ($translations as $t) {
                    if (in_array($t, $sticky_posts)) {
                        $is_sticky = true;
                        break;
                    }
                }
                if (isset($_GET['trid']) && ($this->settings['sync_ping_status'] || $this->settings['sync_comment_status'])) {
                    $res = $wpdb->get_row("SELECT comment_status, ping_status FROM {$wpdb->prefix}icl_translations t \r\n                    JOIN {$wpdb->posts} p ON t.element_id = p.ID WHERE t.trid='" . intval($_GET['trid']) . "'");
                    ?>
                    <script type="text/javascript">addLoadEvent(function(){
                    <?php 
                    if ($this->settings['sync_comment_status']) {
                        ?>
                        <?php 
                        if ($res->comment_status == 'open') {
                            ?>
                        jQuery('#comment_status').attr('checked','checked');
                        <?php 
                        } else {
                            ?>
                        jQuery('#comment_status').removeAttr('checked');
                        <?php 
                        }
                        ?>
                    <?php 
                    }
                    ?>
                    <?php 
                    if ($this->settings['sync_ping_status']) {
                        ?>
                        <?php 
                        if ($res->ping_status == 'open') {
                            ?>
                        jQuery('#ping_status').attr('checked','checked');
                        <?php 
                        } else {
                            ?>
                        jQuery('#ping_status').removeAttr('checked');
                        <?php 
                        }
                        ?>
                    
                    <?php 
                    }
                    ?>
                    });</script><?php 
                }
                if (isset($_GET['trid']) && $this->settings['sync_private_flag']) {
                    if ('private' == $wpdb->get_var("\r\n                        SELECT p.post_status FROM {$wpdb->prefix}icl_translations t\r\n                        JOIN {$wpdb->posts} p ON t.element_id = p.ID\r\n                        WHERE t.trid='{$_GET['trid']}' AND t.element_type='post_post'\r\n                    ")) {
                        ?>
<script type="text/javascript">addLoadEvent(function(){
                            jQuery('#visibility-radio-private').attr('checked','checked');
                            jQuery('#post-visibility-display').html('<?php 
                        echo icl_js_escape(__('Private', 'sitepress'));
                        ?>
');
                        });
                        </script><?php 
                    }
                }
                //get menu_order for page
            }
            ?>
            <?php 
            if ($is_sticky && $this->settings['sync_sticky_flag']) {
                ?>
                <script type="text/javascript">
                    addLoadEvent(function(){
                            jQuery('#sticky').attr('checked','checked');
                            jQuery('#post-visibility-display').html(jQuery('#post-visibility-display').html()+', <?php 
                echo icl_js_escape(__('Sticky', 'sitepress'));
                ?>
');
                    });
                </script>
            <?php 
            }
            ?>
               
            <?php 
        }
        if ('page-new.php' == $pagenow || 'post-new.php' == $pagenow && $_GET['post_type'] == 'page') {
            if (isset($_GET['trid']) && ($this->settings['sync_page_template'] || $this->settings['sync_page_ordering'])) {
                $res = $wpdb->get_row("\r\n                    SELECT p.ID, p.menu_order FROM {$wpdb->prefix}icl_translations t\r\n                    JOIN {$wpdb->posts} p ON t.element_id = p.ID\r\n                    WHERE t.trid='{$_GET['trid']}' AND p.post_type='page' AND t.element_type='post_page'\r\n                ");
                if ($this->settings['sync_page_ordering']) {
                    $menu_order = $res->menu_order;
                } else {
                    $menu_order = false;
                }
                if ($this->settings['sync_page_template']) {
                    $page_template = get_post_meta($res->ID, '_wp_page_template', true);
                } else {
                    $page_template = false;
                }
                if ($menu_order || $page_template) {
                    ?>
<script type="text/javascript">addLoadEvent(function(){ <?php 
                    if ($menu_order) {
                        ?>
                        jQuery('#menu_order').val(<?php 
                        echo $menu_order;
                        ?>
);
                    <?php 
                    }
                    if ($page_template && 'default' != $page_template) {
                        ?>
                        jQuery('#page_template').val('<?php 
                        echo $page_template;
                        ?>
');
                    <?php 
                    }
                    ?>
});</script><?php 
                }
            }
        } elseif ('edit-comments.php' == $pagenow || 'index.php' == $pagenow || 'post.php' == $pagenow) {
            wp_enqueue_script('sitepress-' . $page_basename, ICL_PLUGIN_URL . '/res/js/comments-translation.js', array(), ICL_SITEPRESS_VERSION);
        }
        if (is_admin()) {
            wp_enqueue_script('thickbox');
            wp_enqueue_script('theme-preview');
            wp_enqueue_script('sitepress-icl_reminders', ICL_PLUGIN_URL . '/res/js/icl_reminders.js', array(), ICL_SITEPRESS_VERSION);
        }
        //if('content-translation' == $page_basename) {
        //    wp_enqueue_script('icl-sidebar-scripts', ICL_PLUGIN_URL . '/res/js/icl_sidebar.js', array(), ICL_SITEPRESS_VERSION);
        //}
        if ('languages' == $page_basename || 'string-translation' == $page_basename) {
            wp_enqueue_script('colorpicker');
        }
    }
    function cms_navigation_js()
    {
        ?>
        <script type="text/javascript">
        addLoadEvent(function(){                   
                    jQuery('#cms_nav_add_section').click(cms_nav_switch_adding_section);    
        });
        function cms_nav_switch_adding_section(){
            if('none'==jQuery("select[name='cms_nav_section']").css('display')){
                jQuery("select[name='cms_nav_section']").show();
                jQuery("input[name='cms_nav_section_new']").hide();
                jQuery("input[name='cms_nav_section_new']").attr('value','');
                jQuery(this).html('<?php 
        echo icl_js_escape(__('enter new', 'sitepress'));
        ?>
');                                    
            }else{
                jQuery("select[name='cms_nav_section']").hide();
                jQuery("input[name='cms_nav_section_new']").show();            
                jQuery(this).html('<?php 
        echo icl_js_escape(__('cancel', 'sitepress'));
        ?>
');
            }
            
        }
        </script>
        <?php 
    }
    public function wpml_js_scripts_setup()
    {
        //TODO: [WPML 3.3] move javascript to external resource (use wp_localize_script() to pass arguments)
        global $pagenow, $wpdb, $sitepress, $wpml_post_translations;
        $default_language = $this->default_language;
        $current_language = $this->current_language;
        $page_basename = $this->page;
        $this->print_js_globals();
        if ('options-reading.php' === $pagenow) {
            $this->print_reading_options_js();
        } elseif (in_array($pagenow, array('edit.php', 'edit-pages.php', 'categories.php', 'edit-tags.php'), true) && $current_language !== $default_language) {
            // display correct links on the posts by status break down
            // also fix links to category and tag pages
            ?>
			<script type="text/javascript">
				addLoadEvent(
					function () {
						jQuery(document).ready(
							function () {
								jQuery('.subsubsub:not(.icl_subsubsub) li a').each(
									function () {
										var h = jQuery(this).attr('href');
										var urlg;
										if (-1 == h.indexOf('?')) {
											urlg = '?';
										} else {
											urlg = '&';
										}
										jQuery(this).attr('href', h + urlg + 'lang=<?php 
            echo $current_language;
            ?>
');
									}
								);
								jQuery('.column-categories a, .column-tags a, .column-posts a').each(
									function () {
										jQuery(this).attr('href', jQuery(this).attr('href') + '&lang=<?php 
            echo $current_language;
            ?>
');
									}
								);
							}
						);
					}
				);
			</script>
		<?php 
        }
        if ('edit-tags.php' === $pagenow) {
            ?>
			<script type="text/javascript">
				addLoadEvent(function () {
					var edit_tag = jQuery('#edittag');
					if (edit_tag.find('[name="_wp_original_http_referer"]').length && edit_tag.find('[name="_wp_http_referer"]').length) {
						edit_tag.find('[name="_wp_original_http_referer"]').val('<?php 
            $post_type = isset($_GET['post_type']) ? '&post_type=' . esc_html($_GET['post_type']) : '';
            echo admin_url('edit-tags.php?taxonomy=' . esc_js($_GET['taxonomy']) . '&lang=' . $current_language . '&message=3' . $post_type);
            ?>
');
					}
				});
			</script>
		<?php 
        }
        $trid = filter_input(INPUT_GET, 'trid', FILTER_SANITIZE_NUMBER_INT);
        $source_lang = $trid !== null ? filter_input(INPUT_GET, 'source_lang', FILTER_SANITIZE_FULL_SPECIAL_CHARS) : null;
        if ('post-new.php' === $pagenow) {
            if ($trid) {
                $translations = $wpdb->get_col($wpdb->prepare("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid=%d", $_GET['trid']));
                remove_filter('pre_option_sticky_posts', array($sitepress, 'option_sticky_posts'));
                // remove filter used to get language relevant stickies. get them all
                $sticky_posts = get_option('sticky_posts');
                add_filter('pre_option_sticky_posts', array($sitepress, 'option_sticky_posts'), 10, 2);
                // add filter back
                $is_sticky = false;
                foreach ($translations as $t) {
                    if (in_array($t, $sticky_posts)) {
                        $is_sticky = true;
                        break;
                    }
                }
                if ($sitepress->get_setting('sync_ping_status') || $sitepress->get_setting('sync_comment_status')) {
                    ?>
					<script type="text/javascript">addLoadEvent(function () {
							var comment_status = jQuery('#comment_status');
							var ping_status = jQuery('#ping_status');
							<?php 
                    if ($sitepress->get_setting('sync_comment_status')) {
                        ?>
							<?php 
                        if ($wpml_post_translations->get_original_comment_status($trid, $source_lang) === 'open') {
                            ?>
							comment_status.attr('checked', 'checked');
							<?php 
                        } else {
                            ?>
							comment_status.removeAttr('checked');
							<?php 
                        }
                        ?>
							<?php 
                    }
                    ?>
							<?php 
                    if ($sitepress->get_setting('sync_ping_status')) {
                        ?>
							<?php 
                        if ($wpml_post_translations->get_original_ping_status($trid, $source_lang) === 'open') {
                            ?>
							ping_status.attr('checked', 'checked');
							<?php 
                        } else {
                            ?>
							ping_status.removeAttr('checked');
							<?php 
                        }
                        ?>
							<?php 
                    }
                    ?>
						});</script><?php 
                }
                if ('private' === $wpml_post_translations->get_original_post_status($trid, $source_lang)) {
                    ?>
					<script type="text/javascript">addLoadEvent(function () {
							jQuery('#visibility-radio-private').attr('checked', 'checked');
							jQuery('#post-visibility-display').html('<?php 
                    echo icl_js_escape(__('Private', 'sitepress'));
                    ?>
');
						});
					</script><?php 
                }
                if ($sitepress->get_setting('sync_post_taxonomies')) {
                    $post_type = isset($_GET['post_type']) ? $_GET['post_type'] : 'post';
                    $source_lang = isset($_GET['source_lang']) ? filter_input(INPUT_GET, 'source_lang', FILTER_SANITIZE_FULL_SPECIAL_CHARS) : $default_language;
                    $translatable_taxs = $sitepress->get_translatable_taxonomies(true, $post_type);
                    $all_taxs = get_object_taxonomies($post_type);
                    $translations = $sitepress->get_element_translations($_GET['trid'], 'post_' . $post_type);
                    $js = array();
                    if (!empty($all_taxs)) {
                        foreach ($all_taxs as $tax) {
                            $tax_detail = get_taxonomy($tax);
                            $terms = get_the_terms($translations[$source_lang]->element_id, $tax);
                            $term_names = array();
                            if ($terms) {
                                foreach ($terms as $term) {
                                    if ($tax_detail->hierarchical) {
                                        $term_id = in_array($tax, $translatable_taxs) ? icl_object_id($term->term_id, $tax, false) : $term->term_id;
                                        $js[] = "jQuery('#in-" . $tax . "-" . $term_id . "').attr('checked', 'checked');";
                                    } else {
                                        if (in_array($tax, $translatable_taxs)) {
                                            $term_id = icl_object_id($term->term_id, $tax, false);
                                            if ($term_id) {
                                                $term = get_term($term_id, $tax);
                                                $term_names[] = esc_js($term->name);
                                            }
                                        } else {
                                            $term_names[] = esc_js($term->name);
                                        }
                                    }
                                }
                            }
                            if ($term_names) {
                                $js[] = "jQuery('#{$tax} .taghint').css('visibility','hidden');";
                                $js[] = "jQuery('#new-tag-{$tax}').val('" . join(', ', $term_names) . "');";
                            }
                        }
                    }
                    if ($js) {
                        echo '<script type="text/javascript">';
                        echo PHP_EOL . '// <![CDATA[' . PHP_EOL;
                        echo 'addLoadEvent(function(){' . PHP_EOL;
                        echo join(PHP_EOL, $js);
                        echo PHP_EOL . 'jQuery().ready(function() {
								jQuery(".tagadd").click();
								jQuery(\'html, body\').prop({scrollTop:0});
								jQuery(\'#title\').focus();
								});' . PHP_EOL;
                        echo PHP_EOL . '});' . PHP_EOL;
                        echo PHP_EOL . '// ]]>' . PHP_EOL;
                        echo '</script>';
                    }
                }
                // sync custom fields
                $tm_settings = $sitepress->get_setting('translation-management');
                $custom_fields_translation = !empty($tm_settings['custom_fields_translation']) ? (array) $tm_settings['custom_fields_translation'] : array();
                foreach ($custom_fields_translation as $key => $sync_opt) {
                    if ($sync_opt == 1) {
                        $copied_cf[] = $key;
                    }
                }
                if (!empty($copied_cf)) {
                    $source_lang = $source_lang ? $source_lang : $default_language;
                    $lang_details = $sitepress->get_language_details($source_lang);
                    $original_custom = get_post_custom($translations[$source_lang]->element_id);
                    $copied_cf = array_intersect($copied_cf, array_keys($original_custom));
                    $copied_cf = apply_filters('icl_custom_fields_to_be_copied', $copied_cf, $translations[$source_lang]->element_id);
                    $user_preferences = $sitepress->get_user_preferences();
                    if ($copied_cf && (!isset($user_preferences['notices']['hide_custom_fields_copy']) || !$user_preferences['notices']['hide_custom_fields_copy'])) {
                        $ccf_note = '<img src="' . ICL_PLUGIN_URL . '/res/img/alert.png" alt="Notice" width="16" height="16" style="margin-right:8px" />';
                        $ccf_note .= '<a class="icl_user_notice_hide" href="#hide_custom_fields_copy" style="float:right;margin-left:20px;">' . __('Never show this.', 'sitepress') . '</a>';
                        $ccf_note .= wp_nonce_field('save_user_preferences_nonce', '_icl_nonce_sup', false, false);
                        $ccf_note .= sprintf(__('WPML will copy %s from %s when you save this post.', 'sitepress'), '<i><strong>' . join('</strong>, <strong>', $copied_cf) . '</strong></i>', $lang_details['display_name']);
                        $sitepress->admin_notices($ccf_note, 'error');
                    }
                }
            }
            ?>
			<?php 
            if (!empty($is_sticky) && $sitepress->get_setting('sync_sticky_flag')) {
                ?>
				<script type="text/javascript">
					addLoadEvent(
						function () {
							jQuery('#sticky').attr('checked', 'checked');
							var post_visibility_display = jQuery('#post-visibility-display');
							post_visibility_display.html(post_visibility_display.html() + ', <?php 
                echo icl_js_escape(__('Sticky', 'sitepress'));
                ?>
');
						});
				</script>
			<?php 
            }
            ?>
		<?php 
        }
        if ('page-new.php' == $pagenow || 'post-new.php' == $pagenow && isset($_GET['post_type'])) {
            if ($trid && ($sitepress->get_setting('sync_page_template') || $sitepress->get_setting('sync_page_ordering'))) {
                $menu_order = $sitepress->get_setting('sync_page_ordering') ? $wpml_post_translations->get_original_menu_order($trid, $source_lang) : null;
                $page_template = $sitepress->get_setting('sync_page_template') ? get_post_meta($wpml_post_translations->get_original_post_ID($trid, $source_lang), '_wp_page_template', true) : null;
                if ($menu_order || $page_template) {
                    ?>
					<script type="text/javascript">addLoadEvent(function () { <?php 
                    if ($menu_order) {
                        ?>
							jQuery('#menu_order').val(<?php 
                        echo $menu_order;
                        ?>
);
							<?php 
                    }
                    if ($page_template && 'default' != $page_template) {
                        ?>
							jQuery('#page_template').val('<?php 
                        echo $page_template;
                        ?>
');
							<?php 
                    }
                    ?>
						});</script><?php 
                }
            }
        }
        // sync post dates
        if (icl_is_post_edit()) {
            // @since 3.1.5
            // Enqueing 'wp-jquery-ui-dialog', just in case it doesn't get automatically enqueued
            wp_enqueue_style('wp-jquery-ui-dialog');
            wp_enqueue_style('sitepress-post-edit', ICL_PLUGIN_URL . '/res/css/post-edit.css', array(), ICL_SITEPRESS_VERSION);
            wp_enqueue_script('sitepress-post-edit', ICL_PLUGIN_URL . '/res/js/post-edit.js', array('jquery-ui-dialog', 'jquery-ui-autocomplete', 'autosave'), ICL_SITEPRESS_VERSION);
            if ($sitepress->get_setting('sync_post_date')) {
                if (!$trid) {
                    $post_id = filter_input(INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT);
                    $trid = $post_id !== null ? $wpml_post_translations->get_element_trid($post_id) : null;
                }
                if ($trid) {
                    $original_date = $wpml_post_translations->get_original_post_date($trid, $source_lang);
                    $exp = explode(' ', $original_date);
                    list($aa, $mm, $jj) = explode('-', $exp[0]);
                    list($hh, $mn, $ss) = explode(':', $exp[1]);
                    ?>
					<script type="text/javascript">
						addLoadEvent(
							function () {
								jQuery('#aa').val('<?php 
                    echo $aa;
                    ?>
').attr('readonly', 'readonly');
								jQuery('#mm').val('<?php 
                    echo $mm;
                    ?>
').attr('readonly', 'readonly');
								jQuery('#jj').val('<?php 
                    echo $jj;
                    ?>
').attr('readonly', 'readonly');
								jQuery('#hh').val('<?php 
                    echo $hh;
                    ?>
').attr('readonly', 'readonly');
								jQuery('#mn').val('<?php 
                    echo $mn;
                    ?>
').attr('readonly', 'readonly');
								jQuery('#ss').val('<?php 
                    echo $ss;
                    ?>
').attr('readonly', 'readonly');
								var timestamp = jQuery('#timestamp');
								timestamp.find('b').append(( '<span> <?php 
                    esc_html_e('Copied From the Original', 'sitepress');
                    ?>
</span>'));
								timestamp.next().html('<span style="margin-left:1em;"><?php 
                    esc_html_e('Edit', 'sitepress');
                    ?>
</span>');
							});
					</script>
				<?php 
                }
            }
        }
        if ('post-new.php' === $pagenow && isset($_GET['trid']) && $sitepress->get_setting('sync_post_format') && function_exists('get_post_format')) {
            $format = $wpml_post_translations->get_original_post_format($trid, $source_lang);
            ?>
			<script type="text/javascript">
				addLoadEvent(function () {
					jQuery('#post-format-' + '<?php 
            echo $format;
            ?>
').attr('checked', 'checked');
				});
			</script><?php 
        }
        wp_enqueue_script('theme-preview');
        if ('languages' === $page_basename || 'string-translation' === $page_basename) {
            wp_enqueue_script('wp-color-picker');
            wp_register_style('wpml-color-picker', ICL_PLUGIN_URL . '/res/css/colorpicker.css', array('wp-color-picker'), ICL_SITEPRESS_VERSION);
            wp_enqueue_style('wpml-color-picker');
            wp_enqueue_script('jquery-ui-sortable');
        }
    }