//begin : скрипт для Лучшего видео

//размеры окна

function getWinSize() {
	var myWidth = 0, myHeight = 0;
	
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
        return {w:984,h:myHeight};
}

//переключение по категориям "По баллам" и "По просмотрам"
var total_tabs = 3;

function hide_best() {

for (var j=0; j<total_tabs; j++) {
	if (gebi("best"+j+"ContainerId")) gebi("best"+j+"ContainerId").style.display="none";
    if (gebi("best"+j+"ContainerId_first")) gebi("best"+j+"ContainerId_first").style.display="none";
    if (gebi("link"+j)) gebi("link"+j).className="";
    if (gebi("scroll_left_best"+j)) gebi("scroll_left_best"+j).style.display="none";
    if (gebi("scroll_right_best"+j)) gebi("scroll_right_best"+j).style.display="none";
 }  
}

function switch_best(i) {

  hide_best();
  if (gebi("best"+i+"ContainerId_first")) gebi("best"+i+"ContainerId_first").style.display="block";   
  if (gebi("best"+i+"ContainerId")) gebi("best"+i+"ContainerId").style.display="block";
  if (gebi("link"+i)) gebi("link"+i).className="act";
  if (gebi("scroll_left_best"+i)) gebi("scroll_left_best"+i).style.display="block";
  if (gebi("scroll_right_best"+i)) gebi("scroll_right_best"+i).style.display="block";
  
}

//end переключение по категориям "По баллам" и "По просмотрам"

// Array
var videopreArray = {};

// Get function
function getVideoPre(id) {
	if (typeof(videopreArray[id]) == "undefined") {
		videopreArray[id] = new videopre(id);
	}
	return videopreArray[id];

}

// Constructor
function videopre(id) {
	this.id = id;
	this.inited = false;
	this.noAngles = false;
	
	this.itemArray = [];
	this.curLeft = 0;
	this.itemOnPage = 0;
	this.defaultContainerWidth = 630;
	this.containerWidth = 0;
	this.sizeCoeff = 1;


	this.emptyAngleUrl = "http://img.mail.ru/r/video2/ico_scroll_empty.gif";
	this.leftAngleUrl = "";
	this.rightAngleUrl = "";
	// Const
	
	
	this.leftAnglePrefix = "LeftAngle";
	this.rightAnglePrefix = "RightAngle";
	this.noscriptPrefix = "Noscript";
	// Objects
	this.leftAngle = null;
	this.rightAngle = null;
	this.noscript = null;
	this.container = null;
	// AJAX
	this.urlBegin = "";
	this.urlEnd = "";
	this.circleStart = 1;
	this.circleEnd = 10;
	this.loadedXML = 0;
	// Errors
	this.errors = {
		1 : "INITIALISATION ERROR: Left angle empty",
		2 : "INITIALISATION ERROR: Right angle empty"
	}
}

