function putsmile(smile) {
   document.getElementById('add_text').value=document.getElementById('add_text').value+'*'+smile+'* ';
}
function putsmiles()  {
   var x='';
   for (var i=1; i<264; i++) {
      x=x+'<img onclick="putsmile('+i+')" src="http://img.skoda-virt.cz/x-smajlici/'+i+'.gif" alt="*'+i+'*"  /> ';
   }
   $('smajls4put').update(x);
}

   function seleSelected(ele) {
      if (ele.value!='0') {
         if (ele.value.indexOf('unreaddel')>-1) {
            if (confirm('Opravdu smazat nečtené?')) {
                  location.href=ele.value;
            }
         } else {
            if (document.getElementById('oinw').checked) {
               window.open(ele.value+'#komentare','','');
            } else {
               location.href=ele.value+'#komentare';
            }
         }
      }
   }

   function urgo(s)
                     {
                        if (s.options[s.selectedIndex].value && s.options[s.selectedIndex].value!="-")
                        {
                        a = s.options[s.selectedIndex].value.split ('|');
                        if (a[0]==0) {
                           if ((a[1]==-1 && confirm('Chcete zrušit VŠECHNY nečtené ve všech sekcích?')) || (a[1]!=-1 && confirm('Chcete zrušit nečtené?'))) location.href="unreaddel.php?id1="+a[1]+"&id="+a[2]+"&id2="+a[3]+"&id3="+a[4];
                           }
                        else
                        {
                        if (document.getElementById('oinw').checked)
                           window.open("index.php?id="+a[0]+"&id2="+a[1]+"&id3="+a[2]+"&oinw=1",'','');
                           else location.href="index.php?id="+a[0]+"&id2="+a[1]+"&id3="+a[2];
                        }
                        }
                     }
      function fillurs(keys,urvals)
         {
            for (var x=0; x<keys.length; x++)
               {
                  var i = keys[x];
                  var lastSectionIndex = 0;
                  for (var j=1; j<=urvals[i].length; j++)
                     {
                     if (urvals[i][j-1][0]=='') {
                        // document.getElementById('sele'+i).options[j]=document.createElement('optgroup');
                        // document.getElementById('sele'+i).options[j].value = urvals[i][j-1][1];
                        document.getElementById('sele'+i).options[j]=new Option(urvals[i][j-1][1],urvals[i][j-1][0]);

                     } else {
                        document.getElementById('sele'+i).options[j]=new Option(urvals[i][j-1][1],urvals[i][j-1][0]);
                     }
                     if (document.getElementById('sele'+i).options[0].value==urvals[i][j-1][0]) {
   //                        document.getElementById('sele'+i).selectedIndex = lastSectionIndex;
                     }
                     if (urvals[i][j-1][0]=='') {
                        lastSectionIndex = j;
                        document.getElementById('sele'+i).options[j].style.fontWeight='bold';
                        document.getElementById('sele'+i).options[j].style.color='#FF6600';
                        }
                     }
               }
         }

      function addUnreadOptions(keys,urvals,baseUrls) {
         var selectElementExists = 0;

         for (var i=0; i<urvals.length; i++) {
            selectElementExists = 0;
            for (var j=0; j<urvals[i].length; j++) {
               var optGroup = 0;
               for(var k=0; k<urvals[i][j].length; k++) {

                  var item = urvals[i][j][k];

                  if (!selectElementExists) {
                     var selectElement = $('sele'+item[0]);
                     selectElementExists = 1;
                  }


                  if (!optGroup) {
                     optGroup = document.createElement('optgroup');
                     optGroup.label = item[3];
                     // optGroup.title = item[3];
                  } else {
                     ele = document.createElement('option');
                     Element.extend(ele);
                     ele.update(item[3]);
                     ele.value = baseUrls[item[0]][item[1]]+item[2];
                     optGroup.appendChild(ele);
                  }

               }
               if (selectElementExists) {
                  selectElement.appendChild(optGroup);
               }
            }
         }
         /*
         for (var x=0; x<keys.length; x++) { // sekce
            var i = keys[x];
            selectElement = $('sele'+i);
            var lastSectionIndex = 0;
            var optGroup = 0;
            for (var j=0; j<urvals[i].length; j++) { // skupiny

               for(var k=1; k<urvals[i][j].length; k++) {
               }

               if (optGroup) {
                  selectElement.appendChild(optGroup);
               }
            }
         }
         * */
      }

   function checksf(f)
      {
         return !(f.searchstr.value=='');
      }
   function putText(s)
      {
         document.getElementById('add_text').focus();
         document.getElementById('add_text').value=document.getElementById('add_text').value+"<b><i>-> "+s+': </i></b>';
      }
   function putText2(s)
      {
         document.getElementById('add_text').focus();
         document.getElementById('add_text').value=document.getElementById('add_text').value+s;
      }

   function putPostLink(pId, jmeno)
      {
         document.getElementById('add_text').focus();
         document.getElementById('add_text').value += "<b><i>-> "+jmeno+' [RE'+pId+']: </i></b>';
      }

   function replaceAll(txt,what,wth) {
      var text = txt;
        var regexp = new RegExp(what);
        var repl = wth;
      var i = text.search(regexp);
      while(i > -1) {
         text = text.replace(regexp,repl);
         i = text.search(regexp);
      }
      return text;
   }

   function putText3(s,t)
      {
         document.getElementById('add_text').focus();
         document.getElementById('add_text').value=document.getElementById('add_text').value+"<i><b>-> "+s+"</b> "+t+": </i>";
      }
   function putCite(s,t,id) {
        document.getElementById('add_text').focus();
        var text = replaceAll(document.getElementById('prispevek_text_'+id).innerHTML,'<img[^>]*smajlici/([0-9]*)\.[^>]*>',"*$1*");
        text = replaceAll(text,'<IMG[^>]*smajlici/([0-9]*)\.[^>]*>',"*$1*");
        text = replaceAll(text,'<([/]?)strong>',"<$1b>");
        text = replaceAll(text,'<([/]?)em>',"<$1i>");
        text = replaceAll(text,'<br>',"");
        text = replaceAll(text,'<a[^>]*href="([^"]*)"[^>]*>[^<]*</a>',"$1");
        text = replaceAll(text,'<[/]?a[^>]*>',"");
        text = replaceAll(text,'&nbsp;'," ");

        /*
        var curPos = 0;
        while (text.indexOf('<cite>', curPos) > -1) {
           var pos =  text.indexOf('<cite>', curPos);
           curPos = pos + 1; 
        }*/

      document.getElementById('add_text').focus();
                document.getElementById('add_text').value=document.getElementById('add_text').value
         +"<cite><i><b>"
         +s
         +"</b> "
         +t
         +" napsal: </i>"+text
         +"</cite>";

   }

   function inpageIdCardDom(uid,htmlId) {
         // alert(navigator.appName);
        contentElementId = 'idcard_content_'+htmlId+'_block';
        var contentElement = $('idcard_content_'+htmlId);

        contentElement.childElements().each(function (ele) { ele.remove(); });
        contentElement.style.marginTop = '-150px';
        contentElement.style.marginLeft = '-100px';
        contentElement.style.display='block';
        contentElement.addClassName('inpage-idcard');

        ele2 = new Element('a', { 'class' : 'btn-small btn-close' });
        Element.observe(ele2, 'click', function() { this.ancestors()[1].hide(); this.ancestors()[1].childElements().each(function (ele) { ele.remove(); } ); } );
        ele2.update('<span>zavrit kartu</span>');

        ele = new Element('span', { 'class' : 'closebutton inpage-idcard-header' });
        ele.appendChild(ele2);

        contentElement.appendChild(ele);

        ele = new Element('span', { 'id': contentElementId, 'class' : 'inpage-idcard-content' });
        contentElement.appendChild(ele);

        new Ajax.Updater(contentElementId,
               '/cz/membership/idcard.ajax.php?uid='+uid,
                  {
                     method:'post',
                     postBody:'uid='+uid,
                     encoding:'WINDOWS-1250',
                     onComplete: function() {
                        $(contentElementId).style.backgroundImage = 'none';
                     }
                  });
   }



   function inpageIdcard(uid,element,contentElement) {
         // alert(navigator.appName);
         $(element).style.display='block';
         if (document.getElementById(contentElement).innerHTML=='')
         {
            new Ajax.Updater(contentElement,
               '/cz/membership/idcard.ajax.php?uid='+uid,
                  {
                     method:'post',
                     postBody:'uid='+uid,
                     encoding:'WINDOWS-1250',
                     onComplete: function() {
                        document.getElementById(contentElement).style.backgroundImage = 'none';
                        if (navigator.appName!='Netscape') {
                           // new Draggable(element,{revert:false});
                           //
                        }
                     }
                  });
         }
         // Effect.Appear(element,{duration: 0.5});

         if (navigator.appName=='Netscape') {
            // new Draggable(element,{revert:false});
            // new Draggable(element,{revert:false});
         } else {
            // document.getElementById(element).style.display='block';
         }

   }

   function inpageIdcardClose(element) {
         if (navigator.appName=='Netscape') {
         Effect.Fade(element,{duration: 0.5});
      } else {
         document.getElementById(element).style.display='none';

      }
   }

   function prispevekEdit(urlRoot, prispevekId) {

      element = $('prispevek_text_'+prispevekId);
      element.style.display = 'block';
      element.style.height = element.getHeight()+'px';
      editHeight = element.getHeight();
      element.oldValue = element.innerHTML;
      element.update('načítám formulář...');


         new Ajax.Request(urlRoot+'skripty/load_prispevek_text.php?id_prispevek='+prispevekId,{
            method: 'get',
            onComplete: function (t) {
               element.update('');
               element.style.height = 'auto';


            aText = t.responseText;


            form = document.createElement('form');

            ele = document.createElement('textarea');
            ele.setAttribute('name','newtext'+prispevekId);
            ele.setAttribute('id','newtext'+prispevekId);
            ele.setAttribute('cols','60');
            ele.style.width="100%";
            ele.setAttribute('rows','10');
            ele.value = aText;
            if (editHeight>50) {
               ele.style.height = editHeight+'px';
            }
            form.appendChild(ele);
            ele = document.createElement('br');
            form.appendChild(ele);
            ele = document.createElement('input');
            ele.setAttribute('type','button');
            ele.setAttribute('value','OK');
            ele.setAttribute('id','update_prispevek_'+prispevekId);

            Element.observe(ele,'click', function(event) {
               Event.element(event).value = 'Ukládám...';
               Event.element(event).disable();
               var params = $H();
               params.set('id_prispevek',prispevekId);
               params.set('newtext',$('newtext'+prispevekId).value);

               new Ajax.Request(urlRoot+'skripty/update_prispevek_text.php', {
                  method: 'post',
                  parameters: params,
                  encoding: 'windows-1250',
                  onComplete: function(t) {
                     // alert(t.responseText);
                     element = $('prispevek_text_'+prispevekId);
                     element.update(t.responseText);
                  }
               });

            });

            form.appendChild(ele);
            ele = document.createElement('input');
            ele.setAttribute('type','button');
            ele.setAttribute('value','Zrušit editaci');

            Element.observe(ele,'click', function(event) {
               element.innerHTML = element.oldValue;
            });

            form.appendChild(ele);


            element.appendChild(form);
         }
      });

   }



