var inProcess = false;

(function($){
	jQuery.fn.imageCorners = function(options){
		return this.each(function(){
			var imgObj = this;
			var imgContainer = jQuery(imgObj).parent();
			jQuery(imgContainer).css('position', 'relative');
			jQuery(imgObj).after('<div class="lefttopcorner"></div>');
			jQuery(imgObj).after('<div class="leftbottomcorner"></div>');
			jQuery(imgObj).after('<div class="righttopcorner"></div>');
			jQuery(imgObj).after('<div class="rightbottomcorner"></div>');
		});
	}
})(jQuery);

function is_ie6(){
	return (/MSIE (5\.5|6).+Win/.test(navigator.userAgent));
}
function is_ie7(){
	return (/MSIE (7).+Win/.test(navigator.userAgent));
}
function em(){
	return parseInt(jQuery('body').css('font-size').substring(2, 0));
}

function wordEnding(count, singular, plural, special){
	var word = plural;
	if (!(/^\d{0,}1\d$/.test(count))){
		if (!(/^\d{0,}[5-9]$/.test(count))){
			if ((/^\d{0,}[2-4]$/.test(count))){
				word = special;
			}
			else if ((/^\d{0,}[1]/.test(count))){
				word = singular;
			}
		}
	}
	return word;
}
function charLeftCounter(id, maxCount){
	var currCount = jQuery('#' + id).val();
	currCount = currCount.replace(/^\s+/, '').replace(/\s+$/, '');
	currCount = maxCount - currCount.length;
	if (currCount >= 0){
		jQuery('#tip' + id).html('В вашем распоряжении осталось <strong>' + currCount + '</strong> ' + wordEnding(currCount, 'символ', 'символов', 'символа'));
		jQuery('#tip' + id).removeClass('errored');
	}
	else{
		currCount = Math.abs(currCount);
		jQuery('#tip' + id).html('<div class="error">Удалите ' + wordEnding(currCount, 'лишний', 'лишние', 'лишние') + ' <strong>' + currCount + '</strong> ' + wordEnding(currCount, 'символ', 'символов', 'символа') + '</div>');
	}
	delete(currCount);
}
function clearInput(objId){
	jQuery('#'+objId).val('');
	jQuery('#cb'+objId).fadeOut('fast');
	jQuery('#'+objId).focus();
}
function modifyInput(objId){
	if (jQuery('#'+objId).val()){
		jQuery('#cb'+objId + ':hidden').fadeIn('fast');
	}
	else{
		jQuery('#cb'+objId + ':visible').fadeOut('fast');
	}
}
function __fillData(html){
	jQuery('#ajaxContainer').html(html);
	jQuery('#UserFrom').val(document.location);
}
function ajaxForm(parentId){
	if (inProcess){
		return false;
	}
	jQuery('form.ajaxForm').each(function(){
		jQuery(this).submit(function(){
			var ajaxForm = jQuery(this);
			var temp = 'var formData = {';
			jQuery(((parentId) ? (parentId + ' ') : '') + 'form.ajaxForm input[type="text"],input[type="hidden"],input[type="password"],select').each(function(){
				temp += "'" + this.name + "':'" + jQuery(this).val() + "',";
			});
			temp = temp.substring(0, temp.length - 1) + '}';
			eval(temp);
			jQuery.post(jQuery(ajaxForm).attr('action'), formData, function(html){
				if (html.substr(0, 5) == 'http:'){
					document.location = html;
				}
				else{
					__fillData(html);
				}
				inProcess = false;
			});
			return false;
		});
	});
}
function ajaxLink(parentId){
	if (inProcess){
		return false;
	}
	jQuery(((parentId) ? (parentId + ' ') : '') + 'a.ajaxLink').each(function(){
		jQuery(this).click(function(){
			var ajaxLink = jQuery(this);
			jQuery.get(jQuery(ajaxLink).attr('href'), null, function(html){
				if (html.substr(0, 5) == 'http:'){
					document.location = html;
				}
				else{
					__fillData(html);
				}
				inProcess = false;
			});
			return false;
		});
	});
}
function NavigateThrough(event){
	if (!document.getElementById){
		return;
	}
	if (window.event){
		event = window.event;
	}
	if (event.ctrlKey){
		var link = null;
		var href = null;
		switch (event.keyCode ? event.keyCode : event.which ? event.which : null){
			case 0x25:
				link = document.getElementById('PrevLink');
				break;
			case 0x27:
				link = document.getElementById('NextLink');
				break;
		}
		if (link && link.href){
			document.location = link.href;
		}
		if (href){
			document.location = href;
		}
	}			
}
jQuery().bind('ajaxStart', function(){
	inProcess = true;
	if (jQuery('#ajaxContainer').length != 1) {
		jQuery('#content').append('<div id="ajaxContainer" style="position:fixed;top:3em;margin-left:20em"></div>');
	}
	if (jQuery('#ajaxLoading').length != 1) {
		jQuery('#ajaxContainer').append('<div id="ajaxLoading"></div>');
	}
	jQuery('#ajaxLoading').show();
	jQuery('#ajaxLoading').css('opacity', 0.75);
});
jQuery().bind('ajaxStop', function(){
	inProcess = false;
	jQuery('#ajaxLoading').hide();
	jQuery('#ajaxContainer').ready(function(){
		ajaxForm('#ajaxContainer');
		ajaxLink('#ajaxContainer');
	});
});
