var agent = navigator.userAgent.toLowerCase();
var major = parseInt(navigator.appVersion);
var minor = parseFloat(navigator.appVersion);

var isNN = ((agent.indexOf('mozilla') != -1) && ((agent.indexOf('spoofer') == -1) && (agent.indexOf('compatible') == -1)));
var isNN4 = (isNN && (major == 4));
var isNN6 = (isNN && (major >= 5));

var isOPERA = agent.indexOf("opera")>-1 && window.opera;
var isIE4 = (agent.indexOf("msie") != -1 && !isOPERA);

var flash_version = 0; // Версия флеш-проигрывателя
var fz=0;
if (isIE4){
	ie = 1;
	for (var i=3; i<7; i++){
		try {
			if (eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash."+i+"')")) flash_version = i;
		}
		catch (e) {}
	}
}
if ((isOPERA || isNN || isNN4 || isNN6) && (navigator.plugins)){
	for (var i=0; i<navigator.plugins.length; i++){
		if (navigator.plugins[i].name.indexOf("Flash")> -1){
			fz = parseInt(navigator.plugins[i].description.charAt(16));
			if (fz > flash_version) flash_version=fz;
		}
	}
}
	
//-------------------------------------------------------------------//
function rusoft() {
  window.open('http://www.rusoft.ru');
}

function ShowHTTP(href, display, target, style, title) {
	if (display == '') display = href;
	if (target == '') target = '_blank';
	document.writeln('<a href="'+href+'" target="'+target+'" title="'+title+'" style="'+style+'">'+display+'</a>');
}

function ShowMail(user, domain1, domain2) {
	var email = user+'&#64;'+domain1+'&#46;'+domain2;
	if (ShowMail.arguments[3]) name = ShowMail.arguments[3];
	else name = email;
	document.writeln('<a href="mailto:'+email+'">'+name+'</a>');
}

function openImage(image, ev) {
    var ev = window.event || ev;
    if (ev == undefined) {
	   if (image) window.open("/popup.php?img="+image, "popupimage", "scrollbars=0,resizable=0,width=100,height=100,location=0,menubar=0,status=0,toolbar=0").focus();
	   return false;
    }
    var obj = ev.srcElement || ev.target;
    if (obj.tagName == 'IMG') obj = obj.parentNode;
    return hs.expand(obj);
}

function openPhoto(id) {
	if (id) window.open("/popupf?id="+id, "popup", "scrollbars=1, resizable=1, width=700, height=450").focus();
}

function imgOpen(imgURL,imgWidth,imgHeight,Title) {
	var imgWndw=window.open('','_blank','width='+imgWidth+',height='+
	imgHeight+',toolbar=no,menubar=no,location=no,status=no,'+
	'resizable=yes,scrollbars=no');
	var imgTitle=(Title)?Title:imgURL+": "+imgWidth+'x'+imgHeight;
	with (imgWndw.document){
		open();
		write('<ht'+'ml><he'+'ad><ti'+'tle>'+imgTitle+'</ti'+'tle>'+
		'</he'+'ad><bo'+'dy leftmargin="0" '+
		' topmargin="0" '+
		'rightmargin="0" bottommargin="0" marginwidth="0" '+
		'marginheight="0"><img src="'+imgURL+'" width="'+imgWidth+
		'" height="'+imgHeight+'" border="0" alt="'+imgTitle+
		'"></bo'+'dy></ht'+'ml>');
		close();
	}
	return false
}

function OpenPopup(src) {
	if (arguments[1]) popup_name = arguments[1];
	else popup_name = 'popup';
	if (arguments[2]) popup_width = arguments[2];
	else popup_width = 500;
	if (arguments[3]) popup_height = arguments[3];
	else popup_height = 400;
	window.open(src, popup_name, 'toolbar=no,location=no,status=no,menubar=no,resizable=yes,directories=no,scrollbars=yes,width='+popup_width+',height='+popup_height).focus();
	return false
}

function SetCookie(sName, sValue){
	document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 2070 23:59:59 GMT; path=/;";
}

function changeImage(id, img) {
	document.getElementById(id).src = img;
}

function insertFlash(v, fl, gif, lnk, w, h, id){
	if (flash_version >= v) {
		document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="' + w + '" height="' + h + '" id="flash' +  id + '" align="center"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="' + fl + '"><param name="quality" value="high" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#ffffff" /><embed src="' + fl + '" wmode="transparent" quality="high" width="' + w + '" height="' + h + '"></embed></object>');
	} else if (gif != '') {
		document.write("<a href='"+lnk+"'><img src='"+gif+"' width='" + w + "' height='" + h + "' border=0></a>");
	}
}


// --------------------------------------------------
// ---- Отображение галереи на странице объекта -----
// --------------------------------------------------
// пропорциональное уменьшение размеров картинок превью
function imgResize(id)
{
    var image = document.getElementById(id);

    var width = image.width;
    var height = image.height;
    var prop = image.height/image.width;
    
    var size = 135;

    if (width > size || height > size)
    {
        height = prop*size;
        width = size;
        
        if (height > size)
        {
            height = size;
            width = size/prop;
        }
	}
    image.width = width;
    image.height = height;
}

var i=0;
var timer;
// --- Клик на кнопку Вперед/Назад
function showGallery(count, mode, rotate){
	i = parseInt(i) + parseInt(mode);
	if (rotate) {
		if (i>=count) i=0;
		if (i<0) i=count-1;
	} else {
		if (i>=count) i=count-1;
		if (i<0) i=0;
	}
	if (i>=0 && (i-1)<count) {
		if (document.getElementById('object_image') && par[i]) {
			document.getElementById('object_image').innerHTML = par[i];	
			document.getElementById('object_alt').innerHTML = alt[i];
		}
	}

	changeImageArrows(count);
	return false;
}
function changeImageArrows(count){
	if (i==0) document.getElementById('link_prev').innerHTML = '<span class="prev" style="background: none">Предыдущая</span>';
	else document.getElementById('link_prev').innerHTML = '<a href="javascript:void(0);" class="prev" onclick="showGallery(' + count + ',-1); return false;" id="prev">Предыдущая</a>';	

	if ((i+1)>=count) document.getElementById('link_next').innerHTML = '<span class="next" style="background: none">Следующая</span>';	
	else document.getElementById('link_next').innerHTML = '<a href="javascript:void(0);" onclick="showGallery(' + count + ',1); return false;" class="next" id="next">Следующая</a>';
}
// --- Клие на превью
function SelectImage(type, image, bigimage, width, height, num, count){
	if (this.timer) stopGallery();
	showGallery(count, num-this.i, false);
}
// --- Клик на кнопку Play
function playGallery(count, timeout){
	// Надо вызывать функцию showGallery с какой-то задержкой
	this.timer = window.setInterval("showGallery("+count+", 1, true)", timeout);
	document.getElementById('play').src = '/images/controls_stop_active.gif';
	document.getElementById('play_link').onclick = function(){stopGallery(count); return false;};
}
// --- Клик на кнопку Stop
function stopGallery(count){
	window.clearInterval(this.timer);
	document.getElementById('play').src = '/images/controls_play_active.gif';
	document.getElementById('play_link').onclick = function(){playGallery(count); return false;};
}

function ShowMiddleImage(imagepath, largeimagepath, alt, page, pos) {
	this.i = pos;
	document.getElementById('object_alt').innerHTML = alt;
	var image = '';
	if (largeimagepath) {
		image += '<a href="' + largeimagepath + '" onclick="openImage(\'' + largeimagepath + '\'); return false">';
	}
	image += '<img src="' + imagepath + '" border="0">';
	if (largeimagepath) {
		image += '</a>';
	}
	document.getElementById('object_image').innerHTML = image;
	document.getElementById('return_link').onclick = function() {ShowSmallImage(page); return false};
	document.getElementById('small_photo_div'+page).style.display = 'none';
	document.getElementById('middle_photo_div').style.display = 'block';
}

function ShowSmallImage(page) {
	document.getElementById('small_photo_div'+page).style.display = 'block';
	document.getElementById('middle_photo_div').style.display = 'none';
}

var _page = 0;
// Переход на заданную страницу галереи
function gotoPage(page) {
	document.getElementById('small_photo_div'+page).style.display = 'block';
	document.getElementById('small_photo_div'+_page).style.display = 'none';
	_page = page;
}

// классы инпутам
function setInputClass() {
	var input = document.getElementsByTagName('input');
	for (var i = 0; i <= input.length - 1; i++) {
		if (input[i].type == 'text') {
			input[i].className = 'text';
		}
		
		if (input[i].type == 'checkbox' || input[i].type == 'radio') {
			input[i].className = 'check';
		}
	}
}

// Добавить в Избранное
function add_favorite(a) {
	title=document.title;
	url=document.location;
	try {
		// Internet Explorer
		window.external.AddFavorite(url, title);
	}
	catch (e) {
		try {
			// Mozilla
			window.sidebar.addPanel(title, url, "");
		}
		catch (e) {
			// Opera
  			if (typeof(opera)=="object") {
				a.rel="sidebar";
				a.title=title;
				a.url=url;
				return true;
			}
			else {
				// Unknown
				alert('Нажмите Ctrl-D чтобы добавить страницу в закладки');
			}
		}
	}
	return false;
}

function isEmail(address){
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(address);
} // isEmail

function checkLogin(){
    var em = document.getElementById('loginform_login').value;
	if (em == "") {
		alert('Вы должны указать свой логин.');
		document.getElementById('loginform_login').focus();
		return false;
	}else if (!isEmail(em)){
        alert('Почтовый адрес указан не верно.');	    
	    return false;
    }
    $('#loginform').submit();
    //window.location = "/cabinet/forget_password?login="+em;
}

function checkLoginMain(){
	if (document.getElementById('loginformmain_login').value == "") {
		alert('Вы должны указать свой логин.');
		document.getElementById('loginformmain_login').focus();
		return false;
	}
	window.location = "/cabinet/forget_password?login="+document.getElementById('loginformmain_login').value;
	return false;
}


// min width ie6
function minWidth() {
	var d = document;
	var winIE = (navigator.userAgent.indexOf('Opera')==-1 && (d.getElementById && d.documentElement.behaviorUrns)) ? true : false;
	
	function bodySize() {
		if(winIE && d.documentElement.clientWidth) {
			sObj = d.getElementsByTagName('body')[0].style;
			sObj.width = (d.documentElement.clientWidth < 1000) ? '1000px' : '100%';
		}
	}
	
	function init() {
		if(winIE) { bodySize(); }
	}
	onload = init;
	if(winIE) { onresize = bodySize; }
}

// news slider
function newsSliderSwitch() {
	$('.newsSlider dt').hover(
		function(){
			$(this).siblings().removeClass('select').end().next('dd').andSelf().addClass('select');
		},
		function(){}
	);
}

// more news drop
function moreNewsDrop() {
	$('div.moreNews ul li, td.moreNews ul li').hover(
		function(){
			$(this).children('.dropDescription').css({ display: 'block' }).end().css({ zIndex: '10' });
		},
		function(){
			$(this).children('.dropDescription').css({ display: 'none' }).end().css({ zIndex: '1' });
		}
	);
}

// cover show/hide
function coverShowHide() {
	$('ul.coverBox li').hover(
		function(){
			$(this).children('.cover').stop(true, true).end().children('.cover').animate({"top": "-=20px", opacity: "show"}, 260);
		},
		function(){
			$(this).children('.cover').animate({"top": "+=20px", opacity: "hide"}, 260);
		}
	);	
}

// slide login portal 1
function slideLogin() {
	$('.panelLeft').click(function(){
		$('.loginContent').slideToggle('slow');
		$(this).toggleClass('open');
	});
}

// show/hide login portal 2
function showHideLogin() {
	$('div.login a.openBtn').click(function(){
		$(this).parent('.login').toggleClass('open');
		return false;
	});
}

function showHideLoginPortal3() {
	$('ul.navigateTop a.openBtn').click(function(){
		$(this).parent('.login').toggleClass('open');
		return false;
	});
}

function displayUL (id){
	var objUL = document.getElementById(id);
	if (objUL.style.display == 'block'){
		objUL.style.display = 'none';
	} else {
		objUL.style.display = 'block';
	}
}