$(function(){
//flags
  var masterPageFlag = $('body').attr('id') === 'master_page' ? true : false;
  var IE6 = $.browser.msie && parseInt($.browser.version) < 7 ? true : false;


/*search field*/
  $('#search_field').mousedown(function(){
				         if(this.value === '') $(this).css('background-position', 'left -25px');
					   }).focus(function(){
						 if(this.value === '') $(this).css('background-position', 'left -25px');
					   }).blur(function(){
						 if(this.value === '') $(this).css('background-position', 'left top');
					   });
  

  /* main nav */
  var mouseOutTimeOut = null;
  var tabsLink =      $('#main_nav li a');
  var currentLink =   $('#main_nav #current')[0]
  var masterLink  =   $('#main_nav .master_page')[0]
  var containerDivs = $('#main_nav #container div');
  var images  =       $('#main_nav #container span');
  var currentId =     $('#main_nav #current')[0].name;
  
  containerDivs.fadeOut(10)
  
  if($('#main_nav #current').size() != 0) {
	 $('#'+$('#main_nav #current')[0].name).fadeIn(10)
  }
  if(!masterPageFlag) images.height(0)
//  masterPageFlag
  tabsLink.hover(function() {
	  clearTimeout(mouseOutTimeOut)
	  var cont = $('#container #'+this.name)
	  if(cont.is(':animated')) return;
	  if(cont.size() == 0) return;
	  if(this.name == currentId && masterPageFlag) {
	     if(IE6) images.css('height', '72px')
         else images.animate({height: 72});
         return;
      }
	  
	  if(masterPageFlag) {
	     $('#'+currentId).fadeOut(100, function(){
									  cont.fadeIn(300)
									  currentId = cont.attr('id')
									  if(IE6) images.css('height', '72px')
									  else    images.animate({height: 72})
									})
	  }
	  else {
		if($(this).hasClass('current')){
	      images.animate({height: 72})
		}
		else {
	      tabsLink.removeClass('current')
		  $(this).addClass('current')
		   $('#'+currentId).fadeOut(100, function(){
									  cont.fadeIn(300)
									  currentId = cont.attr('id')
									  if(IE6) images.css('height', '72px')
									  else    images.animate({height: 72})
									})
		}
	   
	  }
  },
  function() {
	  if(!masterPageFlag) mouseOutTimeOut = setTimeout(function(){ 
                           if(IE6) images.css('height', '0')
                           else images.animate({height: 0}) 
                        }, 4000)
  })
  
  
  $('#main_nav #container').hover(function(){
	  clearTimeout(mouseOutTimeOut)
	  images.each(function() {
		 if($(this).is(':visible')) {
	         if(IE6) images.css('height', '72px')
			 else    images.animate({height: 72})
		 }
	  })
  },
  function(){
	  if(!masterPageFlag) mouseOutTimeOut = setTimeout(function(){
                          if(IE6) images.css('height', '0')
                          else images.animate({height: 0})
                        }, 4000)
      $('#main_nav #container a').css('opacity', 1)
  })
  
  
  $('#main_nav #container a').hover(function(){
	  $(this).siblings().css('opacity', 0.5)
	  $(this).css('opacity', 1)
  }, 
  function(){
  })
  
  
  
  //back to current
  var currentItem = $('#main_nav #current');
  var mainNavLi = $('#main_nav li');
  mainNavLi.hover(
     function() {
       if(this !== currentItem[0]) currentItem.removeAttr('id', 'current')
	   tabsLink.removeClass('current')
     }, 
	 function(event){
	   if(event.relatedTarget == $('#main_nav #container')[0] && !$('a', this).hasClass('master_page')) {
		 $('a', this).addClass('current')
		 return;
	   }
       currentItem.attr('id', 'current')
     }
  )
  
  $('#main_nav #container').hover(
	  function(){
	    
	  },
	  function(){
        currentItem.attr('id', 'current')
		tabsLink.removeClass('current')
      })

  
  
  /* mirrow popup */
  var popupTimeOut;  
  $('#top_menu #mirrow_popup p +').hide();
  
  $('#top_menu #mirrow_popup p:first').toggle(function(event){
								   event.stopPropagation();
								   if($.browser.msie && parseInt($.browser.version) < 7) {
								       $('#top_menu #mirrow_popup').css({
										  backgroundColor: 'white',
										  border: '1px solid #cbcfd1'
										})
								   }
								   else {
								       $('#top_menu #mirrow_popup').css({
										  backgroundColor: 'white',
										  outline: '1px solid #cbcfd1'
										})
								   }
								   $('#top_menu #mirrow_popup p +').show();
								   
                                 },
							     function(event){
								    event.stopPropagation();
								    $('#top_menu #mirrow_popup').css({
									  backgroundColor: 'transparent',
									  border: 'none',
									  outline: 'none'
									})
									$('#top_menu #mirrow_popup p +').hide();
							     })
  
   $('#top_menu #mirrow_popup').hover(
									function(){
									  clearTimeout(popupTimeOut)
									}, 
									function(){
									  popupTimeOut = setTimeout(function(){
											$('#top_menu #mirrow_popup').css({
											   backgroundColor: 'transparent',
											   border: 'none',
											   outline: 'none'
											})
											$('#top_menu #mirrow_popup p +').hide();
									  }, 4000)
								    })
  
  
  /* Style and functional block. Click on tabs */
  $('#style_and_func #tabs td').mousedown(function(){
    $('>a', this).addClass('click');
	$(this).siblings().children('a').removeClass();
	var innerHTML = $('div.description', this).html();
	$('#style_and_func #block_for_text').html(innerHTML);
  })
  
  $('#style_and_func #tabs td:first-child > a').addClass('click')
  $('#style_and_func #block_for_text').html(  $('#style_and_func td:first-child .description').html()  ) 
  
    
  
  
  /* registration form */
  var heightRegistrationForm = $('#registration_form').height();
  
  $('#registration_form').css({
					        top: '50%',
							marginTop: -((70+heightRegistrationForm)/2)
						  })
  
  var regFormWelcomes = {};
  $('#registration_form input[type=text]').each(function(){
    regFormWelcomes[this.name] = {}
    regFormWelcomes[this.name].thisElement = this;
	if(this.value !== this.title) this.value = this.title // AntiCach Reright;
    regFormWelcomes[this.name].value = this.value;
  }).focus(function(){
	for(var i in regFormWelcomes) {
       if(regFormWelcomes[i].thisElement == this) {
	       if(regFormWelcomes[i].value == this.value) {
			   this.value = ''
			   $(this).css('color', '#000')
                 if(this.id == 'unreal_password' || this.id == 'unreal_repeatPassword') {
				   $(this).hide();
				   $('+ input', this).css('color', '#000').show().focus();
				 }
		   }
	   } 
	}
  }).blur(function(){
    if(this.value === '') {
	   for(var i in regFormWelcomes) {
          if(regFormWelcomes[i].thisElement == this) {
	             this.value = regFormWelcomes[i].value
			     $(this).css('color', '#a2a2a2')
	        } 
	   } 
	}
  });
  
  
  $('#registration_form input[type=password]').blur(function(){
	  if($(this).attr('value') == '') {
		  $(this).hide();
	      $(this).prev('input').show().blur();
	  }
  });
  
  
  $('#registration_form #generateAndEmail, #autarisationAllWrap #rememberMe, #jurRegForm #equalPhys, #jurRegForm #equalJur, #jurRegForm a.icqNo, #jurRegForm a.skypeNo').mousedown(function(){
    $('input', this)[0].checked = !$('input', this)[0].checked;
	if($('input', this)[0].checked) {
	  $('span', this).css('background-position', 'left 0')
	}
	else {
	  $('span', this).css('background-position', 'left -10px')
	}
  })
  
  
  /* registration link click  and close form   and autorization click*/
  $('a.registrationLink').click(function(event){
    event.preventDefault();
	$('#autarisationAllWrap').fadeOut(function(){
       $('#transparenty_layer').fadeIn(function(){
          $('#registration_form').fadeIn();
	   })  
	})
  })
  
  $('a.toRegJur').click(function(event){
    event.preventDefault();
	$('#autarisationAllWrap, #registration_form').fadeOut(function(){
       $('#transparenty_layer').fadeIn(function(){
          $('#jurRegForm').fadeIn();
	   })  
	})
  })
  
  $('#transparenty_layer, #registration_form .closeForm, #autarisationAllWrap .closeForm, #remindPasswordForm .closeForm, #jurRegForm .closeForm').mousedown(function(){
    $('#registration_form, #autarisationAllWrap, #remindPasswordForm, #jurRegForm').fadeOut(function(){
        $('#transparenty_layer').fadeOut();
	})
	$('#autorizationForm #emailWrap').fadeOut(function(){
		$('#autorizationForm div').fadeIn()
	})
  })
  
  $('a.autorizationLink').click(function(event){
    event.preventDefault();
	$('#registration_form').fadeOut(function(){
       $('#transparenty_layer').fadeIn(function(){
          $('#autarisationAllWrap').fadeIn();
	   })  
	})
  })
  
  
  
  /* autorization fields click */
  $('#autarisationAllWrap input[type=text], #autarisationAllWrap input[type=password], #remindPasswordForm input[type=text]').focus(function(){
     if(this.value === '') $(this).css('background-position', 'left -250px');
  }).blur(function(){
     if(this.value === '') $(this).css('background-position', '7px 65%');
  });  
  
  
  /* forgotPassword? */
  $('#forgotPassword').click(function(event){
    event.preventDefault();
	$('#autorizationForm div').fadeOut(function(){
		$('#autorizationForm #emailWrap').fadeIn()
	})
  })
  
  
  
  
  /* jur reg form */
  var curTabJurFormName = $('#jurRegForm #regTabs .current').attr('id');
  $('#jurRegForm div.description').each(function(){
     if($(this).attr('name') == curTabJurFormName) $(this).css('display', 'block')
  })
  
  var allowSubmitJur = false;
  $('#jurRegForm #submitJurReg').mousedown(function() {
	 //alert(allowSubmitJur)
	 $(this).removeClass()
	 var curH = $('#regTabs h4.current')
	 if(curH.next().size() != 0) {
			curH = curH.next()
		    $('#regTabs h4').removeClass();
		    curH.addClass('current');
		   
			var curTabJurFormName = curH.attr('id');
			
		    $('#jurRegForm div.description').css('display', 'none').each(function(){
		      if($(this).attr('name') == curTabJurFormName) {$(this).css('display', 'block')}
		    })
	 }
	 else {
		   allowSubmitJur = true;
	 }
		 
	 if(curH.next().size() == 0) $(this).addClass('submit')
	 
	 $('#jurRegForm #prevTabJurForm').show()
  })
  
  $('#prevTabJurForm').mousedown(function(){
     var curH = $('#regTabs h4.current')
	 
	 if(curH.prev().size() != 0) {
		    $('#jurRegForm #submitJurReg').removeClass()
			curH = curH.prev()
		    $('#regTabs h4').removeClass();
            if(curH.prev().size() == 0) $('#jurRegForm #prevTabJurForm').hide()
		    curH.addClass('current');
		    var curTabJurFormName = curH.attr('id');
			
		    $('#jurRegForm div.description').css('display', 'none').each(function(){
		      if($(this).attr('name') == curTabJurFormName) {$(this).css('display', 'block')}
		    })
	 }
	 else {
		   return;
	 }
  })
  
  
  $('#jurRegForm').submit(function(){if(!allowSubmitJur) return false;})

  $('#jurRegForm p input').not('[readonly]').attr('value', '').focus(function(){
	if(this.value == '') $(this).prev().css('visibility', 'hidden')
  }).blur(function(){
    if(this.value == '') $(this).prev().css('visibility', 'visible')
  })
  
  
  $('#jurRegForm span#regJurDropDown').mousedown(function(event){event.stopPropagation()})
  $('#jurRegForm span#regJurDropDown input').focus(function(event){$(this).blur()})
  $('#jurRegForm span#regJurDropDown').mousedown(function(){
    if($('label', this).is(':hidden')) $('label', this).css('visibility', 'visible')
	else $('label', this).css('visibility', 'hidden')
  })
  $('#jurRegForm span#regJurDropDown a').mousedown(function(){
	var text = $(this).text();
	$('#jurRegForm span#regJurDropDown input').attr('value', text)
  })
  $('#jurRegForm').mousedown(function(){
    $('#jurRegForm span#regJurDropDown label').css('visibility', 'hidden')
  })
  
  
  //
  function breakOnFragments(input, config) {
  
  /*
  config is object {
	count: ,
	fill: ,
	generalStyle: is object style for jquery - {},
	currentStyle: ,
	connectElement: 
  }
  */
    var wrapper = $('<span></span>')
	var massiv = $()
	wrapper.css({
	  border: 'none',
	  background: 'none',
	  float: 'none',
	  padding: 0,
	  margin: 0
	})
	
	input.css({
	  width: 0,
	  height: 0,
	  visibility: 'hidden',
	  display: 'none',
	  lineHeight: 0,
	  overflow: 'hidden'
	})
	

	for(var i=0; i<config.count; i++){
	  $('<input>').css(config.generalStyle).attr({value: config.fill, size: 1, maxLength: 1}).appendTo(wrapper)
	}
	
    var children = wrapper.children()
	
	children.focus(function(event){
	  $(this).css(config.currentStyle)
	})
	.blur(function(){
	  $(this).css(config.generalStyle)
	}).keydown(function(event){
	   if(event.target == this) {
			 var code = event.which;
			 if(code == 37) {
				 $(this).blur();
				 if($(this).prev().size() != 0) $(this).prev().focus();
				 else $(children[children.size()-1]).focus();
			 }
			 else if(code == 39) {
				 $(this).blur();
				 if($(this).next().size() != 0) $(this).next().focus();
				 else $(children[0]).focus()
			 }
			 else if(code == 9) {
				 $(this).blur()
				 event.preventDefault();
			     config.nextTabOrder.next().children()[0].focus()
				 return;
				 //alert(config.nextTabOrder[0])
			 }
			 else {
				var value = String.fromCharCode(code);
				    value = value.replace(/\D/, config.fill)
		        this.value = value;
				var s=''
				children.each(function(){
				  s += this.value;
				})
				input.attr('value', s)

				$(this).blur();
				if($(this).next().size() != 0) $(this).next().focus();
				else $(children[0]).focus()
			 }
	   }
	}).keypress(function(event){
	   if(event.target == this) {
	         var code = event.which;
			 if(code == 13) {
			   //enter	 
			 }
	   }
	})
	
	wrapper.insertAfter(input)
  }//end function
  
  breakOnFragments($('#jurFormINN'), {
    count: 10,
	fill:  '0',
	generalStyle: {margin: 0, color: '#000', background: '#fff', fontSize: '16px', fontWeight: 'bold', padding: '2px 1px', width: '11px', height: '14px', textAlign: 'center'},
	currentStyle: {color: '#fff', background: '#000'},
	nextTabOrder: $('#jurFormKPP')
  })
  
  breakOnFragments($('#jurFormKPP'), {
    count: 10,
	fill:  '0',
	generalStyle: {width: 11, margin: 0, width: 10, height: 16, padding: '1px 0  0 1px', color: '#000', background: '#fff', fontSize: '15px', fontWeight: 'bold', fontFamily: 'monospace'},
	currentStyle: {color: '#fff', background: '#000'},
	nextTabOrder: $('#jurFormOKPO')
  })
  
  breakOnFragments($('#jurFormOKPO'), {
    count: 5,
	fill:  '0',
	generalStyle: {width: 11, margin: 0, width: 10, height: 16, padding: '1px 0  0 1px', color: '#000', background: '#fff', fontSize: '15px', fontWeight: 'bold', fontFamily: 'monospace'},
	currentStyle: {color: '#fff', background: '#000'},
	nextTabOrder: $('#jurFormOGRN')
  })
  
  breakOnFragments($('#jurFormOGRN'), {
    count: 5,
	fill:  '0',
	generalStyle: {width: 11, margin: 0, width: 10, height: 16, padding: '1px 0  0 1px', color: '#000', background: '#fff', fontSize: '15px', fontWeight: 'bold', fontFamily: 'monospace'},
	currentStyle: {color: '#fff', background: '#000'},
	nextTabOrder: null
  })
  
  



  breakOnFragments($('#jurFormRubN'), {
    count: 20,
	fill:  '0',
	generalStyle: {width: 11, margin: 0, width: 10, height: 16, padding: '1px 0  0 1px', color: '#000', background: '#fff', fontSize: '15px', fontWeight: 'bold', fontFamily: 'monospace'},
	currentStyle: {color: '#fff', background: '#000'},
	nextTabOrder: $('#jurFormRubInN')
  })
  breakOnFragments($('#jurFormRubInN'), {
    count: 20,
	fill:  '0',
	generalStyle: {width: 11, margin: 0, width: 10, height: 16, padding: '1px 0  0 1px', color: '#000', background: '#fff', fontSize: '15px', fontWeight: 'bold', fontFamily: 'monospace'},
	currentStyle: {color: '#fff', background: '#000'},
	nextTabOrder: $('#jurFormBIK')
  })
  breakOnFragments($('#jurFormBIK'), {
    count: 9,
	fill:  '0',
	generalStyle: {width: 11, margin: 0, width: 10, height: 16, padding: '1px 0  0 1px', color: '#000', background: '#fff', fontSize: '15px', fontWeight: 'bold', fontFamily: 'monospace'},
	currentStyle: {color: '#fff', background: '#000'},
	nextTabOrder: $('#jurFormCor')
  })
  breakOnFragments($('#jurFormCor'), {
    count: 9,
	fill:  '0',
	generalStyle: {width: 11, margin: 0, width: 10, height: 16, padding: '1px 0  0 1px', color: '#000', background: '#fff', fontSize: '15px', fontWeight: 'bold', fontFamily: 'monospace'},
	currentStyle: {color: '#fff', background: '#000'},
	nextTabOrder: null
  })
  
  
  
  
  
  $('#jurRegForm #equalJur, #jurRegForm #equalPhys').mouseup(function(){
    if( $('input', this)[0].checked ) {
		var thisAddress = $('input', $(this).parent()[0]).not('[type=checkbox]').get()
		var prevAddress = $('input', $(this).parent().prev()[0]).not('[type=checkbox]').get()
		for(var i=0; i<thisAddress.length; i++) {
		  if(prevAddress[i].value != '') thisAddress[i].focus()
		  thisAddress[i].value = prevAddress[i].value
		}
	}

  })
 
})

