/*
$Date: 2007/10/13 11:10:55 $
$Name: ukconcept_v1_r56 $
$Revision: 1.4 $
$State: Exp $
*/

function Rotator() {
	
  this.framecount = 0;
  this.frames = new Array();
  this.timer;
  this.running = true;
	  
  this.init = function(){
    this.data = $('rotator_data_'+this.id);
    this.target = $('rotator_main_'+this.id);
    this.tools = $('rotator_tools_'+this.id);
    this.initialising = true;
  			
    for(var i = 0;i<=this.data.childNodes.length-1;i++) {
      if(this.data.childNodes[i].nodeType==1) {
    		this.framecount++;
    		this.frames[this.frames.length] = this.data.childNodes[i].innerHTML;
      }
    }
  }

  this.currentFrame = 0;
  this.start = function(){
    this.running = true;
    this.run();
  }

  this.run = function() {
    if (this.running) {
  	  if (this.maxFrames > 0) {
    		this.currentFrame++;
    		if(this.currentFrame > this.maxFrames) {
    			this.currentFrame = 1;
    		}
    		if (!this.initialising) {
      		// Stop Netscape flickering
      		if (navigator.userAgent.indexOf("Netscape") > -1) {
        		$$('#whole_page iframe').each(function(el) {
            	el.hide();
            });
        		this.move();
        		$$('#whole_page iframe').each(function(el) {
            	el.show();
            });
      		}
      		else {
        		this.fadeOut();
        		setTimeout('rotator'+this.id+'.move()', 1000);
      		}
      		this.timer = setTimeout('rotator'+this.id+'.run()',this.interval);
    		}
    		else {
      		this.initialising = false;
      		this.target.innerHTML = this.frames[this.currentFrame-1];
      		this.buildTools();
      		this.timer = setTimeout('rotator'+this.id+'.run()',this.interval);
    		}
  	  }
    }
  }
  
  this.fadeOut = function() {
    currentPicture = $($$('.rotator_main .module_picture')[0]);
    if (currentPicture) {
  		Effect.SwitchOff(currentPicture);
		}
  }
  
  this.stop = function(){
	  this.running = false;
  }
 
  this.forward = function() {
    clearTimeout(this.timer);
    this.run();
    return false;
  }

  this.back = function() {
    clearTimeout(this.timer);
    this.fadeOut();	
    if (this.currentFrame == 0) {
      this.currentFrame = this.maxFrames;
    }
    else {
      this.currentFrame--;
    }
    setTimeout('rotator'+this.id+'.move()', 1000);
    return false;
  }

  this.move = function() {
    if(this.currentFrame>this.maxFrames) {
       this.currentFrame = 1;
    } else if(this.currentFrame==0) {
       this.currentFrame = this.maxFrames;
    }
    this.target.innerHTML = this.frames[this.currentFrame-1];
    this.buildTools();
  }
  
  this.jump = function(i) {
    clearTimeout(this.timer);
    if(this.currentFrame<i) {
        this.currentFrame = i-1;
        this.run();
    } else {
        this.currentFrame = i;
        this.move();
    }
    return false;
  }
	 
  this.BACK_TEXT = "<span class='arrows'>&lt;</span> Back";
  this.FORWARD_TEXT = "Next <span class='arrows'>&gt;</span>";
  this.pageNavArray = new Object();
  this.jumpEvent = function(ev){stopDefaultAction(ev);return ev.currentTarget.rotator.jump(ev.currentTarget.numberIndex)}

  this.buildTools = function() {
    this.toolsContainerElem = document.createElement("p");
    this.toolsContainerElem.className = 'module_body';
    
    this.toolsBackSpanElem = document.createElement("span");
    this.toolsBackSpanElem.className = 'back';
    
    this.toolsBackLinkElem = document.createElement("a");
    this.toolsBackLinkElem.href = '#';
    this.toolsBackLinkElem.innerHTML = this.BACK_TEXT;
    this.toolsBackLinkElem.rotator  = this;
    addEvent(this.toolsBackLinkElem, "mouseup", function(ev){stopDefaultAction(ev);return ev.currentTarget.rotator.back()}, false);
    
    this.toolsNumbersSpanElem = document.createElement("span");
    this.toolsNumbersSpanElem.className = 'item_numbers';
    
    for (var i in this.pageNavArray){
    	if (this.pageNavArray.hasOwnProperty(i)) {
    		this.pageNavArray[i].rotator = null;
    		this.pageNavArray[i] = null;
    	}
    }
    this.pageNavArray = new Array();
    
    for(var i = 0;i<=this.maxFrames-1;i++) {
    	var frameIndex = i+1;
    	if(frameIndex==this.currentFrame) {
    	  this.pageNavArray[i] = document.createElement("span");
    	  this.pageNavArray[i].innerHTML = frameIndex;
    	  this.pageNavArray[i].numberIndex = frameIndex;
    	} else {
    	  this.pageNavArray[i] = document.createElement("a");
    	  this.pageNavArray[i].href="#";
    	  this.pageNavArray[i].numberIndex = frameIndex;
    	  this.pageNavArray[i].rotator  = this;
    	  addEvent(this.pageNavArray[i], "click", this.jumpEvent, false);
    	  this.pageNavArray[i].innerHTML = frameIndex;
    	}
    	this.toolsNumbersSpanElem.appendChild(this.pageNavArray[i]);
    }
    
    this.toolsForwardSpanElem = document.createElement("span");
    this.toolsForwardSpanElem.className = 'forward';
    
    this.toolsForwardLinkElem = document.createElement("a");
    this.toolsForwardLinkElem.href = '#';
    this.toolsForwardLinkElem.innerHTML = this.FORWARD_TEXT;
    this.toolsForwardLinkElem.rotator  = this;
    
    addEvent(this.toolsForwardLinkElem, "mouseup", function(ev){stopDefaultAction(ev); return ev.currentTarget.rotator.forward()}, false);
    
    //add dummy functions for Safari to cancel default onclick
    addEvent(this.toolsBackLinkElem, "click", function(ev){stopDefaultAction(ev); return false;});
    addEvent(this.toolsForwardLinkElem, "click", function(ev){stopDefaultAction(ev); return false;});
    
    this.toolsBackSpanElem.appendChild(this.toolsBackLinkElem);
    this.toolsForwardSpanElem.appendChild(this.toolsForwardLinkElem);
    
    this.toolsContainerElem.appendChild(this.toolsBackSpanElem);
    this.toolsContainerElem.appendChild(this.toolsNumbersSpanElem);
    this.toolsContainerElem.appendChild(this.toolsForwardSpanElem);
    
    this.tools.innerHTML = '';
    this.tools.appendChild(this.toolsContainerElem);
  }
}

