// Copyright (c) 2005-2007 Roman Malakhov

function go(u) {
	document.location=u;
}

function newWinRes(src, width, height) {
	w=window.open(src, "photo", "resizable,scrollbars,width="+width+",height="+height);
	w.focus();
	return false
}

function newWin(src, width, height) {
	w=window.open(src, "photo", "width="+width+",height="+height);
	w.focus();
	return false
}

function newWinEx(src, width, height) {
	return newWin(src, width, height + 1)
}

function closeWin(url) {
	if (wo = window.opener) {
		if (wo.closed)
			window.open(url,'_blank');
		else if (wo.location.href.search(url) == -1)
			wo.location.href = url;
		self.close()
		wo.focus()
		return false
	}
	window.open(url,'_blank')
	self.close()
	return false
}

function togglePanel(pname) {
	p = document.getElementById(pname);
	if (p) {
		if (p.style.display=='none')
			p.style.display='block';
		else
			p.style.display='none';
	}
}

function addBookmark(bookmarktitle) {
	var bookmarkurl=window.location.href
	
	if (window.sidebar) { // firefox
		window.sidebar.addPanel(bookmarktitle, bookmarkurl,"");
	} else if( document.all ) { //MSIE
		window.external.AddFavorite(bookmarkurl, bookmarktitle);
	}
}

function getEl(elId) {
	return document.getElementById(elId)
}

function showEl(pname) {
	p = document.getElementById(pname)
	if (p) {
		if (p.style.display!='block') p.style.display='block'
	}
}

function showElInline(pname) {
	p = document.getElementById(pname)
	if (p) {
		if (p.style.display!='inline') p.style.display='inline'
	}
}

function hideEl(pname) {
	p = document.getElementById(pname)
	if (p) {
		if (p.style.display!='none') {
			p.style.display='none'
			return true
		}
	}
}

function showElPos(elId, sender, offsetX, offsetY) {
	if (!offsetX) offsetX = 8
	if (!offsetY) offsetY = 4
	
	el = getEl(elId)
	if (el) {
		var pos = getElPosition(sender)
		el.style.top  = (pos.y+offsetY) + 'px'
		el.style.left = (pos.x+offsetX) + 'px'
		el.style.visibility = 'visible'
	}
}

function getElPosition(el) {
	var pos = {x:0, y:0}
	if (el.offsetParent) {
		while (el.offsetParent) {
			pos.x += el.offsetLeft
			pos.y += el.offsetTop
			el = el.offsetParent
		}
	}
	else if (el.x) {
		pos.x += el.x
		pos.y += el.y
	}
	return pos
}

function hideElPos(elId) {
	el = getEl(elId)
	if (el) el.style.visibility = 'hidden'
}

function showElVis(elId) {
	el = getEl(elId)
	if (el) el.style.visibility = 'visible'
}

function hideElVis(elId) {
	el = getEl(elId)
	if (el) el.style.visibility = 'hidden'
}

function test(e, url) {
	//alert('test: '+ e + ' url:' + url)
}

function createMedia(url, el, width, height, e) {
	w = width ? width: 320
	h = height ? height: 200
	
	var sAgent = navigator.userAgent.toLowerCase()
	var is_ie = (sAgent.indexOf("msie") != -1)
	var is_gecko = !is_ie
	
	// allow right button click
	if (!e) e = window.event
	if (e) {
		if (e.which) rightClick = (e.which == 3)
		else if (e.button) rightClick = (e.button == 2)
		
		if (rightClick) {
			return false
		}
	}
	
	// for quicktime
	isQTR = new RegExp("\\.mov$")
	is_qt = (isQTR.exec(url)) ? true : false
	
	// QT includes toolbar to the height
	if (is_qt) {
		if (is_gecko) h = h + 16;
		else h = h + 32;
	}
	else {
		// FF includes toolbar to the height
		if (is_gecko) h = h + 42
		else h = h + 64
	}
	
	
	supported = 1
	notSupportedSrc = 'Не установлен плагин для проигрывания. Загрузите <a href="'+url+'">ролик</a> на диск.<br/>'
	
	
	// delete link
	el.href=''
	el.removeAttribute('onclick', 0)
	el.removeAttribute('onmousedown', 0)
	el.innerHTML = ''
	
	if (!is_qt) {
		src='<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" id="player" width="'+w+'" height="'+h+'">' +
		'<param name="url" value="'+url+'"/><param name="src" value="'+url+'"/>'+
		'<param name="showcontrols" value="true"/><param name="autostart" value="true"/>'+
		'<!--[if !IE]>-->'+
		'<object type="video/x-ms-wmv" data="'+url+'" width="'+w+'" height="'+h+'">'+
		'  <param name="src" value="'+url+'"/>'+
		'  <param name="controller" value="true"/><param name="autostart" value="true"/>'+
		'</object>'+
		'<!--<![endif]-->'+
		'</object>';
	}
	if (is_qt) {
		src='<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0" '+
		'width="'+w+'" height="'+h+'">'+
		'<param name="src" value="'+url+'"/>'+
		'<param name="autoplay" value="true"/>'+
		'<param name="controller" value="true"/>'+
		'<param name="type" value="video/quicktime"/>'+
		'<embed src="'+url+'" autoplay="true" type="video/quicktime" width="'+w+'" height="'+h+'" controller="true" '+
		'pluginspage="http://www.apple.com/quicktime/download/"></embed>'+
		'</object>';
	}
	
	if (is_gecko) {
		if (opar = el.parentNode) {
			try {
				oel = document.createElement('div')
				oel.innerHTML = src
				opar.insertBefore(oel, el)
			}
			catch (ex) {
				//alert("Error occured: " + ex.message); 
				el.innerHTML = notSupportedSrc
			}
		}
	}
	else if (is_ie) {
		try {
			el.insertAdjacentHTML('AfterEnd', src)
		}
		catch (ex) {
			//alert("Error occured: " + ex.message); 
			el.insertAdjacentHTML('AfterEnd', notSupportedSrc)
		}
	}
	
	return
}

function showComment(elLink, elBody) {
	hideEl(elLink)
	p = document.getElementById(elBody)
	if (p) {
		p.className=''
	}
}
