var regexpNum = /[0-9]+/;
var regexpWidth = /[0-9]+/;
var modal_timeout;
var search_t = false;
function getNumericId(id){
	return regexpNum.exec(id);
}
function getIntWidth(width){
	return regexpWidth.exec(width);
}

$(function(){
	$('#blackbackground').live('click', modalWindowClose );

	if( screen.width<=1024 ){
		$('body').append('<link rel="stylesheet" type="text/css" href="/i/css/mini.css" media="all" />');
	}
	
	if ( $('#auth').length ){
		var auth = $('#auth');
		modalWindow($(auth).find('h1').html(), $(auth).find('div').html());
		modalWindowClass('auth');
		$(auth).remove();
	}
	
	if ( $('#messages_error').length ){
		var  err = new Array;
		var err_size = 0;
		$('#messages_error li').each(function(){ err[err_size] = $(this).html(); ++err_size; });
		$('#messages_error').remove();
		alertError(err);
	}
		
	$('#auth span').click(function(){ $('#auth').hide(); });
});

function nl2br(s) {
	s = s.split("\u000A").join("<br />\u000A");
	return s;
}

function relocation(link){
	window.location = link.toString();
}

function modalWindow(title, text, ajax, timeout){
	modalWindowClose();
	
	$('body')
		.append('<div id="blackbackground"></div>')
		.append('<div id="modal"></div>');
	$('#modal')
		.append('<span class="exitsettings"></span>')
		.append('<h1>' + title + '</h1>')
		.append('<div class="in"></div>');
	
	$('#modal .exitsettings').click(function(){
		modalWindowClose();
		return false;
	});
	
	if ( text ){
		$('#modal .in').append( text );
		if ( timeout ){
			modal_timeout = setTimeout(modalWindowClose, timeout);
		}
		$('#modal .cancel').click(function(){
			clearTimeout(modal_timeout);
			modalWindowClose();
			return false;
		});
	}
	else if ( ajax ){
		$.ajax({
			type: 'POST',
			codepage: 'koi8-r',
			url: ajax.url,
			data: ajax.data,
			beforeSend: function(){
				$('#modal').append('<div class="loader"></div>');
			},
			complete: function(){
				$('#modal .loader').remove();
			},
			success: function(json){
				jsonAjax(json);
				
				if ( json.content ){
					$('#modal .in').html( json.content.toString() );
					$('#modal form').submit(function(){
						return ajaxForm($(this));
					});
					$('#modal .cancel').click(function(){
						modalWindowClose();
						return false;
					});
				}
				
				if ( ajax.onload ){
					ajax.onload();
				}
			},
			error: function(){
				alertError('Ошибка AJAX');
			}
		});
	}
}

function modalWindowConfirm(title, text, func){
	text += '<div class="confirm"><span class="green-button"><input type="button" value="Ok" class="ok" /></span><span class="green-button"><input type="button" value="Отмена" class="cancel" /></span></div>';
	
	modalWindow(title, text);
	$('#modal .confirm .ok').click(modalWindowClose);
	$('#modal .confirm .ok').click(func);
	$('#modal .in').addClass('question');
}

function modalWindowQuestion(title, text, func1, func2){
	text += '<div class="confirm"><span class="green-button"><input type="button" value="Ok" class="ok" /></span><span class="green-button"><input type="button" value="Отмена" class="cancel" /></span></div>';
	
	modalWindow(title, text);
	$('#modal .confirm .ok').click(modalWindowClose);
	$('#modal .confirm .ok').click(func1);
	$('#modal .in').addClass('question');
	
	$('#modal .confirm .cancel').click(modalWindowClose);
	$('#modal .confirm .cancel').click(func2);
	$('#modal .in').addClass('question');
}

