var regExpEmail = /^(([a-z0-9_\-]+\.)*[a-z0-9_\-]+@[a-z0-9\-]+(?:\.?[a-z0-9]+)?\.[a-z]{2,5})$/i;
var errTimeout = 'Превышено время ожидания ответа';
var msgSaved = {'yes': 'Изменения успешно сохранены', 'not': 'Изменения не сохранены'};
var msgNotSavedAlert = 'Данные не сохранены. Вы действительно хотите покинуть страницу?';
var tout = 12000;

$(document).ready(function(){
	$('#run').hide();
	$.each($('a img'), function(){ $(this).parent().addClass('img_link') });
	socialIcons(document.location.href);
	$(document).keypress(function(e){
		var code = (e.charCode) ? e.charCode : ((e.keyCode) ? e.keyCode : ((e.which) ? e.which : 0));
		if (code == 27 && $('div[id^="outer"]').length > 0) { popupHide(); }
	});
});

$.fn.extend({
	check: function() { return this.each(function() { this.checked = true; }); },
	uncheck: function() { return this.each(function() { this.checked = false; }); },
	enable:  function() { return this.each(function() { this.disabled = false }); },
	disable: function() { return this.each(function() { this.disabled = true }); }
});

function logout() {
	$.ajax({
		type: 'post',
		timeout: tout,
		url: '/ajax/logout.ajax.php',
		data: '',
		dataType: 'json',
		success: function(data) { document.location.href = 'http://' + data.uri; }
	});
}

function toggleElms(el1, el2) {
	var id1 = el1.attr('id'), id2 = el2.attr('id');
	el1.hide();
	el2.hide();
	var html = el1.html();
	el1.html(el2.html());
	el2.html(html);
	el1.fadeIn('slow');
	el2.fadeIn('slow');
	el1.attr('id', 'curr');
	el2.attr('id', id1);
	$('#curr').attr('id', id2);
	return;
}

var jGrowlOpts = {
		'yes': { life: 5000, theme: 'jGrowl-green' },
		'not': { life: 5000, theme: 'jGrowl-red' }
};

function flashVersion() {
	var ua = navigator.userAgent.toLowerCase();
	var isIE = (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1);
	var version = 0;
	var lastVersion = 10;
	var i;
	if (isIE) {
		try {
			for (i = 3; i <= lastVersion; i++) {
				if (eval('new ActiveXObject("ShockwaveFlash.ShockwaveFlash.'+i+'")')) { version = i; }
			}
		} catch(e) {}
	} else {
		for (i = 0; i < navigator.plugins.length; i++) {
			if (navigator.plugins[i].name.indexOf('Flash') > -1) {
				var curr = navigator.plugins[i].description.replace(/Shockwave Flash /gi, '');
				curr = parseInt(curr.substring(0, curr.indexOf('.')));
				version = (curr > version) ? curr : version;
			}
		}
	}
	return version;
}

function setFind() {
	$('#q').val($('#spanFind').text());
	return;
}

function socialIcons(pURL) {
	if ($('#socialIcons').length > 0) {
		$('#socialIconsBookmarks').jFav();
		var pTitle = encodeURIComponent(document.title);
		pURL = pURL.replace(/#[a-z0-9=&]+/i, '');
		$.each($('#socialIcons a'), function(){
			var key = $(this).attr('id').replace(/socialIcons/, '');
			switch (key) {
				case 'Bookmarks': {
					$(this).click(function() {
						pageTracker._trackPageview('/ajaxevents/butfav/');
						return false;
					});
					break;
				}
				case 'Delicious': {
					$(this).click(function() {
						pageTracker._trackPageview('/ajaxevents/butdel/');
						window.open('http://delicious.com/save?jump=yes&url=' + pURL + '&title=' + pTitle);
						return false;
					});
					break;
				}
				case 'Facebook': {
					$(this).click(function() {
						pageTracker._trackPageview('/ajaxevents/butface/');
						window.open('http://facebook.com/sharer.php?u=' + pURL + '&t=' + pTitle);
						return false;
					});
					break;
				}
				case 'Twitter': {
					$(this).click(function() {
						pageTracker._trackPageview('/ajaxevents/buttwit/');
						window.open('http://twitter.com/home?status=' + pTitle + ' - ' + pURL);
						return false;
					});
					break;
				}
				case 'LJ': {
					$(this).click(function() {
						pageTracker._trackPageview('/ajaxevents/butlj/');
						window.open('http://www.livejournal.com/update.bml?subject=' + pTitle + '&event=' + pTitle + ' - ' + pURL);
						return false;
					});
					break;
				}
				case 'BobrDobr': {
					$(this).click(function() {
						pageTracker._trackPageview('/ajaxevents/butbobr/');
						window.open('http://bobrdobr.ru/addext.html?url=' + pURL + '&title=' + pTitle);
						return false;
					});
					break;
				}
				case 'News2': {
					$(this).click(function() {
						pageTracker._trackPageview('/ajaxevents/butnews2/');
						window.open('http://news2.ru/add_story.php?url=' + pURL);
						return false;
					});
					break;
				}
				case 'Memory': {
					$(this).click(function() {
						pageTracker._trackPageview('/ajaxevents/butmem/');
						window.open('http://memori.ru/link/?sm=1&u_data[url]=' + pURL + '&u_data[name]=' + pTitle);
						return false;
					});
					break;
				}
			}
					
		});
	}
	return;
}

function log(obj) {
	for (var k in obj) {
		console.log(k + ': ' + obj[k]);
	}
	return;
}

function popupShow(html) {
	$('select').hide();
	$('body').prepend(html);
}

function popupHide() {
	$('select').show();
	$('#outer,#outer2').remove();
	window.scrollBy(0, 1);
	window.scrollBy(0, -1);
	return;
}

function pluralNumber(count, arg0, arg1, arg2, arg3) {
	var result = arg0;
	var last_digit = count % 10;
	var last_two_digits = count % 100;
	if (last_digit == 1 && last_two_digits != 11) {
		result += arg1;
	} else if ((last_digit == 2 && last_two_digits != 12) || (last_digit == 3 && last_two_digits != 13) || (last_digit == 4 && last_two_digits != 14)) {
		result += arg2;
	} else {
		result += arg3;
	}
	return result;
}

String.prototype.trim = function() {
	return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,' ');
}

String.prototype.encode = function() {
	return encodeURIComponent(this);
}