
//for link opened in new tab
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
for (var i=0; i < anchors.length; i++) {
	if (anchors[i].getAttribute("href") &&

	anchors[i].getAttribute("rel") == "external") {
		anchors[i].target = "_blank";
	}
}
}


function moveTitle(){
	var arrH1 = document.getElementsByTagName('h1');
	for(var i = 0; i < arrH1.length; i++){
		var arrClass = arrH1[i].className.split(' ');
		for( var j = 0; j < arrClass.length; j++){
			if(arrClass[j] == 'main'){
				var t = arrH1[i].innerHTML.charAt(0);
				switch(t){
					case '?':
						arrH1[i].style.left = '-10px';
						break;
				}
			}
		}
	}
}

//===================================================================================================

$(document).ready(function(){
	externalLinks();
	if(typeof(show_open_online) != 'undefined'){
		var show;
		$(".online_long a").animate({marginRight: "10px"}, 1000, function(){
			show = setTimeout(function(){
				$(".online_long a").animate({marginRight: "-250px"}, 1000);
			}, 2000);
		});
	}
	$(".online_long a").hover(function(){
		if(typeof(show_open_online) != 'undefined') clearTimeout(show);
		$(this).stop(true).animate({marginRight: "10px"}, 1000);
	},function(){
		$(this).stop(true).animate({marginRight: "-250px"}, 1000);
	});
	$(".faq_list h3").click(function(){
		$(this).toggleClass('active');
		$(this).nextAll("p").slideToggle();
	});
	$(".callback").click(function(){
		$( "#callback_dialog" ).dialog({
			height: 420,
			width: 400,
			modal: true,
			title: 'Оформление обратного звонока',
			buttons: {
				"Отправить": function() {
					$(this).dialog( "disable" );
					var form = $('#callback_dialog :input').serializeArray();
					form.push({name:'w',value:'callback'});
					$.post('/send.php',form,function(data){
						if(data=='OK'){
							$('#callback_result').slideUp('fast').empty().html('<div class="ui-widget"><div class="ui-state-highlight ui-corner-all" style="margin-top: 20px; padding: 10px 5px;">	<p><span class="ui-icon ui-icon-circle-check" style="float: left; margin-right: .3em;"></span><strong>Данные отправлены!</strong> В ближайшее время данная информация поступит на обработку менеджеру из выбранного вами офиса.</p></div></div>').slideDown();
						}
						else{
							$('#callback_result').slideUp('fast').empty().html('<div class="ui-widget"><div class="ui-state-error ui-corner-all" style="margin-top: 20px; padding: 10px 5px;">	<p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span><strong>Ошибка!</strong> '+data+'</p></div></div>').slideDown();
						}
					});
					$(this).dialog("enable");
					//$( this ).dialog( "close" );
				}
			},
			open: function(event, ui){
				$('#callback_dialog input[type=text]').val('');
				$('#callback_dialog input[type=radio]').removeAttr('checked');
				$('#callback_result').hide().empty();
				$("#callback_dialog .radio").buttonset();
			}
		});
	});
	$('.actions_name a, .slide_name a').click(function(){
		$(this).parent().next().slideToggle('slow');
	});
	
	var name_width = $('.block_action .top span.name').outerWidth();
	var block_name = function(){ 
			$('.block_action').each(function(){
	    	var width = 0;    	
	    	$('.top',this).children().not('.name').each(function(){
	    		width += $(this).outerWidth()+10;
	    	});
	    	var name_width_new = $(this).outerWidth()-width-30;
	    	if(name_width_new>name_width) name_width_new = name_width;
	    	$('.top span.name',this).css('width',name_width_new);
	    	var name_margintop = 30 - $('.top span.name',this).height()/2;
	    	$('.top span.name',this).css('marginTop',name_margintop>0?name_margintop:0);
		});
	}
	$(window).resize(block_name);
	block_name();
	
	$('.block_action').hover(function(){
		$(this).stop(true,true).animate({height:$(this).height()+21},200,function(){
			$('.panel',this).fadeIn();
		});
	},function(){
		var block = $(this); 
		$('.panel',this).fadeOut('fast',function(){
			block.stop(true,true).animate({height:block.height()-21},200);
		});
	});
	
	$('.block_action .panel').toggle(function(){
		$(this).removeClass('more').addClass('hide');
		var block = $(this).parent();
		block.stop(true,true).animate({height:block.height()+$('.bottom',block).outerHeight()+21},200,function(){
			$('.bottom',block).slideDown();
		});
	},function(){
		$(this).removeClass('hide').addClass('more');
		var block = $(this).parent();
		$('.bottom',block).slideUp('normal',function(){
			$('.panel',block).fadeOut();
			block.stop(true,true).animate({height:block.height()-$('.bottom',block).outerHeight()-21},200);
		});
	});

});

