var frm, tymer;

jQuery.noConflict();
$j = jQuery;
$j().ready(
  function()
  {
    //alrt('Under Construction.');  // test client comms
    
    // bind the top nav
    $j('#main-nav li').hover(
      function()
      {
        $j(this).addClass('hover');
      },
      function()
      {
        $j(this).removeClass('hover');
      }
    );
    
    var colheight = 0;
    var news_parts = $j('tr.partials .news .news-wrapper');
    if( news_parts.size() > 1 ){
      news_parts.each(
        function()
        {
          if( $j(this).height() > colheight ) colheight = $j(this).height();
        }
      )
      news_parts.height( colheight+'px' );
    } else {
      news_parts.addClass('noborder');
    }
  }
);




//---------------------------------------------------------------------
//  preload images
//---------------------------------------------------------------------
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}
//$j.preloadImages( "_img/prof-laura.jpg", "_img/prof-myrna.jpg", "_img/prof-ena.jpg", "_img/prof-frances.jpg" );




//---------------------------------------------------------------------
//  client messaging
//---------------------------------------------------------------------
function alrt( msg )
{
  if( $j('body').find('#alrt').length ){
    $j('body').find('#alrt').remove();
    clearTimeout( tymer );
  }
  var bw = $j('body').width();
  $j('body').append( '<div id="alrt"></div>' );
  var aw = $j('#alrt').width();
  var ml = ((bw - aw)/2);
  $j('#alrt').css({marginLeft:ml+'px'});
  //console.log( $j('#alrt').width() );
  var str = '<p>';
      str += msg;
      str += '</p>';
  $j('#alrt').html( str );
  $j('#alrt').append( '<a class="close" onclick="$j(\'#alrt\').remove(); clearTimeout( tymer );"><img src="/_img/btn-alrt-close.png"></a>' );
  $j('#alrt').css({opacity:0,display:"block"});
  $j('#alrt').animate({ opacity: 0.90}, 700 );  
  tymer = window.setTimeout("$j('#alrt').animate({opacity: 0}, 1500, function(){ $j(this).remove() } )",4000);
}



//---------------------------------------------------------------------
//  dbug.log functionality
//---------------------------------------------------------------------
/*
dbug = {
	firebug: false, debug: false, log: function(msg) {},
	enable: function() { if(this.firebug) this.debug = true; dbug.log = console.debug; dbug.log('enabling dbug');	},
	disable: function(){ if(this.firebug) this.debug = false; dbug.log = function(){}; }
}
if (typeof console != "undefined") { // safari, firebug
	if (typeof console.debug != "undefined") { // firebug
		dbug.firebug = true; 
    dbug.enable();
    //if( window.location.href.indexOf("debug=true")>0 ) dbug.enable();
	}
}
*/


//---------------------------------------------------------------------
//  propose a url based on the title given
//---------------------------------------------------------------------
function titleToUrl( $title, $url )
{
  $title.keyup(
    function( e )
    { 
      dbug.log( 'p' );
      if( e.keyCode != 9 || e.keyCode != 11 ){
        var v     = $j(this).val().toLowerCase();
        var regx  = /[\s_:;]+/gi;
        var path  = v.replace( regx, '-' );
        var regx  = /[,.!?&\/\\]+/gi;
        var path  = path.replace( regx, '' );
        $url.val( path );
      }
    }
  );
}


//---------------------------------------------------------------------
// add a highlight method to jquery for the filter stuff
//---------------------------------------------------------------------
jQuery.fn.extend({
  highlight: function( search, insensitive, hclass ){
    //var regex = new RegExp("(<[^>]*>)|(\\b"+ search.replace(/([-.*+?^${}()|[\]\/\\])/g,"\\$1") +")", insensitive ? "ig" : "g"); // the word boundary flag (\\b) is messing with our middle-of the-word-awesome-highlighting mojo
    var regex = new RegExp("(<[^>]*>)|("+ search.replace(/([-.*+?^${}()|[\]\/\\])/g,"\\$1") +")", insensitive ? "ig" : "g");
    return this.html( this.html().replace(regex, 
      function( a, b, c )
      {
        return ( a.charAt(0) == "<" ) ? a : "<strong class=\""+ hclass +"\">" + c + "</strong>";
      }
    ));
  }
});