/*** ALL ***/

function setHeight() {
	if (document.all&&!window.opera) {
		$('#contentB').height($('.centerContainer').height() - 30);
	} else {
		$('#contentB').css({'min-height' : $('.centerContainer').height() - 30});
	}
}

function getBrowserInfo() {
	var t,v = undefined;
	if (window.opera) {
		t = 'Opera';
	} else if (document.all) {
		t = 'IE';
		var nv = navigator.appVersion;
		var s = nv.indexOf('MSIE')+5;
		v = nv.substring(s,s+1);
	}
	else if (navigator.appName) t = 'Netscape';
	return {type:t,version:v};
}
 
function bookmark(a){
	var url = window.document.location;
	var title = window.document.title;
	var b = getBrowserInfo();
	if (b.type == 'IE' && 7 > b.version && b.version >= 4) {
		window.external.AddFavorite(url,title);
	} else if (b.type == 'Opera') {
		a.href = url;
		a.rel = "sidebar";
		a.title = url+','+title;
		return true;
	} else if (b.type == "Netscape") {
		window.sidebar.addPanel(title,url,"");
	} else {
		alert("Нажмите CTRL-D, чтобы добавить страницу в закладки.");
	}
	return false;
}

function homePage(HomePage) {
	if (document.all&&!window.opera) {
		this.style.behavior = 'url(#default#homepage)';
		this.setHomePage(HomePage);
	}
}

function pager(id,urlya,str,page) {
	$.ajax({
		type:'GET',
		url:urlya,
		data:str + page,
		success:function(dt) {
			$('#'+ id).html(dt);
			pagerBuild(0,page,0,0,null);
			document.location.href="#body";
		}
	});
}

function pagerBuild(prm,page,minPage,maxPage,elem) {
	if (prm == 0) {
		$('.pager').each(function() {
			quan = $(this).children(':not(.arr)').length;
			if (quan > 6) {
				page *= 1;
				count = 1;
				$(this).children(':not(.arr)').each(function() {
					if ((count == (page - 2) && page > 4) || (count == (page + 2) && page < (quan - 3))) {
						str = '<a href="#" onclick="pagerBuild(1,0,';
						if (count == (page - 2)) {
							str += '3,'+ count;
						} else {
							str += count +','+ (quan - 2);
						}
						str += ',this); return false;">...</a>';
						$(this).before(str);
					}
					if ((count < (page - 1) && (count > 2)) || (count > (page + 1) && (count < (quan - 1)))) {
						$(this).hide();
					}
					count++;
				});
			}
		});
	} else if (prm == 1) {
		elem = $(elem).parent('.pager:first');
		$(elem).children(':not(.arr)').each(function() {
			if ($(this).html() == '...') {
				$(this).remove();
			} else if ($(this).css('display') == 'none') {
				$(this).show();
			}
		});
		page = Math.ceil((minPage + maxPage) / 2);
		pagerBuild(2,page,0,0,elem);
	} else if (prm == 2) {
		quan = $(elem).children(':not(.arr)').length;
		if (quan > 6) {
			page *= 1;
			count = 1;
			$(elem).children(':not(.arr)').each(function() {
				if ((count == (page - 2) && page > 4) || (count == (page + 2) && page < (quan - 3))) {
					str = '<a href="#" onclick="pagerBuild(1,0,';
					if (count == (page - 2)) {
						str += '3,'+ count;
					} else {
						str += count +','+ (quan - 2);
					}
					str += ',this); return false;">...</a>';
					$(this).before(str);
				}
				if ((count < (page - 1) && (count > 2)) || (count > (page + 1) && (count < (quan - 1)))) {
					$(this).hide();
				}
				count++;
			});
		}
	}
}

function printPage() {
	window.print();
}

musicPlay = 0;
function music(id) {
	if (musicPlay == 0) {
		actMusic(id).startMusic();
		musicPlay = 1;
		 $('.gimnStop').show();
		 $('.gimnStart').hide();
	} else if (musicPlay == 1) {
		actMusic(id).stopMusic();
		musicPlay = 0;
		 $('.gimnStop').hide();
		 $('.gimnStart').show();
	}
}

function actMusic(id) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[id];
	} else if (document[id].length != undefined) {
		return document[id][1];
	} else {
		return document[id];
	}
}

function footBanners() {
	l = $('.footBannersBlock div').length;
	if (l > 0) {
		l = l * ($('.footBannersBlock div').width() + 2 * $('.footBannersBlock div').css('marginLeft').replace(/\D*$/ig,''));
		$('.footBannersBlock').width(l);
		if (l > 0) {
			$('.footer').height(230);
		}
	}
}

