/**
	* file: rf_banner_rotation
	* author: Matryoshka Ltd.
	* date: June 7th, 2006
	* description:
	*     Banner (image) rotation classes
	*    
*/
 
/// Banner Rotation System classes
/// June 7th, 2006
 
// global constants
var DEFAULT_CURRENT = 0;
var STATIC_DURATION = 8000;
var TRANSITION_DURATION = 1000;
var BANNER_CLASS = ".banner";
var BANNER_ACTIVE_CLASS = ".active";
 
/// factory functions
 
// create banner rotator
function rf_bannersFromDiv(container_path, banner_class, active_class) {
	var banners;
	var banner;
	var first = 0;
	var selected;
	var a;
	var img;
	var ret = new rf_Array();
	
	banners = new rf_Array();
	//selected = $(container_path).find(active_class).get(0); // find active
	selected = $(container_path).find(active_class).get(0);
	
	$(container_path).find(banner_class).each(function (i) {
		banner = $(this);
		a = banner.find("a").get(0);
		img = banner.find("img").get(0);
		banners[i] = new rf_Array();
		banners[i][0] = banner;
		banners[i][1] = a.title;
		banners[i][2] = img.alt;
		banners[i][3] = a.href;
		if(this == selected) {
			first = i;
		}
	});

	ret[0] = banners;
	ret[1] = first;
	
	return ret;
 }
 
 // Basic banner class
 // Creates a simple container for holding individual banner data
 // id is required
 rf_extends(rf_BasicBanner, rf_Core);
 function rf_BasicBanner(id, title, description, url, duration) {
	 rf_BasicBanner.baseConstructor.call(this);
	 this.id = id;
	 this.title = title || "";
	 this.description = description || "";
	 this.url = url || "#";
	 this.duration = duration || STATIC_DURATION;
 }
// override 
// Object toString
rf_BasicBanner.prototype.toString = function() {
	return "[" + this.id + "]: " + this.title + " ( " + this.description + ") link: " + this.url + " duration: " + this.duration;
}
// new methods
// Show the element
rf_BasicBanner.prototype.show = function(speed) {
	this.id.show(speed);
};
// Hide the element
rf_BasicBanner.prototype.hide = function(speed) {
	this.id.hide(speed);
};
rf_BasicBanner.prototype.displayNone = function() {
	this.id.css("display", "none");
};
 
// Basic banner class with ability to fade in and out
// Extends rf_BasicBanner
rf_extends(rf_FadingBanner, rf_BasicBanner);
function rf_FadingBanner(id, name, description, url, duration, transition) {
		rf_FadingBanner.baseConstructor.call(this, id, name, description, url, duration);
		this.transition = transition || TRANSITION_DURATION;
}
// overrides
rf_FadingBanner.prototype.toString = function() {
 	return rf_FadingBanner.baseClass.toString.call(this) + " transition: " + this.transition;
}
// new methods
// Fade element in
rf_FadingBanner.prototype.fadeIn = function(speed) {
	this.id.fadeIn(speed);
};
// Fade element out
rf_FadingBanner.prototype.fadeOut = function(speed) {
	this.id.fadeOut(speed);
};

// Basic banner class with ability to slide up and down
// Extends rf_BasicBanner
rf_extends(rf_SlidingBanner, rf_BasicBanner);
function rf_SlidingBanner(id, name, description, url, duration, transition) {
		rf_SlidingBanner.baseConstructor.call(this, id, name, description, url, duration);
		this.transition = transition || TRANSITION_DURATION;
}
// overrides
rf_SlidingBanner.prototype.toString = function() {
 	return rf_SlideBanner.baseClass.toString.call(this) + " transition: " + this.transition;
}
// new methods
// Fade element in
rf_SlidingBanner.prototype.slideDown = function(speed) {
	this.id.slideDown(speed);
};
// Fade element out
rf_SlidingBanner.prototype.slideUp = function(speed) {
	this.id.slideUp(speed);
};
 


// Main banner rotation class
// Creates a simple timed rotation of elements without a transition
rf_extends(rf_BasicBannerRotator, rf_Core);
function rf_BasicBannerRotator(banners, first, static_duration) {
	rf_BasicBannerRotator.baseConstructor.call(this);
	
	this.timer_id = null;
	this.banners = new rf_Array();
	
	this.current = this.original = first || DEFAULT_CURRENT;
	this.static_duration = static_duration || STATIC_DURATION;
	
	// wait for init and start
	this.status = false;
	if(banners) {
		this.load(banners);
	}
}

