
function init() {
	var obj = document.getElementById("menu");
	var res = document.getElementById("res");
	if(obj && obj.hasChildNodes()) {
		items = obj.childNodes;
		for(i=0; i<items.length; i++) {
			if(items[i].nodeType == 1 && items[i].className.indexOf('act') < 0) {
				items[i].style.height = "19px";
				if(document.addEventListener) {
					items[i].addEventListener("mouseover", over, false);
					items[i].addEventListener("mouseout", out, false);
					items[i].addEventListener("click", click, false);
				}else{
					items[i].attachEvent("onmouseover", over);
					items[i].attachEvent("onmouseout", out);
					items[i].attachEvent("onclick", click);
				}
			}
		}
	}
}

function over(e) {
	var obj = e.srcElement;
	if(obj) {
		obj.style.top="-5px";
		obj.style.height="44px";
	}else{
		this.style.top="-5px";
		this.style.height="44px";
	}
}

function out(e) {
	var obj = e.srcElement;
	if(obj) {
		obj.style.top="0px";
		obj.style.height="19px";
	}else{
		this.style.top="0px";
		this.style.height="19px";
	}
}

function click(e) {
	var obj = e.srcElement;
	if(obj) {
		url=obj.getAttribute("url");
	}else{
		url=this.getAttribute("url");
	}
	if(url) {
		document.location = url;
	}
}

/***************************************************************************/

Scroll = Class.create();

Scroll.prototype = {
	initialize: function(upId, downId, cntId, cntRealId) {
		this.res = $("player");

		this.oUp = $(upId);        /*   верхняя кнопка     */
		this.oDown = $(downId);    /*    нижняя кнопка    */
		this.oCnt = $(cntId);      /*    прокручиваемый контейнер     */
		this.oCntReal = $(cntRealId); /*   реальный контейнер содержимого    */
		
		this.max = parseInt(this.oCntReal.getHeight()) - parseInt(this.oCnt.getHeight()); /*    макс значение скрола = реальная высота - видимая высота    */
		this.top = this.oCnt.scrollTop; /*   положение скрола    */
		
		this.timeId = 0;
		this.i = 5; /*    шаг    */
		
		if(this.top == 0)
			this.oUp.className = "arrow up_not_active";
		else if(this.top == this.max)
			this.oDown.className = "arrow down_not_active";
			
		//this.res.innerHTML = "top: " + this.top + " / max: " + this.max + " / real height: " +this.oCntReal.getHeight() + " / height: " + this.oCnt.getHeight();
		//this.res.innerHTML+= "<br>" + "event.wheelDelta: " + Event.wheelDelta;
		
		this.oUp.onmouseover = this._over.bindAsEventListener(this);
		this.oUp.onmouseout = this._out.bindAsEventListener(this);
		this.oUp.onmousedown = this._up.bindAsEventListener(this);
		this.oUp.onmouseup = this._stop.bindAsEventListener(this);

		this.oDown.onmouseover = this._over.bindAsEventListener(this);
		this.oDown.onmouseout = this._out.bindAsEventListener(this);
		this.oDown.onmousedown = this._down.bindAsEventListener(this);
		this.oDown.onmouseup = this._stop.bindAsEventListener(this);
	},
	
	_over: function(e) {
		var self = this.getElement(e);
		if(self.className.indexOf("not_active") < 0) {
			var currClass = this.getClass(self);
			self.className = "arrow " + currClass + "_over";
		}
	},
	
	_out: function(e) {
		var self = this.getElement(e);
		if(self.className.indexOf("not_active") < 0) {
			var currClass = this.getClass(self);
			self.className = "arrow " + currClass;
		}
	},
	
	getClass: function(obj){
		return obj.className.substring(6).replace("_over","");
	},
	
	getElement: function(e) {
		var obj = e.srcElement?e.srcElement:e.target;
		return obj;
	},
	
	down_text: function() {
		if(this.top > 0){
			this.oCnt.scrollTop -= this.i;
			this.top = this.oCnt.scrollTop;
			this.oDown.className = "arrow down";
		}else if(this.top < 0) 
			this.top = 0;
		else {
			this._stop();
			this.oUp.className = "arrow up_not_active";
		}
	},
		
	_up: function () {
		var self = this;
		if(this.top > 0) 
			this.timeId = setInterval(function(){self.down_text()}, 20);	
		else return false;
	},
		
	up_text: function(flag) {
		if(this.top < this.max) {
			this.oCnt.scrollTop += this.i;
			this.top = this.oCnt.scrollTop;
			this.oUp.className = "arrow up";
		}else if(this.top > this.max)
			this.top = this.max;
		else {
			this._stop();
			this.oDown.className = "arrow down_not_active";
		}
	},
	
	_down: function() {
		var self = this;
		if(this.top < this.max) 
			this.timeId = setInterval(function(){self.up_text()}, 20);	
	}, 
	
	_stop: function(e) {
		clearInterval(this.timeId);
		this.timeId = 0;
	}
}


