// Служебные функции
// код функций getPageScroll() и getPageSize() взят из бибилиотеки Lightbox2 (http://www.huddletogether.com/projects/lightbox2/)

// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
function getPageScroll(){
	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) { // all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}

	arrayPageScroll = new Array(xScroll,yScroll)
	return arrayPageScroll;
}

// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
function getPageSize(){

	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}


	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		}
		else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	}
	else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
	return arrayPageSize;
}

// ---------------------------------------------------

function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}


// Работа с блоком вывода информации над контентом страницы
// *****************************************************************************

// Формирование блока оверлэя
function makeOverlay() {
	hideSelectBoxes();
	var overBox = document.getElementById('overlay');
	overBox.style.display = 'block';
	// Назначаем прозрасность элементу
	if(/MSIE/.test(navigator.userAgent)) { overBox.style.filter = 'alpha(opacity=80)';} //Если это контуженый по колено Осел
	else { overBox.style.opacity = 0.8;} // коли нормальный браузер

	// Устанавливаем высоту div'а
	var wSize = getPageSize();
	overBox.style.height = wSize[1];

	return false;
}

function closeOverlay() {
	showSelectBoxes();
	var overBox = document.getElementById('overlay');
	overBox.style.display = 'none';
	overBox.innerHTML = '';

	var infoBox = document.getElementById('infoBox');
	infoBox.style.display = 'none';
	infoBox.innerHTML = '';
}


// Создание контейнера для показа видео
function makeVideoBox(videoWidth, videoHeight, videoNote){
	makeOverlay();
	var infoBox = document.getElementById('infoBox');
	infoBox.style.display = 'block';

	// Устанавливаем отступ от верхнего края до места показа ролика
	var wSize = getPageSize();
	var wScroll = getPageScroll();
	infoBox.style.top = wScroll[1]+wSize[3]/2-videoHeight/2;

	var infoContent = '<div style="width:100%; text-align: center;"><table class="videoView" align="center" style="width: '+videoWidth+';"> '
				+'<tr><td class="video">';
	infoContent += '<div id="overlayPlayer"></div>';
	infoContent += '</td><td class="close"><img onClick="closeOverlay(); return false;" '
				+'src="/www_lib/video/imgs/ico_close.gif" width="13" height="13" border="0" alt="[x]" title="закрыть" /></div>'
	infoContent += '</td></tr>';
	if(videoNote){
		infoContent +='<tr><td colspan="2" class="videoNote">'+videoNote+'</td></tr>';
	}
	infoContent += '</table></div>';
	infoBox.innerHTML = infoContent;
};

// *** Показ видео - настройка и запуск видеоролика *************************************
// *****************************************************************************
function showVideo(vURL, vWidth, vHeight, vText, vEvent1, vEvent3, vShowCounterId) {
	// Если функцию вызвали без параметров - не работаем
	if(!arguments.length) return false;
	// Определяемся с умолчаниями для аргументов функции
	if(!vWidth) vWidth = 400;
	if(!vHeight) vHeight = 300;
	if(!vText) vText = '';
	if(!vEvent1) vEvent1 = 'video';
	if(!vEvent3) vEvent3 = vURL;
	if(!vShowCounterId) vShowCounterId = false;

	// Запускаем просмотр видеоролика
	makeVideoBox(vWidth, vHeight, vText);

	// Регистрируем события запуска видео

/*
	JsHttpRequest.query(
		"/_banners/_video/ajax_show_video_stat.php",
		{
			'event1' : vEvent1,
			'event2' : 'play',
			'event3' : vEvent3,
			'vShowCounterId' : vShowCounterId
		},
		function (result, errors){

		},
		false
	);
*/
	/* Формируем блок вызова FLV-плеера --- */
	var so = new SWFObject('/www_lib/video/flvplayer.swf','overlaySwf',vWidth,vHeight,'8');
	so.addParam('allowscriptaccess','always');
	so.addParam('allowfullscreen','true');
	//so.addVariable('logo','http://www.site.ru/imgs/logo.png');
	so.addVariable('height',vHeight);
	so.addVariable('width',vWidth);
	so.addVariable('file',vURL);
	so.addVariable('displayheight',vHeight);
	so.addVariable('autostart','true');
	so.addVariable('bufferlength','5');
	so.write('overlayPlayer');
}