var ingateTimeoutValue = 900; // скока можно ниченеделать
var ingateCurentTimeout = 0;	// время ничегонеделания
var ingateTimeoutDescriptor;	// дексриптор таймера

/*ingateBindTimeot = function ( ) {
	$('body').bind ( 'mousemove', clearIngateTimeout );
	$('body').bind ( 'keydown', clearIngateTimeout );
	$('body').bind ( 'scroll', clearIngateTimeout );
	$('body').bind ( 'click', clearIngateTimeout );
	$('body').bind ( 'focus', ingateCheckTimeout );
	//$(window).bind('resize', resizewindow);
	ingateCheckTimeout();
}*/

function resizewindow() {
	//alert("I'm done resizing for the moment");
	var imageHeight = $('#fancy_outer').height();
   	var imageWidth = $('#fancy_outer').width();
	var windowWidth = $(window).width();
	//window.scroll(0,0)
	var windowHeight = $(window).height() + window.scrollY;
	$('#fancy_outer').css({
	        "left" : windowWidth / 2 - imageWidth / 2,
	        "top" : windowHeight /2 - imageHeight / 2
	    });
};

/*clearIngateTimeout = function ( ) {
	ingateCurentTimeout = 0;
	if ( ingateTimeoutDescriptor == null )
		ingateCheckTimeout();
}
ingateCheckTimeout = function ( ) {
	ingateCurentTimeout++;
	//alert ( ingateCurentTimeout );
	if ( ingateCurentTimeout >= ingateTimeoutValue ) {
		var formLinkElement = $('#random_popup');
		ingateCurentTimeout = 0;
		clearTimeout ( ingateTimeoutDescriptor );
		try {
		urchinTracker('/send-application/?popup=done');
		var yaCounter217833 = new Ya.Metrika(217833); } catch(e){}
		ingateTimeoutDescriptor = null;
		formLinkElement.click();
		return;
	}
	ingateTimeoutDescriptor = setTimeout ( 'ingateCheckTimeout()', 15000 );
}*/


ingateAllPages = function(){
	$("input.inputSearch").focus(function(){
		if ( $(this).attr("value") == "поиск" ){
			$(this).attr({"value": ""});
		}
	});
	$("input.inputSearch").blur(function(){
		if ( $(this).attr("value") == "" ){
			$(this).attr({"value": "поиск"});
		}
	});
	$("input.clearme").focus(function(){
		if ( $(this).attr("title") == $(this).attr("value") ){
			$(this).removeAttr("value");
		}
	});
	$("input.clearme").blur(function(){
		if ( $(this).attr("value") == "" ){
			$(this).attr({"value": $(this).attr('title')});
		}
	});
	$("#toggle-menu li.toggle-close div").hide();
	$("#toggle-menu>li>a").click(function(){
		$(this).find('~div').slideToggle();
		return false;
	});
	$("#toggle-menu2 li.toggle-close div").hide();
	$("#toggle-menu2>li>a").click(function(){
		$(this).find('~div').slideToggle();
		return false;
	});

}
$(document).ready(function(){
	
	//alert($.cookie('form_redirect_url'));

	if ( $.cookie('form_redirect_url') && $.cookie('form_redirect_url') != '' ) {
		var elm = document.createElement ( 'a' );
		elm.setAttribute ( 'href', $.cookie('form_redirect_url') );
		elm.setAttribute ( 'rel', 'ajax-form-services_2' );
		elm.setAttribute ( 'id', 'immediatlyHref' );

		var body = document.getElementsByTagName ( 'body' );
		body = body[0];
		body.appendChild ( elm );
	}
	$.cookie('form_redirect_url', '')

	$("a[rel=ajax-form-services]").fancybox({
		'frameWidth':	675, 
		'frameHeight':	417, 
		'overlayShow':	true,
		'overlayOpacity': 0.8,
		'zoomSpeedIn': 100,
		'zoomSpeedOut': 0
	});
	$("a[rel=ajax-form-services_1]").fancybox({
		'frameWidth':	700, 
		'frameHeight':	500, 
		'overlayShow':	true,
		'overlayOpacity': 0.8,
		'zoomSpeedIn': 100,
		'zoomSpeedOut': 0
	});
	$("a[rel=ajax-form-services_2]").fancybox({
		'frameWidth':	675, 
		'frameHeight':	417, 
		'overlayShow':	true,
		'overlayOpacity': 0.8,
		'zoomSpeedIn': 100,
		'zoomSpeedOut': 0
	});
	$("a[rel=ajax-form-services-p]").each ( function ( data ) {
												var params = this.href.match ( /([0-9]+)\;([0-9]+)\;([0-9]+)$/ );
												var popupWidth = params[2] ? params[2] : 673;
												var popupHeight = params[3] ? params[3] : 600;
												$(this).fancybox({
													'frameWidth':	popupWidth,
													'frameHeight':	popupHeight, 
													'overlayShow':	true,
													'overlayOpacity': 0.8,
													'zoomSpeedIn': 100,
													'zoomSpeedOut': 0
												});
											}
	);
	$("#company-menu .popup-blog-title").css({opacity: 0});
	$("#company-menu").find("li a:contains('Корпоративный блог')").hover(function(){
		$(this).parent().find(".popup-blog-title").animate({opacity: 1}, {duration: 100, queue: false});
	}, function(){
		$(this).parent().find(".popup-blog-title").animate({opacity: 0}, {duration: 100, queue: false});
	});
	
	$('#toSmm').find("a").hover(function(){
		$(this).next().animate({opacity: 1}, {duration: 100, queue: false});
	}, function(){
		$(this).next().animate({opacity: 0}, {duration: 100, queue: false});
	});
	
	$(".postMan .avatarAuthor").hide();
	$(".postMan a").hover(function(){
		$(this).parent().find(".avatarAuthor").show({duration: 100});
	}, function(){
		$(this).parent().find(".avatarAuthor").hide({duration: 100});
	});

	if ( $('#immediatlyHref') ) {

		$('#immediatlyHref').click();
	}

		// preload windows
	$("a[rel=ajax-form-services]").each ( function ( data ) {
		var popupFrame = document.createElement ( 'iframe' );
		popupFrame.setAttribute ( 'width', 1 );
		popupFrame.setAttribute ( 'height', 1 );
		popupFrame.setAttribute ( 'frameborder', 0 );
		popupFrame.setAttribute ( 'hspace', 0 );
		popupFrame.setAttribute ( 'name', 'fancy_iframe' + Math.round(Math.random()*1000) );
		popupFrame.style.display = 'none';
		//alert ( this.href );
		popupFrame.src = this.href;
		//alert ( popupFrame );
		var body = document.getElementsByTagName ( 'body' );
		body = body[0];
		body.appendChild ( popupFrame );
	});
});

$(document).ready(function(){
	ingateAllPages();
	//ingateBindTimeot();
});