function addPlayer(url,img) {
	var w = "470";
	var h = "353";
	var wmode = "transparent";
	//var wmode = "window";
	vars = 'buffer_first=1.0&image=http://img.rutube.ru/thumbs/' + img;
	//&fsb=true&logo=false&newWnd=false&rAngle=0&referer=http://rutube.ru/tracks/1332949.html?v=a1c294d41bc22fcf7e03f3ef7192e613&track_id=1332949&tail=1';
	
	var t = "";
	t += '<object width="' + w +'" height="' + h + '" CODETYPE="application/x-shockwave-flash">';
	t += '<param name="movie" value="'+ url  +'"></param>';
	t += '<param name="wmode" value="'+wmode+'"></param>';
	t += '<param name="allowFullScreen" value="true"></param>';
	t += '<param name="flashVars" value="' + vars + '" ></param>';
	t += '<embed src="'+ url +'" flashVars="' + vars + '" type="application/x-shockwave-flash" wmode="'+wmode+'" width="'+ w +'" height="'+ h +'" allowScriptAccess="always" allowFullScreen="true"></embed>'
	t += '</object>';    
	document.getElementById("player").innerHTML = t;
}

//plStartId=112860
var lastUrl = "";
function addPlayerPL(url, id, img) {
    var w = "470";
	var h = "353";
	var wmode = "transparent";
	//var wmode = "window";
    
    if(id)
        var vars = "autoStart=false&plStartId=" + id + "&image=http://img.rutube.ru/thumbs/" + img;
    else
        var vars = "autoStart=false&image=http://img.rutube.ru/thumbs/" + img;
	var t = "";
	t += '<object width="' + w +'" height="' + h + '" CODETYPE="application/x-shockwave-flash">';
	t += '<param name="movie" value="http://video.rutube.ru/p/'+ url  +'"></param>';
	t += '<param name="wmode" value="'+wmode+'"></param>';
	t += '<param name="allowFullScreen" value="true"></param>';
	t += '<param value="' + vars + '" name="flashVars"></param>';
	t += '<embed src="http://video.rutube.ru/p/'+ url + '" flashVars="' + vars + '" type="application/x-shockwave-flash" wmode="'+wmode+'" width="'+ w +'" height="'+ h +'" allowScriptAccess="always" allowFullScreen="true"></embed>'
	t += '</object>';    
	document.getElementById("player").innerHTML = t; 
	$("t" + url)?$("t" + url).className = "itemAct":"";
	$("t" + lastUrl)?$("t" + lastUrl).className = "item":"";
	lastUrl = url;
}
/*  for video archive load page */
function _getUrl() {
	var mas = window.location.search.toQueryParams('&');
	var id = mas['sid']?mas['sid']:"";
	var url = mas['url']?mas['url']:'9eebc7d896ab5f955659879a6ed1e16b';
	var img = mas['img']?mas['img']:"";
    if(id || url || img){
        addPlayerPL(url, id, img);
        return true;
     }else return false;
}
/*  tabs on the left of player in index page    */
function addTrackInfo(mas) {
	var cnt = $("scrCntReal");
	var res = "";
	/*
	mas[0] - url array
	mas[1] - description array
	mas[2] - images array
	*/
	for(var i = 0; i < mas[0].length; i++) {
		if(mas[1][i]) {
			if(mas[2][i] != "")
				var img = mas[2][i];
			else
				var img = mas[0][i].substring(0,2) + "/" + mas[0][i].substring(2,4) + "/" + mas[0][i] + "-1-2.jpg";
	    res += '<div class="tr">';
			res += ' <div class="tr-thumb">';
			res += '  <img class="thumb" src="http://img.rutube.ru/thumbs/' + img + '" onClick=addPlayer(\"http://video.rutube.ru/' + mas[0][i] + '\",\"' + img + '\")>';
			res += ' </div>';
			res += mas[1][i];
			res += ' <div class="both"></div>';
      res +='</div>';
		}
	}
	cnt.innerHTML = res;
	new Scroll("scrUp","scrDown","scrCnt", "scrCntReal");
}

function showTab(currId, id, array) {
	var currObj = $(currId); /*   сделать активным        */ 
	var obj = $(id);         /*   сделать неактивным    */ 
	if(currObj.className.indexOf("Act") < 0) {
		currObj.className = currObj.className + "Act";
		obj.className = obj.className.replace("Act","");
		addTrackInfo(array);
	}else return false;
}

/*   portfolio page    */
function showPlaylists() {
	var str = "";
	pl_array.each(function(item) {
		title = item[0]?item[0]:"";
		url = item[1]?item[1]:"";
		img = item[2]?item[2]:"";
		big_img = item[3]?item[3]:"";
		startId = item[4]?item[4]:"";
		str += '<div id="t' + url + '" class="item">';
		str += ' <div class="thumb">';
		str += '<img src="http://img.rutube.ru/thumbs/'+ img +'" onClick="addPlayerPL(\'' + url + '\', \'' + startId + '\',\''+ big_img +'\')">';
		str += ' </div>';
		str += title;
		str += '</div>';
	});
	//alert(str);
	//alert($("pls"));
	$("pls").innerHTML = str + '<div class="both"></div>';
}

