$(document).ready(function() {
    
    function ie_magic_crutch(delay){
        setTimeout("jQuery('body').addClass('fail_ie_fix'); jQuery('body').removeClass('fail_ie_fix')", delay + 10);
        }
 // dropmenu	
	$('ul.uslugi .dropblock').hide();
	$('ul.uslugi .dropblock:first').show();
    $('ul.uslugi li a.dashed').click(function() {
        if($(this).next('.dropblock').css('display') == 'none'){
            $('ul.uslugi .dropblock').hide();
            $(this).next('.dropblock').show();
        }else{
            $('ul.uslugi .dropblock').hide();
        }
		return false;
	});
    
    $('a.nolink').live('click', function(){
        return false;
    });

	// block-aud
    $('.block-aud').hide();
	$('.key-aud').click( function () {
		$(this).toggleClass('arr-b');
		$(this).toggleClass('arr-t');
		$(this).parent().next('.block-aud').slideToggle('500');
		ie_magic_crutch(500);
		return false;
	});

	
	$('.dropblock-info').hide();
	$('.block-aud a.dashed').click ( function () {
		$(this).next('.dropblock-info').toggle();
		$(this).hide();
		return false;
	});

	$('.x-close').click ( function () {
		$(this).parent('.dropblock-info').hide();	
		$(this).parent('.dropblock-info').prev('a.dashed').show();
		ie_magic_crutch(0);
		return false;
	});

	$('.overlay').click( function () {
		$('.popup, .overlay').hide();							   
	});

	// key-company
	$('#key-company').click ( function () {
	    $(this).children('span').toggle();
		$(this).toggleClass('arr-b');
		$(this).toggleClass('arr-t');
		$(this).parents().next('.dropblock-info').slideToggle('500');
		ie_magic_crutch(500);
		return false;
	});
	
    $('input:radio[name="havesite"]').change ( function () {
        if($('.haveSite').attr('checked')){
            $('#haveSite').show();
            $('.haveSite').parents('.hsContainer').addClass('label-cur');
        }else{
            $('#haveSite').hide();
            $('.haveSite').parents('.hsContainer').removeClass('label-cur');
        }
        return false;
    });



	// tbl-striped
	$('.tbl-striped tr:nth-child(odd)').addClass('striped');


	$('.block-b-zayavka .form-b-zayavka').hide();
	$('.block-b-zayavka a').click ( function () {
		$(this).toggleClass('active-dashed');										  
		$('.block-b-zayavka .form-b-zayavka').slideToggle();
        return false;
	});





	$('.block-dopinfo').hide();
	$('.block-tooltip a').click( function() {
		$('.block-dopinfo').hide();
		if($(this).next('.block-dopinfo').size() == 0)
        {
    		$(this).after(
    			'<div class="block-dopinfo">'+
    				'<div class="x-close"></div>' +
    				'<div class="arr-blue-l"></div>' +
    				'<table cellspacing="0">' +
    				  '<tr>' +
    					'<td><div class="corn-tl"></div></td>' +
    					'<td class="td-bg">&nbsp;</td>' +
    					'<td><div class="corn-tr"></div></td>' +
    				  '</tr>' +
    				  '<tr>' +
    					'<td class="td-bg">&nbsp;</td>' +
    					'<td class="td-bg">' +
    						'<p id="tooltip"></p>' +
    					'</td>' +
    					'<td class="td-bg">&nbsp;</td>' +
    					'</tr>' +
    					'<tr>' +
    						'<td><div class="corn-bl"></div></td>' +
    						'<td class="td-bg">&nbsp;</td>' +
    						'<td><div class="corn-br"></div></td>' +
    					'</tr>' +
    				'</table>' +
    			'</div>').show();
        }else{
            $(this).next('.block-dopinfo').remove();
        }
		$('.block-dopinfo').find('#tooltip').text($(this).attr('rel'));
		$('.block-dopinfo .x-close').click( function() {
			$(this).parent('.block-dopinfo').remove();
		});
		return false;
	});





// If window was resized, calculate the new overlay dimensions
	var arrPageSizes = ___getPageSize();
	var arrPageScroll = ___getPageScroll();

	$('.overlay').css({
		height:	arrPageSizes[0]
	});

	$(window).resize(function() {
		var arrPageSizes = ___getPageSize();
		var arrPageScroll = ___getPageScroll();
		$('.popup').css({
			top:	arrPageScroll[1] + (arrPageSizes[3] / 10)
		});
	});
			
	function ___getPageSize() {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	};


	function ___getPageScroll() {
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
		arrayPageScroll = new Array(xScroll,yScroll);
		return arrayPageScroll;
	};




});
