<center><div id='contentload'></div></center>

<link rel="stylesheet" href="<?php echo WP_PLUGIN_URL; ?>/wpspin/css/jquery.fancybox-1.3.4.css">
<link rel="stylesheet" href="<?php echo WP_PLUGIN_URL; ?>/wpspin/css/datePicker.css">

<script src="<?php echo WP_PLUGIN_URL; ?>/wpspin/js/date.js"></script>
<script src="<?php echo WP_PLUGIN_URL; ?>/wpspin/js/datePicker.js"></script>
<script src="<?php echo WP_PLUGIN_URL; ?>/wpspin/js/jquery.datePicker.js"></script>
<script src="<?php echo WP_PLUGIN_URL; ?>/wpspin/js/jquery.base64.min.js"></script>
<script src="<?php echo WP_PLUGIN_URL; ?>/wpspin/js/jquery.string.1.0.js"></script>
<script src="<?php echo WP_PLUGIN_URL; ?>/wpspin/js/jquery.mousewheel-3.0.4.pack.js" type="text/javascript"></script>
<script src="<?php echo WP_PLUGIN_URL; ?>/wpspin/js/jquery.fancybox-1.3.4.pack.js" type="text/javascript"></script>

<script>
var buffer1 = '';
var buffer2 = '';

    jQuery( function() {        
        jQuery("#rWindow").fancybox({
            'titlePosition'	: 'inside',
            'transitionIn'	: 'elastic',
            'transitionOut'	: 'elastic',
            'overlayShow'	: false,
            'opacity'		: true
        });
        jQuery("#iWindow").fancybox({
            'titlePosition'	: 'inside',
            'transitionIn'	: 'elastic',
            'transitionOut'	: 'elastic',
            'overlayShow'	: false,
            'opacity'		: true ,
            'onStart': function() {
                buffer1 = tinyMCE.activeEditor.getContent();
                buffer2 = tinyMCE.activeEditor.selection.getContent();
            }
        });
        
        jQuery( "#close-r").click(function () {
            jQuery.fancybox.close();
        }); 
        jQuery( "#close-i").click(function () {
            jQuery.fancybox.close();
        }); 

    });
        
    function buttonAutoReplace() {
        var content = tinyMCE.activeEditor.getContent({format : 'raw'});
        if(content=='' || content=='<p><br mce_bogus="1"></p>') {
            alert('Please write some text first!');
            jQuery.fancybox.close();
            return false;
        }

        var auto_options = jQuery( ":radio[name=wpspin_auto_options]" ).filter(":checked").val();
        var spin_title_r = jQuery( ":checkbox[name=wpspin_spin_title_r]" ).filter(":checked").val();
        if(spin_title_r=='on') {
            var spin_title='1'; 
        } else { 
            var spin_title='0'; 
        }
        jQuery( "#publishing-action" ).append('<input type="hidden" id="spin_mode" name="spin_mode" value="replace">');
        switch( auto_options ) {
            case '1':
                jQuery( "#publishing-action" ).append('<input type="hidden" id="auto_options" name="auto_options" value="1">');
                break;
            case '2':
                jQuery( "#publishing-action" ).append('<input type="hidden" id="auto_options" name="auto_options" value="2">');
                jQuery( "#publishing-action" ).append('<input type="hidden" name="spin_title" value="'+spin_title+'">');
                jQuery( "#publishing-action" ).append('<input type="hidden" id="quality" name="quality" value="'+jQuery( "#quality" ).val()+'">');
                jQuery( "#publishing-action" ).append('<textarea rows="0" cols="0" name="excluded" style="display:none">'+jQuery( "#excluded_words" ).val()+'</textarea>');
                //jQuery.fancybox.close();
                //return false;
                break;
            case '3':
                jQuery( "#publishing-action" ).append('<input type="hidden" name="auto_options" value="3">');
                jQuery( "#publishing-action" ).append('<input type="hidden" name="spin_title" value="'+spin_title+'">');
                jQuery( "#publishing-action" ).append('<input type="hidden" name="quality" value="'+jQuery( "#quality" ).val()+'">');
                jQuery( "#publishing-action" ).append('<input type="hidden" name="spin_count" value="'+jQuery( "#wpspin_spin_count" ).val()+'">');
                jQuery( "#publishing-action" ).append('<input type="hidden" name="spin_publish" value="'+jQuery( ":radio[name=wpspin_publish]").filter(":checked").val()+'">');
                jQuery( "#publishing-action" ).append('<input type="hidden" name="datepick" value="'+jQuery( '#datepicker-r').dpGetSelected()+'">');
                jQuery( "#publishing-action" ).append('<textarea rows="0" cols="0" name="excluded" style="display:none">'+jQuery( "#excluded_words" ).val()+'</textarea>');
                //jQuery.fancybox.close();
                //return false;
                break;
        }

        var data="goform=1&content="+jQuery.base64.encode(escape(content))+"&method=replaceEveryonesFavorites&maxsyns="+jQuery("#maxsyns").val()+"&quality="+jQuery( "#quality" ).val()+"&excluded="+jQuery( "#excluded_words" ).val();
        
        jQuery( "#contentload").html('please wait ...');
        jQuery.ajax({  
            type: "POST",  
            url: "<?php echo WP_PLUGIN_URL; ?>/wpspin/ajax.php", 
            data: data,  
            success: function(get){  
                tinyMCE.activeEditor.setContent('');
                if(get=='666') return false;
                if(get=='Information is incorrect [1]' || get=='Information is incorrect [2]') {
                    alert('Please activate WPSpin API!');
                    tinyMCE.activeEditor.setContent(content);
                    jQuery( "#contentload").html('<font color="red">unsuccessful spin</font>');
                } else if( get == 'Server timeout' ) {
                    alert('Server busy, try again later!');
                    tinyMCE.activeEditor.setContent(content);
                    jQuery( "#contentload").html('<font color="red">unsuccessful spin</font>');
                } else {
                    tinyMCE.activeEditor.setContent(get);
                    if(spin_title_r=='on' && jQuery( "#title" ).val()) {
                        var data="goform=1&content="+jQuery.base64.encode(jQuery( "#title" ).val())+"&method=replaceEveryonesFavorites&maxsyns="+jQuery("#maxsyns").val()+"&quality="+jQuery( "#quality" ).val()+"&excluded="+jQuery( "#excluded_words" ).val();
                        //alert(data);
                        jQuery.ajax({  
                            type: "POST",  
                            url: "<?php echo WP_PLUGIN_URL; ?>/wpspin/ajax.php", 
                            data: data,  
                            success: function(get){  
                                if(get=='Information is incorrect [1]' || get=='Information is incorrect [2]' || get == 'Server timeout') {
                                    //alert('Please activate WPSpin API!');
                                    //$( "#contentload").html('<font color="red">unsuccessful spin</font>');
                                } else {
                                    jQuery( "#title" ).val(get);
                                    //$( "#contentload").html('<font color="green">successful spin</font>');
                                }								
                            }  
                        });
                    }
                    jQuery( "#contentload").html('<font color="green">successful spin</font>');
                }								
            }  
        });
        jQuery.fancybox.close();
        return false;  
    }
    
    function buttonIdentify() {

        tinyMCE.activeEditor.setContent(buffer1.replace('selected_tinymce_content',''));
        tinyMCE.activeEditor.setContent(tinyMCE.activeEditor.getContent().replace(buffer2, '<span class="selected_tinymce_content">'+buffer2+'</span>'));
        tinyMCE.activeEditor.selection.select(tinyMCE.activeEditor.dom.select('span.selected_tinymce_content')[0]);
        
        var content = tinyMCE.activeEditor.selection.getContent({format : 'raw'}).replace(/\s+/g, ' ');        
        
        if(content=='' || content=='<p><br mce_bogus="1"></p>') {
            alert('Please highlight some text first!');
            jQuery.fancybox.close();
            return false;
        }
        
        var auto_options = jQuery( ":radio[name=wpspin_auto_options_i]" ).filter(":checked").val();
        var spin_title_i = jQuery( ":checkbox[name=wpspin_spin_title_i]" ).filter(":checked").val();
        if(spin_title_i=='on') { 
            var spin_title='1'; 
        } else { 
            var spin_title='0'; 
        }
        jQuery( "#publishing-action" ).append('<input type="hidden" id="spin_mode" name="spin_mode" value="identify">');
        jQuery( "#publishing-action" ).append('<textarea style="display:none" name="spin_select">'+content+'</textarea>');

        switch( auto_options ) {
            case '1':
                jQuery( "#publishing-action" ).append('<input type="hidden" id="auto_options" name="auto_options" value="1">');
                break;
            case '2':
                jQuery( "#publishing-action" ).append('<input type="hidden" id="auto_options" name="auto_options" value="2">');
                jQuery( "#publishing-action" ).append('<input type="hidden" name="spin_title" value="'+spin_title+'">');
                jQuery( "#publishing-action" ).append('<input type="hidden" id="quality" name="quality" value="'+jQuery( "#quality" ).val()+'">');
                jQuery( "#publishing-action" ).append('<textarea rows="0" cols="0" name="excluded" style="display:none">'+jQuery( "#excluded_words" ).val()+'</textarea>');
                //jQuery.fancybox.close();
                //return false;
                break;
            case '3':
                var spin_publish = jQuery( ":radio[name=wpspin_publish_i]" ).filter(":checked").val();
                jQuery( "#publishing-action" ).append('<input type="hidden" name="auto_options" value="3">');
                jQuery( "#publishing-action" ).append('<input type="hidden" name="spin_title" value="'+spin_title+'">');
                jQuery( "#publishing-action" ).append('<input type="hidden" name="quality" value="'+jQuery( "#quality" ).val()+'">');
                jQuery( "#publishing-action" ).append('<input type="hidden" name="spin_count" value="'+jQuery( "#wpspin_spin_counti" ).val()+'">');
                jQuery( "#publishing-action" ).append('<input type="hidden" name="spin_publish" value="'+jQuery( ":radio[name=wpspin_publish_i]" ).filter(":checked").val()+'">');
                jQuery( "#publishing-action" ).append('<input type="hidden" name="datepick" value="'+jQuery( '#datepicker-i' ).dpGetSelected()+'">');
                jQuery( "#publishing-action" ).append('<textarea rows="0" cols="0" name="excluded" style="display:none">'+jQuery( "#excluded_words" ).val()+'</textarea>');
                //jQuery.fancybox.close();
                //return false;
                break;
        }

        var data="goform=1&content="+jQuery.base64.encode(escape(content))+"&method=identifySynonyms&maxsyns="+jQuery("#maxsynsi").val()+"&quality=1&excluded="+jQuery( "#excluded_words_i" ).val();
        //alert(data);
        
        jQuery( "#contentload").html('please wait ...');
        jQuery.ajax({  
            type: "POST",  
            url: "<?php echo WP_PLUGIN_URL; ?>/wpspin/ajax.php",  
            data: data,  
            success: function(get){  
                if(get=='666') return false;
                if(get=='Information is incorrect [1]' || get=='Information is incorrect [2]') {
                    alert('Please activate WPSpin API!');
                    tinyMCE.activeEditor.selection.setContent(content);
                    jQuery( "#contentload").html('<font color="red">unsuccessful spin</font>');
                } else if( get == 'Server timeout' ) {
                    alert('Server busy, try again later!');
                    tinyMCE.activeEditor.selection.setContent(content);
                    jQuery( "#contentload").html('<font color="red">unsuccessful spin</font>');
                } else {								
                    tinyMCE.activeEditor.selection.setContent(get);
                    if(spin_title_i=='on' && jQuery( "#title" ).val()) {
                        var data="goform=1&content="+jQuery.base64.encode($( "#title" ).val())+"&method=replaceEveryonesFavorites&maxsyns=1&quality=1&excluded="+jQuery( "#excluded_words_i" ).val();
                        jQuery.ajax({  
                            type: "POST",  
                            url: "<?php echo WP_PLUGIN_URL; ?>/wpspin/ajax.php", 
                            data: data,  
                            success: function(get){  
                                if(get=='Information is incorrect [1]' || get=='Information is incorrect [2]' || get == 'Server timeout') {
                                    //alert('Please activate WPSpin API!');
                                    //$( "#contentload").html('<font color="red">unsuccessful spin</font>');
                                } else {
                                    $( "#title" ).val(get);
                                    //$( "#contentload").html('<font color="green">successful spin</font>');
                                }								
                            }  
                        });
                    }
                    jQuery( "#contentload").html('<font color="green">successful spin</font>');	
                }
            }  
        });
        jQuery.fancybox.close();
        return false;            
    }
    
    function horror($go) {
        var max = jQuery( "#wpspin_spin_count" ).val();
        if( $go == 2 ) { 
            jQuery( "#horrors").css("display","block"); 
            jQuery( '#datepicker-r').datePicker({inline:true,selectMultiple:true,numSelectable:10});
            
            jQuery( '.date-pick').datePicker();
        }
        else jQuery( "#horrors").css("display","none");
    }

    function horrori($go) {
        var max = jQuery( "#wpspin_spin_counti" ).val();
        
        if( $go == 3 ) {
            jQuery( "#horrorsi").css("display","block");
            //$( "#maxs").css("display","block");
            jQuery( '#datepicker-i').datePicker({inline:true,selectMultiple:true,numSelectable:10});
            jQuery( '.date-pick').datePicker();
        } else jQuery( "#horrorsi").css("display","none");
        if( $go == 1 ) {
            //$( "#maxs").css("display","none");
            //$( '#ibutton').html('Spin Post!');
        }
        else {
            //$( "#maxs").css("display","block");
            //$( '#ibutton').html('Save');
        }
    }
 
    function dateMaxr() {
        var max = jQuery( "#wpspin_spin_count" ).val();
        jQuery( '#datepicker-r').datePicker({inline:true,selectMultiple:true,numSelectable:10});
        jQuery( '.date-pick').datePicker();
    } 
    function dateMaxi() {
        var max = jQuery( "#wpspin_spin_counti" ).val();
        jQuery( '#datepicker-i').datePicker({inline:true,selectMultiple:true,numSelectable:10});
        jQuery( '.date-pick').datePicker();
    }
        