/*** HEAD ***/

function headTime(id) {
	d = new Date();
	h = d.getHours();
	m = d.getMinutes();
	str = 'Московское время ';
	if (h < 10) {
		str += '0'+ h +':';
	} else {
		str += h +':';
	}
	if (m < 10) {
		str += '0'+ m;
	} else {
		str += m;
	}
	
	$('#'+ id).html(str);
	setTimeout('headTime(\''+ id +'\')',500);
}

var block = new Array; //tekst begushej stroki
var rSWidth = new Array; //shirina begushej stroki
var rBWidth = new Array; //shirina blocka s tekstom
var rBsWidth = new Array; //shirina vseh blockov s tekstom
var left = new Array; //smeshenie teksta
var rSDelay = new Array; //zaderzhka mezhdu smesheniyami
var rSStep = new Array; //smeshenie
var rSTimeout = new Array; //timeout smesheniya

function getDataRunString(number) {
	block[number] = $('#runStr'+ number +' .runStrBlock .runBlock:first').html();
	rSWidth[number] = $('#runStr'+ number).width();
	rBWidth[number] = $('#runStr'+ number +' .runStrBlock .runBlock:first').width();
	left[number] = 0;
}

function runStringRoll(number,step,delay) {
	$('#runStr'+ number +' .runStrBlock').css('left',left[number] -= step);
	if(left[number] <= -rBWidth[number]) {
		$('#runStr'+ number +' .runStrBlock').css('left',left[number] = 0);
	}
	rSTimeout[number] = setTimeout(function(){runStringRoll(number,step,delay);},delay);
}

function runStringBuild(number,step,delay) {	
	var a = 0;
	
	$('#runStr'+ number +' .runStrBlock').css('left',0);
	
	if($('#runStr'+ number +' .runStrBlock .runBlock').length == 1) {
		$('#runStr'+ number +' .runStrBlock').append('<div class="runBlock">'+ block[number] +'</div>');
	}
   
	while(a == 0) {
		rBsWidth[number] = $('#runStr'+ number +' .runStrBlock .runBlock:last').prevAll().length * rBWidth[number];
		if(rBsWidth[number] < rSWidth[number]) {
			$('#runStr'+ number +' .runStrBlock').append('<div class="runBlock">'+ block[number] +'</div>');
		} else {
			a = 1;
		}    
	}
   
	runStringRoll(number,step,delay);
}

/*** SEARCH ***/

function getAllSearchers(searcher, id) {
	inpVal = $('#'+ id).val().replace(/\040+/,' ');
	arr = inpVal.split(' ');
	switch (searcher) {
		case 'google': {
			str = 'http://www.google.ru/#hl=ru&q=';
		}
		break;
		case 'rambler': {
			str = 'http://nova.rambler.ru/srch?query=';
		}
		break;
		case 'yandex': {
			str = 'http://yandex.ru/yandsearch?text=';
		}
		break;
		default: {
			str = 'http://www.google.ru/#hl=ru&newwindow=1&q=';
		}
	}
	for (i = 0, lenI = arr.length; i < lenI; i++) {
		str += arr[i] +'+';
	}
	str = str.substr(0,str.length - 1);
	window.open(str);
}

function searchResView(arr) {
	if (arr.length) {
		for (i = 0,lenI = arr.length; i < lenI; i++) {
			r = new RegExp('('+ arr[i] +')','gi');
			$('#contentB').html($('#contentB').html().replace(r,'<span class="serchResMarked">$1</span>'));
		}
	}
}

/*** VOTING ***/

function sendVote(id,prm) {
	str = '';
	count = 0;
	$('#'+ id +' input:checked').each(function(){
		str += 'var'+ count +'='+ $(this).val() +'&';
		count++;
	});
	str += 'q='+ count +'&prm='+ prm;
	$.ajax({
		type: 'POST',
		url: '/getVoting.php',
		data: str,
		success: function(dt) {
			if (prm == 'small') {
				if (dt == 'doubleVote') {
					$('#'+ id).append('<div class="voteAlert">Вы уже участвовали в этом опросе.</div>');
				} else if (dt == 'crack') {
					$('#'+ id).append('<div class="voteAlert">Почти получилось =)</div>');
				} else if (dt == 'ok') {
					$('#'+ id).append('<div class="voteAlert">Ваш голос учтен</div>');
				}
				setTimeout('clearVoteAlert("'+ id +'","voteAlert")',6000);	
			} else if (prm == 'big') {
				$('#'+ id).replaceWith(dt);
			}
		}
	});
}

