// globals for dropdown timeout
var hideDropTO = false
function hideDrop() {
	$(".b-drop__tale").fadeOut("fast")
	}

// index cols
function IndexCols() {
    var side_cols_height = Math.max($(".l-body__side").height(),$(".l-body__extra").height()) + 40
    if ($(".b-tex__lr").height() < side_cols_height) {
        $(".b-tex__lr").css({'min-height': side_cols_height});
    }
}
// for inputs wrapper
function CaptBg() {
	$(".fs__captcha .wrap").parent('div.text').removeClass("text_short").removeClass("text");
	}
function CapWrap() {
	$(".fs__captcha .text").wrap("<div class='text text_short'></div>");							
	}
function LogWrap() {
	$(".b-login input.text").wrap("<div class='text'></div>");							
	}

function error_reg() {
	$('.b-form dd').each(function() {
		var error_one = $(this).find('.b-error');
		if (error_one.hasClass('errmsg_show')) {
			error_one.addClass('marker');
			}
		if (error_one.hasClass('marker')) {	
			error_one.addClass('display-n');
			$(this).parent('dl').find('dt label').prepend("<span class='approval'><strong>!</strong>&nbsp;</span>");
			$(this).parents().filter('fieldset').hover(
					function() { error_one.removeClass('display-n'); },
					//
					function() { error_one.addClass('display-n'); }
				);
			
			$('#RegErrorMes').fadeIn('slow');
		}
		
		var Scream = function(context) { 
				var Screamer = $(context).parents().filter('fieldset').find('.approval');
				if (Screamer.length) {
					Screamer.remove();
					error_one.removeClass('errmsg_show').removeClass('marker');
					// After clicking on field marked as error, 
					// unbind hover() events to let error notifications 
					// subsequently raised by ajax calls be permanently visible on screen. 
					$(context).parents().filter('fieldset').unbind('mouseenter mouseleave');
				}
		}
		$(this).find('#recaptcha_response_field').click(function(){
			Scream(this);
		});
		$(this).find('input.text').click(function(){
            Scream(this);
        });
		$(this).find('.b-textarea textarea').click(function(){
            Scream(this);
        });
		$(this).find('.jNiceCheckbox').click(function(){
            Scream(this);
        });
		$(this).find('.jNiceSelectText').click(function(){
            Scream(this);
        });
		$(this).find('.jNiceRadio').click(function(){
            Scream(this);
        });	
	})
}

/*  function for centering elements   */
(function($){
    $.fn.extend({
         center: function (options) {
              var options =  $.extend({ // Default values
                   inside:window, // element, center into window
                   transition: 0, // millisecond, transition time
                   minX:0, // pixel, minimum left element value
                   minY:0, // pixel, minimum top element value
                   withScrolling:true, // booleen, take care of the scrollbar (scrollTop)
                   vertical:true, // booleen, center vertical
                   horizontal:true // booleen, center horizontal
              }, options);
              return this.each(function() {
                   var props = {position:'absolute'};
                   if (options.vertical) {
                        var top = ($(options.inside).height() - $(this).outerHeight()) / 2;
                        if (options.withScrolling) top += $(options.inside).scrollTop() || 0;
                        top = (top > options.minY ? top : options.minY);
                        $.extend(props, {top: top+'px'});
                   }
                   if (options.horizontal) {
                         var left = ($(options.inside).width() - $(this).outerWidth()) / 2;
                         if (options.withScrolling) left += $(options.inside).scrollLeft() || 0;
                         left = (left > options.minX ? left : options.minX);
                         $.extend(props, {left: left+'px'});
                   }
                   if (options.transition > 0) $(this).animate(props, options.transition);
                   else $(this).css(props);
                   return $(this);
              });
         }
    });
})(jQuery);

$(document).ready(function(){

	// index cols
	IndexCols()
	$(window).bind("load resize",function(){
		IndexCols()
		})

	// drop
	$(".b-drop").hover(function(){
		if (hideDropTO) clearTimeout(hideDropTO)
		},
		function(){
		hideDropTO = setTimeout("hideDrop()", 500)
		})
	$(".b-drop__arr").click(function(){
		if ($(this).parents(".b-drop").find(".b-drop__tale").get(0).style.display == "block") {
			$(".b-drop__tale").fadeOut("fast")	
			}
		else {
			$(this).parents(".b-drop").find(".b-drop__tale").fadeIn("fast")
			}
		return false
		})
		
	// text inputs hints
	//$(".text_hint[value!=]")
	//	.each(function(){this.hint=this.value})
	//	.bind("focus",function(){if(this.hint==this.value){$(this).attr("value","").removeClass("text_hint")}})
	//	.bind("blur",function(){if(this.value==""){	$(this).attr("value",this.hint).addClass("text_hint")}})
	CaptBg();
	CapWrap();
	LogWrap();
	$('#RegErrorMes').css("text-align", 'center').hide().find('h2').css('padding-bottom', "2px");
	error_reg();
	//for errors in the forms
	/*$(function() {
		$('.b-form dd').each(function() {	
			var parheight = $(this).height();
			var errorheiht = $(this).find('.b-error').height();
			var errorwidth = $(this).find('.b-error').width();
			if (parheight < errorheiht) {
				//errorwidth = errorwidth +10 ;
				d =parseInt((errorwidth*(errorheiht-parheight))/parheight);	
				if (d >160) {
					$(this).find('.b-error .input_errmsg').css("width", d + "px" );
					$(this).find('.b-error ').css("right", "-" + (d +30) +"px");
					}
				
				}
			});
		});	
	*/
	
	//center cluster overlay wisget
	$('.cluster-change-content').center();

	//.b-admin-tools-ul li
	$('.b-admin-tools-ul li').each(function() {
		$(this).append("<span>&nbsp;|</span>");
		/*$(this).find('li').last().css('border', 'none');
		$(this).find('li').first().css('padding-left', '0');*/
		});
	$('.b-admin-tools-ul').each(function() {
		/*$(this).append("<span>&nbsp;|</span>");*/
		$(this).find('li').last().find('span').css('display', 'none');
		});
	
});
