<?
global $lang;
?>
<fieldset>
	<script>
		$(document).ready(function()
		{
			$("#tabs").tabs();
      
      $( '.img_bg' ).click( function(){        
        $( this ).hide();
        $( '.img_big' ).hide();
      })
      
      $( '#image-list li' ).hover(
        function(){
          $( this ).children( 'div' ).css( { 'display' : 'block', 'margin-top' : '-26px' });
        },
        function(){
          $( this ).children( 'div' ).hide();
        }
      )
		});
    
    function showImage( id )
    {
      var src = document.getElementById( 'img' + id ).src;
      $( '.img_bg' ).show();
      $( '.img_big' ).html( '<img src="' + src + '">' ).show();
      var height  = parseInt( $( '.img_big img' ).css( 'height' ) );
      var width   = parseInt( $( '.img_big img' ).css( 'width' ) );
      $( '.img_big' ).css( { 'height' : height, 'width' : width, 'margin-top' : '-' + height / 2 + 'px', 'margin-left': '-' + width / 2 + 'px' } );
    }

    function saveArticle()
    {
      $.prompt('<?=$lang -> getTrans( 'T_TRWA_ZAPISYWANIE' );?>');
      var id            = $("#idk").val();
      var enabled       = $( ".enabled:checked" ).val();
      var comments      = $( ".comments:checked" ).val();
      var print         = $( ".print:checked" ).val();
      var show_title    = $( ".show_title:checked" ).val();
      var show_date     = $( ".show_date:checked" ).val();
      var show_author   = $( '.show_author:checked' ).val();
      var keep_archive  = $( '.keep_archive:checked' ).val();
      var layout        = $( '#layout' ).val();
      var pages         = '';
      $('input:checkbox:checked').each(
        function() {
          if ( pages != '' )
            pages = pages + ':';
          pages = pages + $(this).val();
        }
      );
      xajax_saveArticle( id , enabled , comments , print , show_title , show_date , pages , show_author , keep_archive, layout );
    }

    function saveArticleTranslation( new_article ) 
    {
      var id            = $("#idk").val();
      var title         = new Object();
      var meta_o        = new Object();
      var meta_d        = new Object();
      var text          = new Object();
      var seo_link      = new Object();
      var keep_archive  = $( '.keep_archive:checked' ).val();
      var version       = $( '#version' ).val();
      <?
      if ( is_array( $this -> _language ) ) foreach ( $this -> _language as $language )
			{
        echo 'title["' . $language['id'] . '"] = $("#title_' . $language['id'] . '").val();' . chr( 13 ) . chr( 10 );
        echo 'meta_o["' . $language['id'] . '"] = $("#meta_description_' . $language['id'] . '").val();' . chr( 13 ) . chr( 10 );
        echo 'meta_d["' . $language['id'] . '"] = $("#meta_keywords_' . $language['id'] . '").val();' . chr( 13 ) . chr( 10 );
        echo 'text["' . $language['id'] . '"] = $("#text_' . $language['id'] . '").val();' . chr( 13 ) . chr( 10 );
        echo 'seo_link["' . $language['id'] . '"] = $("#seo_link_' . $language['id'] . '").val();' . chr( 13 ) . chr( 10 );
      }
      ?>
      var count = 0;
      for ( var klucz in title) count++;

      var i = 0;
      for ( var klucz in title) 
      {
        i++;
        message = false;
        if ( i == count )
          message = true;
        xajax_saveArticleTranslation( klucz , id , title[klucz] , meta_o[klucz] , meta_d[klucz] , text[klucz] , message , keep_archive , version, seo_link[klucz], new_article );
      }
    }
	</script>
	<form method='POST' action='index.php' id='<?=$this -> _id_form;?>' enctype="multipart/form-data" name="article_edit">
    <?
    if ( $this -> _rw == 'add' )
    {
      ?>
      <input type='hidden' name='rw' value='add_new'>
      <input type='hidden' name='<?=base64_encode("time");?>' value='<?=base64_encode(date("Y-m-d H:i:s"));?>'>
      <input type='hidden' id="idk" name='id' value=''>
      <?
    }
    if ( $this -> _rw == 'edit' )
    {
      ?>
      <input type='hidden' name='rw' value='save'>
      <input type='hidden' id="idk" name='id' value='<?=$this -> _id;?>'>
      <input type="hidden" id="version" name="version" value="<?=$this -> _version;?>">
      <?
    }
    ?>    
    <div id="article_edit_left">
      <div id="tabs">
        <ul>
          <?
          if ( is_array( $this -> _language ) ) foreach ( $this -> _language as $language )
          {
            ?>
            <li><a href="#lang_<?=$language['id'];?>"><span><?=$language['name'];?></span></a></li>
            <?
          }
          ?>
        </ul>
        <?
        if ( is_array( $this -> _language ) ) foreach ( $this -> _language as $language )
        {
          ?>
          <script type="text/javascript">
            $(document).ready(function(){
              $( 'textarea#text_<?=$language['id'];?>' ).ckeditor( {
                toolbar : 'MyToolbar',
                height:'250',
                extraPlugins : 'gallery'
              });
            });
          </script>

          <div id="lang_<?=$language['id'];?>">

            <div class="edit_line">
              <label><?=ucfirst( $lang -> getTrans( 'T_TYTUL' ) );?>:</label>
              <div class="input">
                <input type='text' id="title_<?=$language['id'];?>" name='title_<?=$language['id'];?>' value='<?=$this -> secureHTML( $language['title'] );?>'>
              </div>
            </div>

            <div class="edit_line">
              <label><?=ucfirst( $lang -> getTrans( 'T_LINK_SEO' ) );?>:</label>
              <div class="input">
                <input type='text' id="seo_link_<?=$language['id'];?>" name='seo_link_<?=$language['id'];?>' value='<?=$this -> secureHTML( $language['seo_link'] );?>'>
              </div>
            </div>

            <div class="edit_line">
              <label><?=ucfirst( $lang -> getTrans( 'T_META_OPIS' ) );?>:</label>
              <div class="input">
                <textarea id="meta_description_<?=$language['id'];?>" name='meta_description_<?=$language['id'];?>' cols='60' rows='5'><?=$language['meta_description'];?></textarea>
              </div>
            </div>

            <div class="edit_line">
              <label><?=ucfirst( $lang -> getTrans( 'T_META_SLOWA_KLUCZOWE' ) );?>:</label>
              <div class="input">
                <textarea id="meta_keywords_<?=$language['id'];?>" name='meta_keywords_<?=$language['id'];?>' cols='60' rows='5'><?=$language['meta_keywords'];?></textarea>
              </div>
            </div>

            <div class="edit_line">
              <label><?=ucfirst( $lang -> getTrans( 'T_TRESC' ) );?>:</label>
              <div class="input">
                <textarea name='text_<?=$language['id'];?>' id='text_<?=$language['id'];?>' class='text' rows='10' cols='10'><?=$this -> eprint( $language['text'] );?></textarea>
              </div>
            </div>

            <div class="edit_line">
              <label><?=ucfirst( $lang -> getTrans( 'T_GALERIA' ) );?>:</label>
              <div class="input">
                <ul id="image-list">
                  <?
                  $img_count = 0;
                  if ( is_array( $this -> _images ) ) foreach ( $this -> _images as $img )
                  {
                    echo '<li id="li_' . $img['id'] . '">';
                      echo '<div class="delete_img" onClick="xajax_deleteArticleImg(' . $img['id'] . ');"></div>';
                      echo '<img src="../' . $img['src'] . '" id="img' . $img_count . '" onclick="showImage( ' . $img_count . ' )">';
                    echo '</li>';
                    $img_count++;
                  }
                  ?>
                </ul>
                <div class="clearfix" style="margin-bottom: 20px;"></div>

                <?
                if ( $this -> _id )
                {
                  ?>
                  <div id="flash_uploader" style="width: 100%; height: 330px;">You browser doesn't have Flash installed.</div>
                  <?
                }
                ?>              
              </div>
            </div>

            <div class="edit_line">
              <label><?=ucfirst( $lang -> getTrans( 'T_PLIKI' ) );?>:</label>
              <div class="input">
                <ul id="files-list">
                  <?
                  $file_count = 0;
                  if ( is_array( $this -> _files ) ) foreach ( $this -> _files as $file )
                  {
                    echo '<li id="file_li_' . $file['id'] . '">';
                      echo '<div class="file" id="file_id_' . $file['id'] . '">';
                        echo '<div class="manage_file" onClick="manageFile( ' . $file['id'] . ' );"></div>';
                        if ( $file['name'] != '' )
                          echo $file['name'];
                        else
                          echo $file['file'];
                      echo '</div>';
                    echo '</li>';
                    $file_count++;
                  }
                  ?>
                </ul>
                <div class="clearfix" style="margin-bottom: 20px;"></div>

                <?
                if ( $this -> _id )
                {
                  ?>
                  <div id="flash_uploader_files" style="width: 100%; height: 330px;">You browser doesn't have Flash installed.</div>
                  <?
                }
                ?>              
              </div>
            </div>

          </div>
          <?
        }
        ?>
      </div>

      <div class="edit_line">
        <label><?=ucfirst( $lang -> getTrans( 'T_STRONY' ) );?>:</label>
        <div class="input">
          <table class='t_category'>
          <?
            $i=0;
            if ( is_array( $this -> _pages ) ) foreach ( $this -> _pages as $page )
            {
              if ( $i%4 == 0 )
              {
                ?><tr><?
              }
              ?>
                <td style='width: 25%;'>
                  <input type='checkbox' name='pages[]' value='<?=$page['id'];?>' style='width:15px;'
                    <?
                    if ( $this -> _rw == 'edit' && $page['check'] == 1 )
                    {
                      ?>checked='checked'<?
                    }
                    ?>
                  ><?=$page['title'];?>
                </td>
              <?
              if ( $i%4 == 3 )
              {
                ?></tr><?
              }
              $i++;
            }
          ?>
          </table>
        </div>
      </div>
    </div>

    <div id="article_edit_right">
      <div class="edit_line">
        <label><?=ucfirst( $lang -> getTrans( 'T_OPUBLIKOWANY' ) );?>:</label>
        <div class="input">
          <input type="radio" name="enabled" class="enabled" id="enabled_0" value="0" <? if ( !isset( $this -> _enabled ) || $this -> _enabled == 0 ) echo "checked='checked'";?>>
          <?=$lang -> getTrans( 'T_NIE' );?>
          <input type="radio" name='enabled' class="enabled" id="enabled_1" value='1' <? if ( isset( $this -> _enabled ) && $this -> _enabled == 1 ) echo "checked='checked'";?>>
          <?=$lang -> getTrans( 'T_TAK' );?>
        </div>
      </div>

      <div class="edit_line">
        <label><?=ucfirst( $lang -> getTrans( 'T_DRUKUJ' ) );?>:</label>
        <div class="input">
          <input type="radio" name="print" class="print" id="print_0" value="0" <? if ( !isset( $this -> _print ) || $this -> _print == 0 ) echo "checked='checked'";?>>
          <?=$lang -> getTrans( 'T_NIE' );?>
          <input type="radio" name="print" class="print" id="print_1" value="1" <? if ( isset( $this -> _print ) && $this -> _print == 1 ) echo "checked='checked'";?>>
          <?=$lang -> getTrans( 'T_TAK' );?>
        </div>
      </div>

      <div class="edit_line">
        <label><?=ucfirst( $lang -> getTrans( 'T_POKAZ_TYTUL' ) );?>:</label>
        <div class="input">
          <input type="radio" name="show_title" class="show_title" id="show_title_0" value="0" <? if ( !isset( $this -> _show_title ) || $this -> _show_title == 0 ) echo "checked='checked'";?>>
          <?=$lang -> getTrans( 'T_NIE' );?>
          <input type="radio" name="show_title" class="show_title" id="show_title_1" value="1" <? if ( isset( $this -> _show_title ) && $this -> _show_title == 1 ) echo "checked='checked'";?>>
          <?=$lang -> getTrans( 'T_TAK' );?>
        </div>
      </div>

      <div class="edit_line">
        <label><?=ucfirst( $lang -> getTrans( 'T_POKAZ_DATE' ) );?>:</label>
        <div class="input">
          <input type="radio" name="show_date" class="show_date" id="show_date_0" value="0" <? if ( !isset( $this -> _show_date ) || $this -> _show_date == 0 ) echo "checked='checked'";?>>
          <?=$lang -> getTrans( 'T_NIE' );?>
          <input type="radio" name="show_date" class="show_date" id="show_date_1" value="1" <? if ( isset( $this -> _show_date ) && $this -> _show_date == 1 ) echo "checked='checked'";?>>
          <?=$lang -> getTrans( 'T_TAK' );?>
        </div>
      </div>

      <div class="edit_line">
        <label><?=ucfirst( $lang -> getTrans( 'T_POKAZ_AUTORA' ) );?>:</label>
        <div class="input">
          <input type="radio" name="show_author" class="show_author" id="show_author_0" value="0" <? if ( !isset( $this -> _show_author ) || $this -> _show_author == 0 ) echo "checked='checked'";?>>
          <?=$lang -> getTrans( 'T_NIE' );?>
          <input type="radio" name="show_author" class="show_author" id="show_author_1" value="1" <? if ( isset( $this -> _show_author ) && $this -> _show_author == 1 ) echo "checked='checked'";?>>
          <?=$lang -> getTrans( 'T_TAK' );?>
        </div>
      </div>

      <div class="edit_line">
        <label><?=ucfirst( $lang -> getTrans( 'T_WERSJONOWANIE' ) );?>:</label>
        <div class="input">
          <input type="radio" name="keep_archive" class="keep_archive" id="keep_archive_0" value="0" <? if ( !isset( $this -> _keep_archive ) || $this -> _keep_archive == 0 ) echo "checked='checked'";?>>
          <?=$lang -> getTrans( 'T_NIE' );?>
          <input type="radio" name="keep_archive" class="keep_archive" id="keep_archive_1" value="1" <? if ( isset( $this -> _keep_archive ) && $this -> _keep_archive == 1 ) echo "checked='checked'";?>>
          <?=$lang -> getTrans( 'T_TAK' );?>
        </div>
      </div>

      <div class="edit_line">
        <label><?=ucfirst( $lang -> getTrans( 'T_SZABLON' ) );?>:</label>
        <div class="input">
          <select name="layout" id="layout">
            <option value="">---- szablon domyślny ----</option>
            <?
            if ( is_array( $this -> _layouts ) ) foreach ( $this -> _layouts as $layout )
            {
              echo '<option value="' . $layout['id'] . '" ';
                if ( $this -> _layout == $layout['id'] )
                  echo 'selected="selected"';
              echo '>' . $layout['name'] . '</option>';
            }
            ?>
          </select>
        </div>
      </div>
    </div>

    <div class="clearfix"></div>

  </form>
