function getBrowserInfo() {
    var t = "", v = "";
	if (navigator.userAgent.indexOf('Safari') >= 0)
		t = 'Safari';
    else if (navigator.userAgent.indexOf('Chrome') >= 0) 
        t = 'Chrome';
    else 
        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 msg = "Функция не поддерживается.\n\nПожалуйста, нажмите CTRL+D для добавления страницы в «Избранное»";
    var url = window.document.location;
    var title = window.document.title;
    var b = getBrowserInfo();
    if (b.type == 'Chrome'|| b.type == 'Safari') {
        alert(msg);
    }
    else 
        if (b.type == 'IE' && 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(msg);
    return false;
}

function updateCaptcha(id, module) {
	var result = $.ajax({
	        type: 		"POST",
	        url: 		"/" + module + "/captcha/new",
			dataType: 	'html',
	        data: 		'',
			
	        success: function(response) {
				$('#' + id).html(response);
	        }
	});
}

function clearControls(names) {
	for (var i = 0; i < names.length; i++) {
		var control = $('#' + names[i]);
		
		if ( control == null ) continue;
		
		switch ( control.attr('type') ) {
			case 'checkbox': 
				control.attr('checked', false);
				break;
				
			default:
				control.val('');
				break;
		}
	} 
}

function runSearch() {
	
	var searchStr = $.trim($('#txtLookedFor').val());
	
	if ( searchStr.length > 3 && searchStr != 'Поиск по сайту...' ) {
		document.forms['frmSearching'].submit();	
	} else {
		alert('Некорректная строка поиска.');
	}
}

function allowSubmenu() {
	if ( window.mainmenuEl == null ) {
		return;
	}
	
	clearTimeout(window.hoverTimerId);
	window.mainSubmenusAllowed = window.mainmenuEl.hasHover;
	
	if ( window.mainSubmenusAllowed ) {
		showSubmenu();
	} 
}

function showSubmenu(item, visible) {
	clearTimeout(window.itemTimerId);
	if ( window.mainSubmenusAllowed == null ) return;
	
	if ( window.mainSubmenusAllowed ) {
		if ( window.mainmenuItem != null ) {
			
			var submenuEl = $(window.mainmenuItem).find('.submenu').get()[0];
			
			if ( submenuEl != null ) {
				$(submenuEl).delay(250).fadeIn('fast');
			}
		}
	}
	
	if ( item != null && ! visible ) {
		var submenuEl = item.find('.submenu').get()[0];
		
		if ( submenuEl != null ) {
			submenuEl.visible = false;
			$(submenuEl).fadeOut('fast');
		}	
	}
}

$(document).ready(function() {

	window.mainmenuEl = $('.main-menu').get()[0];
	window.hoverTimerId = 0;
	window.mainSubmenusAllowed = false;

	$('.main-menu .last .submenu').width($('.main-menu td.last').width() + 25);

	$('#txtLookedFor').focus(function() {
		
		if ( $(this).val() == 'Поиск по каталогу...' ) {
			$(this).val('');
		}
			
	});
	
	$('#txtLookedFor').blur(function() {
		
		if ( $.trim($(this).val()) == '' ) {
			$(this).val('Поиск по каталогу...');
		}
			
	});

	$('div.button').click(
		function() {
			$(this).addClass('pushed');
		},
		
		function() {
			$(this).removeClass('pushed');
		}
	);
	
	$('div.button').hover(
		function() {
			$(this).addClass('hover');
		},
		
		function() {
			$(this).removeClass('hover');
		}
	);
	
	$("a.fancy").fancybox();
	
	$("a[rel=thumb_group]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});

	$('.main-menu .dropdown').hover(
		
		function() {
			window.mainmenuItem = $(this).get()[0];
			window.itemTimerId = setTimeout('showSubmenu($(this), true)', 200);
		}, 
		
		function() {
			window.mainmenuItem = null;
			showSubmenu($(this), false);
		}
	);
	
	$('.main-menu').hover(
		
		function () {
			clearTimeout(window.hoverTimerId);
			window.hoverTimerId = setTimeout('allowSubmenu()', 200);
			window.mainmenuEl.hasHover = true;
		},
		
		function () {
			clearTimeout(window.hoverTimerId);
			window.mainmenuEl.hasHover = false;
			allowSubmenu();
		}
		
	);

	$('#inter-map .hitarea-container div').hover(
		function() {
			var ttlId = $(this).attr('id') + '-ttl';
			$('#' + ttlId).fadeIn('fast');
		},
		
		function() {
			var ttlId = $(this).attr('id') + '-ttl';
			$('#' + ttlId).fadeOut('fast');
		}
	);
	
	if ( $.browser.opera ) {
		$('#inter-map .hitarea-container img').addClass('opera');
	}
	
	if ( $.browser.msie ) {
		$('.hitarea-container div a').html('<img src="/public/images/spacer.gif"/>');
		$('#inter-map .hitarea-container img').addClass('opera');
		
		$('.main-menu .mitem').each(function() {
			if ( ! $(this).hasClass('last') ) {
				$(this).find('div.title').css('width', $(this).width() - 40);
			} else {
				$(this).find('div.title').css('width', $(this).width());
			}
		});
		
		//$('.main-menu div.title').css('width', $('.main-menu div.title').parent().width());
	}
	
});