// Prototype
videopre.prototype = {
	adaptive : function(arr) {
		for (var i = 0; i < arr.length; i++) {
			if (typeof arr[i]["link"] == "undefined") continue;
			var len = this.itemArray.length;
			
			if (this.id == "best0" || this.id == "best1" ||  this.id == "best2") {
			this.itemArray[len] = {};
			this.itemArray[len]["url"] = arr[i]["link"];
			this.itemArray[len]["prevurl"] = arr[i]["img"];
			this.itemArray[len]["videoname"] = arr[i]["video_name"];
			this.itemArray[len]["usermail"] = arr[i]["email"];
			this.itemArray[len]["usernick"] = arr[i]["user_name"];
			this.itemArray[len]["userdir"] = arr[i]["user_info"];
			this.itemArray[len]["duration"] = arr[i]["duration"];
			this.itemArray[len]["rate"] = (arr[i]["rate"]) ? arr[i]["rate"] : 0 ;
			}
			
			if (this.id == "now") {
			this.itemArray[len] = {};
			this.itemArray[len]["url"] = arr[i]["link"];
			this.itemArray[len]["prevurl"] = arr[i]["img"];
			this.itemArray[len]["videoname"] = arr[i]["video_name"];
			this.itemArray[len]["duration"] = arr[i]["duration"];
			}
			
		}
	},
	init : function(id) {
		this.leftAngle = gebi(this.id + this.leftAnglePrefix);
		if (this.leftAngle == null && !this.noAngles) {
			alert(this.errors[1]);
			return;
		}
		this.rightAngle = gebi(this.id + this.rightAnglePrefix);
		if (this.rightAngle == null && !this.noAngles) {
			alert(this.errors[2]);
			return;
		}
		this.noscript = gebi(this.id + this.noscriptPrefix);
		if (this.noscript == null) {
			alert(this.errors[2]);
			return;
		}
		// Replace noscript
		this.container = document.createElement("div");
		this.container.id = this.id + "ContainerId";
		this.container.style.display = "none";
		this.noscript.parentNode.replaceChild(this.container, this.noscript);
		this.inited = true;
		//this.drawPreview();
	},
	detectSize : function() {
		this.containerWidth = parseInt(getWinSize().w * 0.9 );
		if(this.id == "best0" || this.id == "best1" || this.id == "best2") this.containerWidth = this.containerWidth * 0.7 + 140 ;
		this.containerWidth = this.containerWidth < this.defaultContainerWidth ? this.defaultContainerWidth : this.containerWidth;
		this.itemOnPage = Math.floor(this.containerWidth / this.itemWidth) * this.itemCoeff;
		
		
	},
	drawPreview : function() { 
	

		this.detectSize();
		
		if (this.itemArray.length - this.curLeft < this.itemOnPage * 2 ) {
			this.loadPreview();
			return;
		}
		
		this.container.style.display = "block";
		
		
		if (this.id == "best0" || this.id == "best1" || this.id == "best2") {
	
		var code = "<table class=\"stripevideos\"><tr>";
		for (var i = this.curLeft; i < this.curLeft + this.itemOnPage; i++) {
			if (this.itemArray[i]) {
				code += "<td valign=\"top\"><div class=\"preview\"><div><a class=\"pv\" href=\""+this.itemArray[i].url+"\" onmouseover=\"pv.getObj(this).display({dynamic : true})\"><img class=\"prevpic\" src=\""+this.itemArray[i].prevurl+"\" width=\"120\" height=\"90\" border=\"0\" alt=\"\" title=\"\" \/><\/a><\/div><div class=\"duration\">"+show_duration(this.itemArray[i].duration)+"<\/div><\/div>\
				<div><div class=\"preview_name\"><a href=\""+this.itemArray[i].url+"\" class=\"video_videoname\">"+this.itemArray[i].videoname+"<\/a><\/div><div class=\"preview_author\"><a href=\"http://www.mail.ru/agent?message&to="+this.itemArray[i].usermail+"\"><img src=\"http://status.mail.ru/?" + this.itemArray[i].usermail + "\" class=\"f2_ico\" alt=\"Щелкни, чтобы пообщаться с этим человеком в Mail.Ru Агенте\" title=\"Щелкни, чтобы пообщаться с этим человеком в Mail.Ru Агенте\" \/><\/a><a href=\"" + this.itemArray[i].userdir + "\" class=\"video_username\">"+cutName(this.itemArray[i].usernick)+"<\/a><span class=\"user-info-menu\" onclick=\"return f2_userInfo('"+ this.itemArray[i].usermail +"', this, event, '"+this.itemArray[i].userdir+"')\"><img src=\"http://img.mail.ru/0.gif\" class=\"ico_dropdown\" alt=\"Меню пользователя\" title=\"Меню пользователя\" \/><\/span><\/div><\/div><\/td>"; 
				
			} else code += "<td>&nbsp;<\/td>";
			if (i != this.curLeft + this.itemOnPage - 1 && i != this.curLeft + this.itemOnPage / 2 - 1) code += "<th>&nbsp;<\/th>\n";
			if (i == this.curLeft + this.itemOnPage / 2 - 1) code += "<\/tr><tr>\n";
		}
		code += "<\/tr><\/table>";
		
		//preloader
		var _this = this;
		var tempCont = document.createElement('div');
		tempCont.style.visibility = 'hidden';
		tempCont.style.position = 'absolute';
		tempCont.style.top = '0';
		tempCont.style.left = '0';
		tempCont.innerHTML = code;
		tempCont.loadedCount = 0;
		var sid = this.id+"ContainerId_first";
		var img_count = 0;
		tempCont = document.body.appendChild(tempCont);
		var imgList = tempCont.getElementsByTagName('img');
		for(var i = 0; i < imgList.length; i++ ) {
		  if (imgList[i].className.indexOf("prevpic") == -1) continue;
		  img_count ++;
			imgList[i].onload = function() {
				tempCont.loadedCount ++;
				
				if( tempCont.loadedCount == img_count ) {
					if(gebi(sid)) gebi(sid).parentNode.removeChild(gebi(sid));
					_this.container.innerHTML = code; 
					tempCont.parentNode.removeChild(tempCont);
				}
			};
		}
		
		}else if (this.id == "now") {
	    
			var code = "<table class=\"stripevideos\"><tr>";
			for (var i = this.curLeft; i < this.curLeft + this.itemOnPage; i++) {
				code += "<td valign=\"top\"><div class=\"preview\" ><div><a class=\"pv\" href=\""+this.itemArray[i].url+"\" onmouseover=\"pv.getObj(this).display({dynamic : true})\"><img class=\"prevpic\" src=\""+this.itemArray[i].prevurl + "\" width=\"120\" height=\"90\" border=\"0\" alt=\"\" title=\"\" \/><\/a><\/div><div class=\"duration\">"+show_duration(this.itemArray[i].duration)+"<\/div><\/div><\/td>";
				if (i != this.curLeft + this.itemOnPage - 1) code += "<th>&nbsp;<\/th>\n";
			}
			code += "<\/tr><\/table>";
			
			var _this = this;
			var tempCont = document.createElement('div');
			tempCont.style.visibility = 'hidden';
			tempCont.style.position = 'absolute';
			tempCont.style.top = '0';
			tempCont.style.left = '0';
			tempCont.innerHTML = code;
			tempCont.loadedCount = 0;
			var sid = this.id+"ContainerId_first";
			var img_count = 0;
			tempCont = document.body.appendChild(tempCont);
			var imgList = tempCont.getElementsByTagName('img');
			
      for(var i = 0; i < imgList.length; i++ ) {
			
      if (imgList[i].className.indexOf("prevpic") == -1) continue;
		  img_count ++;
				imgList[i].onload = function() {
					tempCont.loadedCount ++;
					
					if( tempCont.loadedCount == img_count ) {
						if(gebi(sid)) gebi(sid).parentNode.removeChild(gebi(sid));
					  _this.container.innerHTML = code; 
					  tempCont.parentNode.removeChild(tempCont);
					}
				};
			}
			//end preloader
		
		}
			
	},
	goLeft : function() {
		this.curLeft -= this.itemOnPage;
		this.curLeft = this.curLeft > 0 ? this.curLeft : 0;
		if (!this.noAngles) this.drawAngles();
		this.drawPreview();
		return false;
	},
	goRight : function() {
	  	if (this.id == "now") this.curLeft++;
		else this.curLeft += this.itemOnPage;
		if (!this.noAngles) this.drawAngles();
		this.drawPreview();
		return false;
	},
	drawAngles : function() {
		this.leftAngle.src = this.curLeft ? this.leftAngleUrl : this.emptyAngleUrl;
		
	},
	loadPreview : function() {
		if (this.loadedXML < this.circleEnd) {
			this.loadedXML++;
			var _this = this;
			ajax_call_static(this.urlBegin + this.loadedXML + this.urlEnd, function(obj) {
				for (var i = 0; i < obj['medialist'].length; i++) {
					_this.itemArray[_this.itemArray.length] = obj['medialist'][i];
				}
				_this.drawPreview();
			});
		}  else if (this.itemArray.length > 0) {
			var copyItemArray = [];
			copyItemArray = this.itemArray;
			this.itemArray = this.itemArray.concat(copyItemArray);
			this.drawPreview();
		}
	}
}

