
var images_count = 13;
var image_ext = ".png";

function GetParameter( param_name ) {
	var param_string = GetCurrentURL();
	var result;
	var param_name_eq = param_name+"=";
	var index = param_string.indexOf( param_name_eq );
	if ( index <= 0 ) return result;
	var param_prefix = param_string.charAt( index-1 );
	if ( param_prefix=='?' || param_prefix=='&' ) {
	    var value_start = index + param_name_eq.length;
	    var next_param = param_string.indexOf( param_string, value_start );	
	    if ( next_param==-1 ) {
	        result = param_string.substr( value_start );
	    } else {
	        result = param_string.substring( value_start, next_param );
	    }
	}
	return result;
}

function ImageTimer(thisname, timerimageid, page_url, currentPageIndex, pagesCount) {

	var dotIndex = page_url.indexOf(".");
	if ( dotIndex>0) {
		var nextPage = page_url.substring(0,dotIndex);
		var nextPageIndex = currentPageIndex + 1;
		if (nextPageIndex<pagesCount ) {
			nextPage +=nextPageIndex;
		}
		nextPage += page_url.substr( dotIndex );
		 this.timer_value = GetParameter("timer");

		if ( this.timer_value==null ) {
			this.startpos = 6;
		} else {
			this.startpos = new Number( this.timer_value );
		}
		this.triggerlocation = nextPage;

	}

	this.name = thisname;
	this.imageid = timerimageid;

	this.state = 0;
	this.cpos = this.startpos;

	this.preload = IT_preload;
	this.next = IT_next;
	this.switchstate = IT_switchstate;
	this.setpos = IT_setpos;
	this.imgs = new Array();
}

function IT_preload( style_dir ) {
	this.style_dir = style_dir;
   for( var i =0;i<images_count*2;i+=2 ) {
	this.imgs[i] = new Image();
	this.imgs[i].src = this.style_dir+"tcs_"+i+image_ext
	this.imgs[i+1] = new Image();
	this.imgs[i+1].src = this.style_dir+"tcp_"+i+image_ext
   }


	if ( this.timer_value != null ) {
		this.switchstate();
	}

}

function IT_next() {
   if (this.state == 0)
	return;
   this.cpos -= 1;
   if (this.cpos < 0)
   {
	if (this.triggerlocation != "") {
		document.location = this.triggerlocation + "?timer=" + this.startpos;
	}
	this.cpos = this.startpos;	
   }
   document.images[this.imageid].src = this.style_dir+"tcs_" + this.cpos + image_ext;
   if (this.cpos > 0)
	window.setTimeout(this.name + ".next()",1000);
   else
	window.setTimeout(this.name + ".next()",200);
}

function IT_switchstate() {
    if (this.state == 0)
    {
	this.state = 1;
	this.cpos = this.startpos + 1;
	window.setTimeout(this.name + ".next()",100);
    }
    else
    {
	this.state = 0;
	document.images[this.imageid].src = this.style_dir+"tcp_" + this.startpos + image_ext;	
    }
}

function IT_setpos(n) {
    if (this.state == 1)
    	this.state = 0;
	
    this.startpos = n;
	var destination = "tcp_" + this.startpos + image_ext;
    if ( document.images[this.imageid].src.lastIndexOf( destination )==-1) {
	document.images[this.imageid].src= this.style_dir+destination;
    } else {
	this.setpos( n-1 );
	}

}

