function print_r(arr, level) {
  var print_red_text = "";
  if(!level) level = 0;
  var level_padding = "";
  for(var j=0; j<level+1; j++) level_padding += "    ";
  if(typeof(arr) == 'object'){
    for(var item in arr){
      var value = arr[item];
      if(typeof(value) == 'object') {
        print_red_text += level_padding + "'" + item + "' :\n";
        print_red_text += print_r(value,level+1);
			} 
      else 
        print_red_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  	}
	} 
  else  print_red_text = "===>"+arr+"<===("+typeof(arr)+")";
  return print_red_text;
}
function convert_json(arConvert){
	arConvert['indexOf'] = ''; 
	arConvert['lastIndexOf'] = '';
	arConvert['every']  = '';
	arConvert['filter'] = '';
	arConvert['forEach'] = '';
	arConvert['map'] = '';
	arConvert['some'] = '';
	arConvert['reduce'] = '';
	arConvert['reduceRight'] = '';
  var sArray = [];
  for (key in arConvert) { 
  		
      if(typeof(arConvert[key])=='object'){ 
        sArray.push('"'+key+'":'+convert_json(arConvert[key]));  
      }else{
        sArray.push('"'+key+'":"'+arConvert[key]+'"');
      }
  }  
  var serialized = sArray.join(',')
  return '{'+serialized+'}';
}

function fieldToArray(str){
	b = false;
	if(str.match(/PROPERTY(\[[0-9]+\]){1,10}/i)){
		str = str.replace(/\]/g, '');
  	arStr = str.split('[');
  	var i = 0;
  	strEval = '';
  	var Test = new Array();
  	strAr = 'Test';
  	var count = arStr.length - 1;
		for(i = 0; i<arStr.length; i++){
			strAr += '\["'+arStr[i]+'"\]';
			if(i<count){
				strEval += strAr+'=\[\];' 
			}else{
				strEval += strAr+'='+5+';'	
			}
		}
		eval(strEval);
		return Test;
	}
}

function documentHeight(){
	var nHeight = $('.content_top').height()
	var box_search_height = 0;
	if($('.box_search').is('div')){
		box_search_height = $('.box_search').height()+($('.box_search').css('paddingTop').replace('px', '')-0)+($('.box_search').css('paddingBottom').replace('px', '')-0);
		$('.box_services').height(nHeight - box_search_height)	
	}else{
		$('.box_services').height(nHeight+2)	
	}	
	$('.box_solution').height(nHeight)
	$('.box_news').height(nHeight)
	$('.box_video').height(nHeight)
	$('.box_content').height(nHeight-1)
	
}

function setHeight(){
	$('.box_content').height($('.content_gradient').height() + 130)
	$('.box_services').height($('.box_content').height() - 332)
}

