/***********************************************************************
/* Javascript file and functions for CB Super Activity Comments Add-On
* Created By Chris Michaelides aka grVulture
* http://www.axxis.gr - info@axxis.gr
* @copyright ajax.js Copyright (C) 2009,2010,2011  Axxis.gr / All rights reserved.
* mootools variation
************************************************************************/
var xmlhttp;
function loadXMLDoc(url, resp, myid, commentid, commentname, dirname)
{
xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for IE7, Firefox, Opera, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
  xmlhttp.onreadystatechange=
        function ()
            {
            if (xmlhttp.readyState==4)
              {// 4 = "loaded"
              if (xmlhttp.status==200)
                {// 200 = "OK"

//==============================================================================
                if (resp=='prepare_comment')
                  {
                var preparedcomment = decodeURIComponent(xmlhttp.responseText);
                var usename = document.getElementById('usename').value;
                var url = dirname+'/components/com_cbactivity/add-on1/helpers/addcomment.php';
  loadXMLDoc(url+'?myid='+myid+'&commentid='+commentid+'&usename='+usename+'&commentname='+encodeURIComponent(preparedcomment), 'save_comment', myid, commentid, preparedcomment, dirname);
                  }
//==============================================================================
                if (resp=='save_comment')
                  {
                          var field = 'mydiv-'+commentid;
                          var button= 'buttondiv-'+commentid;
                          var inputdiv = 'comment-name'+commentid;
                  // process the time display...
                          var currentTime = new Date();
                          var hours = currentTime.getHours();
                          var minutes = currentTime.getMinutes();
                          var suffix = "am";
                          if (hours >= 12) {
                            suffix = "pm";
                            hours = hours - 12;
                          }
                          if (hours == 0) {
                            hours = 12;
                          }
                          if (minutes < 10) minutes = "0" + minutes;
                        
                          var minas = getDateStrWithDOW();
                          
                  // append my comment to the comments div
                          var data = xmlhttp.responseText;
                          var mycommenter = decodeURIComponent(document.getElementById('append-input'+commentid).value);
                          var superaddon_Write = document.getElementById('superaddon_Write').value;
                          now = new Date();
                          now = now.format("isoDateTime");
                          document.getElementById('commentsdiv-'+commentid).innerHTML += "<div class=\"comments_div\" id=\""+data+"\" style=\"display:none;\">"+mycommenter+" <span class=\"activity_infotext\"><span class=\"timeago\" id=\""+now+"\">"+hours+":"+minutes+suffix+" "+minas+"</span></span><br />"+decodeURIComponent(commentname)+"<a style=\"float:right;\" href=\"javascript:delete_comment("+data+")\" title=\"remove this comment\"><img style=\"padding-left:3px;\" src=\""+dirname+"/components/com_cbactivity/add-on1/images/cancel.png\" width=\"10\" /></a></td></tr></table></div>";
                          //$('commentsdiv-'+commentid).htmlText = $('commentsdiv-'+commentid).text;
                          document.getElementById(data).style.display = 'block';
                  // Re-initialize the comment-box for next comment...
                          document.getElementById(field).style.display = 'none';
                          document.getElementById(button).style.display = 'none';
                          document.getElementById(inputdiv).style.fontSize = "10px";
                          document.getElementById(inputdiv).style.color = "#999999";
                          document.getElementById(inputdiv).value = superaddon_Write;
                          document.getElementById(inputdiv).style.display = 'block';
                  }
//==============================================================================
                if (resp=='comment_update')
                  {
                  var data = xmlhttp.responseText;
                  var already = document.getElementById('hidden-div').value;           
                  var appendtocomment = unescape(decodeURIComponent(data));
                  if (already != appendtocomment) {
                    document.getElementById('hidden-div').value = appendtocomment;
                    if (strstr(appendtocomment,'-epomeno-')) {               
                      var data = appendtocomment.split('-epomeno-');
                      var comment = data[0].split('-sxolia-');
                      var actid = data[1].split(','); 
                      var newtime = data[2];
                      var commentsid = data[3].split(','); 
                      if (newtime!='') {document.getElementById('pageopened').value = newtime;}
                      if (comment!='') {
                        for(i=0;i<actid.length;i++) {
                          document.getElementById('commentsdiv-'+actid[i]).innerHTML += comment[i]; //alert(commentsid[i]);
                          if (commentsid!='') document.getElementById(commentsid[i]).style.display = 'block';
                        }
                      }
                    }
                  }
                  setTimeout(commentUpdate, 15000);
                }
//==============================================================================              
                if (resp=='delete_comment')
                  {
                  // Re-initialize the comment-box for next comment...
                          document.getElementById(commentid).style.display = 'none';
                  }
//==============================================================================              
                if (resp=='gr_like') {
                  var superaddon2_UnLike = document.getElementById('superaddon_UnLike').value;
                  var data = xmlhttp.responseText;
                  i = commentid;
                  appenddata = unescape(decodeURIComponent(data));
                  if (document.getElementById('like'+i).style.display!='none') { 
                    document.getElementById('like'+i).innerHTML = appenddata+', '+document.getElementById('like'+i).innerHTML;
                  } else { 
                    document.getElementById('like'+i).innerHTML = appenddata;//+document.getElementById('like'+i).innerHTML;
                    document.getElementById('like'+i).style.display = 'block';
                  }
                  document.getElementById('likespan'+i).innerHTML = '<a href="javascript:gr_unlike('+id+','+i+')">'+superaddon2_UnLike+'</a>';
                }
//==============================================================================              
                if (resp=='gr_unlike') {
                  var superaddon2_Like = document.getElementById('superaddon_Like').value;
                  var data = xmlhttp.responseText;
                  i = commentid;
                  appenddata = unescape(decodeURIComponent(data));  
                  document.getElementById('like'+i).innerHTML = appenddata;
                  document.getElementById('likespan'+i).innerHTML = '<a href="javascript:gr_like('+id+','+i+')">'+superaddon2_Like+'</a>';
                }
//==============================================================================              
                if (resp=='gr_remove')
                  {
                  i = commentid;
                    document.getElementById('activity_inner_'+i).style.display = 'none';                    
                }
//==============================================================================              
                if (resp=='gr_hide')
                  {
                  i = commentid;
                    document.getElementById('activity_inner_'+i).style.display = 'none';                    
                }
//==============================================================================              
            //      document.getElementById('A1').innerHTML=xmlhttp.status;
              //    document.getElementById('A2').innerHTML=xmlhttp.statusText;
                //  document.getElementById('A3').innerHTML=xmlhttp.responseText;
                }
              else
                {
                //alert("Problem retrieving XML data:" + xmlhttp.statusText);// + "|" + resp + "|" + url+'?myid='+myid+'&commentid='+commentid+'&commentname='+preparedcomment+'&dirname='+dirname);
                }
              }
            };
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }
}