//end : скрипт для Лучшего видео


function v2_createblocks(v2_num, t)
{
	var winsizes=getWinSize();
	var v2_howmanyblocks;
	var width_block = 150;
	var v2_clientwidth;
	if (t == 1) v2_clientwidth = Math.round(winsizes.w * 0.9);
	if (t == 2) v2_clientwidth = Math.round(winsizes.w * 0.9 * 0.7) + 140 ;
	if (v2_clientwidth <= 700) v2_clientwidth = 700;
	var v2_counter = 0;
	var v2_howmanyblocks = parseInt(v2_clientwidth / width_block); 
	if(t == 2) v2_howmanyblocks = v2_howmanyblocks * 2;
	var v2_start = 1;
	var v2_current;
	var v2_name;
	var i=1;
	var html;
	var show_userinfo;
	if (v2_num == 1) { v2_name = "nowContainerId_first"; items=now_videos ; show_userinfo = 0; } //смотрят сейчас
	if (v2_num == 2) { v2_name = "v2_new"; items=v2_new ;  show_userinfo = 1; } //новое видео
	if (v2_num == 3) { v2_name = "v2_sms"; items=v2_sms ;  show_userinfo = 1; } //смс-топ
	if (v2_num == 4) { v2_name = "best1ContainerId_first"; items=best_videos_rating ;  show_userinfo = 1; } //лучшее видео по баллам
	if (v2_num == 5) { v2_name = "best2ContainerId_first"; items=best_videos_views ;  show_userinfo = 1; } //лучшее видео по просмотрам
	if (v2_num == 6) { v2_name = "best0ContainerId_first"; items=best_videos_medals ;  show_userinfo = 1; } //лучшеиз лучших
	var v2=document.getElementById(v2_name);  

	html='<table class="stripevideos"><tr>';
	while (v2_counter < v2_howmanyblocks && v2_counter <= 35) {
		html+='<td valign="top">';
		if (v2_num == 1) { if (items[v2_counter]) html+='<div class="preview" ><div><a class="pv" href="' + items[v2_counter].link+'" onmouseover="pv.getObj(this).display({dynamic : true})"><img src="' + items[v2_counter].img + '" width="120" height="90" border="0" alt="" title="" \/><\/a><\/div><div class="duration">' + show_duration(items[v2_counter].duration) + '<\/div><\/div>\n';}

		if (v2_num == 2 || v2_num == 3 || v2_num == 4 || v2_num == 5 ||v2_num == 6)
		{
			//if (!items[v2_counter].rate) items[v2_counter].rate = "0";
			if (items[v2_counter]) 
			html+='<div class="preview" ><div><a class="pv" href="' + items[v2_counter].link + '" onmouseover="pv.getObj(this).display({dynamic : true})"><img src="' + items[v2_counter].img + '" width="120" height="90" border="0" alt="" title="" \/><\/a><\/div><div class="duration">' + show_duration(items[v2_counter].duration) + '<\/div><\/div><div><div class="preview_name"><a href="' + items[v2_counter].link + '" class="video_videoname">' + items[v2_counter].video_name + '<\/a><\/div><div class="preview_author"><a href="http://www.mail.ru/agent?message&to=' + items[v2_counter].email + '"><i email="' + items[v2_counter].email + '" class="f2_mrim_status f2_off"><img src="http://img.mail.ru/0.gif" class="f2_ico" alt="Щелкни, чтобы пообщаться с этим человеком в Mail.Ru Агенте" title="Щелкни, чтобы пообщаться с этим человеком в Mail.Ru Агенте" \/></i><\/a><a href="' + items[v2_counter].user_info + '" class="video_username">' + cutName(items[v2_counter].user_name) + '<\/a><span class="user-info-menu" onclick="return f2_userInfo(\''+ items[v2_counter].email +'\', this, event, \'' + items[v2_counter].user_info + '\')"><img src="http://img.mail.ru/0.gif" class="ico_dropdown" alt="Меню пользователя" title="Меню пользователя" \/><\/span><\/div><\/div>';
		}
		html+='<\/td>';

		if (t == 1 && i != v2_howmanyblocks ) html += '<th>&nbsp;<\/th>';
		if (t == 2 && (i != v2_howmanyblocks / 2  && i != v2_howmanyblocks )) html += '<th>&nbsp;<\/th>';
		if ( t == 2 && i == v2_howmanyblocks / 2  ) html += '<\/tr><tr>';
		v2_counter++;
		v2_start++;
		i++; 
	}
	html+='<\/tr><\/table>'; 
	v2.innerHTML=html;
}

