var brows = getBrowserName();

function pInt(s) {
	var tint = parseInt(s);
	if (isNaN(tint)) tint = 0;
	return tint;
}

function onFocusLogin(elem,tmp) {
	if (elem.value==tmp)
		elem.value='';
}
function onBlurLogin(elem,tmp) {
	if (elem.value=='')
		elem.value=tmp;
}

function getDimensions()
{
	var el = $(window);
	// fix a jQuery/Opera bug with determining the window height
	var h = $.browser.opera && $.browser.version > '9.5' && $.fn.jquery <= '1.2.6' ? document.documentElement['clientHeight'] :
		$.browser.opera && $.browser.version < '9.5' && $.fn.jquery > '1.2.6' ? window.innerHeight :
		el.height();
	return [h, el.width()];
}

function loadXMLDocAsync(url,readyFunc) {
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				readyFunc(xmlhttp.responseText);
			}
		}
	};
	xmlhttp.send(null);
}

function loadXMLDocAsyncRes (a)
{
	$('#div_message_body').html(a);
}

function dis(e,e2n){
	document.getElementById(e2n).disabled = e;
}
function checkSomething(prefix,num,c) {
	var ii,tmpelem;
	for (ii=0;ii<num;ii++) {
		tmpelem = document.getElementById(prefix+ii);
		if (tmpelem)
			tmpelem.checked = c
	}
}


function CPDialog (res)
{
	cs_color = res.value;
	if (cs_color == '')
		cs_color = '000000';
	w = getDimensions();
	$('#color-picker').css({
			display: 'none',
			//height: w[0],
			//width: w[1],
			position: 'fixed',
			left: w[1] / 2 - 550 / 2,
			top: w[0] / 2 - 310 / 2,
			zIndex: 999
		});
	$('#color-picker').jPicker({color:{active:cs_color}},
		function (color)
		{
			var hex = color.get_Hex();
			res.value = hex;
			res.onchange();
			$('#color-picker').css({display: 'none'});
			//$(this).destroy();
		},
		function (color)
		{
		},
		function (color)
		{
			//alert('a');
			//cs_color = ;
			$('#color-picker').css({display: 'none'});
			//$(this).destroy();
		}
	);
}

function encFetched(a) {
	var ind = 0;
	switch (a) {
		case 'utf-8': ind=0; break;
		case 'windows-1251': ind=1; break;
		case 'iso-8859-1': ind=2; break;
	}
	var DText = 'Скорее всего указанный сайт использует кодировку <a href="#" onclick="document.getElementById(\'usf_enc\').selectedIndex='+ind+';$.modal.close();return false;">'+a+'</a>.<br />Нажмите по ссылке, для выбора этой кодировки.';
	$('#div_message_body').html(DText);
}

function EncodingDialog (url) {
	var DText = '<img src="/images/loading.gif" alt="" /> Пожалуйста, подождите...';
	var Links = '<a class="simplemodal-close" href="#">Закрыть</a>';
	$('#div_message_body').html(DText);
	$('#div_message_foot').html(Links);

	$('#div_sm_dialog').css({"height": "165px"});
	$('#div_sm_dialog').css({"width": "490px"});
	$('#div_message_body').css({"height": "78px"});

	$('#div_sm_dialog').modal({ closeHTML: '',containerCss:{"height":"188px","width":"520px"}, onOpen: modalOpen, onClose: modal_Close, overlayClose:true });


	loadXMLDocAsync('/main.php?act=getcharset&pageurl='+url,encFetched);
}

function uinfoFetched(a) {
	$('#div_message_body').html(a);
}

function userInfoDialog (id) {
	var DText = '<img src="/images/loading.gif" alt="" /> Пожалуйста, подождите...';
	var Links = '<a class="simplemodal-close" href="#">Закрыть</a>';
	$('#div_message_body').html(DText);
	$('#div_message_foot').html(Links);

	$('#div_sm_dialog').css({"height": "443px"});
	$('#div_sm_dialog').css({"width": "490px"});
	$('#div_message_body').css({"height": "360px"});

	$('#div_sm_dialog').modal({ closeHTML: '',containerCss:{"height":"500px","width":"520px"}, onOpen: modalOpen, onClose: modal_Close, overlayClose:true });


	loadXMLDocAsync('/main.php?act=userinfo&id='+id,uinfoFetched);
}

function DEDialog (elem,uname,t) {
	switch (t) {
		case 1:
			DText = "Вы действительно хотите удалить пользователя <b>"+uname+"</b>?"; break;
		case 2:
			DText = "Вы действительно хотите удалить категорию<div class=\"mt10px\">&laquo;<b>"+uname+"</b>&raquo;?</div>"; break;
		case 3:
			DText = "Вы действительно хотите удалить объявление <b>"+uname+"</b>?"; break;
		case 4:
			DText = "Вы действительно хотите удалить сайт<div class=\"mt10px\">&laquo;<b>"+uname+"</b>&raquo;?"; break;
		case 5:
			DText = "Вы действительно хотите удалить новость<div class=\"mt10px\">&laquo;<b>"+uname+"</b>&raquo;?"; break;
		case 6:
			DText = "Вы действительно хотите удалить адрес <b>"+uname+"</b>?"; break;
		case 7:
			DText = "Вы действительно хотите удалить заголовок<div class=\"mt10px\">&laquo;<b>"+uname+"</b>&raquo;?"; break;
		case 8:
			DText = "Вы действительно хотите удалить изображение <b>"+uname+"</b>?"; break;
		default:
			DText = "Вы действительно хотите удалить <b>"+uname+"</b>?";
	}
	Links = '<a href="'+elem.href+'">Удалить</a> <a class="simplemodal-close" href="#">Отмена</a>';
	$('#div_message_body').html(DText);
	$('#div_message_foot').html(Links);

	$('#div_sm_dialog').css({"height": "165px"});
	$('#div_sm_dialog').css({"width": "490px"});
	$('#div_message_body').css({"height": "78px"});

	$('#div_sm_dialog').modal({ closeHTML: '',containerCss:{"height":"188px","width":"520px"}, onOpen: modalOpen, onClose: modal_Close, overlayClose:true });
}