// override

// rf_Core callIn now handles object's timer_id
rf_BasicBannerRotator.prototype.callIn = function(f, t) {
	//alert("BasicBannerRotator callIn: " + f);
	if(this.timer_id) {
		clearTimeout(this.timer_id);
	}
	var id = rf_BasicBannerRotator.baseClass.callIn.call(this, f, t);
	this.timer_id = id;
	return id;
}

// accessories

// check if the provided index is correct
rf_BasicBannerRotator.prototype.checkIndex = function(id) {
	return this.banners.getDefault(id, false);
}
// give the next banner index
rf_BasicBannerRotator.prototype.nextIndex = function() {
	return (this.current+1 > this.banners.length-1)?0:this.current+1;
}
// give the preview banner index
rf_BasicBannerRotator.prototype.prevIndex = function() {
	return (this.current-1<0)?this.banners.length-1:this.current-1;
}

// methods

// initialize rotator before starting
rf_BasicBannerRotator.prototype.init = function() {
	this.hideAll();
	this.current = this.original;
	this.showCurrent();
	
	var obj = this;
	this.dispatchEvent(function () { obj.onInit(); });
}
// load banners
rf_BasicBannerRotator.prototype.load = function(banners) {
	var i;
	var banner;
	
	for(i=0;i<banners.length;++i) {
		banner = new rf_BasicBanner(banners[i][0], banners[i][1], banners[i][2], banners[i][3]); // convert
		banner.id.css("z-index", i * 100 + "");
		this.banners[i]  = banner;
	}
}
// show all banners 
rf_BasicBannerRotator.prototype.showAll = function() {
	var i;
	for(i=0;i<this.banners.length;i++) {
		this.banners[i].show();
	}
}
// hide all banners
rf_BasicBannerRotator.prototype.hideAll = function() {
	var i;
	for(i=0;i<this.banners.length;++i) {
		this.banners[i].hide();
	}
}
// completely hide element
rf_BasicBannerRotator.prototype.displayNone = function(i) {
	this.banners[i].id.css("display", "none");
}
// get current
rf_BasicBannerRotator.prototype.getCurrent = function() {
	return this.banners[this.current];
}
// show current
rf_BasicBannerRotator.prototype.showCurrent = function() {
	this.banners[this.current].show();
}
// hide current
rf_BasicBannerRotator.prototype.hideCurrent = function() {
	this.banners[this.current].hide();
}
// toggle between two banners
rf_BasicBannerRotator.prototype.toggle = function(old_i, new_i) {
	this.banners[old_i].hide();
	this.banners[new_i].show();
}

// transition states

rf_BasicBannerRotator.prototype.next = function() {	
	var obj = this;
	this.callIn(function () { obj.startTransition(); }, this.static_duration);
	this.dispatchEvent(function () { obj.onNext(); });
}

rf_BasicBannerRotator.prototype.startTransition = function(old_i, new_i) {
	var obj = this;
	this.callIn(function () { obj.transition(); }, 0);
	this.dispatchEvent(function () { obj.onStartTransition(); });
}

rf_BasicBannerRotator.prototype.transition = function(old_i, new_i) {
	this.hideCurrent();
	this.current = this.nextIndex();
	this.showCurrent(); // show the new one
	
	var obj = this;
	this.callIn(function () { obj.endTransition(); }, 0);
	this.dispatchEvent(function () { obj.onTransition(); });
}

rf_BasicBannerRotator.prototype.endTransition = function(old_i, new_i) {
	//alert(this.getCurrent().id.find("a").size());
	var obj = this;
	if(this.status) {
		this.callIn(function () { obj.next(); }, 0);
	}
	this.dispatchEvent(function () { obj.onEndTransition(); });
}

// action methods

// start rotator
rf_BasicBannerRotator.prototype.start = function() {
	this.status = true;
	this.init();
	this.next();
	
	var obj = this;
	this.dispatchEvent(function () { obj.onStart(); });
}
rf_BasicBannerRotator.prototype.stop = function() {
	clearTimeout(this.timer_id);
	this.status = false;
	
	var obj = this;
	this.dispatchEvent(function () { obj.onStop(); });
}
// pause rotator
rf_BasicBannerRotator.prototype.pause = function() {
	clearTimeout(this.timer_id);
	this.status = false;
	
	var obj = this;
	this.dispatchEvent(function () { obj.onPause(); });
}
rf_BasicBannerRotator.prototype.resume = function() {
	clearTimeout(this.timer_id);
	this.status = true;
	
	var obj = this;
	this.callIn(function () { obj.next(); }, 0);
	this.dispatchEvent(function () { obj.onResume(); });
}

