function Product( title, age, description, link, image )
{
  this.title       = title;
  this.age         = age;
  this.description = description;
  this.link        = link;
  this.image       = image;
}
function Products()
{
  this.size    = 5;
  this.current = 0;
  this.prods   = new Array();
  this.addProduct = function( title, age, description, link, image )
  {
    this.prods[this.prods.length] = new Product( title, age, description, link, image );
  }
  this.rotate = function()
  {
    setTimeout("products.next()",15000);
  }
  this.next = function()
  {
    this.current++;
    if ( this.current>=this.prods.length )
    {
      this.current = 0;
    }
    for ( var i=0; i<this.size; i++ )
    {
      var index = this.current+i;
      if ( index>=this.prods.length )
      {
        index = index-this.prods.length;
      }
      if ( index>=0 && index<this.prods.length )
      {
        var p = this.prods[index];
        $("#featured"+i+" #featuredTitle").html( p.title );
        $("#featured"+i+" #featuredAge").html( p.age );
        $("#featured"+i+" #featuredDescription").html( p.description );
        $("#featured"+i+" a").each( function() { this.href=p.link } );
//alert( p.image );
        if ( p.image=="" )
        {
          $("#featured"+i+" .featuredImage .border").css( "display","none" );
        }
        else
        {
          $("#featured"+i+" img").each( function() { this.src=p.image } );
          $("#featured"+i+" .featuredImage .border").css( "display","block" );
        }
       }
    }
    this.rotate();
  }
}
var products = new Products();


function ProductImage( imageId, thumbImage, fullImage, title )
{
  this.imageId    = imageId;
  this.thumbImage = thumbImage;
  this.fullImage  = fullImage;
  this.title      = title;
}
function ProductImages()
{
  this.images   = new Array();
  this.addImage = function( imageId, thumbImage, fullImage, title )
  {
    this.images[imageId] = new ProductImage( imageId, thumbImage, fullImage, title );
  }
  this.getImage = function( imageId )
  {
    return this.images[imageId];
  }
  this.swapImages = function( img )
  {
    var from = this.getImage( img.id );
    var to   = this.getImage( $(".mainimage img").attr("id") );
    $("#"+from.imageId).parent().html( '<img id="'+ to.imageId +'" src="'+ to.thumbImage +'" onclick="return productImages.swapImages(this)" title="'+to.title+'" />' );
    $(".mainimage img").parent().html( '<img id="'+ from.imageId +'" src="'+ from.fullImage +'" title="'+from.title+'" />' );
    //alert($(".mainimage img").attr("id")); //html( '<img id="'+ to.imageId +'" src="'+ to.fullImage +'" title="'+to.title+'" />' );
    return false;
  }
}
var productImages = new ProductImages();