</fieldset>
<div class="img_bg"></div>
<div class="img_big"></div>

<div class="plik_bg"></div>

<div class="plik_edycja">
  
  <div class="edit_line">
    <label style="width: 60px;">Nazwa:</label>
    <div class="input" style="margin-left: 70px;">
      <input type="text" id="name">
    </div>
  </div>
  
  <div class="edit_line">
    <labell style="width: 60px;"></label>
    <div class="input" style="margin-left: 70px;">
      <input type="button" class="btn" value="Zapisz" onClick="xajax_saveArticleFileInfo( jQuery( '#file_edit_id' ).val(), jQuery( '#name' ).val() )">
      <input type="button" class="btn" value="Anuluj" onClick="jQuery( '.plik_bg, .plik_edycja' ).hide();">
    </div>
  </div>
  
  <div class="edit_line">
    <labell style="width: 60px;"></label>
    <div class="input" style="padding: 0; margin-left: 70px;">
      <a class="delete_img_link" onClick="xajax_deleteArticleFile( jQuery( '#file_edit_id' ).val() )">usuń</a>
    </div>
  </div>
  
  <input type="hidden" id="file_edit_id">
</div>

<link rel="stylesheet" href="../resources/plupload/jquery.plupload.queue/css/jquery.plupload.queue.css" type="text/css" media="screen" />