function prispevekVote(prispId, vote)
{
   var params = $H();
   params.set('id_prispevek',prispId);
   params.set('vote',vote);

   new Ajax.Request(urlRoot+'skripty/prispevek_vote.php', {
                  method: 'post',
                  parameters: params,
                  encoding: 'windows-1250',
                  onComplete: function(t) {
                     alert(t.responseText);
                  }
               });


}



function inPagePopupShow() {
   $('inpage-popup').style.paddingTop =
      (
         document.viewport.getScrollOffsets()[1]
         // + (document.viewport.getDimensions().height/2)
         // - 200
         + 40
         )
      +'px';
   $('inpage-popup').style.height = document.body.clientHeight + 'px';
   $('inpage-popup').show();
   $('inpage-popup-content').style.backgroundImage =  'url(\'http://img.skoda-virt.cz/grafika/urloading.gif\')';

}
function inPagePopupHide() {
   $('inpage-popup').hide();
   $('inpage-popup-content').update('');
}

function inPagePopupLoaded() {
   $('inpage-popup-content').style.backgroundImage = '';
}


function loadPostChanges(id) {
   url = urlRoot + 'skripty/post-changes.php?id_prispevku=' + id;
   new Ajax.Updater('inpage-popup-content', url,
      {
         onComplete: function() {
                        inPagePopupLoaded();
                     }
         });
   inPagePopupShow();
   
}


function idCardAjax(uid) {
        new Ajax.Updater('inpage-popup-content',
               urlRoot + 'membership/idcard.ajax.php?uid='+uid,
                  {
                     method:'post',
                     postBody:'uid='+uid,
                     encoding:'WINDOWS-1250',
                     onComplete: function() {
                        inPagePopupLoaded();
                     }
                  });
         inPagePopupShow();

}


Event.observe(document,'dom:loaded', function(event) {
$$('.prispevek_text cite').each(function(cite) {
   if (1 || cite.up().up().hasClassName('prispevek_text')) {
   cite.style.overflow = 'hidden';
   cite.style.height = '24px';
   cite.style.cursor = 'pointer';
   if (cite.down('br')) {
      cite.down('br').style.display = 'none';
   }
   cite.observe('click', function(event) {
      // alert('x');
      Event.findElement(event, 'cite').style.height = 'auto';
      Event.findElement(event, 'cite').style.overflow = 'none';
      Event.findElement(event, 'cite').style.cursor = 'normal';
      if (Event.findElement(event, 'cite').down('br')) {
         Event.findElement(event, 'cite').down('br').style.display = 'inherit';
      }
      
      
   });
   }

});
});
