﻿jQuery(document).ready(function(){



jQuery(".salles .editThis").live("click", function(){
		  
	  window.top.location.href=$(this).attr('href');
	  return false;
});	


/*
ajax ссылки о предприятии
*/

var savedHTML;

jQuery(".popupDets").live("click", function(){
	 savedHTML = $('#popupText').html();
 	var maxHeight = $('body').height()-230;
	try
		 {
		  jQuery.ajax({
		  url: jQuery(this).attr('href'),
		  type: 'get',
		   timeout: 5000,
		   success: function(gettedval){
 
			   
			 jQuery("#popupText").slideUp(400);
			 setTimeout(function() {jQuery("#popupText").empty()}, 400);
			 setTimeout(function() {jQuery("#popupText").html(gettedval);$('.factoryPopup').css('max-height',maxHeight+'px');}, 401);
			 
			 setTimeout(function() {jQuery("#popupText").slideDown(400)}, 401);
			 
			  
		
		  }
		  });
		 }
		 catch(e)
		 {
		  alert("При поиске произошла неизвестная ошибка. \n Попробуй еще раз через несколько минут.");
		 }
		
		
		return false;
	  
    });



/*попап инфо о продаже*/
jQuery("ul.listOfChosenProd li a").live("click", function(){
														  
	if ((jQuery.browser.msie == true)&(jQuery.browser.version <=8))
			{
				jQuery("#popupDark").css('display','block');
			}
			else
			{
				jQuery("#popupDark").fadeIn(400);
			}
		  
		  
		  
														  
 	var maxHeight = $('body').height()-130;
	try
		 {
		  jQuery.ajax({
		  url: jQuery(this).attr('href'),
		  type: 'get',
		   timeout: 5000,
		   success: function(gettedval){
			 $("#popupText").html('<div id="goodWideDets"></div>');
			 $("#goodWideDets").append(gettedval); 
			 jQuery("#popupWrap").addClass('onlyWhite').fadeIn(400);
			 $('#goodWideDets').css('display','block');
			 var scrollerHeight = $('#goodWideDets').height();
			 $('#goodWideDets').css('display','none');
			 if(maxHeight < scrollerHeight)
			 { 
			 }
			 else
			 {
				 var topper = maxHeight-scrollerHeight;
				 jQuery("#popupWrap").css('top',topper/2+10+'px');
			 }
			$('#goodWideDets').css('max-height',maxHeight+'px');	 
			 $("#goodWideDets").slideDown(400);
			 
			  
		
		  }
		  });
		 }
		 catch(e)
		 {
		  alert("При поиске произошла неизвестная ошибка. \n Попробуй еще раз через несколько минут.");
		 }
		
		
		return false;
	  
    });


jQuery(".backToSell").live("click", function(){
	  
			 jQuery("#popupText").slideUp(400);
			 setTimeout(function() {jQuery("#popupText").empty()}, 400);
			 setTimeout(function() {jQuery("#popupText").html(savedHTML)}, 401);
			 setTimeout(function() {jQuery("#popupText").slideDown(400)}, 401);

		
		return false;
	  
    });




/*клик по кнопке редактированиея*/

jQuery("#factPopupped").live("click", function(){
		  var factEditing = $('#factPopupped #factoryEditing').attr('value');
		  //alert(factEditing);
		  //if(factEditing == 0)
		  //{
			  
			  var button = jQuery(this);
			  
			  var maxHeight = $('body').height()-200;
			  
			  if ((jQuery.browser.msie == true)&(jQuery.browser.version <=8))
				{
					jQuery("#popupDark").css('display','block');
				}
				else
				{
					jQuery("#popupDark").fadeIn(400);
				}
			  
			  var factId = $('#repalcedName input[name=idFactory]').val();
			  var factNew = $('#repalcedName  input[name=newFactory]').val();
		  
			  
			  try
			 {
			  jQuery.ajax({
			  url: button.attr('data-link'),
			  type: 'get',
			  data: 'id=' + factId + '&new=' + factNew + '',
			  timeout: 5000,
			  success: function(gettedval){
				   
				jQuery("#popupWrap").addClass('onlyWhite').fadeIn(400);
				 jQuery("#popupText").append(gettedval);
				 $('#addPrPopup').addClass('editingForm');
				 $('#addPrPopup').css('display','block');
				 repositionScroll();
				 $('#addPrPopup').css('display','none');
				 $('#addPrPopup .scroller').css('max-height',maxHeight+'px');
				 setTimeout(function() {$('#addPrPopup').slideDown(400)}, 400);
				 
				 var params = {
					changedEl: "#naim_oao",
					visRows: 7
				}
				cuSel(params);
				
				
				
			
			  }
			  });
			 }
			 catch(e)
			 {
			  alert("При поиске произошла неизвестная ошибка. \n Попробуй еще раз через несколько минут.");
			 }
		  //}
		  //else
		  //{
			//  alert('Редактирование предприятия запрещено, поскольку оно уже проверено модератором.');
		  //}
			
		  return false;
	  
});	
		
		
/*factory details*/	

jQuery("#factories article").live("click", function(){
													
		var maxHeight = $('body').height()-230;
	  
		var elem = $(this).find('h1').find('span');
		  

		    if ((jQuery.browser.msie == true)&(jQuery.browser.version <=8))
			{
				jQuery("#popupDark").css('display','block');
			}
			else
			{
				jQuery("#popupDark").fadeIn(400);
			}
		  
		  
		  jQuery("#popupWrap").addClass('onlyWhite').fadeIn(400);
		  jQuery("#popupText").html('<div class="factoryPopupWrap"><h1>Информация о предприятии</h1><h2>'+elem.html()+'</h2><div class="factoryPopup"><dl><dt>ИНН</dt><dd>'+elem.parent().next().find('span').html()+'</dd><dt class="info">Информация о предприятии</dt><dd>'+elem.parent().next().next().html()+'</dd></dl><h3>Контакты</h3><ul id="contactPersons"></ul></div></div>');
		  
		  var contArray = elem.attr('data-contacts').split('%n%');
									
			for(i = 0; i < contArray.length; i++) {
					
				 var subArr = contArray[i].split('%d%');	
					
				$('#contactPersons').append('<li><span class="arr"></span><span class="title">'+subArr[0]+'</span><p>'+subArr[1]+'</p></li>');
				
				for(k = 2; k < subArr.length; k++) {
						
					$('#contactPersons li:last').append('<span class="phone">'+subArr[k]+'</span>');
		
				}
	
			}
			
			$('.factoryPopup').css('max-height',maxHeight+'px');
			
			$('#contactPersons li:first-child').addClass('first-child');
			
			return false;
	  
});	


/*sale details*/	

jQuery(".salles article").live("click", function(){
	  
		var elem = $(this).find('h1 a');
		
		  var maxHeight = $('body').height()-230;
		  
		  if ((jQuery.browser.msie == true)&(jQuery.browser.version <=8))
			{
				jQuery("#popupDark").css('display','block');
			}
			else
			{
				jQuery("#popupDark").fadeIn(400);
			}
			
		  jQuery("#popupWrap").addClass('onlyWhite').fadeIn(400);
		  jQuery("#popupText").html('<div class="factoryPopupWrap"><h1>Информация о продаже</h1><h2><a class="popupDets" href="'+elem.attr('data-link')+'">'+elem.html()+'</a><span>'+elem.parent().next().html()+'</span></h2><div class="factoryPopup"></div>');
		  
		  
		  
		  var linker = elem.attr('href');
	
			try
				 {
				  jQuery.ajax({
				  url: linker,
				  type: 'get',
				   timeout: 5000,
				   success: function(gettedval){
					 $('.factoryPopup').css('display','none');
					 $('.factoryPopup').html(gettedval); 
					 $('.factoryPopup').css('max-height',maxHeight+'px');
					 jQuery(".factoryPopup").slideDown(400);
					 
					  
				
				  },
					error: 	function (){
						alert("Произошла ошибка. Сервер не отвечает. Повторите попытку позже.");
						return false;
					}
				  });
				 }
				 
				 catch(e)
				 {
				  alert("При поиске произошла неизвестная ошибка. \n Попробуй еще раз через несколько минут.");
				 }
				
		  
		  return false;
	  
});	
		
		
		
		
// show/hide popup


jQuery("header nav a, .footerMenu li a, .catalogProductHeader ul li a, .popupLangNav").live("click", function(){
	  
	  if($(this).attr("data-url").indexOf("/")==-1)
	  {
		  return true;
	  }
	  else
	  {  
		  if ((jQuery.browser.msie == true)&(jQuery.browser.version <=8))
			{
				jQuery("#popupDark").css('display','block');
			}
			else
			{
				jQuery("#popupDark").fadeIn(400);
			}
			
		  jQuery("#popupWrap").fadeIn(400);
		  var linker = jQuery(this).attr('data-url');
	
		try
			 {
			  jQuery.ajax({
			  url: linker,
			  type: 'get',
			  data:jQuery(this).attr('data-value') ,
			   timeout: 5000,
			   success: function(gettedval){
				   //alert('done');
				 jQuery("#popupText").html(gettedval); 
				 setTimeout(function() {jQuery("#popupText .showerWrap").slideDown(400)}, 401);
				 
				  
			
			  }
			  });
			 }
			 catch(e)
			 {
			  alert("При поиске произошла неизвестная ошибка. \n Попробуй еще раз через несколько минут.");
			 }
			
			return false;
	  }
	  
});


jQuery("#popupClose").live("click", function(){
	  
	  // переход на главную с формы персонала
	  if (jQuery('#pForm').length > 0) location.href = '/';
	  	  
	  jQuery("#popupDark").css('display','none');
	  jQuery(".ketchup-error").fadeOut(400);
	  jQuery("#popupWrap").fadeOut(400);
	  setTimeout(function() {jQuery("#popupText").removeClass('onlyWhite').empty();chooserShow();}, 450);
	  jQuery("#popupWrap").css('top','10px');
	  
	  return false;
	  
    });


// preload images
jQuery('body').append('<img src="http://atf.ru/images/popup-sprite.png" style="visibility: hidden; position: absolute; top: -100px; left: 0; z-index: -1"><img src="http://atf.ru/images/popup-dots.png" style="visibility: hidden; position: absolute; top: -100px; left: 0; z-index: -1">');



/*ajax popup open*/



/*
ajax ссылки внутри поапа
*/

jQuery(".ajaxPopupLink").live("click", function(){
	  
	  jQuery(this).addClass('clickedLink');
	  
	  var maxHeight = $('body').height()-300;

	try
		 {
		  jQuery.ajax({
		  url: jQuery(".clickedLink").eq(0).attr('href'),
		  type: 'get',
		  data:jQuery(".clickedLink").eq(0).attr('value') ,
		   timeout: 5000,
		   success: function(gettedval){
			   
			 jQuery(".clickedLink").removeClass('clickedLink');  
			   
			 jQuery("#popupText .showerWrap").slideUp(400);
			 setTimeout(function() {jQuery("#popupText").empty()}, 400);
			 setTimeout(function() {jQuery("#popupText").html(gettedval)}, 401);
			 setTimeout(function() {
				$('#popupText .showerWrap').css('display','block');
				 repositionScroll2();
				 $('#popupText .showerWrap').css('display','none');
				 jQuery("#popupText .showerWrap").slideDown(400);$('#goodsPopupWrap').css('max-height',maxHeight+'px');																							 
																											 
				}, 401);
			 
			 chooserShow();
			 
			  
		
		  }
		  });
		 }
		 catch(e)
		 {
		  alert("При поиске произошла неизвестная ошибка. \n Попробуй еще раз через несколько минут.");
		 }
		
		
		return false;
	  
    });




/*
back button click
*/

jQuery("#prevPageBtn").live("click", function(){
	  
	  var maxHeight = $('body').height()-300;

	try
		 {
		  jQuery.ajax({
		  url: jQuery("#prevPageBtn").attr('name'),
		  type: 'get',
		  data:jQuery("#prevPageBtn").attr('value') ,
		   timeout: 5000,
		   success: function(gettedval){
			   
			 jQuery("#popupText .showerWrap").slideUp(400);
			 setTimeout(function() {jQuery("#popupText").empty()}, 400);
			 setTimeout(function() {jQuery("#popupText").html(gettedval)}, 401);
			 setTimeout(function() {
				$('#popupText .showerWrap').css('display','block');
				 repositionScroll2();
				 $('#popupText .showerWrap').css('display','none');					 
				 jQuery("#popupText .showerWrap").slideDown(400);$('#goodsPopupWrap').css('max-height',maxHeight+'px');
				}, 401);
			 chooserShow();
			 
			  
		
		  }
		  });
		 }
		 catch(e)
		 {
		  alert("При поиске произошла неизвестная ошибка. \n Попробуй еще раз через несколько минут.");
		 }
		
		
		return false;
	  
    });









/*++выбор продукта+++*/	

/*клик на продукт в попапе*/

jQuery("a.pickingItem").live("click", function(){
				
	if ($(this).hasClass('pickedItem'))
	{
		$(this).removeClass('pickedItem');	
	}
	else
	{
		$('.pickedItem').removeClass('pickedItem');	
		$(this).addClass('pickedItem');	
	}
	chooserShow();
				
	return false;						
});

/*клик кнопку выбора продукта*/

jQuery("#searchProduct").live("click", function(){
	  
	  var maxHeight = $('body').height()-300;
	  
		  if ((jQuery.browser.msie == true)&(jQuery.browser.version <=8))
			{
				jQuery("#popupDark").css('display','block');
			}
			else
			{
				jQuery("#popupDark").fadeIn(400);
			}
		  var linker = jQuery(this).attr('data-link');
	
		try
			 {
			  jQuery.ajax({
			  url: linker,
			  type: 'get',
			  data:jQuery(this).attr('data-value') ,
			   timeout: 5000,
			   success: function(gettedval){
				 jQuery("#popupText").html(gettedval); 
				 
				 $('#goodsPopupWrap').css('max-height',maxHeight+'px');
				 
				 jQuery("#popupWrap").fadeIn(400);
				 				 
				 var maxHeight = $('body').height()-130;
				 
				 $('.showerWrap').css('display','block');
				 var scrollerHeight = $('#popupText').height();
				 $('.showerWrap').css('display','none');
				 if(maxHeight < scrollerHeight)
				 { 
				 }
				 else
				 {
					 var topper = maxHeight-scrollerHeight-10;
					 jQuery("#popupWrap").css('top',topper/2+10+'px');
				 }
				 
				 setTimeout(function() {jQuery("#popupText .showerWrap").slideDown(400)}, 401);
				 
				  
			
			  }
		  });
		 }
		 catch(e)
		 {
		  alert("При поиске произошла неизвестная ошибка. \n Попробуй еще раз через несколько минут.");
		 }
		  
			
		  return false;
	  
});	

/*клик регистрации предприятия*/

jQuery("#addNewFct").live("click", function(){
	  	
			
		
		  var button = jQuery(this);
		  
		  var innTransfer = $('#Afactory').val();
		  
		  var maxHeight = $('body').height()-200;
	  
		  
		  if ((jQuery.browser.msie == true)&(jQuery.browser.version <=8))
			{
				jQuery("#popupDark").css('display','block');
				$('.ketchup-error').css('display','none');
			}
			else
			{
				jQuery("#popupDark").fadeIn(400);
			}
			
		  
		  
		  try
		 {
		  jQuery.ajax({
		  url: button.attr('data-link'),
		  type: 'get',
		  data:jQuery('#Afactory').val() ,
		   timeout: 5000,
		   success: function(gettedval){
			   
			jQuery("#popupWrap").addClass('onlyWhite').fadeIn(400);
			
			 jQuery("#popupText").append(gettedval);
			 $('#addPrPopup').css('display','block');
			 repositionScroll();
			 $('#addPrPopup').css('display','none');
			 $('#addPrPopup .scroller').css('max-height',maxHeight+'px');
			 setTimeout(function() {$('#addPrPopup').slideDown(400)}, 400);
			 $('#PInnHidd').val(innTransfer);
			 $('#innToPaste').text(innTransfer);
			 
			 var params = {
				changedEl: "#naim_oao",
				visRows: 7
			}
			cuSel(params);
			
			
			
		
		  }
		  });
		 }
		 catch(e)
		 {
		  alert("При поиске произошла неизвестная ошибка. \n Попробуй еще раз через несколько минут.");
		 }
		  
			
		  return false;
	  
});	

/*клик на кнопку ВЫБРАТЬ*/

jQuery(".popupAddProduct").live("click", function(){
		
		$('.listOfChosenProd').empty();
		$('#searchProduct').addClass('hidenn');
		
		if((jQuery("#Ainform").hasClass('blackText'))&&(jQuery("#Atecnics").hasClass('blackText'))&&(jQuery("#Aearlier").val()!==''))
		{
			formEn();
		}
		
		$('.Aimplementing .markGood').removeClass('markDis').animate({
	        width:"15px"
	    }, 500 );
		
		
	jQuery("#popupWrap a.pickedItem").each(function() {   
		 
			 var elem = $(this),
				 elemHref = $(this).attr('data-link'),
				 elemText = $(this).attr('data-parent-name') + ' ' +$(this).text(),
				 elemValue = $(this).attr('data-name'),
				 targetIns = $('.listOfChosenProd'),
				 elemPath = $(this).attr('data-product-path');
				 
				 

		 

				targetIns.append('<li><a href="'+elemHref+'" title="' + elemPath + '">'+elemText+'</a><input name="idProduct" type="hidden" value="'+elemValue+'"><span title="изменить"></span></li>');

		 
	});		
				
				
	$('#popupClose').click();
	return false;						
});


/*клик на кнопку УДАЛИТЬ*/

jQuery("ul.listOfChosenProd li span").live("click", function(){
	
	/*$('.Aimplementing .markGood').removeClass('markApproved').addClass('markDis');	*/
	/*$(this).parent().remove();*/
	$('#searchProduct')/*.removeClass('hidenn')*/.click();

	return false;						
});




	
jQuery("#goodChooserPopup div.hitarea, #goodChooserPopup .folder").live("click", function(){


	if(jQuery(this).parent().hasClass("ajaxLang"))
{
	var target = jQuery(this).parent();
				var targethref = jQuery(this).parent().attr("data-href");
				jQuery(target).append("<img id='preloadBrach' width='16' height='16' src='images/ajax-loader.gif' />");
				try
					 {
					  jQuery.ajax({
					  url: targethref,
					   timeout: 5000,
					   success: function(gettedval){
						 var inserplace = jQuery(target).find('ul').addClass('searchNow');
						 jQuery('#preloadBrach').remove();

						  jQuery(gettedval).appendTo(jQuery(inserplace));
							
							highlightPicked();
						 
						 $('.searchNow').removeClass('searchNow');
						
						 $(inserplace).treeview({
							animated: "fast",
							collapsed: true,
							unique: false
						});

						
						
						
						
						
						target.removeClass("ajaxLang");
					
					  }
					  });
					 }
					 catch(e)
					 {
					  alert("При попытке открыть данную ветвь произошла неизвестная ошибка. \n Попробуй еще раз через несколько минут.");
					 }
					
					
					return false;
}
else
{
	return false;
}
});	





								
});
	
	
	