var arrMRIMStatus;
function getTagsMrimStatus(class_name,tag_name,el)
{
	var mrimstatus_url = '';
	if (!el) el = document;
	mrimstatus_url = 'http://' + VideoPerlHost() + '/proxy-mrim-status?';
	var arrTagsI = document.getElementsByTagName(tag_name);
	var arrTagsIL = arrTagsI.length;
	arrMRIMStatus = [];
	for (var i = 0; i < arrTagsIL; i++)
	{
		if (arrTagsI[i].className && arrTagsI[i].className != 'undefined' && arrTagsI[i].className.indexOf(class_name) != -1) {
			var comma = arrMRIMStatus.length > 0 ? ',' : '';
			arrMRIMStatus[arrMRIMStatus.length] = arrTagsI[i];
			mrimstatus_url += comma + arrTagsI[i].attributes.email.value;
		}
	}
	return mrimstatus_url;
}


function mrimstatus_callback(mrimstatus,error)
{
	var arrMrimStatus_callback = mrimstatus.split('');
	var arrMrimStatus_callbackL = arrMrimStatus_callback.length;
	var arrMRIMStatusL = arrMRIMStatus.length;
	for (var i = 0; i < arrMRIMStatusL; i++)
	{
		switch (arrMrimStatus_callback[i]) {
		case '1' :
			arrMRIMStatus[i].className = 'f2_mrim_status f2_on';
			break;
		case '2' :
			arrMRIMStatus[i].className = 'f2_mrim_status f2_left';
			break;
		default :
			arrMRIMStatus[i].className = 'f2_mrim_status f2_off';
			break;
		}
	}
}