var picture = function(){
	this.conteiner = $('#conteiner');
	this.files = Array();
	var temp = $('#conteiner').attr('images').split('|');
	for(var i in temp){
		this.files.push({name:temp[i],load:false});
		this.preload(i);
	}
	this.active = 0;
	this.img = this.conteiner.find('img');
}

picture.prototype.preload = function(i){
	var preloader = new Image();
	var instance = this;
	preloader.onload = function() {
		instance.files[i].load = true;
		preloader.onload = null;
		preloader = null;
	};
	preloader.src = siteroot + imgpath + this.files[i].name;
}

picture.prototype.change = function(){
	var file = this.select();
	if (!file){
		return false;
	}
	var pos = getOffset(this.img);
	var img = this.img.clone()
	var instance = this;
	$(img).css({
			position:'absolute',
			left:pos.left+"px",
			top:pos.top+"px",
			opacity:0,
			zIndex:2}).attr('src',siteroot + imgpath + file.name);
	$('body').append($(img));
	$(img).animate({opacity:1},2000,'linear',function(){
		instance.img.attr('src',siteroot + imgpath + file.name);
		$(this).remove();
	});
}

picture.prototype.select = function() {
	var i = this.active;
	var check = false;
	while (!check) {
		i = i<this.files.length-1 ? i+1 : 0;
		check = i==this.active ? true : this.files[i].load;
	}
	if (this.active != i){
		this.active = i;
		return this.files[this.active];
	}else{
		return false;
	}
}

function getOffset(elm){
	var elm = $(elm).get(0);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (elm) {
      offsetLeft += elm.offsetLeft;
      offsetTop += elm.offsetTop;
      elm = elm.offsetParent;
    }
    return {'top':offsetTop, 'left':offsetLeft};
}
$(document).ready(function(){
	var interval = (typeof(rotateInterval)=="number") ? rotateInterval*1000 : 7000;
	var rotate = new picture();
	$(rotate.img).click(function(){
		try{
			window.clearTimeout(t);
		}catch (e){
			
		}
		rotate.change();
		t = setTimeout("timer()",interval);
	});
	timer = function(){
		if(!stopslideshow){ $(rotate.img).click();}
	};
	t = setTimeout("timer()",interval);
});

function stopgoslideshow(action)
{
	if(stopslideshow || action=='run')
	{
		var stopslideshow =0;
		timer();
	} else if(!stopslideshow || action=='stop')
	{
		var stopslideshow =1;
	}
}
