/* ========================================================================= */
/* Common JavaScripts - r013 (05.11.2008)
/* ========================================================================= */

var clientIE = (navigator.userAgent.indexOf("MSIE") > -1);

// ------ Buttons

function btnMouseOver(btn)
{
	btn.style.backgroundPosition = '0px -23px';
}

function btnMouseOut(btn)
{
	btn.style.backgroundPosition = '0px 0px';
}

function btnMouseDown(btn)
{
	btn.style.backgroundPosition = '0px -46px';
}

function btnMouseUp(btn)
{
	btnMouseOver(btn);
}


// ------ Form controls & Table row over

function objMouseOver(el)
{
	if (el.className != '')
		el.setAttribute('_custClass', el.className);
	el.className += ' over';
}

function objMouseOut(el)
{
	var _class = el.getAttribute('_custClass', el.className);
	el.className = (_class ? _class : '');
	el.removeAttribute('_custClass', true);
}


function tdMouseOver(id)
{
	var objTD = null;
	for(var i=0; i>-1; i++)
	{
		objTD = document.getElementById(id+i);
		if (!objTD)
			break;
		objMouseOver(objTD);
	}
}

function tdMouseOut(id)
{
	var objTD = null;
	for(var i=0; i>-1; i++)
	{
		objTD = document.getElementById(id+i);
		if (!objTD)
			break;
		objMouseOut(objTD);
	}
}

function chkClick(el)
{
	var id = el.id.substring(0, el.id.length-3);  //Chk
	var fld = document.getElementById(id);
	fld.value = (parseInt(fld.value)==0 ? 1 : 0);
	el.className =(parseInt(fld.value)==0 ? el.className.replace('on','off') : el.className.replace('off','on') );
}

function doDelete(page, id, txt)
{
	if (!txt)
		var txt = 'записа';

	if (confirm('Сигурни ли сте, че искате да изтриете '+txt+'?'))
		document.location = '?page='+page+'&action=delete&id='+id;
}

function doSubmit(id)
{
	if (!id)
		id = 'idForm';
	document.getElementById(id).submit();
}

// ------ Other

function changeLang(lng)
{
	var str = document.location.href;
	document.location = str + ((str.indexOf('?')<0) ? '?' : '&') + 'lang=' + lng;
}

function showhide(id)
{
	var el = document.getElementById(id);
	el.style.display = (el.style.display == 'none' ? '' : 'none');
}

function show(id)
{
	document.getElementById(id).style.display = '';
}

function hide(id)
{
	document.getElementById(id).style.display = 'none';
}

function limitStr(s, maxlen)
{
	if (s.length > maxlen)
		s = s.substr(0, maxlen-1)+'...';
	return s;
}

function strTrunc(str, max, min, addstr)
{
	if (str.length > max)
	{
		if (!min)
			var min = -1;
		if (!addstr)
			var addstr = '...';

		var p = -1;
		if (min > -1)
			p = str.lastIndexOf('.', max);

		if (min > -1 && p > -1 && p > min)
			str = str.substr(0, p) + addstr;
		else
		{
			p = str.indexOf(' ', max);
			if (p > -1 && p+1 < str.length)
				str = str.substr(0, p+1) + addstr;
		}
	}
	return str;
}

function getOffsetTop(el)
{
	if (el.tagName.toLowerCase() != 'body') {
		var val = el.offsetTop;
		if (el.tagName.toLowerCase() == 'tr' || el.tagName.toLowerCase() == 'form')
			val = 0;
		return val +  getOffsetTop(el.parentNode);
	}
	return 0;
}


/* ------ Flash and video ------ */

function showVideo(url)
{
	var str = '<object id="idWMVPlayer" width="320" height="266" type="video/x-ms-wmv" classid="clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95">';

	if (clientIE)
		str += '<param name="filename" value="'+url+'"/>' +
			'<param name="autostart" value="true"/>' +
			'<param name="showtracker" value="false"/>' +
			'<param name="showcontrols" value="true"/>' +
			'<param name="showpositioncontrols" value="false"/>' +
			'<param name="showstatusbar" value="false"/>' +
			'</object>';
	else
		str += '<embed width="320" height="266" src="'+url+'" type="video/x-ms-wmv" autostart="true" showstatusbar="0" showtracker="0" showpositioncontrols="0"/>' +
			'</object>';

	document.write(str);
}

function showFlash(url, sizex, sizey, id)
{
/*	document.write(
		'<div style="width: '+sizex+'px; height: '+sizey+'px; border: #00B000 1px solid; text-align:center;"><br /><br />' +
		' <a href="http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" target="_blank" title="Изтегли Macromedia Flash Player">' +
		'	За да видите този сайт, ви е нужен Macromedia Flash Player 9.<br>' +
		'	За инсталация натиснете тук.' +
		' </a>' +
		'</div>');
	var flObj = new SWFObject(url, id, sizex, sizey, '8', '#000000');
	flObj.addParam('wmode', 'transparent');
	flObj.addParam('menu',0);
	flObj.write(id+'Div');
*/
	var str = '<object type="application/x-shockwave-flash" data="'+url+'" width="'+sizex+'" height="'+sizey+'" style="vertical-align: top"';
	if (clientIE)
		str +=  ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"';
	str += '><param name="wmode" value="transparent" />' +
		'<param name="movie" value="'+url+'" />' +
		'<param name="quality" value="high" />' +
		'<param name="menu" value="0" />' +
		'<embed src="'+url+'" width="'+sizex+'" height="'+sizey+'" style="vertical-align: top" quality="high" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
		'</object>';
	document.write(str);
}


/* ------ Fade html element ------ */

var fadeObj;

function fadeImage(id, src, w, h)
{
	fadeObj = document.getElementById(id+'FD');

	var speed = 15;
	for (var i=0; i<=10; i=i+2)
	{
		setTimeout('setOpacity('+i+')', speed*i);  // Show fade object
		setTimeout('setOpacity('+(10-i)+')', speed*(i+12));  // Hide fade object
	}
	setTimeout('setImageSrc("'+id+'", "'+src+'", '+w+', '+h+')', speed*11);
}

function setImageSrc(id, src, w, h)
{
	var img = document.getElementById(id);
	img.src = src;
	if (clientIE)
		fixPng(img);
	img.width = w;
	img.height = h;

	updateFadeObj(id, w, h);
}

function updateFadeObj(id, w, h)
{
	if (!fadeObj)
		fadeObj = document.getElementById(id+'FD');
	fadeObj.width = w;
	fadeObj.height = h;
	fadeObj.style.display = '';
}

function setOpacity(value)
{
	fadeObj.style.opacity = value / 10;
	fadeObj.style.filter = 'alpha(opacity=' + value*10 + ')';
}

