var clr;
jQuery.noConflict();
$j = jQuery;
$j().ready(
  function() {
      // get the current body bg color and convert it to hex
      syncPicker();
      /*
      var bodyBgColor = $j( 'body' ).css( 'backgroundColor' );  
      if( !$j.browser.msie ){ // ie returns a hex value
      var re  = new RegExp( /rgb\(([\d\s,]*)\)/ );
      var clr = bodyBgColor.replace( re, '$1' );
      clr = clr.split(', ');
      clr = '#'+RGBtoHex( clr[0],clr[1],clr[2] );
      } else {
      clr = bodyBgColor;
      }*/


      // init the color picker and set it to update the DOM on the fly
      if( $j('#colorpicker').length>0 ){
        $j.farbtastic('#colorpicker',
          function(color) {
              $j('body').css('backgroundColor', color);
          }
        ).setColor(clr);
      
        // show the color wheel in a dialog window
        $j('.change-color').click(
          function() {
              $j('#colorpicker-wrapper').show().dialog({
                  width: ($j.browser.msie) ? 223 : 220,
                  resizable: false,
                  close: function(event, ui) {
                      $j('#colorpicker-wrapper').dialog('destroy');
                  }
              });
              return false;
          }
        );
        // bind the save and cancel buttons
        $j('#colorpicker-wrapper .btns input[value="Save"]').click(
          function() {
              syncPicker();
              var surl = $j('.change-color')[0].href;
              $j.ajax({
                  type: "GET",
                  url: surl,
                  data: "color=" + clr.split('#')[1],
                  success: function(msg) {
                      // we are not to worry about a successful save
                  }
              });
              $j('#colorpicker-wrapper').dialog('destroy');
              return false;
          }
        );
        $j('#colorpicker-wrapper .btns input[value="Cancel"]').click(
          function() {
              $j.farbtastic('#colorpicker').setColor(clr);
              $j('#colorpicker-wrapper').dialog('destroy');
              return false;
          }
        );
      }
      
      // bind the art button
      $j('#artButton').click(
      function() {
          artButton();
          return false;
      }
    );

      // get the exact width of the top nav in order to center it
      var w = 0;
      $j('.menu_top li').each(
        function() 
        {
          w += $j(this).width() + 19; // padding
        }
      )
      $j('.menu_top').width(w);
  }
);



//---------------------------------------------------------------------
//  assign the current bg color to a global var and sync the color picker to the same color
//---------------------------------------------------------------------
function syncPicker()
{
  var bodyBgColor = $j( 'body' ).css( 'backgroundColor' );  // ie returns a hex value
  if( !$j.browser.msie ){
    var re  = new RegExp( /rgb\(([\d\s,]*)\)/ );
    clr = bodyBgColor.replace( re, '$1' );
    clr = clr.split(', ');
    clr = '#'+RGBtoHex( clr[0],clr[1],clr[2] );
  } else {
    clr = bodyBgColor;
  }
}



//---------------------------------------------------------------------
//  preload images
//---------------------------------------------------------------------
$j.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    $j("<img>").attr("src", arguments[i]);
  }
}
var imgs = [];
$j( 'img' ).each( 
  function()
  {
    imgs.push( this.src );
  }
);
$j.preloadImages( imgs.join('","') );





function RGBtoHex( r,g,b )
{
  return toHex(r) + toHex(g) + toHex(b);
}
function toHex( n )
{
  if( n==null ) return "00";
  n=parseInt( n ); 
  if( n==0 || isNaN(n) ) return "00";
  n=Math.max( 0, n ); 
  n=Math.min( n, 255 ); 
  n=Math.round( n );
  return "0123456789ABCDEF".charAt((n-n%16)/16) + "0123456789ABCDEF".charAt(n%16);
}