// Открываем слой
function openLayer(id)
{			
	var layer = $('#' + id);
		
	__set_positions = function () {
		var pos = [$(window).width(), $(window).height(), $(document).scrollLeft(), $(document).scrollTop() ];				
		layer.css('left', ((layer.width()) > pos[0] ? pos[2] : pos[2] + Math.round((pos[0] - layer.width())	/ 2)));
		layer.css('top',  ((layer.height()	+ 50) > pos[1] ? pos[3] : pos[3] + Math.round((pos[1] - layer.height()	- 50)	/ 2)));						
	}
	
	__set_layer = function() {					
		/* if (isIE) { $('embed, object, select').css('visibility', 'hidden'); } */		
		$("#overlay").css('opacity', '0.5').show();
		$("#overlay, #closer").bind('click', function () { hideLayer(); });
		$(window).bind('resize scroll', function() { __set_positions(); });	
		__set_positions();
		layer.fadeIn("normal");		
		/* if (isIE) { $('embed, object, select', layer).css('visibility', 'visible'); } */
	};	
	
	__set_layer();	
	
	document.onkeydown = function(e){ 	
		if (e == null) { // ie
			keycode = event.keyCode;
		} else { // mozilla
			keycode = e.which;
		}
		if(keycode == 27){ // close
			hideLayer();		
		}
	};
	
	return false;
}

// Прячем слой
function hideLayer()
{
	$("#overlay").unbind();
	$(document).unbind("keydown");
	$(window).unbind("resize scroll");				
	/* if (isIE) { $('embed, object, select').css('visibility', 'visible'); } */
	$("#overlay, .layer").hide();
	
	return false;
}

//Листалка визуалов на главной
function switchVisual(index) {

	if (index || index == 0) {
		loadVisual(index);
	}
	else {		
		var next = current_visual + 1;
		if (next >= visuals.length) next = 0;
		loadVisual(next);
	}	
	return false;
}

//Загрузка визуала
function loadVisual(index) {
				
	var visual = $('#visual img:eq(0)');
	if (!visuals[index].length) //|| visual.attr('src') == visuals[index][0] 
		return false;
								
	visual.hide();
	
	var promo_link = $('#visualPromo > a');
	promo_link.show();
	
	var img = new Image(); 
	img.onload = function() {							
		visual.attr('src', visuals[index][0]);
		promo_link.attr('href', visuals[index][1]);
		promo_link.children('h2').html(visuals[index][2]);
		promo_link.children('p').html(visuals[index][3]);
		Cufon.replace('#visualPromo h2');		
		visual.show('normal');				
	};		
	
	img.src = visuals[index][0];
	current_visual = index;
		
	return false;		
}

// Карта в контактах
//function showMap(index) {
//	link = $('#link'+index); 
//	map = $('#map'+index);
//	
//	$('h2.section').removeClass('active');
//	$('.map').hide();
//	map.show();
//	link.parents('h2:eq(0)').addClass('active');
//	link.unbind('click');
//	Cufon.replace('h2 a');	
//	$('#nw-pointer').text(link.text());
//	
//	return;
//}

function showMap(index) {
	link = $('#link'+index); 
	map = $('#map'+index);
	
	$('h2.section').removeClass('active');
	$('.map').css('z-index', 0);
	map.css('z-index', 100);
	link.parents('h2:eq(0)').addClass('active');
	link.unbind('click');
	Cufon.replace('h2 a');	
	
	return;
}

$(document).ready(function(){ 
			
	$('html').addClass('js');
	$('body').append('<div id="overlay"></div>');
		
	//browser
		/* var isIE = ($.browser.msie && parseInt($.browser.version.substr(0,1)) < 8); */
		var browser = "";
		if (jQuery.browser.version == "6.0" && jQuery.browser.msie) {
			browser = "ie6";
		}		
		else if (jQuery.browser.opera) {
			browser = "opera";
			$('head').append('<link href="/css/styles-opera.css" type="text/css" rel="stylesheet" media="all" />');
		}
	
	//preloader
		preloadImage('/img/ajax-loader.gif');
		preloadImage('/img/pointer-orange2-left.png');
		preloadImage('/img/star-orange2.gif');		
	
	//js-links and layers
		$('.js-link').removeAttr('href');
		$('.js-link').addClass('click dotted');
		//$('.layer-opener').attr('href', '#' + $('.layer-opener').attr('layer')).bind('click', openLayer($('.layer-opener').attr('layer')));
		$('.layer-opener').removeAttr('href').bind('click', function() {
			return openLayer($(this).attr('layer')); 
		});
						
	//buttons
		var submit = $(".submit-button:eq(0)");
		//submit.append('<a onclick="$(this).parents(\'form\').submit();" class="click ' + submit.children('input:eq(0)').attr('class') + '">' + submit.children('input:eq(0)').attr('value') + '</a>');
		//submit.children('input').hide();
			
	//common text margins		
		$("p, ul").next("h2").addClass('in-text');
		$("p, ul").next("h2.section").addClass('section-in-text');
		$("p, ul").next("h3").addClass('in-text');
		$("div p:last-child, h2:last-child, .box:last-child").addClass('last');
		$("div .box:first-child").addClass('first');
		$("#menu li:last-child, #menu2 li:last-child, form div.item:last-child").addClass('last');
		
	//fonts
		$('h1, h2, .h1, .h2, h3, .h3, .lead p').each(function(){		
			if ( !$(this).hasClass('type')) {				
				$(this).addClass('type');
				$(this).filter('h2').addClass('t36');
				$(this).filter('h3').addClass('t18');
				$(this).filter('.lead').addClass('t30');				
			}							
		});					
		Cufon.replace(".type");
		//Cufon.replace(".h2");
						
	//colors
	$(".type a").livequery(function() {
		$(this).hover(
			function () {				
				if (!($(this).parents('li, h2').hasClass('active')))
					Cufon.replace($(this), {color:'#f5b300'});
			},
			function () {
				if (!($(this).parents('li, h2').hasClass('active'))) {	
				 	if ($(this).hasClass('dark'))
						Cufon.replace($(this), {color:'#333'});
				 	else
						Cufon.replace($(this), {color:'#f7953d'});
				}
				else {
					if ($(this).parents('ul:eq(0)').attr('id') == 'menu') { 				 	
						Cufon.replace($(this), {color:'#fff'});					
					}
					else if (!($(this).parents('h2').hasClass('active'))) {  				 	
						Cufon.replace($(this), {color:'#f7953d'});
					}
				}
			}
		);		
	});
	
	$("a").livequery(function() {
		var types = $(this).children('.type');
		$(this).hover(
			function () {	
				types.addClass('orange-hover');
				Cufon.replace(types);
			},
			function () {
				types.removeClass('orange-hover');
				Cufon.replace(types);				
			}
		);	
		//rocon.update();
	});			
	
});