<script type="text/javascript" src="../resources/plupload/plupload.js"></script>
<script type="text/javascript" src="../resources/plupload/plupload.flash.js"></script>
<script type="text/javascript" src="../resources/plupload/jquery.plupload.queue/jquery.plupload.queue.js"></script>
<script type="text/javascript" src="../resources/plupload/i18n/pl.js"></script>


<script type="text/javascript">
  var img_count   = <?=$img_count;?>;
  var file_count  = <?=$file_count;?>;
  <?
  if ( $this -> _id )
  {
    ?>
    $(function() {      
      
      jQuery( '.plik_bg' ).click( function(){        
        jQuery( this ).hide();
        jQuery( '.plik_edycja' ).hide();
      })
      
      $("#flash_uploader").pluploadQueue({
        runtimes : 'flash',
        init : {
          Refresh: function(up) {
            $(".plupload_buttons").css("display", "inline");
            $(".plupload_upload_status").css("display", "inline");
            $(".plupload_start").addClass("plupload_disabled");
            $(".plupload_start").removeClass("plupload_disabled");        
          },

          FileUploaded: function(up, file, response) {        
            data = jQuery.parseJSON(response.response);
            jQuery('#image-list').append('<li id="li_' + data.id_img + '" onmouseover="$( this ).children( \'div\' ).css( { \'display\' : \'block\', \'margin-top\' : \'-26px\' });" onmouseout="$( this ).children( \'div\' ).hide();">'
              + '<div onclick="xajax_deleteArticleImg(' + data.id_img + ');" class="delete_img"></div>' 
              + '<img onclick="showImage( ' + img_count + ' )" src="' + data.data_link + '" id="img' + img_count + '"></li>');
            img_count++;
          }
        },
        url : '../resources/upload.php?article_id=<?=$this -> _id;?>&hash=22292f4756dfed68e8397e356c28b32a',
        chunk_size : '1mb',
        unique_names : true,
        filters : [
          {title : "Obrazki", extensions : "jpg,gif,png,bmp,jpeg"}
        ],
        resize : {width : 1920, height : 1080, quality : 100},
        flash_swf_url : '../resources/plupload/plupload.flash.swf'
      });

      $("#flash_uploader_files").pluploadQueue({
        runtimes : 'flash',
        init : {
          Refresh: function(up) {
            $(".plupload_buttons").css("display", "inline");
            $(".plupload_upload_status").css("display", "inline");
            $(".plupload_start").addClass("plupload_disabled");
            $(".plupload_start").removeClass("plupload_disabled");        
          },

          FileUploaded: function(up, file, response) {           
            data = jQuery.parseJSON(response.response);
            jQuery('#files-list').append('<div class="file" id="file_li_' + data.id_file + '"><div class="manage_file" onClick="manageFile( ' + data.id_file + ' );"></div>' + data.data_link + '</div>' );
            file_count++;
          }
        },
        url : '../resources/upload_files.php?article_id=<?=$this -> _id;?>&hash=22292f4756dfed68e8397e356c28b32a',
        chunk_size : '1mb',
        unique_names : false,
        filters : [
          {title : "Wszystkie pliki", extensions : "*"}
        ],
        flash_swf_url : '../resources/plupload/plupload.flash.swf'
      });
    });
    <?
  }
  ?>
    
  function manageFile( id ) 
  {
    jQuery( '.plik_edycja, .plik_bg' ).show();
    jQuery( '#file_edit_id' ).val( id );
    jQuery( '#name' ).val( '' );
    xajax_articleFileInfo( id );
  }
</script>