/*
** we're using the jQuery based javascript, but our design pattern will be
** the Yui object paradigm which is much better at stability and 
** expandability.
*/
var Remax = (("undefined" == typeof Remax) || (!Remax)) ? {} : Remax;

/*
** Extend the Remax object with the rotation.
*/
Remax.Rotation = function ()
{
  /*  
  ******************************************************************
  ** private variables available only to methods of the module.
  */

  /*  
  ** private shorthand variables to comon YUI/Prototype parts/utilities
  */

  /*  
  ** private shorthand variable for built YUI objects. set in init()
  */
  
  /*  
  ** constants used in this module.
  */
  var start_small = { height:	'0px'
		    , width:	'0px'
		    , opacity:	0
		    , top:	'120px'
		    , left:	'75px'
		    }
    , start_right = { width:	'151px'
		    , opacity:	0
		    , left:	'151px'
		    }
    , shrink_up	= { opacity:	  0
		  , height:	  '0px'
		  , top:	  '0px'
		  , left:	  '75px'
		  , paddingLeft:  '0px'
		  , paddingRight: '0px'
		  , paddingTop:	  '0px'
		  , paddingBottom:'0px'
		  }
    , slide_off	= { opacity:  0
		  , left:     '-151px'
		  }
    , grow_up = { opacity:  1
		, top:      '0px'
		}
    , slip_on = { opacity:  1
		, left:	    '0px'
		}
    ;
  /*  
  ** module state:
  */
  var once  = false
    , index = 0
    , goup  = false
    ;
  /*  
  ** these are Dom refrences that won't change, they are set by direct
  ** calls when needed, and then used freely after that.
  */
  var rt_top  = null
    , rt_bot  = null
    , tt_top  = null
    , tt_bot  = null
    , images  =	[]
    , texts   =	[]
    , heights = []
    , widths  = []
    , lefts   = []
    , tops    = []
    ;
  /*  
  ** ******************************************************************
  ** private methods available only to other methods of the module.
  */
  var swap = function (go, come)
  {
    var	grow	  = grow_up
      ,	img_out	  = images[go]
      , img_in	  = images[come]
      , text_out  = texts[go]
      , text_in	  = texts[come]
      ;
    grow.paddingTop	= tops[come];
    grow.paddingBottom	= tops[come];
    grow.paddingLeft	= lefts[come];
    grow.paddingRight	= lefts[come];
    grow.width		= widths[come];
    grow.height		= heights[come];

    $(img_in  ).css(start_small);
    $(img_out ).animate(shrink_up,  'slow');
    $(img_in  ).animate(grow,	    'slow');
    $(text_out).slideToggle('slow');
    $(text_in ).slideToggle('slow');
  };

  var switcher = function ()
  {
    old = index;

    index++;

    if (that.last <= index)
    {
      index = 0;
    }
    swap(old, index);
  };

  var that =
    /*
    ** publicly accessible variables where we store AJAX results by
    ** evaluating valid js pointing to these arrays.
    */
    { data:	[]  // Remax.Rotation.data[]  holds data for rotations.
    , linkurl:	''  // Remax.Rotation.linkurl holds the url for links
    , last:	0   // Remax.Rotation.last    holds the last index into data

      /*
      ** Public Remax.Rotation.switching() -- switches the images showing in
      **				      the rotation.  Intended to be
      **    called as in:
      **
      **      Remax.Rotation.switching();
      **
      **    or:
      **
      **      setInterval('Remax.Rotation.switching()', 5000);
      **
      **    Rotates the data.
      */
    , switching:  function ()
      {
	switcher();
      }

      /*
      ** Public Remax.Rotation.init() -	init for initiationlization, sets up
      **				the rotation.  Intended to be called
      **    as in:
      **
      **      $(document.ready(Remax.Rotation.init);
      **
      **    Prepares rotation.
      **
      */
    , init:  function ()
      {
	var text_cap  = null
	  , image_cap = $('#ret_image')
	  ;
	$(image_cap).append('<a href="' + that.linkurl + '"></a>');

	image_cap = $('#ret_image a');
	text_cap  = $('#ret_text');

	var i = 0;

//$('#ret_word').append('<span>src: [' + $(img_in).attr('src') + ']</span>');

	for (i = 0; i < that.last; i++)
	{
	  $(image_cap).append(  '<img id="ri_'	+ i
			      + '" src="'	+ that.data[i].img
			      + '" alt="" />');
	  $(text_cap).append(	'<p id="rt_'  + i
			      +	'">'	      + that.data[i].capt
			      + '</p>');

	  images[i]   = $('#ri_' + i);
	  texts[i]    = $('#rt_' + i);
	  widths[i]   = $(images[i]).width();
	  heights[i]  = $(images[i]).height();
	  lefts[i]    = 0;
	  tops[i]     = 0;

	  if (i)
	  {
	    $(images[i]).hide();
	    $(texts[i]).hide();
	  }
	  working_height  = 120;
	  working_width	  = widths[i] / heights[i] * 120;  

	  if (151 < working_width)
	  {
	    working_height  = heights[i] / widths[i] * 151;
	    working_width   = 151;
	  }
	  heights[i]  = working_height;
	  widths[i]   = working_width;
	  tops[i]     = (120 - working_height) / 2;
	  lefts[i]    = (151 - working_width) / 2;
	}
	$(images[0]).css( { paddingLeft:    lefts[0]
			  , paddingRight:   lefts[0]
			  , paddingTop:	    tops[0]
			  , paddingBottom:  tops[0]
			  , height:	    heights[0]
			  , width:	    widths[0]
			  });
	setInterval('Remax.Rotation.switching()', 10000);
      }
    };
      /*
      ** for finding .attr('style', 'background:#ee9933');
      */
        //attr('style', 'background:#ee9933');

  return that;

}(); // the parens here cause the anonymous func to execute and return