// Kids YSI Module

function responseFunc(url, str){ 
	document.getElementById('usaidit').innerHTML = str;
	addButtonEvents();
}

function buttonClickHandler(ev){
	var obj = ev.currentTarget;
	var url= obj.href;
	stopDefaultAction(ev);

	var buttonElems = document.getElementById('ysi_more_button');
	removeClass(buttonElems, 'selected');

	if (classExists(obj, 'switch-on')){
		obj.className = 'switch-off';
		addClass(obj,'selected');
	} else {
		obj.className = 'switch-on';
	}
	addClass(obj,'selected');
	makeRequest(url, responseFunc);
	return false;
}

function buttonDownHandler(ev){
	stopDefaultAction(ev);
	var obj = ev.currentTarget;
	addClass(obj,'click');
}
function buttonUpHandler(ev){
	stopDefaultAction(ev);
	var obj = ev.currentTarget;
	removeClass(obj,'click');
}
function emptyHandler(ev){
	stopDefaultAction(ev);
}
function dragOffHandler(ev){
	stopDefaultAction(ev);
	removeClass(buttonElem,'click');
}
function addButtonEvents(){
	var buttonElem = document.getElementById('ysi_more_button');
	addEvent(buttonElem,"mousedown",buttonDownHandler,false );
	addEvent(buttonElem,"mouseup",buttonUpHandler,false );
	addEvent(buttonElem,"mouseout",buttonUpHandler,false );
	addEvent(buttonElem,"click",buttonClickHandler,false );
	addEvent(buttonElem,"drag",emptyHandler,false );
}

addEvent(this,"load",addButtonEvents,false );