</script>
<?php
$this->getOptions();
$this->wpDB();
if( $row = $this->wpdb->get_row("SELECT * FROM `".$this->table."` WHERE `id`='".stripslashes( $_GET['post'] )."'", ARRAY_A)) {
    foreach( $row as $a=>$b ) {
        $$a = $b;
    }
}
?>
<a id="rWindow" href="#dialog-form-r"></a>
<a id="iWindow" href="#dialog-form-i"></a>

<div style="display: none;">
<div id='dialog-form-r' title='Auto Spin Mode' style='width:650px;height:540px;overflow:true;'>
    <br><table cellpadding="5" cellspacing="4">
        <tr><td><input type='checkbox' name='wpspin_spin_title_r' <?php if($mode=='replace' AND $spin_title == 1)echo "checked";?>></td><td> Spin Titles</td></tr>
        <tr><td><br><select id='maxsyns' name='maxsyns' style='width: 50px'><?php for($i=1;$i<7;$i++) echo "<option value='$i'>$i</option>"; ?></select></td><td>Max synonyms</td></tr>
        <tr><td>&nbsp</td><td><?php _e('Quality') ?>: <select id='quality' style='width: 80px'>
<?php
    if( $mode=='replace' AND $quality ) $option = str_replace( "value='".$quality."'", "value='".$quality."' selected", "<option value='1'>Good</option><option value='2'>Better</option><option value='3'>Best</option>" );
    else $option = str_replace( "value='".$this->option['wpspin_spin_quality']."'", "value='".$this->option['wpspin_spin_quality']."' selected", "<option value='1'>Good</option><option value='2'>Better</option><option value='3'>Best</option>" );
    echo $option; 
