$(document).ready(function() {

   $("a.trackit-super, button.trackit-super").click(function(e){
      e.preventDefault();
      var url = location.href;
      if (url.substring(url.length-1)=='#')    // if url ends with '#', then remove it 
         url = url.substring(0, url.length-1); // removing trailing '#' character
                                               // as it would confuse params we append as anchor.
      var id=$(this).attr("tid");  // super tracklet id
      var title=$(this).attr("ttitle");  // super tracklet title
      var premium=0;  // super tracklet (i.e. search) is not premium
      url = replaceQueryString(url,'dr',0);  // reset any date criteria to 'now'
      url = replaceQueryString(url,'start',0); // reset any starting page to 1st page.
      url = replaceQueryString(url,'raw','XML'); // append 'raw' param to get raw Solr's XML dump.
      url = removeQueryString(url,'orig'); // remove 'orig' param to get rid of origin info

      var queryTerm = urlParam('q','<empty-query-term>');
      queryTerm = queryTerm.replace(/\+/g, ' ');
      queryTerm = decodeURIComponent(queryTerm);
      queryTerm = htmlspecialchars(queryTerm);
      var param = '{"searchURL":"'+url+'","term":"'+queryTerm+'","searchterm":"'+queryTerm+'"}';
      Soruka.Search.Dialog.showSearch( id,'a',title,param,premium,0 );
      return false;
   });

   $("a.trackit-a, button.trackit-a").live("click", function(e){
      e.preventDefault();
      var aqid=$(this).attr("tid");
      var item_id=$(this).attr("iid");
      var title=$(this).attr("ttitle");
      var premium=parseInt($(this).attr("premium"));  // whether is premium tracklet
      Soruka.Search.Dialog.showSearch( aqid,'a',title,"{}",premium,item_id );
      return false;
   });
   $("a.trackit-c, button.trackit-c").live("click", function(e){
      e.preventDefault();
      var cqid=$(this).attr("tid");
      var item_id=$(this).attr("iid");
      var title=$(this).attr("ttitle");
      var premium=parseInt($(this).attr("premium"));  // whether is premium tracklet
      Soruka.Search.Dialog.showSearch( cqid,'c',title,"{}",premium,item_id );
      return false;
   });

   $("a.tweetit").live("click", function(e){
      e.preventDefault();
      var url='';
      var title='';
                           // span  // div   // td    // tr    //tbody
      var myparent=$(this).parent().parent().parent().parent().parent(); // "tbody.alertTBody";
                              // tr              // td.alertTitle       // a.frameit             
      var a_frameit=myparent.children(":first").children(":first").children("a.frameit"); // a.frameit

      // if item has url...
      if ((a_frameit.length>0) && (a_frameit[0].tagName=='A')) {
         url=a_frameit.attr('href');
         title=a_frameit.text();
      } else { // if item has *no* url...
                              // tr               // td.alertTitle         // span.alertTitle    
         var spanVal=myparent.children(":first").children(":first").children("span.alertTitle"); // span.alertTitle
         title=spanVal.text();
      }

      var tweeturl = "tweetit.php?t="+encodeURIComponent(title);
      if (url!='')
         tweeturl += "&u="+encodeURIComponent(url);

      var popwin = window.open(tweeturl, 'tweetit', 'toolbar=0, status=0, width=777, height=450'); 
      popwin.focus();
      return false;
   });

   $("a.fbook_post").live("click", function(e){
      e.preventDefault();
                           // span  // div   // td    // tr    //tbody
      var myparent=$(this).parent().parent().parent().parent().parent(); // "tbody.alertTBody";
                              // tr               // td             // a
      var a_frameit=myparent.children(":first").children(":first").children(".frameit"); // a.frameit

      var u=a_frameit.attr('href');
      var t=a_frameit.text();

      var popwin = window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),
                  'fbshare', 'toolbar=0, status=0, width=626, height=436'); 
      popwin.focus();
      return false; 
   });

   function str_replace (search, replace, subject, count) {
    // Replaces all occurrences of search in haystack with replace  
      var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
          f = [].concat(search),
          r = [].concat(replace),
          s = subject,
          ra = r instanceof Array, sa = s instanceof Array;
      s = [].concat(s);
      if (count) {
          this.window[count] = 0;
      }

      for (i=0, sl=s.length; i < sl; i++) {
         if (s[i] === '') {
             continue;
         }
         for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
         }
      }
      return sa ? s : s[0];
   }
}); 