function clearVoteAlert(id,cl) {
	$('#'+ id +' .'+ cl).animate({
		height:0,
		opacity:0
	},1000,null,function(){
		$('#'+ id +' .'+ cl).remove();
	});
}

/*** END OF VOTING ***/
/*** CHAT ***/

function chatText(id,urlya,limit,prm) {
	if ($('#'+ id).attr('id')) {
		$.ajax({
			type:'get',
			url:urlya,
			data:'lastTime='+ lastChatTextTime +'&ajax=1&val=4',
			success:function(dt) {
				chatArr = eval('('+ dt +')');
				for (i = 0, lenI = chatArr.length; i < lenI; i++) {
					exist = true;
					if ((chatArr[i][3] * 1) > lastChatTextTime || (chatArr[i][0] != lastChatTextAuthor && (chatArr[i][3] * 1) == lastChatTextTime)) {
						exist = false;
					} else if (chatSpeechCount <= 30) {
						exist = false;
						$('#'+ id +' div').each(function() {
							if ($(this).html() == '<span class="time">'+ chatArr[i][2] +'</span><span class="name">'+ chatArr[i][0] +'</span>'+ chatArr[i][1]) {
								exist = true;
							}
						});
					} else if (chatSpeechCount > 30) {
						exist = false;
						$('#'+ id +' div:gt('+ (chatSpeechCount - 30) +')').each(function() {
							if ($(this).html() == '<span class="time">'+ chatArr[i][2] +'</span><span class="name">'+ chatArr[i][0] +'</span>'+ chatArr[i][1]) {
								exist = true;
							}
						});
					}
					
					if (!exist && (prm == 0 || (prm == 1 && chatArr[i][0] != 'bot'))) {
						if ($('#'+ id +' div:last').hasClass('color')) {
							color = '';
						} else {
							color = ' color';
						}
						
						if (chatArr[i][0] == 'bot') {
							text = 'botText';
						} else {
							text = 'userText';
						}
						
						$('#'+ id).append('<div class="'+ text + color +'"><span class="time">'+ chatArr[i][2] +'</span><span class="name">'+ chatArr[i][0] +'</span>'+ chatArr[i][1] +'</di>');
						while ($('#'+ id +' div').length > limit) {
							$('#'+ id +' div:first').remove();
						}
						
						lastChatTextAuthor = chatArr[i][0];
						lastChatTextText = chatArr[i][1];
						lastChatTextTime = chatArr[i][3];
						chatSpeechCount++;
						$height = 0;
						$('#'+ id +' div').each(function() {
							$height += $(this).innerHeight();
						});
						$('#'+ id).scrollTop($height - 350);
					}
				}
			}
		});
		if (chatFirstEnter == 1) {
			chatFirstEnter = 0;
			$height = 0;
			$('#'+ id +' div').each(function() {
				$height += $(this).innerHeight();
			});
			$('#'+ id).scrollTop($height - 350);
		}
	}
	setTimeout(function(){chatText(id,urlya,limit,prm);},1000);
}