?>
        </select></td></tr>
        <tr><td>&nbsp</td></tr>
        <tr><td><input name="wpspin_auto_options" value="1" onClick="horror(1);" type="radio" checked> </td><td> <?php echo __('Spin Now Once','wpspin'); ?></td></tr>
        <tr><td><input name="wpspin_auto_options" value="2" onClick="horror(1);" type="radio"> </td><td> <?php echo __('Spin When Viewed (Post article will Show New version each time Viewed)','wpspin'); ?></td></tr>
        <tr><td><input name="wpspin_auto_options" onClick="horror(2);" value="3" type="radio"> </td><td> <?php echo __('Schedule Post Updates (Will Update and Replace Posts with New Version)','wpspin'); ?></td></tr>
        <tr><td>&nbsp</td><td><div id='horrors' style='display:none'><br>
        <input type='hidden' id='wpspin_spin_count' value='10'>
        Using calendar below select post updates to schedule (max. 10 dates):
        <br><br><div id="datepicker-r"></div><br>
            <table width="300">
            <tr><td><input name="wpspin_publish" type="radio" value="1" <?php if($this->option['wpspin_publish']!='2') echo "checked";?>> <?php echo __('Auto publish','wpspin'); ?></td><td><input name="wpspin_publish" type="radio" value="2" <?php if($this->option['wpspin_publish']=='2')echo "checked";?>> </td><td> <?php echo __('Publish as Draft','wpspin'); ?></td></tr>
            </table><br>
        </div></td></tr>
    </table>
    <br><?php _e('Excluded words') ?>:<br>
    <textarea rows='10' cols='35' id='excluded_words'><?php if( $mode=='replace' AND $excluded ) echo $excluded; else if( $this->option['wpspin_excluded_words'] ) echo $this->option['wpspin_excluded_words']; ?></textarea>
    <br><br><center>
    <button id='buttonAutoReplace' style='width:110px' class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" onclick='buttonAutoReplace();'><span class="ui-button-text">Spin & Save</span></button> <button id='close-r' class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"><span class="ui-button-text" style='width:60px'>Cancel</span></button>
    </center>