function pricePolicyDialog (id) {
	Links = '<a class="simplemodal-close" href="#">Закрыть</a>';
	$('#div_message_body').html('<img src="/images/loading.gif" alt="" /> Загрузка...');
	$('#div_message_foot').html(Links);
	
	$('#div_sm_dialog').css({"height": "443px"});
	$('#div_sm_dialog').css({"width": "490px"});
	$('#div_message_body').css({"height": "360px"});
	
	$('#div_sm_dialog').modal({ closeHTML: '',containerCss:{"height":"500px","width":"520px"}, onOpen: modalOpen, onClose: modal_Close, overlayClose:true });
	//$('#simplemodal-container').css({"height": "500px"});
	//$('.simplemodal-wrap').css({"height": "470px"});
	//$('.simplemodal-wrap').css({"width": "520px"});
	loadXMLDocAsync('/main.php?act=top20&id='+id,loadXMLDocAsyncRes);
}

function activateSiteDialog(id) {
	Links = '<a class="simplemodal-close" href="#">Закрыть</a>';
	$('#div_message_body').html('<img src="/images/loading.gif" alt="" /> Загрузка...');
	$('#div_message_foot').html(Links);
	
	$('#div_sm_dialog').css({"height": "443px"});
	$('#div_sm_dialog').css({"width": "490px"});
	$('#div_message_body').css({"height": "360px"});
	
	$('#div_sm_dialog').modal({ closeHTML: '',containerCss:{"height":"500px","width":"520px"}, onOpen: modalOpen, onClose: modal_Close, overlayClose:true });
	loadXMLDocAsync('/main.php?act=activate&id='+id,loadXMLDocAsyncRes);
}


function modalOpen (dialog) {
	dialog.overlay.fadeIn('normal');
	dialog.container.fadeIn('fast'); dialog.data.fadeIn('fast');
}

function modal_Close (dialog) {
	dialog.overlay.fadeOut('fast');
	dialog.container.fadeOut('fast',function () { $.modal.close(); });
}

function getBrowserName() {
	return "opera";
	var ua = navigator.userAgent.toLowerCase();
	// Определим Internet Explorer
	if (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1) {
		return "msie"
	}
	// Opera
	if (ua.indexOf("opera") != -1) {
		return "opera"
	}
	if (ua.indexOf("chrome") != -1) {
		return "chrome"
	}
	// Safari, используется в MAC OS
	if (ua.indexOf("safari") != -1) {
		return "safari";
	}
	// Gecko = Mozilla + Firefox + Netscape
	if (ua.indexOf("gecko") != -1) {
		return "gecko";
	}
	// Konqueror, используется в UNIX-системах
	if (ua.indexOf("konqueror") != -1) {
		return "konqueror";
	}
	return "unknown";
}

function onChangeImgPos(element) {
	var img,ii;
	var floatValues = Array('left','right','');
	var displayValues = Array('','','none');
	var tmpval0 = document.getElementById('bcf_imgpos').selectedIndex;
	for (ii=0;ii<adCnt;ii++) {
		img = document.getElementById('adimg'+ii);
		img.style.cssFloat = floatValues[tmpval0];
		img.style.display = displayValues[tmpval0];
		if (tmpval0==0) {
			img.style.marginRight = '4px';
			img.style.marginLeft = '';
		} else if (tmpval0==1) {
			img.style.marginRight = '';
			img.style.marginLeft = '4px';
		}
	}
}

function onChangeType(element) {
	var elem,ii;
	var tmpval0 = element.selectedIndex;
	for (ii=0;ii<adCnt;ii++) {
		elem = document.getElementById('adblock'+ii);
		if (tmpval0==1) {
			elem.style.width = 'auto';
			elem.style.marginLeft = '5px';
		} else
			elem.style.marginLeft = (ii==0?'5px':'');
	}
	if (tmpval0==0)
		onChangeWidth(document.getElementById('bcf_width'));
}

function onChangeBWidth(element) {
	var elem = document.getElementById('adblock');
	var val = parseInt(element.value);
	if (isNaN(val))
		val = 1;
	if (val < 0 || val > 3)
		val = 1;
	elem.style.borderWidth = val+'px';
}

function onChangeWidth(element) {
	var val = parseInt(element.value);
	if (isNaN(val))
		val = 800;
	if (val < 300 || val > 1600)
		val = 800;
			
	document.getElementById('adblock').style.width = val+'px';
	var elem,ii,tmpw;
	
	tmpw = Math.floor((val-((adCnt+1)*5))/adCnt);

	for (ii=0;ii<adCnt;ii++) {
		elem = document.getElementById('adblock'+ii);
		elem.style.width = tmpw+'px';
	}
}

function onChangeBCol(element) {
	var val = element.value;
	document.getElementById('adblock').style.borderColor = val;
}

function onChangeBGCol(element) {
	var val = element.value;
	document.getElementById('adblock').style.backgroundColor = val;
}

function onChangeLCol(element) {
	var val = element.value;
	var ii;
	for (ii=0;ii<adCnt;ii++)
		document.getElementById('adblock'+ii).style.color = val;
}