function chatSend(prm,id,urlya,arr) {
	s = false;
	switch (prm) {
		case 'add': {
			inpVal = $('#'+ id +' input:text').val().substr(0,arr[0]);
			d = 'val=5&ajax=1&message='+ inpVal;
			type = 'POST';
			if (inpVal.replace(/\s+/ig,'') != '') {
				s = true;
			}
		}
		break;
		case 'login': {
			login = $('#'+ id +' input:text').val();
			password = $('#'+ id +' input:password').val();
			d = 'val=1&ajax=1&login='+ login +'&password='+ password;
			type = 'GET';
			if (login != '' && password != '') {
				s = true;
			}
		}
		break;
		case 'exit': {
			d = 'val=6&ajax=1';
			type = 'GET';
			s = true;
		}
		break;
		case 'regForm': {
			d = 'val=2&ajax=1';
			type = 'GET';
			s = true;
		}
		break;
		case 'reg': {
			s = true;
			if ($('#'+ id +' input:text:first').val().length <= 2) {
				$('#'+ id +' input:text:first').after('<div class="error">Логин слишком короткий.</div>');
				s = false;
			}
			
			if ($('#'+ id +' input:text:eq(1)').val() != $('#'+ id +' input:text:eq(2)').val()) {
				$('#'+ id +' input:text:eq(2)').after('<div class="error">E-mail`ы не совпадают.</div>');
				s = false;
			}
			
			if ($('#'+ id +' input:text:eq(1)').val() == '' || $('#'+ id +' input:text:eq(2)').val() == '') {
				$('#'+ id +' input:text:eq(2)').after('<div class="error">E-mail не введен.</div>');
				s = false;
			}
			
			if ($('#'+ id +' input:password:first').val() != $('#'+ id +' input:password:last').val()) {
				$('#'+ id +' input:password:last').after('<div class="error">Пароли не совпадают.</div>');
				s = false;
			}
			
			if ($('#'+ id +' input:password:first').val() == '' || $('#'+ id +' input:password:last').val() == '') {
				$('#'+ id +' input:password:last').after('<div class="error">Пароль не введен.</div>');
				s = false;
			}
			
			if ($('#'+ id +' input:text:last').val().length == 0 || $('#'+ id +' input:text:last').val().length > 6) {
				$('#'+ id +' input:text:last').after('<div class="error">Проверочный код введен неверно.</div>');
				s = false;
			}
			
			if (s) {
				login = $('#'+ id +' input:text:first').val();
				password = $('#'+ id +' input:password:first').val();
				email = $('#'+ id +' input:text:eq(1)').val();
				captcha = $('#'+ id +' input:text:last').val();
				d = 'val=3&ajax=1&login='+ login +'&password='+ password +'&email='+ email +'&captcha='+ captcha;
				type = 'POST';
			}
			
		}
		break;
	}
	
	if (s) {
		$.ajax({
			type:type,
			url:urlya,
			data:d,
			success:function(dt) {
				switch (prm) {
					case 'add': {
						if (dt != '') {
							$('#'+ id).replaceWith(dt);
						} else {
							$('#'+ id +' input:text').val('').focus();
						}
					}
					break;
					case 'login': {
						if (dt != 'error' && dt != 'ban') {
							$('#'+ id).replaceWith(dt);
						} else if (dt == 'ban') {
							$('#'+ id).prepend('<div class="error">Вы не можете войти в чат по причине временного бана.</div>');
						}
					}
					break;
					case 'exit': {
						$('#'+ id).replaceWith(dt);
					}
					break;
					case 'regForm': {
						$('#'+ id).replaceWith(dt);
					}
					break;
					case 'reg': {
						$('#'+ id).replaceWith(dt);
					}
					break;
				}
			}
		});
	}
}

/*** END OF CHAT ***/
/*** ORGANISATIONS ***/

function submitOrg(prm,urlya,id) {
	switch (prm) {
		case 'enter': {
			email = $('#email').val();
			pass1 = $('#password').val();
			if ($('#rem').attr('checked') == 'checked' || $('#rem').attr('checked') == true) {
				rem = 1;
			} else {
				rem = 0;
			}
			str = 'email='+ email +'&password='+ pass1 +'&rem='+ rem +'&val=0&step=1&ajax=true';
		}
		break;
		case 'change': {
			type = $('#type').val();
			title = $('#title').val();
			address = $('#address').val();
			phone = $('#phone').val();
			fax = $('#fax').val();
			email = $('#email').val();
			site = $('#site').val();
			info = $('#info').val();
			str = 'type='+ type +'&title='+ title +'&address='+ address +'&phone='+ phone +'&fax='+ fax +'&email='+ email +'&site='+ site +'&info='+ info +'&val=1&step=2&ajax=true';
		}
		break;
		case 'reg': {
			str = 'val=2&step=0&ajax=true';
		}
		break;
		case 'regSubmit': {
			email = $('#email').val();
			pass1 = $('#password1').val();
			pass2 = $('#password2').val();
			captcha = $('#captcha').val();
			str = 'email='+ email +'&password1='+ pass1 +'&password2='+ pass2 +'&captcha='+ captcha +'&val=2&ajax=true&step=1';
		}
		break;
		case 'pass': {
			str = 'val=3&step=0&ajax=true';
		}
		break;
		case 'forgot': {
			email = $('#forgotPass').val();
			str = 'email='+ email +'&val=3&step=1&ajax=true';
		}
		break;
	}
	
	$.ajax({
		type:'POST',
		url:urlya,
		data:str,
		success:function(dt) {
			$('#'+ id).replaceWith(dt);
			switch (prm) {
				case 'enter': {
					text = 'Личный кабинет';
				}
				break;
				case 'regSubmit': {
					text = 'Регистрация новой организации';
				}
				break;
				case 'forgot': {
					text = 'Восстановление пароля';
				}
				break;
				case 'change': {
					text = 'Личный кабинет';
				}
				break;
				case 'reg': {
					text = 'Регистрация новой организации';
				}
				break;
				case 'pass': {
					text = 'Восстановление пароля';
				}
				break;
			}
			$('.bigHead').text(text);
		}
	});
}

/*** END OF ORGANISATIONS ***/
/*** INDEX ***/


/*** END OF INDEX ***/
/*** ARTICULS ***/

