/****

Requires:
	mootools-1.2.4.2-more-yc.js
	mootools-1.2.4-core.js
	
****/
//<![CDATA[
window.addEvent('domready', function() {
   new HighlightingRotation({
		containerId: 'RotationGroup', //id of container holding the anchors
		//speeds in milliseconds
		hoverSpeed: 300, //speed of fade on hover; should be less or equal to fadeSpeed
		fadeSpeed: 1500, //speed of fade during rotation
		rotateSpeed: 5000, //speed between rotations; should be more than fadeSpeed
		baseTagType: '*', 
		highlightClass: 'interiorRotate'
	});
});
//]]>
//<![CDATA[
window.addEvent('domready', function() {
   new HighlightingRotation({
		containerId: 'RotationGroup2', //id of container holding the anchors
		//speeds in milliseconds
		hoverSpeed: 300, //speed of fade on hover; should be less or equal to fadeSpeed
		fadeSpeed: 1500, //speed of fade during rotation
		rotateSpeed: 10000, //speed between rotations; should be more than fadeSpeed
		baseTagType: '*', 
		highlightClass: 'interiorRotate'
	});
});
//]]>
//Highlight Rotation

var HighlightingRotation = new Class({
    Implements: Options,
	
    options: {
		containerId: null,
        hoverSpeed: 500,
        fadeSpeed: 1000,
		rotateSpeed: 3000,
		textClass: 'text',
		hoverClass: 'hoverEffect',
		highlightClass: 'highlight',
		interActiveClass: 'interactive',
		fadeText: 1,
		showText: 1,
		baseTagType: 'a',
		limitInclusion: {  //may be set on page with global variable 'HR_limitInclusion'
			action: '', //valid values are 'include', or 'exclude'
			files: [],
			start: 0 //zero means do random rotation
		},
		/*number of pictures or less that causes rotateSpeed to increase by increaseFactor:
		  useful to have longer time with extremely small number of pictures
		*/
		increaseRotateOn: 3,
		increaseFactor: 2000
    },
	
    initialize: function(options){
        this.setOptions(options);
		if(!this.options.containerId) return;

		var container = $(this.options.containerId);
		if(!container) return;
		var elements = container.getChildren(this.options.baseTagType);
		if(elements.length == 1) return; //no need to rotate 
		if(elements.length < 1) container.destroy(); //eliminate unnecessary container
		
		//check for page specific limitations
		if($defined(window.HR_limitInclusion)) {
			var defaultStart = this.options.limitInclusion.start;
			if($defined(HR_limitInclusion.action)) {this.options.limitInclusion.action = HR_limitInclusion.action.toLowerCase()}
			if($defined(HR_limitInclusion.files)) {this.options.limitInclusion.files = HR_limitInclusion.files}
			if($defined(HR_limitInclusion.start)) {this.options.limitInclusion.start = HR_limitInclusion.start};
		}

		//limit elements
		var limits = this.options.limitInclusion.action;
		if(limits == 'include') {
			limits = this.options.limitInclusion.files;

			elements = elements.filter(function(item, index){
				var uri = new URI (item.getProperty('src'));
				uri = uri.get('file');
				if(limits.indexOf(uri) == -1) {
					item.destroy();
				}
				else {
					return true;
				}
					
			});
		}
		else if(limits == 'exclude') {
			limits = this.options.limitInclusion.files;
			limits.each(function(item, index){
				var p = elements[0].getParent();
				var limitEl = p.getElement('img[src$='+item+']');
				if(limitEl) {
					elements.erase(limitEl);
					limitEl.destroy();
				}
			});
		}	
	
		if(elements.length <= 1) return; //recheck modified elements for rotation 
		
		var highClass = '.'+this.options.highlightClass;
		var hoverClass = '.'+this.options.hoverClass;
		var textClass = '.'+this.options.textClass;
		var interActClass = this.options.interActiveClass;
		var startNum = this.options.limitInclusion.start;
		
		//determine start number
		if(startNum) {
			if($type(startNum) != 'number' && $type(startNum) != 'string') {
				startNum = 0; //default to first picture if unexpected type
			}
			else if($type(startNum.toInt()) == 'number') {
				startNum = startNum.toInt().limit(1, elements.length+1) - 1;
			}
			else {
				startNum = elements.indexOf(container.getElement('img[src$='+startNum+']'));
				if(startNum == -1) { startNum = 0 } 
			}
		}
		else {
			startNum = Math.floor(Math.random()*elements.length);
		}
		
		//start showing first image right away
		elements[startNum].setStyle('z-Index',100);
		
		container.removeClass('noScript');
		container.elTags = elements;
		container.currentActive = 0;
		container.hoverSpeed = this.options.hoverSpeed; 
		container.fadeSpeed = this.options.fadeSpeed;

		container.rotateSpeed = (elements.length  <= this.options.increaseRotateOn) ? this.options.rotateSpeed+this.options.increaseFactor : this.options.rotateSpeed ;
		container.fadeText = this.options.fadeText;
		container.showText = this.options.showText;
		container.hasAnchors = 0;
		container.needsInteraction = 0;
		
		container.currentSpeed = container.fadeSpeed;
		
		container.setSpeed = function(item) {
			if(item.parent.hasAnchors) {
				item.hover.set('tween', {duration: item.parent.currentSpeed});
				item.txt.set('tween', {duration: item.parent.currentSpeed});
			    item.hl.set('tween', {duration: item.parent.currentSpeed});
			}
			else {
				item.set('tween', {duration: item.parent.currentSpeed});
			}
		}

		container.fadeGroup = function(item, action) {
			if(item.parent.hasAnchors) {
				item.hover.fade(action);
				item.hl.fade(action);
				if(action == 'in') {
					item.txt.fade(item.parent.showText);
				}
				else {
					item.txt.fade(item.parent.fadeText);
				}
			}
			else {
				item.fade(action);
			}
		}
				
		if(this.options.baseTagType == 'a') {
			container.hasAnchors = 1;
			elements.each(function(item, index){
				//initilize
				item.parent = container;
				item.hl = item.getElement(highClass);
				item.hover = item.getElement(hoverClass);
				item.txt = item.getElement(textClass);
				item.index = index;
				
				if(index == startNum) {
					item.addClass('active');
					item.hover.fade('show');
					item.hl.fade('show');
					item.txt.fade(item.parent.showText);
				}
				else {
					item.hl.fade('hide');
					item.hover.fade('hide');
					item.txt.fade(item.parent.fadeText);
				}	
			
				//actions
				item.hide = function() {
						this.parent.setSpeed(this);
						this.removeClass('active');
						this.parent.fadeGroup(this, 'out');
					}.bind(item);
				item.show = function() {
						this.parent.setSpeed(this);
						this.parent.getElement('*.active').hide();
						this.parent.fadeGroup(this, 'in');
						this.addClass('active');
						this.parent.currentActive = this.index;
					}.bind(item);
				item.addEvent('mouseenter', function() {
					this.parent.currentSpeed = this.parent.hoverSpeed;
					if(!this.hasClass('active')) this.show();
				});
			
			});
			
			container.addEvents({
				'mouseenter': function(){
					this.go = $clear(this.go);
				},
				'mouseleave': function(){
					this.currentSpeed = this.fadeSpeed;
					this.go = function(){
						(this.currentActive >= this.elTags.length - 1) ? this.currentActive = 0 : this.currentActive++;
						this.elTags[this.currentActive].show();
					}.periodical(this.rotateSpeed, container);
				}
			});
			
			container.fireEvent('mouseleave');
		}//end if anchor association	
		else { //just a picture rotation
			elements.each(function(item, index){
				//initilize
				item.parent = container;
				item.index = index;
				if(index == startNum) {
					item.addClass('active');
					item.fade('show');
				}
				else {
					item.fade('hide');
				}	
				
				//actions
				item.hide = function() {
						this.parent.setSpeed(this);
						this.removeClass('active');
						this.parent.fadeGroup(this, 'out');
					}.bind(item);
				item.show = function() {
						this.parent.setSpeed(this);
						this.parent.getElement('*.active').hide();
						this.parent.fadeGroup(this, 'in');
						this.addClass('active');
						this.parent.currentActive = this.index;
					}.bind(item);
					
				if(item.hasClass(interActClass)) container.needsInteraction = 1;
				

			});
							

			if(container.needsInteraction) {
				container.addEvents({
				'mouseenter': function(){
					this.go = $clear(this.go);
				},
				'mouseleave': function(){
					this.currentSpeed = this.fadeSpeed;
					this.go = function(){
						(this.currentActive >= this.elTags.length - 1) ? this.currentActive = 0 : this.currentActive++;
						this.elTags[this.currentActive].show();
					}.periodical(this.rotateSpeed, container);
				}
			});
			}
			else {
				container.go = function(){
					(this.currentActive >= this.elTags.length - 1) ? this.currentActive = 0 : this.currentActive++;
					this.elTags[this.currentActive].show();
				}.periodical(container.rotateSpeed, container);
			}
			
	
				
		}//end else
		elements[startNum].setStyle('z-Index', null);
    } //end initilize
});//end Class Definition


//]]>

