var filmStripTimer;
function filmStrip(hnd, tabsCnt, skipId, thumbsCnt, videosCnt, mode, period, ownerId, categoryId, videos){
	this.hnd = hnd;
	this.tabsCnt = tabsCnt;
	this.skipId = skipId;
	this.mode = mode;
	this.ownerId = ownerId;
	this.categoryId = categoryId;
	this.period = period;
	this.thumbsCnt = thumbsCnt;
	this.currentThumb = 2;
	this.currentTab = 0;
	this.tabs = new Array();
	this.videos = new Array();
	this.pagesCnt = new Array();
	this.videosCnt = new Array();
	for(var i = 0; i < this.tabsCnt; i++){
		this.tabs[i] = {};
		this.tabs[i].page = 0;
		this.videos[i] = {};
		this.pagesCnt[i] = 0;
		this.videosCnt[i] = 0;
	}
	this.videos[0] = videos;
	this.pagesCnt[0] = Math.ceil(videosCnt / thumbsCnt);
	this.videosCnt[0] = videosCnt;
	
	this.dragId = false;
	
	this.dragMove = function(ev){
		ev = ev || window.event;
		if(ev.pageX || ev.pageY){ 
			var coords = {x:ev.pageX, y:ev.pageY}; 
		}else{
			var coords = {
				x:ev.clientX + document.body.scrollLeft - document.body.clientLeft, 
				y:ev.clientY + document.body.scrollTop  - document.body.clientTop 
			}
		}
		var dragBox = document.getElementById("j_dragBox_0");
		document.getElementById("asd").innerHTML = "X: " + coords.x + " Y: " + coords.y + " ID is: " + dragBox;
		dragBox.style.top = coords.y + "px";
		dragBox.style.left = coords.x + "px";
	}
		
	this.drag = function(frame){
		this.dragId = frame;
		var dragBox = document.getElementById("j_dragBox_" + this.hnd);
		dragBox.innerHTML = document.getElementById(frame).innerHTML;
		dragBox.style.display = 'block';
		document.onmousemove = this.dragMove;
		//document.onmouseup = this.drop;
	}
	this.drop = function(frame){
		if(!this.dragId){
			return;
		}
		document.body.style.cursor = "default";
		alert("Dropped " + this.dragId + " on " + frame);
		this.dragId = false;
		document.getElementById("j_dragBox_" + hnd).style.display = 'none';
		document.onmousemove = '';
		//document.onmouseup = '';
	}
	
	//window.clearInterval();
	
	this.rotateThumbStart = function(index){
		var thumb = document.getElementById("j_thumb_" + this.hnd + "_" + index);
		var vindex = this.tabs[this.currentTab].page * this.thumbsCnt + index;
		if(vindex >= this.videosCnt[this.currentTab]){
			vindex = vindex - this.videosCnt[this.currentTab];
		}
		if(this.currentThumb == this.videos[this.currentTab][vindex].thumb.length - 1){
			this.currentThumb = 0;
		}else{
			this.currentThumb++;
		}
		
		
		var tmpThumb = new Image();
		filmStripThumbId = "j_thumb_" + this.hnd + "_" + index;
		tmpThumb.onload = function(){
			eval("document.getElementById('" + filmStripThumbId + "').style.backgroundImage = \"url(" + this.src + ")\";");
		}
		tmpThumb.src = this.videos[this.currentTab][vindex].thumb[this.currentThumb];
		
		
		//thumb.style.backgroundImage = "url(" + this.videos[this.currentTab][vindex].thumb[this.currentThumb] + ")";
		//document.getElementById("testSrc").innerHTML = "url(" + this.videos[index].thumb[this.currentThumb] + ")";
	}
	
	
	this.rotateThumbStop = function(index){
		var thumb = document.getElementById("j_thumb_" + this.hnd + "_" + index);
		var vindex = this.tabs[this.currentTab].page * this.thumbsCnt + index;
		if(vindex >= this.videosCnt[this.currentTab]){
			vindex = vindex - this.videosCnt[this.currentTab];
		}
		this.currentThumb = 2;
		thumb.style.backgroundImage = "url(" + this.videos[this.currentTab][vindex].init_thumb + ")";
		window.clearInterval(filmStripTimer);
		//document.getElementById("testSrc").innerHTML = "url(" + this.videos[index].thumb[this.currentThumb] + ")";
	}
	
	this.loadData = function(page){
		var con = new rJSON();
		var params = {};
		params.module = "film_strip";
		params.mode = this.mode;
		params.offset = page * 5;
		params.limit = 100;
		params.id = this.skipId;
		params.period = this.period;
		params.owner_id = this.ownerId;
		params.category_id = this.categoryId;
		
		res = con.getData("exec.php", params);
		
		if(res){
			this.videos[this.currentTab] = res.video;
			this.pagesCnt[this.currentTab] = Math.ceil(res.videosCnt / thumbsCnt);
			this.videosCnt[this.currentTab] = res.videosCnt;
			return true;
		}else{
			return false;
		}
	}
	
	this.setThumbs = function(){
		var j = this.tabs[this.currentTab].page * this.thumbsCnt;
		
		for(var i = 0; i < thumbsCnt; i++, j++){
			if(j >= this.videosCnt[this.currentTab]){
				j = 0;
			}
			var thumb = document.getElementById("j_thumb_" + this.hnd + "_" + i);
			thumb.style.backgroundImage = "url(" + this.videos[this.currentTab][j].init_thumb + ")";
			var thumbLink = document.getElementById("j_thumbPicLink_" + this.hnd + "_" + i);
			thumbLink.href = this.videos[this.currentTab][j].href;
			thumbLink.title = "Видео клип " + this.videos[this.currentTab][j].title;
			var title = document.getElementById("j_title_" + this.hnd + "_" + i);
			title.innerHTML = this.videos[this.currentTab][j].title;
			title.href = this.videos[this.currentTab][j].href;
			title.title = "Видео клип " + this.videos[this.currentTab][j].title;
			document.getElementById("j_prevTime_" + this.hnd + "_" + i).innerHTML = this.videos[this.currentTab][j].duration;
			document.getElementById("j_prevRating_" + this.hnd + "_" + i).style.backgroundImage =  "url('imgs/rating" + this.videos[this.currentTab][j].rating + "ico.png')";
			
		}
	}
	
	this.nextPage = function(){
		if(this.tabs[this.currentTab].page == this.pagesCnt[this.currentTab] - 1){
			var page = 0;
		}else{
			var page = this.tabs[this.currentTab].page + 1;
		}
		this.tabs[this.currentTab].page = page;
		this.setThumbs();
	}
	
	this.prevPage = function(){
		if(this.tabs[this.currentTab].page == 0){
			var page = this.pagesCnt[this.currentTab] - 1;
		}else{
			var page = this.tabs[this.currentTab].page - 1;
		}
		this.tabs[this.currentTab].page = page;
		this.setThumbs();
	}
	
	this.setTab = function(tab, mode){
		this.currentTab = tab;
		this.mode = mode;
		if(!this.videos[tab][0]){
			this.tabs[this.currentTab].page = 0;
			var res = this.loadData(0);
		}
		this.setThumbs();
		for(var i = 0; i < this.tabsCnt; i++){
			if(i == tab){
				document.getElementById("j_tab_" + hnd + "_" + i).className = 'rvt_selected';
			}else{
				document.getElementById("j_tab_" + hnd + "_" + i).className = '';
			}
		}
	}
	
	this.setPeriod = function(period){
		this.period = period;
		var res = this.loadData(0);
		if(res){
			this.setThumbs();
			this.tabs[this.currentTab].page = 0;
		}
	}
}
	
