/* Additional Javascript only for BTX */
var $j = jQuery.noConflict();

$j(function(){

  // Sidebar Search
  
  $j('#sidebar-search').focus(function(){
    if($j(this).val('Search Templates')){ $j(this).val(''); }
  })
  
  // Removed because of translations
  /*
	$j('#sidebar-search').blur(function(){
    if($j(this).val('')){ $j(this).val('Search Templates'); }
  })
  
  $j('#template-search-top').focus(function(){
    if($j(this).val('Search Templates')){ $j(this).val(''); }
  })
	$j('#template-search-top').blur(function(){
    if($j(this).val('')){ $j(this).val('Search Templates'); }
  })
  */
  
  // Truncate
  // Below was conflicting with decode script
  // moved to the end of individual decode scripts on search_results.js, detail.js, and index.js
  //------------------------------------------------------------------------------------------//
  // $j('#top-downloads .shortenize').truncate(30);
  // $j("#whats-new .shortenize, #search-results .search-desc").truncate( 150, {
  // $j("#whats-new .shortenize, #search-results .search-desc").truncate( 150, { 
  //  chars: /\s/
  // })
  
  // Popup the submit a template window
  $j('a.popup').bind('click', function(){
  	newwindow=window.open($j(this).attr('href'),'submitProcess','width=750,height=600,toolbar=no,location=no,scrollbars=yes');
  	if(window.focus){ newwindow.focus(); }
  	return false;
  })
  
  // TOP DOWNLOADS
  // decode and and truncate
  //----------------------//
  $j('#top-downloads .shortenize').truncate( 40, { 
     chars: /\s/
   })
  // Decode taken out 2009-06-18
  // $j('#top-downloads .shortenize').each(function(){
  //  URLDecode2(this.id);
  // })
  
  // DECODE
  // modified from http://www.albionresearch.com/misc/urlencode.php
  //-------------------------------------------------------------//
  function URLDecode2(ele)
  {
     // Replace + with ' '
     // Replace %xx with equivalent character
     // Put [ERROR] in output if %xx is invalid.
     var HEXCHARS = "0123456789ABCDEFabcdef"; 
     var encoded = $j('#'+ele).html();
  
     var plaintext = "";
     var i = 0;
     while (i < encoded.length) {
         var ch = encoded.charAt(i);
  	   if (ch == "+") {
  	       plaintext += " ";
  		   i++;
  	   } else if (ch == "%") {
  			if (i < (encoded.length-2) 
  					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
  					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
  				plaintext += unescape( encoded.substr(i,3) );
  				i += 3;
  			} else {
  				alert( 'Bad escape combination near ...' + encoded.substr(i) );
  				plaintext += "%[ERROR]";
  				i++;
  			}
  		} else {
  		   plaintext += ch;
  		   i++;
  		}
  	} // while
  	 // alert(encoded+'          '+plaintext);
     // document.URLForm.F1.value = plaintext;
     // document.URLForm.F1.select();
     
     // replace text and truncate
     $j('#'+ele).html(plaintext).truncate( 30, { 
       chars: /\s/
     })
  };
  
})
