var frm, tymer;

jQuery.noConflict();
$j = jQuery;
$j().ready(
  function()
  {
    //alrt('Under Construction.');  // test client comms
    
    // bind the top nav
    $j('#topmenu li').hover(
      function()
      {
        $j(this).addClass('hover');
      },
      function()
      {
        $j(this).removeClass('hover');
      }
    );
    
    // bind tabbed UI's
    $j('.tabbed .tabs a').click(
      function()
      {
        $j( this ).parent().siblings().find('a').removeClass( 'selected' ).end().end().find('a').addClass('selected').blur();
        var href = this.href.split('#');
        if( href.length > 1 ){
          href = '#'+href[1];
          var $temp = $j( href ).parent().siblings().removeClass( 'selected' ).end().addClass( 'selected' );
        }
        return false;
      }
    );
    // enable the default selected tabs
    $j('.tabbed .tabs a.selected').each(
      function()
      {
        $j( $j(this).attr('href') ).parent().addClass( 'selected' );
      }
    );
    
      
    $j('a.modal').fancybox({
      onClosed: function()
      {
        $j('body').trigger('fancybox-closed');
      }
    });
    $j('a.youtube').fancybox();
    
  }
);

function adjustTickerHeight()
{
  return;
  var h = $j('.slot-1 .col-1').height();
  if( h > 0 ) $j('.ticker').height( (h-70)+'px' );
}




//---------------------------------------------------------------------
//  preload images
//---------------------------------------------------------------------
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}




//---------------------------------------------------------------------
//  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() } )",8000);
}



//---------------------------------------------------------------------
//  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 = function( msg ){ console.log.apply(console,arguments) };
              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 )
    { 
      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>";
      }
    ));
  }
});