if ('function' == typeof window.makeDoubleDelegate) {
  window.onload = makeDoubleDelegate(window.onload, commentUpdate );
} else {
  setTimeout(commentUpdate, 15000);
}

	function commentUpdate(){
     var dirname = document.getElementById('dirname').value;
     var url = dirname+'/components/com_cbactivity/add-on1/helpers/commentupdate.php';

           var updatedata= document.getElementById('updatedata').value;
           var appendarray= encodeURIComponent(document.getElementById('appendarray').value);
           var pageopened= encodeURIComponent(document.getElementById('pageopened').value);

    loadXMLDoc(url+'?updatedata='+updatedata+'&appendarray='+appendarray+'&pageopened='+pageopened, 'comment_update', 0, 0, 0, dirname);
    //myRequest.send('updatedata='+updatedata+'&appendarray='+appendarray+'&pageopened='+pageopened);
  }
// END OF LOADING PAGE FUNCTIONS
// ***************************************************************************************

  function get_commentid(id){
      var dirname = document.getElementById('dirname').value;
      var commentid = id;
      var superaddon_Login = document.getElementById('superaddon_Login').value;
      var superaddon_Write = document.getElementById('superaddon_Write').value;
      var url = dirname+'/components/com_cbactivity/add-on1/helpers/preparecomment.php';
      var commentname = encodeURIComponent(document.getElementById('comment-name'+commentid).value);
  
      var field = 'mydiv-'+commentid;
      var button= 'buttondiv-'+commentid;
      var inputdiv = 'comment-name'+commentid;
      var login = 'login'+commentid;
      
  // if not logged in user...
/*      if (commentname != superaddon_Login) {
        $(login).style.display = 'none';
      }*/
      
  // if no comment yet...
      if (commentname=='') {
        var browser=navigator.appName;
        var commentshow = document.getElementById('commentshow-'+commentid).value;
        document.getElementById(field).style.display = 'none';
        document.getElementById(button).style.display = "none"; // let's go for this instead
        document.getElementById(inputdiv).style.fontSize = "10px";
        document.getElementById(inputdiv).style.color = "#999999";
        document.getElementById(inputdiv).value = superaddon_Write;
        //alert (document.activeElement.focus);
        if (commentshow=="block") {
          document.getElementById(inputdiv).style.display = 'block';
          if (browser=="Microsoft Internet Explorer") document.getElementById('commentshow-'+commentid).value = "none";
        } else {
          document.getElementById(inputdiv).style.display = 'none';
          if (browser=="Microsoft Internet Explorer") document.getElementById('commentshow-'+commentid).value = "block";
        }
      } else {
        var pressed = document.getElementById('button-pressed'+commentid).value;
  
  //if button clicked...      
        if (pressed=='PRESSED') {
  
  // prepare elements...
          document.getElementById('button-pressed'+commentid).value = "button-pressed"+commentid;    
  
  // make the AJAX call...
            var myid = document.getElementById('myid').value;

          loadXMLDoc(url+'?commentname='+commentname, 'prepare_comment', myid, commentid, commentname, dirname);
          //myRequest.send('myid='+myid+'&commentid='+commentid+'&commentname='+commentname);

        }
      }
  }

  function showfield(id){
      var browser=navigator.appName;
      var commentid = id;
      var superaddon_Login = document.getElementById('superaddon_Login').value;
      var superaddon_Write = document.getElementById('superaddon_Write').value;
      var commentname = document.getElementById('comment-name'+commentid).value;
      var field = 'mydiv-'+commentid;
      var button= 'buttondiv-'+commentid;
      var inputdiv = 'comment-name'+commentid;
      var login = 'login'+commentid;
      if (document.getElementById(inputdiv).style.display=='none') {
        if (browser=="Microsoft Internet Explorer") document.getElementById('commentshow-'+commentid).value = "block";
        if (commentname != superaddon_Login) {
          document.getElementById(field).style.display = 'block';
          document.getElementById(button).style.display = 'block';
          document.getElementById(inputdiv).style.fontSize = "14px";
          document.getElementById(inputdiv).style.color = "#000000";
          document.getElementById(inputdiv).value = "";
          document.getElementById(inputdiv).style.display = 'block';
          var xx = document.getElementById(inputdiv).focus();
        } else {
          document.getElementById(login).style.display = 'block';
    // calling an anonymous function that calls the function you need with your desired parameters! SWEEEET, but head-banger to figure out!
        	setTimeout(function(){get_commentid(commentid)}, 4000);
        }
      } else {
        if (commentname == superaddon_Write) {
          if (browser=="Microsoft Internet Explorer") document.getElementById('commentshow-'+commentid).value = "block";
          document.getElementById(field).style.display = 'block';
          document.getElementById(button).style.display = 'block';
          document.getElementById(inputdiv).style.fontSize = "14px";
          document.getElementById(inputdiv).style.color = "#000000";
          document.getElementById(inputdiv).value = "";
          document.getElementById(inputdiv).style.display = 'block';
          var xx = document.getElementById(inputdiv).focus();
        }
      }
  }
  
  function showmore(id){
      var commentid = id;
      var more = 'more'+commentid;
      var show = 'show'+commentid;
      var hide = 'hidecomments'+commentid;
      document.getElementById(show).style.display = 'none';
      document.getElementById(more).style.display = 'block';
      document.getElementById(hide).style.display = 'block';
  }
  function hidemore(id){
      var commentid = id;
      var more = 'more'+commentid;
      var show = 'show'+commentid;
      var hide = 'hidecomments'+commentid;
      document.getElementById(hide).style.display = 'none';
      document.getElementById(more).style.display = 'none';
      document.getElementById(show).style.display = 'block';
  }
  
  function delete_comment(id){
      var dirname = document.getElementById('dirname').value;
      var commentid = id;
      var url = dirname+'/components/com_cbactivity/add-on1/helpers/deletecomment.php';
          
        loadXMLDoc(url+'?commentid='+commentid, 'delete_comment', 0, commentid, 0, dirname);
        //myRequest.send('commentid='+commentid);
  }
  
  function gr_like(id,i){
      var dirname = document.getElementById('dirname').value;
      var commentid = id;
      var myid = document.getElementById('myid').value;
      var url = dirname+'/components/com_cbactivity/add-on2/like.php';
        loadXMLDoc(url+'?myid='+myid+'&commentid='+commentid, 'gr_like', 0, i, 0, dirname);
  }
  
  function gr_unlike(id,i){
      var dirname = document.getElementById('dirname').value;
      var commentid = id;
      var myid = document.getElementById('myid').value;
      var commentname = encodeURIComponent(document.getElementById('like'+i).innerHTML);
      var url = dirname+'/components/com_cbactivity/add-on2/unlike.php';
        loadXMLDoc(url+'?myid='+myid+'&commentid='+commentid+'&commentname='+commentname, 'gr_unlike', 0, i, 0, dirname);
  }
  
  function gr_remove(id,i){
      var dirname = document.getElementById('dirname').value;
      var commentid = id;
      var url = dirname+'/components/com_cbactivity/add-on2/remove.php';
        loadXMLDoc(url+'?commentid='+commentid, 'gr_remove', 0, i, 0, dirname);
  }
  
  function gr_hide(id,i){
      var dirname = document.getElementById('dirname').value;
      var commentid = id;
      var myid = document.getElementById('myid').value;
      var url = dirname+'/components/com_cbactivity/add-on2/hide.php';
        loadXMLDoc(url+'?myid='+myid+'&commentid='+commentid, 'gr_hide', 0, i, 0, dirname);
  }
  
  function get_ready(id){
      var commentid = id;
      var field = 'mydiv-'+commentid;
          document.getElementById(field).style.display = 'none';
      var imagediv = 'imagediv-'+commentid;
      var inputdiv = 'button-pressed'+commentid;
      document.getElementById(inputdiv).value = "PRESSED";
      get_commentid(commentid);
  }
  
  function GetDay(intDay){
      var DayArray = new Array("Sunday", "Monday", "Tuesday", "Wednesday", 
                           "Thursday", "Friday", "Saturday");
      return DayArray[intDay];
  }
  
  function GetMonth(intMonth){
      var MonthArray = new Array("January", "February", "March",
                                 "April", "May", "June",
                                 "July", "August", "September",
                                 "October", "November", "December"); 
      return MonthArray[intMonth]; 	  	 
  }
  
  function getDateStrWithDOW(){
      var today = new Date();
      var year = today.getYear();
      if(year<1000) year+=1900;
      var todayStr = GetMonth(today.getMonth()) + " " + today.getDate();
      todayStr +=  ", " + GetDay(today.getDay());
      return todayStr;
  }