function viewBigImg(elem) {
	src = $(elem).attr('src');
	src = src.replace(/((.jpg)|(.gif))/ig,'big$1');
	if ($('#bigImg').attr('id')) {
		$('#bigImg').hide();
		$('#bigImg').css({'overflow':'visible','width':'auto','height':'auto'});
		$('#bigImg img').attr('src',src);
		w = $('#bigImg').width();
		h = $('#bigImg').height();
		$('#bigImg').css({'overflow':'hidden','width':'20px','height':'5px'});
	} else {
		$('.mainContainer').append('<div onclick="$(this).hide(\'normal\');" id="bigImg" style="position:absolute; left:50%; display:none; padding:4px; cursor:pointer; background:#f0f0f4; z-index:11;"><img alt="" src="'+ src +'" style="padding:4px; border:1px solid #fff;" /></div>');
		w = $('#bigImg').width();
		h = $('#bigImg').height();
		$('#bigImg').css({'overflow':'hidden','width':'20px','height':'5px'});
	}
	$('#bigImg').css({'top':((self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop)) + 200),'margin':'0 0 0 -10px'}).show();
	$('#bigImg').animate({
		width:w,
		height:h,
		marginLeft:(0 - w / 2)
	},200);
}

function setArtBlockHeight() {
	if ($('.smallArticuls').attr('class')) {
		if (document.all&&!window.opera) {
			$('.smallArticuls .border .background').height($('.contentContainer').height() - 30);
		} else {
			$('.smallArticuls .border .background').css('min-height',$('.contentContainer').height() - 30);
		}
	} else if ($('.smallArticuls2').attr('class')) {
		rCol = $('.rCol').height();
		lCol = $('.lCol').height();
		cC = $('.contentContainer').height();
		if (rCol <= cC && lCol <= cC) {
			h = cC - 30;
		} else if (rCol > lCol) {
			h = rCol - 30;
		} else {
			h = lCol - 30;
		}
		
		if (document.all&&!window.opera) {
			$('.smallArticuls2 .border .background').height(h);
			$('.bigArticuls2').height(h);
		} else {
			$('.smallArticuls2 .border .background').css('min-height',h);
			$('.bigArticuls2').css('min-height',h);
		}
	}
}
/*** END OF ARTICULS ***/
/*** ADV ***/

function bannerCalc(elem) {
	id = $(elem).attr('id');
	
	if (id == 'first') {
		val = $(elem).val() * 1;
		switch (val) {
			case 1: {
				inp = '<select onchange="bannerCalc(this);" id="second" style="display:none"></select>';
			}
			break;
			case 2: {
				quan = $('#quans').val();
				quan = quan.split('|');
				quan = quan[1] * 1;
				inp = '<label id="secondLabel">Порядковый номер: </label><select onchange="bannerCalc(this);" id="second">';
				for (i = 0; i < quan; i++) {
					inp += '<option value="'+ i +'">'+ (i + 1) +'</option>';
				}
				inp += '</select>';
			}
			break;
			case 3: {
				inp = '<label id="secondLabel">Введите высоту баннера (px)</label><input type="text" id="second" onkeyup="bannerCalc(this);" />';
			}
			break;
			case 4: {
				inp = '<select onchange="bannerCalc(this);" id="second" style="display:none"></select>';
			}
			break;
		}
		if ($('#secondLabel').attr('id')) {
			$('#secondLabel').remove();
		}
		$('#second').replaceWith(inp);
		val1 = val;
		if (val1 > 1 && val1 < 4) {
			val2 = $('#second').val() * 1;
		}
	} else if (id == 'second') {
		val1 = $('#first').val() * 1;
		if (val1 > 1 && val1 < 4) {
			val2 = $(elem).val() * 1;
		}
	}
	
	switch (val1) {
		case 1: {
			price = Math.round($('#top').val());
		}
		break;
		case 2: {
			price = Math.round($('#right').val() * Math.pow($('#rightK').val(),val2));
		}
		break;
		case 3: {
			price = 0;
			for (i = 0,lenI = Math.ceil(val2 / 10); i < lenI; i++ ) {
				price += $('#bottom').val() * Math.pow($('#bottomK').val(),i);
			}
			price = Math.round(price);
		}
		break;
		case 4: {
			price = Math.round($('#foot').val());
		}
		break;
	}
	
	currency = $('#currency').val();
	if (currency == 'RUR') {
		price += ' рублей';
	} else if (currency == 'USD') {
		price = '$'+ price;
	} else if (currency == 'EUR') {
		price += ' евро';
	}
	
	$(elem).parent('.bannersCalc:first').children('.result').children('span').html(price);
}

/*** END OF ADV ***/