</div>
</div>

<div style="display: none;">
<div id='dialog-form-i' title='Identify Synonyms Mode' style='width:650px;height:540px;overflow:true;'>
    <br><br><table width='200px'><tr><td><input type='checkbox' name='wpspin_spin_title_i' <?php if($mode=='identify' AND $spin_title == 1)echo "checked";?>></td><td>Spin Titles</td></tr></table>
    <table id='maxs' style='display:block' width='500px'><tr><td><br><select id='maxsynsi' style='width: 50px'><?php for($i=1;$i<7;$i++) echo "<option value='$i'>$i</option>"; ?></select></td><td>Max synonyms</td></tr></table>
    <br>
    <table cellpadding="5" cellspacing="4">
        <tr><td><input name="wpspin_auto_options_i" value="1" onClick="horrori(1);" type="radio" checked> </td><td> <?php echo __('Spin Now Once','wpspin'); ?></td></tr>
        <tr><td><input name="wpspin_auto_options_i" value="2" onClick="horrori(2);" type="radio"> </td><td> <?php echo __('Spin When Viewed (Post article will Show New version each time Viewed)','wpspin'); ?></td></tr>
        <tr><td><input name="wpspin_auto_options_i" value="3" onClick="horrori(3);" type="radio"> </td><td> <?php echo __('Schedule Post Updates (Will Update and Replace Posts with New Version)','wpspin'); ?></td></tr>
        <tr><td>&nbsp</td><td><div id='horrorsi' style='display:none'><br>
        <input type='hidden' id='wpspin_spin_counti' value='10'>
        Using calendar below select post updates to schedule (max. 10 dates):
        <br><br><div id="datepicker-i"></div><br>
        <table width="300">
            <tr><td><input name="wpspin_publish_i" type="radio" value="1" <?php if($wpspin_publish!='2')echo "checked";?>> <?php echo __('Auto publish','wpspin'); ?></td><td><input name="wpspin_publish_i" type="radio" value="2" <?php if($wpspin_publish=='2')echo "checked";?>> </td><td> <?php echo __('Publish as Draft','wpspin'); ?>
            </td></tr>
        </table></div>
        </td></tr>
    </table>
    <br><?php _e('Excluded words') ?>:<br>
    <textarea rows='10' cols='35' id='excluded_words_i'><?php if( $mode=='identify' AND $excluded ) echo $excluded; else if( $this->option['wpspin_excluded_words'] ) echo $this->option['wpspin_excluded_words']; ?></textarea>
    <br><br><center>
    <button style='width:110px' id='buttonIdentify' class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" onclick='buttonIdentify();'><span class="ui-button-text"><div id='ibutton'>Spin & Save</div></span></button> <button id='close-i' class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style='width:70px'><span class="ui-button-text">Cancel</span></button>
    </center>
</div>
</div>