$(document).ready(function(){

	$('input[type="text"]').focus(function() {	
		if (this.value == this.defaultValue){
			this.value = ''
		}
		if(this.value != this.defaultValue){
			this.select()
		}
	})
	$('input[type="text"]').blur(function() {	
		if ($.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '')
		}
	})
	
	if( $.browser.msie && $.browser.version == '6.0'){
		var ie6 = true;
	}	
	
	if(!ie6){
	
		// Подгонка высоты блоков главной страницы
		documentHeight();		
		
		// Всплывающие окна		
			$(".solution_open").click(function() {
				$(".solution.popup").fadeToggle("fast", "linear")
				return false
			})
			$(".open_popup").click(function() {
				$('.popup').hide()
				var obj = $('.'+$(this).attr('id')+'.popup')
				var position = $(this).position()
				var thisWidth = $(this).outerWidth()
				var objWidth = obj.width()
				obj.css({'left':position.left - objWidth + thisWidth + 22, 'top':position.top - 5})
				obj.fadeIn("fast", "linear")
				return false
			})
			$(".arrow_up").click(function() {
				$(this).parent('.popup').fadeOut("fast", "linear")
				return false
			})
			$(".popup_close").click(function() {
				$(this).parent('.popup').fadeOut("fast", "linear")
				return 	false
			})

		
		// Выбор языка	
		$('.header_lang').children('div:gt(0)').hide()	
		$('.lang_arrow').toggle(function(){	
			$(this).removeClass('down').addClass('up')
			var n = $(this).parent().children('div').length
			$(this).parent().css({'height': 25*n + 'px'})
			$(this).parent().children('div:gt(0)').show()	
		}, function(){	
			$(this).removeClass('up').addClass('down')
			$(this).parent().css({'height':'25px'})
			$(this).parent().children('div:gt(0)').hide()	
		})
		
		// Карта сайта	
		$('.site_map_open').toggle(function(){	
			$(this).addClass('open')
			$('.content_bottom_line').removeClass('red_line')
			$('.site_map').slideDown('slow', function(){
				$('.content_bottom_line').addClass('gray_line')
				$('.site_map').height($('.site_map').height());
			})	
		}, function(){	
			$(this).removeClass('open')
			$('.content_bottom_line').removeClass('gray_line')
			$('.site_map').slideUp('slow', function(){		
				$('.content_bottom_line').addClass('red_line')
			})	
		})
	
	}
	
	$('.ajax_send').click(function(){

		arParam = [];
		$(this).parents('form:first').find('input').each(function(){
			arParam[$(this).attr('name')] = $(this).val();
		});
		$(this).parents('form:first').find('textarea').each(function(){
			arParam[$(this).attr('name')] = $(this).val();  
		});
		$popup = $(this).parents('.popup');
		var urlOfData = '/data/data.php';
		var current_uri = window.location.href;
		if(current_uri.match('/en/')){
			urlOfData = '/en/data/data.php';
		}
		$.post(urlOfData, {
				'data': convert_json(arParam)
			},
			function(data){ 
				if(data.success){
					alert(data.success);
					$popup.fadeOut("fast", "linear");
			  } 
				if(data.error){  
			  	alert(data.error);
				}
			}, 'json');
		
		return false;
	});
	
	box_search_height = $('.box_search').height();
	box_content_height = $('.box_content').height(); 
	box_solution_height = $('.box_solution').height();
	box_news_height = $('.box_news').height();
	box_video_height = $('.box_video').height();    
	
	if($('#catalog_search_adv').is('.catalog_search_adv_show')){
		$('.box_search').height(box_search_height+$('#catalog_search_adv').height());	
		$('.box_content').height(box_content_height+$('#catalog_search_adv').height());
		$('#button_plus_catalog').addClass('button_minus');
		$('#button_plus_catalog span span').text('Скрыть дополнительные параметры');
	}
		
	$('#button_plus_catalog').click(function(){
		if(!$('#catalog_search_adv').is('.catalog_search_adv_show')){
			var adv_h = $('#catalog_search_adv').height();
			$('#catalog_search_adv').show().addClass('catalog_search_adv_show');
			$(this).parents('.box_search').height(box_search_height+$('#catalog_search_adv').height());
			$('.box_content').height(box_content_height + adv_h); 
			$('.box_solution').height(box_solution_height + adv_h);
			$('.box_news').height(box_news_height + adv_h);
			$('.box_video').height(box_video_height + adv_h);
			$('#button_plus_catalog').addClass('button_minus');
			$('#button_plus_catalog span span').text('Скрыть дополнительные параметры');
		}else{
			$('#catalog_search_adv').hide().removeClass('catalog_search_adv_show');	
			$(this).parents('.box_search').height(box_search_height);
			$('.box_content').height(box_content_height);  
			$('.box_solution').height(box_solution_height);
			$('.box_news').height(box_news_height);
			$('.box_video').height(box_video_height);
			$('#button_plus_catalog').removeClass('button_minus'); 
			$('#button_plus_catalog span span').text('Показать дополнительные параметры');   
		}
		return false;
	});
	
	$('#service_centers_ekf').click(function(){
		var url = '';
	 	url = $(this).parents('form').find('select').val();
	 	if(url!=0){
			window.location.href = url;
		}else{
			alert('Выберите регион из списка!');
		}
	 	return false;
	});
	
	$('#distribution_send').click(function(){
		var url = '';
	 	url = $(this).parents('form').find('select').val();
	 	if(url!=0){
			window.location.href = url;
		}else{
			alert('Выберите регион из списка!');
		}
	 	return false;
	});
	
	$('#assembly_centers_send').click(function(){
		var url = '';
	 	url = $(this).parents('form').find('select').val();
	 	if(url!=0){
			window.location.href = url;
		}else{
			alert('Выберите регион из списка!');
		}
	 	return false;
	});
	
	$('.jcarousel-item-name').click(function(){
		var url = '/';
		url = $(this).find('input[type=hidden]').val();
		window.location.href = url;
	});
	
	
})
