/*============================================================================
	JS scripts, Janmedia Interactive Inc.
	$Id: scripts.js,v 1.8 2010-08-17 14:57:39 pcharasimiuk Exp $
=============================================================================*/
//Banner generator
function createBannerScript(no,categoryId){
	document.write("<script src=\"/banner?action=view&id=" + no
	+ ((categoryId.length > 0) ? ('&category_id=' + categoryId ) : '')
	+ "&js&rand=" +	Math.random() + "\" type=\"text/javascript\" /></script>");
}

// Image rotator
function Rtr() {
	this.count = $("div#rtr .slide").length;
	this.current = 1;
	this.autoplayMs = 10000;
	this.autoplayDelayMs = 20000;
	this.autoplayHandler;
	this.autoplayDelayHandler;
	
	$("div#rtr .slide").css("display","none").css("position","absolute").css("left","0").css("top","0");
	$("div#rtr .slide.active").css("display","block");
	$("ul#rtr_nav li#toslide"+this.current).addClass("active");
	
	this.rotateById = function(link){
		this.rotateTo(parseInt(link.hash.replace(new RegExp("#slide","gi"),""),10));
	}
	
	this.rotateBy = function(offset){
		if (!offset){
			offset = 1;
		}
		if (this.count > 1){
			this.rotateTo(this.current + offset);
		}
		return false;
	}
	
	this.rotateTo = function(destNo){
		if (destNo == this.current){
			return;
		}
		if (this.count > 1 && isNaN(parseInt(destNo,10))){
			destNo = this.current + 1; 
		}
		if (destNo > this.count){
			destNo = 1;
		}
		if (destNo <= 0){
			destNo = this.count;
		}		
		
		$("div#rtr #slide" + this.current).fadeOut();
		$("div#rtr #slide" + destNo).fadeIn();
		$("ul#rtr_nav li#toslide"+this.current).removeClass("active");
		$("ul#rtr_nav li#toslide"+destNo).addClass("active");
		this.current = destNo;
	}
	
	this.resetAutoplay = function(){
		var that = this;
		this.autoplayHandler=clearInterval(this.autoplayHandler);
		this.autoplayDelayHandler=clearTimeout(this.autoplayDelayHandler);
		
		this.autoplayDelayHandler = setTimeout(function(){that.autoplay();},that.autoplayDelayMs);
	}
	
	this.autoplay = function(){
		var that = this;
		this.autoplayHandler = setInterval(function(){that.rotateBy(1)},that.autoplayMs);
	}	

}

$(document).ready(function(){
	if ($("div#rtr .slide").length > 1){
		var rotator = new Rtr();
		$("div#rtr p#rtr_next a").click(function(event){event.preventDefault();rotator.resetAutoplay();rotator.rotateBy(1);});
		$("div#rtr p#rtr_prev a").click(function(event){event.preventDefault();rotator.resetAutoplay();rotator.rotateBy(-1);});
		$("ul#rtr_nav li a").click(function(event){event.preventDefault();rotator.resetAutoplay();rotator.rotateById(this);});
		//rotator.autoplayHandler = setInterval(function(){rotator.rotateBy(1)},rotator.autoplayMs);
		rotator.autoplay();
	}
});
