x200_img_amed = new Object();
x200_img_abig = new Object();
x200_img_play = '';
x200_img_PLAY_INTERVAL=4000;

x200_img_objs = new Array();
x200_img_prefix = '_';

function x200_SLIDE(id, useLightbox) {
	this.offerID = id;
	this.containerID = '#x200_VOFF_slide_' + id;
	this.picCount = $(this.containerID + ' .x200_VOFF_slide_pics a').size();
	this.current = 1;
	this.picWidth = 200;
	this.ctrlInterval = 4000;
	this.timer;
	this.mode = 'stop';
	
	this.next = function() {
		if (this.picCount == 1) return;
		if (this.current == this.picCount) {
			$(this.containerID + ' .x200_VOFF_slide_mask').animate({ left: 0 }, { easing: 'swing'});
			this.current = 1;
		}
		else {
			$(this.containerID + ' .x200_VOFF_slide_mask').animate({ left: "-="+this.picWidth }, { easing: 'swing'});
			this.current++;
		}
		$('#'+this.offerID+'x200_IMG_med').html(this.current +' z '+ this.picCount);
	}
	
	this.prev = function() {
		if (this.picCount == 1) return;
		if (this.current == 1) {
			$(this.containerID + ' .x200_VOFF_slide_mask').animate({ left: -(this.picWidth*(this.picCount-1)) }, { easing: 'swing'});
			this.current = this.picCount;
		}
		else {
			$(this.containerID + ' .x200_VOFF_slide_mask').animate({ left: "+="+this.picWidth }, { easing: 'swing'});
			this.current--;
		}
		$('#'+this.offerID+'x200_IMG_med').html(this.current +' z '+ this.picCount);
	}
	
	this.toggle = function() {
		if (this.picCount == 1) return;
		if (this.mode == 'stop') {
			img = document.images[this.offerID+'imgpla'];
			img.src = img.src.replace('play', 'stop');
			this.mode = 'play';
			clearInterval(this.timer);
			this.timer = setInterval("x200_img_play_next("+ this.offerID +")", this.ctrlInterval);
		}
		else if (this.mode == 'play') {
			img = document.images[this.offerID+'imgpla'];
			img.src = img.src.replace('stop', 'play');
			this.mode = 'stop';
			clearInterval(this.timer);
		}
	}
	
	this.stop = function() {
		clearInterval(this.timer);
	}
	
	$(this.containerID + ' .x200_VOFF_slide_mask').width(this.picWidth*this.picCount);
	$('#'+this.offerID+'x200_IMG_med').html(this.current +' z '+ this.picCount);
	this.toggle();
	if (useLightbox) {
		prefix = x200_cmn_get_app_path()+'x200/themes/_images/lightbox/';
		$(this.containerID + ' .x200_VOFF_slide_pics a').lightBox();
	}
}

function x200_img_play_init(id, useLightbox) {
	if (obj = x200_img_objs[x200_img_prefix + id]) {
		x200_img_objs[x200_img_prefix + id].stop();
		delete x200_img_objs[x200_img_prefix + id];
	}
	x200_img_objs[x200_img_prefix + id] = new x200_SLIDE(id, useLightbox);
}

function x200_img_play_next(id) {
	x200_img_objs[x200_img_prefix + id].next();
}

function x200_img_play_prev(id) {
	x200_img_objs[x200_img_prefix + id].prev();
}

function x200_img_play_toggle(id) {
	x200_img_objs[x200_img_prefix + id].toggle();
}

/* DEPRECIATED */

function x200_img_setup(arr,id,qty,path){
	iid = 'i'+id;
	arr[iid] = new Array();
	arr[iid][0] = new Array(); // list of images
	arr[iid][0][0] = null;
	arr[iid][1] = 1; // current image
	arr[iid][2] = qty;
	arr[iid][3] = path;
}

function x200_img_setup_med(id,qty,path){
	x200_img_setup(x200_img_amed,id,qty,path);
}

function x200_img_setup_big(id,qty,path){
	x200_img_setup(x200_img_abig,id,qty,path);
}

function x200_img_setup_med_arr(id,img){
	iid = 'i'+id;
	a = x200_img_amed[iid][0];
	x200_img_amed[iid][0][a.length] = img;
	pre = new Image(); 
	pre.src = x200_img_get_src_med(iid,a.length-1);
}

function x200_img_setup_big_arr(id,img,grp,cmt){
	iid = 'i'+id;
	a = x200_img_abig[iid][0];
	lng = a.length;
//	alert(lng)
	x200_img_abig[iid][0][lng] = new Array();
	x200_img_abig[iid][0][lng][0] = img;
	x200_img_abig[iid][0][lng][1] = grp;
	x200_img_abig[iid][0][lng][2] = cmt;
	pre = new Image(); 
	pre.src = x200_img_get_src_big(iid,a.length-1);
}

function x200_img_prev(id){
	iid = 'i'+id;
	ctr = x200_img_amed[iid][1];
	if(ctr<=1) return;
	x200_img_set_med(id,--ctr);
}

function x200_img_next(id){
	iid = 'i'+id;
	ctr = x200_img_amed[iid][1];
	if((ctr)>=x200_img_amed[iid][2]) ctr=0;
	x200_img_set_med(id,++ctr);
}

function x200_img_set_med(id,i){
	iid = 'i'+id;
	x200_img_amed[iid][1] = i;
	document.images[id+'imgmed'].src = x200_img_get_src_med(iid,i);
	$('#'+id+'x200_IMG_med').html(i+' of '+ x200_img_amed[iid][2]);
}

function x200_img_set_big(id,i) {
	iid = 'i'+id;
	x200_img_abig[iid][1] = i;
	document.images[id+'imgbig'].src = x200_img_get_src_big(iid,i);
	info = x200_img_abig[iid][0][i][2];
	el = '#'+id+'x200_IMG_big';
	$(el).html(info);
}

function x200_img_get_src_med(iid,i){
	return x200_img_amed[iid][3] + x200_img_amed[iid][0][i];
}

function x200_img_get_src_big(iid,i){
	return x200_img_abig[iid][3] + x200_img_abig[iid][0][i][0];
}

function x200_img_show_group(id,grp) {
	iid = 'i'+id;
	for(i=1;i<=x200_img_abig[iid][2];i++){
		show = ((grp==0) || (x200_img_abig[iid][0][i][1]==grp));
		if (show) $('#'+id+'_'+i).addClass('x200_thumb_sel');
		else $('#'+id+'_'+i).removeClass('x200_thumb_sel');
	}
}
x200_imgtimer=null;
function x200_imgplaystop(id){
	i=document.images[id+'imgpla'];
	if(i.src.indexOf('play')>0) i.src=i.src.replace('play','stop');
	else i.src=i.src.replace('stop','play');
	if(x200_imgtimer){
		clearTimeout(x200_imgtimer);
		x200_imgtimer=setTimeout("x200_img_startplaying()",x200_img_PLAY_INTERVAL);
	}
}
function x200_img_startplaying(mode){
	if (mode) {
		x200_img_play=mode;
	} else {
		a=(x200_img_play=='med'?x200_img_amed:x200_img_abig);
		for(pr in a){
			off=pr.substr(1);
			if(document.getElementById('offer-tab-'+off) || document.getElementById('x200_OFF_details')) {
				i=document.images[off+'imgpla'];
				if(i && i.src.indexOf('stop')>0) x200_img_next(off);
			}
		}
	}
	x200_imgtimer=setTimeout("x200_img_startplaying()",x200_img_PLAY_INTERVAL);
}