// events

rf_BasicBannerRotator.prototype.onInit = function() {
}
rf_BasicBannerRotator.prototype.onStart = function() {
}
rf_BasicBannerRotator.prototype.onStop = function() {
}
rf_BasicBannerRotator.prototype.onPause = function() {
}
rf_BasicBannerRotator.prototype.onResume = function() {
}
rf_BasicBannerRotator.prototype.onNext = function() {
}
rf_BasicBannerRotator.prototype.onStartTransition = function() {
}
rf_BasicBannerRotator.prototype.onTransition = function() {
}
rf_BasicBannerRotator.prototype.onEndTransition = function() {
}

// end

// Fading banner rotation class
// Creates a simple timed rotation of elements with a fade transition
rf_extends(rf_FadingBannerRotator, rf_BasicBannerRotator);
function rf_FadingBannerRotator(banners, first, static_duration, transition_duration) {
	rf_FadingBannerRotator.baseConstructor.call(this, banners, first, static_duration);
	
	this.transition_duration = transition_duration || TRANSITION_DURATION;
}

// overrides

// show current
rf_FadingBannerRotator.prototype.showCurrent = function() {
	this.banners[this.current].fadeIn("slow");
}
// hide current
rf_FadingBannerRotator.prototype.hideCurrent = function() {
	this.banners[this.current].fadeOut("slow");
}
// load banners
rf_FadingBannerRotator.prototype.load = function(banners) {
	var i;
	var banner;
	
	for(i=0;i<banners.length;++i) {
		banner = new rf_FadingBanner(banners[i][0], banners[i][1], banners[i][2], banners[i][3]); // convert
		banner.id.css("z-index", i * 100 + "");
		this.banners[i]  = banner;
	}
}
// start transition
rf_FadingBannerRotator.prototype.startTransition = function(old_i, new_i) {
	this.hideCurrent();
	
	var obj = this;
	this.callIn(function () { obj.transition(); }, this.transition_duration);
	this.dispatchEvent(function () { obj.onStartTransition(); });
}

rf_FadingBannerRotator.prototype.transition = function(old_i, new_i) {
	this.displayNone(this.current);
	this.current = this.nextIndex();
	this.showCurrent(); // show the new one
	
	var obj = this;
	this.callIn(function () { obj.endTransition(); }, 0);
	this.dispatchEvent(function () { obj.onTransition(); });
}

// Sliding banner rotation class
// Creates a simple timed rotation of elements with a sllide transition
rf_extends(rf_SlidingBannerRotator, rf_BasicBannerRotator);
function rf_SlidingBannerRotator(banners, first, static_duration, transition_duration) {
	rf_SlidingBannerRotator.baseConstructor.call(this, banners, first, static_duration);
	
	this.transition_duration = transition_duration || TRANSITION_DURATION;
}

// overrides

// show current
rf_SlidingBannerRotator.prototype.showCurrent = function() {
	this.banners[this.current].slideDown("normal");
}
// hide current
rf_SlidingBannerRotator.prototype.hideCurrent = function() {
	this.banners[this.current].slideUp("normal");
}
// load banners
rf_SlidingBannerRotator.prototype.load = function(banners) {
	var i;
	var banner;
	
	for(i=0;i<banners.length;++i) {
		banner = new rf_SlidingBanner(banners[i][0], banners[i][1], banners[i][2], banners[i][3]); // convert
		banner.id.css("z-index", i * 100 + "");
		this.banners[i]  = banner;
	}
}
// start transition
rf_SlidingBannerRotator.prototype.startTransition = function(old_i, new_i) {
	this.hideCurrent();
	
	var obj = this;
	this.callIn(function () { obj.transition(); }, this.transition_duration);
	this.dispatchEvent(function () { obj.onStartTransition(); });
}

rf_SlidingBannerRotator.prototype.transition = function(old_i, new_i) {
	this.current = this.nextIndex();
	this.showCurrent(); // show the new one
	
	var obj = this;
	this.callIn(function () { obj.endTransition(); }, 0);
	this.dispatchEvent(function () { obj.onTransition(); });
}