function highlightPicked() {

	jQuery(".listOfChosenProd li input").each(function() {   
		 
		 var elemVal = $(this).val();

		 
		 	$('.searchNow .pickingItem').each(function() {   

		 
				 if(elemVal == $(this).attr('data-name'))
				 {
					 $(this).addClass('pickedItem');
				 }

			});	
			 
		
		 
	});	

}


function chooserShow() {

	if(jQuery("#popupWrap a.pickedItem").length == 0)
		{
			$('.popupAddProduct').css('display','none');
		}
		else
		{
			$('.popupAddProduct').css('display','block');
		}

}


function repositionScroll() {

	 var scrollerHeight = $('#addPrPopup .scroller').height();
	 
	 var maxHeight = $('body').height()-200;
			 
			 if(maxHeight < scrollerHeight)
			 { 
			 }
			 else
			 {
				 var topper = maxHeight-scrollerHeight;
				 jQuery("#popupWrap").css('top',topper/2+10+'px');
			 }

}
function repositionScroll2() {

	 var scrollerHeight = $('#goodsPopupWrap').height();
	 
	 var maxHeight = $('body').height()-200;
			 
			 if(maxHeight < scrollerHeight)
			 { 
			 	jQuery("#popupWrap").css('top','20px');
			 }
			 else
			 {
				 var topper = maxHeight-scrollerHeight;
				 jQuery("#popupWrap").css('top',topper/2+10+'px');
			 }

}