function modalTop(title, text, type){
	if (type == 'confirm'){
		text += '<div class="confirm"><span class="green-button"><input type="button" value="Ok" class="ok" /></span><span class="green-button"><input type="button" value="Отмена" class="cancel" /></span></div>';
	}
	
	$('body')
		.append('<div id="black_sourse_top"></div>')
		.append('<div id="modal_top"></div>');
	$('#modal_top')
		.append('<span class="exitsettings"></span>')
		.append('<h1>' + title + '</h1>')
		.append('<div class="in"></div>');
	
	$('#modal_top .exitsettings').click(function(){
		modalTopClose();
		return false;
	});

	$('#modal_top .in').append( text );	

	$('#modal_top .confirm .cancel').click(modalTopClose);
	$('#modal_top .confirm .top').click(modalTopClose);
	$('#modal_top .in').addClass('question');
}



function modalTopClose(){
	$('#modal_top').html('').remove();
	$('#black_sourse_top').html('').remove();
}

function modalWindowClass(className){
	$('#modal').addClass( className );
}

function ajaxForm(form){
	$.ajax({
		type: 'POST',
		codepage: 'koi8-r',
		url: $(form).children("input[name=url]").val().toString(),
		data: $(form).serializeArray(),
		beforeSend: function(){
			$('#modal').append('<div class="loader"></div>');
		},
		success: function(json){
			jsonAjax(json);
			if ( json.content ){
				$('#modal .in').html( json.content.toString() );
				$('#modal form').submit(function(){
					return ajaxForm($(this));
				});
				$('#modal .cancel').click(function(){
					modalWindowClose();
					return false;
				});
			}
		},
		error: function(){
			alertError('Ошибка AJAX');
		},
		complete: function(){
			$('#modal .loader').remove();
		}
	});
	return false;
}

function modalWindowClose(){
	$('#blackbackground').remove();
	$('#modal').html('').remove();
	clearTimeout(modal_timeout);
}

function modalWindowError(title, errors){
	if ( !title ) title = 'Ошибка';
	var text = '<ul>';
	if( typeof(errors)=='object' ){
		var length = errors.length;
		for ( i=0;i<length;++i ){
			text += '<li>' + errors[i] + '</li>';
		}
	}
	else{
		text += '<li>' + errors + '</li>';
	}
	text += '<li><span class="green-button"><input type="submit" value="Ok" id="red_ok"/></span></li>';
	text += '</ul>';
	
	modalWindow(title, text);
	$('#modal').addClass('error');
	
	$(document).keypress(function(e){
		if (e.keyCode == 27 && $('#modal').length ){
			modalWindowClose();
		}
	});
}

$('#red_ok').live('click', function(){
	modalWindowClose();
});

function modalWindowInfo(title, info, timeout){
	if ( !title ) title = 'Информация';
	if ( !timeout ) timeout = 2000;
	var text = '<ul>';
	if( typeof(info)=='object' ){
		var length = info.length;
		for ( i=0;i<length;++i ){
			text += '<li>' + info[i] + '</li>';
		}
	}
	else{
		text += '<li>' + info + '</li>';
	}
	text += '</ul>';
	
	modalWindow(title, text, false, timeout);
	$('#modal').addClass('info');
	
	$(document).keypress(function(e){
		if (e.keyCode == 27 && $('#modal').length ){
			modalWindowClose();
		}
	});
}

function jsonAjax(json){
	if ( json.errors ){
		alertError( json.errors );
	}
	
	if ( json.info ){
		alertInfo( json.info );
	}
	
	if ( json.eval ){
		ev = toString(json.eval);
		for ( i in json.eval ){
			eval( json.eval[i] );
		}
	}
}

function alertError(errors){
	modalWindowError('Ошибка', errors);
}

function alertInfo(info){
	modalWindowInfo('Информация', info);
}

function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
		document.cookie = curCookie;
}

function getCookie(name) {
	var prefix = name + "=";
	var cookieStartIndex = document.cookie.indexOf(prefix);
	if (cookieStartIndex == -1)
		return null;
	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
	if (cookieEndIndex == -1)
		cookieEndIndex = document.cookie.lengthl;
	return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" + 
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}