function whichBrs() {
var agt=navigator.userAgent.toLowerCase();
if (agt.indexOf("opera") != -1) return 'Opera';
if (agt.indexOf("staroffice") != -1) return 'Star Office';
if (agt.indexOf("webtv") != -1) return 'WebTV';
if (agt.indexOf("beonex") != -1) return 'Beonex';
if (agt.indexOf("chimera") != -1) return 'Chimera';
if (agt.indexOf("netpositive") != -1) return 'NetPositive';
if (agt.indexOf("phoenix") != -1) return 'Phoenix';
if (agt.indexOf("firefox") != -1) return 'Firefox';
if (agt.indexOf("safari") != -1) return 'Safari';
if (agt.indexOf("skipstone") != -1) return 'SkipStone';
if (agt.indexOf("msie") != -1) return 'Internet Explorer';
if (agt.indexOf("netscape") != -1) return 'Netscape';
if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
if (agt.indexOf('\/') != -1) {
if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
return navigator.userAgent.substr(0,agt.indexOf('\/'));}
else return 'Netscape';} else if (agt.indexOf(' ') != -1)
return navigator.userAgent.substr(0,agt.indexOf(' '));
else return navigator.userAgent;
}

   function sleepy(naptime){
      naptime = naptime * 1000;
      var sleeping = true;
      var now = new Date();
      var alarm;
      var startingMSeconds = now.getTime();
      //alert("starting nap at timestamp: " + startingMSeconds + "\nWill sleep for: " + naptime + " ms");
      while(sleeping){
         alarm = new Date();
         alarmMSeconds = alarm.getTime();
         if(alarmMSeconds - startingMSeconds > naptime){ sleeping = false; }
      }      
      //alert("Wakeup!");
   } 

function strstr (haystack, needle, bool) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: strstr('Kevin van Zonneveld', 'van');
    // *     returns 1: 'van Zonneveld'
    // *     example 2: strstr('Kevin van Zonneveld', 'van', true);
    // *     returns 2: 'Kevin '
    // *     example 3: strstr('name@example.com', '@');
    // *     returns 3: '@example.com'
    // *     example 4: strstr('name@example.com', '@', true);
    // *     returns 4: 'name'
 
    var pos = 0;
    
    haystack += '';
    pos = haystack.indexOf( needle );
    if (pos == -1) {
        return false;
    } else{
        if (bool){
            return haystack.substr( 0, pos );
        } else{
            return haystack.slice( pos );
        }
    }
}
