function Fader(faderId, contentClassName)
{
 var self = this;
 var contents = new Array();
 var currentContentIndex = 0;
 var autoPlay = true;

 var intervalId;
 var currentContent = new Content();
 var pager;
 var _autoPlayDuration = 0;

 this.init = function() {
     var faderEl = YUD.get(faderId);
     var contentEls = YUD.getElementsByClassName(contentClassName, "div", faderEl);
     for (var i = 0; i < contentEls.length; i++) {
         self.AddContent(contentEls[i]);
     }
 }
 
 this.AddContent = function(contentEl)
 {
    var content = new Content();
    content.setId(contentEl);
    content.onMouseOver.subscribe(MouseOver);
    content.onMouseOut.subscribe(MouseOut);
    contents.push(content);
 }
 
 this.SwitchContents = function()
 {
	currentContent = contents[currentContentIndex];
	currentContent.Hide();
    currentContentIndex++;
	currentContentIndex = currentContentIndex%contents.length;
	currentContent = contents[currentContentIndex];
	currentContent.Show();
	pager.SetSelected(currentContentIndex);
 }
 
 this.StartAutoPlay = function(autoPlayDuration)
 {
  _autoPlayDuration = autoPlayDuration;
  intervalId = setInterval(this.SwitchContents,autoPlayDuration*1000);
 }
 
 this.StopAutoPlay = function()
 {
  clearInterval(intervalId);
 }
 
 this.AddPager = function(_pager)
 {
    pager = _pager;
    pager.onButtonSelected1.subscribe(ButtonSelected);
 }
 
 function ButtonSelected(type, args, scope)
 {
    if(intervalId!=null)
    {
        self.StopAutoPlay();
    }
    autoPlay = false;

    self.SwitchContentsByIndex(args[0]);
 }
 
 this.SwitchContentsByIndex = function(contentIndex)
 {
	currentContent = contents[currentContentIndex];
	currentContent.Hide();
    currentContentIndex = contentIndex;
	currentContentIndex = currentContentIndex%contents.length;
	currentContent = contents[currentContentIndex];
	currentContent.Show();
 }
 
 function MouseOver(type, args, scope)
 {
    if(intervalId!=null)
    {
        self.StopAutoPlay();
    }
 }
 
 function MouseOut(type, args, scope) {
    if (autoPlay)
        self.StartAutoPlay(_autoPlayDuration);
 }
}



//////////////////////////////////


function Content()
{
 var self = this;
 var contentEl, showAnim, hideAnim;
 
 this.setId = function(contentEl)
 {
    YUE.addListener(contentEl, "mouseover", function(){
      self.onMouseOver.fire();
    });
    
    YUE.addListener(contentEl, "mouseout", function(){
      self.onMouseOut.fire();
    });
    
    showAnim = new YAHOO.util.Anim(contentEl, {opacity: {to: 1}}, 0.5);
    showAnim.onStart.subscribe(function(){
      YUD.setStyle(contentEl, "z-index", 1);
    });
    
    hideAnim = new YAHOO.util.Anim(contentEl, {opacity: {to: 0}}, 0.5);
    hideAnim.onStart.subscribe(function(){
      YUD.setStyle(contentEl, "z-index", 0);
    });
 }
 
 this.onMouseOver = new YAHOO.util.CustomEvent("onMouseOver", this);
 this.onMouseOut = new YAHOO.util.CustomEvent("onMouseOut", this);
 
 this.Show = function()
 {
 	if (hideAnim.isAnimated()) {hideAnim.stop();}
    showAnim.animate();
 }
 
 this.Hide = function()
 {
 	if (showAnim.isAnimated()) {showAnim.stop();}
    hideAnim.animate();
 }
}

////////////////////////////// 

function Pager(pagerId)
{
 var self = this;
 var buttons = new Array();
 var currentlySelectedButtonIndex = 0;

 this.init = function(buttonIndex)
 {
    var pager = YUD.get(pagerId);
    var links = YUD.getElementsByClassName("lnk_pager", "a", pager);
    for (var i = 0; i<links.length; i++){
        self.AddButton(links[i], i); 
    }
 }
 
 this.Next = function()
 {
  var selectedButtonIndex = (currentlySelectedButtonIndex+1)%buttons.length;
	ButtonSelected("", [selectedButtonIndex]);
 }
 
 this.Previous = function()
 {
  var selectedButtonIndex = currentlySelectedButtonIndex-1;
	if(selectedButtonIndex < 0)
	{
	 selectedButtonIndex = buttons.length - 1;
	}
	ButtonSelected("", [selectedButtonIndex]);
 }

 this.AddButton = function(buttonEl,buttonIndex)
 {
    var btn = new Button();
    btn.SetId(buttonEl,buttonIndex);
    btn.onButtonSelected.subscribe(ButtonSelected);
    buttons.push(btn);
 }
 
 this.onButtonSelected1 = new YAHOO.util.CustomEvent("onButtonSelected1", this);
 var ButtonSelected = function(type, args, scope)
 {
    var buttonIndex = args[0];
    self.SetSelected(buttonIndex);
    self.onButtonSelected1.fire(buttonIndex);
 }
 
 this.SetSelected = function(buttonIndex)
 {
    if (currentlySelectedButtonIndex != buttonIndex) {
        buttons[currentlySelectedButtonIndex].DeselectPage();
        buttons[buttonIndex].SelectPage();
        currentlySelectedButtonIndex = buttonIndex;
    }
 }
 
 this.SetClass = function(buttonId,className)
 {
  YUD.replaceClass(buttonId,"-",className);
 }
}



///////////////////////////////

function Button()
{
 var self = this;
 var buttonEl;
 var buttonIndex;

 this.SetId = function(_buttonEl,_buttonIndex)
 {
    buttonEl = _buttonEl;
    buttonIndex = _buttonIndex;
    
    YUE.addListener(buttonEl, "click", function(){
        self.onButtonSelected.fire(buttonIndex);
    });
 }
  
 this.onButtonSelected = new YAHOO.util.CustomEvent("onButtonSelected", this);
 this.SelectPage = function()
 {
	YUD.addClass(buttonEl,"selected");
 }
 this.DeselectPage = function()
 {
	YUD.removeClass(buttonEl,"selected");
 }
}
