
// popup script – additional properties for jQuery object
$(document).ready(function(){
	popupFunctions();
	initFormPopup();
	switchFormsInPopup();
	formHTML();
	dropDown();
});

$(window).load(partnersAlign)

function initFormPopup(){
	$('#popup_form_trigger, .popup_form_trigger').click(function(e){
		e.preventDefault();
		$('#popup_form').togglePopup();
	})
}

function switchFormsInPopup(){
	$('#popup .body .left a').live('click', function(e){
		e.preventDefault();

		$(this).parent().children('a').removeClass('active');
		$(this).addClass('active');
		$(this).parent().next().children().removeClass('active').removeAttr('style');
		var actID = $(this).attr('href').split('#')[1];

		$('#' + actID).fadeIn(333, function(){
			$(this).addClass('active');
		})
	})
}

function dropDown(){
  $('a.drop').click(function(e){
    e.preventDefault();
    $('#' + $(this).attr('href').split('#')[1]).slideToggle();
  })
}
function formHTML(){
	var strings = $('.form .string');
	$.each(strings, function(){
		var tempHTML = $(this).children().clone();
		$(this).children().remove();
		$('<div />', {
			'class': 'string_bg',
			html: tempHTML
		}).appendTo(this);
	})

	var texts = $('.form .textarea');
	$.each(texts, function(){
	  var tempHTML = $(this).children().clone();
	  $(this).children().remove();
	  var inner_1 = $('<div />', {
	    'class' : 'text-inner-1'
	  }).appendTo(this);
	  var inner_2 = $('<div />', {
	    'class' : 'text-inner-2'
	  }).appendTo(inner_1);
	  var inner_3 = $('<div />', {
	    'class' : 'text-inner-3'
	  }).appendTo(inner_2);
	  var inner_3 = $('<div />', {
	    'class' : 'text-inner-4',
	    html : tempHTML
	  }).appendTo(inner_3);
	})
	
	var widthFix = $('.form .field');
	var expr = /w_\d{1,}px/;

	$.each(widthFix, function(){
	  var currentClass = $(this).attr('class')
	  if (expr.test(currentClass)){
      $(this).css({
        width: currentClass.match(expr)[0].replace('w_','')
      })
	  }
	})
}

function partnersAlign(){
  var box = $('#partners .box');
  var expr = /h_\d{1,}px/;
	$.each(box, function(){
	  var currentClass = $(this).attr('class')
	  if (expr.test(currentClass)){
      var height = currentClass.match(expr)[0].replace('h_','');
      
      $(this).children('.item').css('height', height);
      $(this).find('a, span').mouseenter(function(){
        $(this).children('img').css('top', '-' + height)
      })
      $(this).find('a, span').mouseleave(function(){
        $(this).children('img').css('top', '0px')
      })
	  }

	  var width = $(this).width();
	  var childrenWidth = 0;

	  var items = $(this).children();
	  for (var i = 0; i < items.length; i ++){
	    childrenWidth += $(items[i]).width();
	  }

	  if (!(/default/.test(currentClass))){
  	  var marginLeft = parseInt((width - childrenWidth) / ($(this).children().length - 1 ? $(this).children().length - 1 : 1));
  	  $(this).children('.item:not(:first-child)').css('margin-left', marginLeft + 'px')
	  }
	})
}

// супер фильтр для цифр 
function digitsFilter(e)	{
	// Make sure to use event.charCode if available
	var key;
	if(typeof e.charCode == 'undefined' || e.charCode == 0){
		key = e.keyCode
	}
	else {
		key = e.charCode;
	}
	// Ignore special keys
	if (e.ctrlKey || e.altKey || key < 32 || key == 37 || key == 39)
		return true;

	key = String.fromCharCode(key);
	return /\d|\+/.test(key);
}


function popupFunctions(){
   //align element in the middle of the screen
   $.fn.alignCenter = function() {
      //get margin left
      var marginLeft =  - $(this).width()/2 + 'px';
      //get margin top
      var marginTop =  - $(this).height()/2 + 'px';
      //return updated element
      return $(this).css({'margin-left':marginLeft, 'margin-top':marginTop});
   };

   $.fn.togglePopup = function(){
     //detect whether popup is visible or not
     if($('#popup').hasClass('hidden'))
     {
       //hidden - then display
       //when IE - fade immediately
       if($.browser.msie)
       {
         $('#opaco').height($(document).height()).toggleClass('hidden')
                    .click(function(){$(this).togglePopup();});
       }
       else
       //in all the rest browsers - fade slowly
       {
         $('#opaco').height($(document).height()).toggleClass('hidden').fadeTo('slow', 0.6)
                    .click(function(){$(this).togglePopup();});
       }

       $('#popup')
         .html($(this).html())
         .alignCenter()
         .toggleClass('hidden');
       $('#popup .closer').click(function(e){
      	 e.preventDefault();
      	 $(this).togglePopup();
      });
    }
     else
     {
       //visible - then hide
       $('#opaco').toggleClass('hidden').removeAttr('style').unbind('click');
       $('#popup .closer').unbind('click');
       $('#popup').toggleClass('hidden');
     }
   };
}
