(function(){

$(function(){
	$('#body form input[@type="hidden"]').addClass('hidden');
	$('#body form input[@type="checkbox"]').addClass('checkbox');
	$('#body form input[@type="radio"]').addClass('radio');

	if ($.browser.msie) {
		$('#body form input.checkbox').css({
			'margin-top': '-4px',
			'margin-left': '-4px'
		});
	}

	initRighSideNav();
	initTips();
});

ItemEdit = function(id) {
	if (!this.frame) this.frame = $('<iframe src="/item_edit?i='+id+'" style="display:none;" frameborder="0" marginheight="0" marginwidth="0" width="400" height="400" />').appendTo('body');
	$.screen.show(this.frame);
}

Medals = {
	addToUser: function(id) {
		var d = prompt('Комментарий к медальке', '');
		if (d !== null) {
			$.post('/medals/add', {'u':id, 'd':d}, function(){
				document.location.reload();
			});
		}
	},
	addToItem: function(id) {
		var d = prompt('Комментарий к медальке', '');
		if (d !== null) {
			$.post('/medals/add', {'i':id, 'd':d}, function(){
				document.location.reload();
			});
		}
	},
	drop: function(id) {
		if (confirm('Отобрать медальку?')) {
			$.post('/medals/delete', {'m':id}, function(){
				document.location.reload();
			});
		}
	}
};

initRighSideNav = function()
{

	$('#rightSide .nav').after('<div class="nav_bg1"><div class="nav_bg2"></div></div>');
	$('#rightSide .nav_bg2').append($('#rightSide .nav'));

	$('#rightSide .nav>li>a').click(function(){
		$('#rightSide .nav li').removeClass('selected');
		$(this).parent().addClass('selected');
		return false;
	});
}

initTips = function()
{
	// simple
	$('.tip').tip();

	// help
	function updatePos(obj, e) {
		var pos = $.mouse(e);
		obj.next().css({'top': pos.top + 16, 'left': pos.left + 16});
	}
	function showHelp(obj, e) {
		updatePos(obj, e);
		obj.next().show();
	}
	function hideHelp(obj) {
		obj.next().hide();
	}
	function showHideHelp(obj, e) {
		if (obj.next().css('display') == 'none') {
			showHelp(obj, e);
		} else {
			hideHelp(obj);
		}
	}
	$('.tip-help').each(function(){
		$(this)
		.removeClass('tip-help')
		.addClass('tip-help-block')
		.css({'position': 'absolute', 'width': ($.browser.msie ? 200 : 'auto')})
		.hide()
		.before(
			$('<span>[?]</span>')
			.addClass('tip-help-what')
			.click(function(e) {
				showHideHelp($(this), e)
			})
			.mouseover(function(e) {showHelp($(this), e)})
			.mouseout(function() {hideHelp($(this))})
			.mousemove(function(e) {updatePos($(this), e)})
		);
	})
}


jQuery.extend({TempUploader: $('<form enctype="multipart/form-data" method="post" />')});

jQuery.TempUploader.isConstruct = false;
jQuery.TempUploader.construct = function() {
	if (this.isConstruct) return;
	this.isConstruct = true;
	this.css({'display': 'none'});
	$('body').append(this);

	this.callback = false;
	this.uploading = false;
	this.addClass('TempUploader-window');
	this.attr('target', 'TempUploaderIFrame');


	//this.frame = $('<iframe name="TempUploaderIFrame" />').appendTo(this);
	this.frame = $('<iframe name="TempUploaderIFrame" style="display:none;" />').appendTo(this);

	this.frame.load(function(){
		if (!jQuery.TempUploader.uploading) return;
		jQuery.TempUploader.uploading = false;
		var r = $(this.contentWindow.document.body).text();
		r = eval('r = '+r+';');
		switch (r.status) {
			case 'ok':
				if (jQuery.TempUploader.callback)
					jQuery.TempUploader.callback(r.hash, r.name);
				jQuery.TempUploader.close();
				break;
			default:
			case 'error':
				jQuery.TempUploader.file.show();
				jQuery.TempUploader.bSubmit.show();
				jQuery.TempUploader.bCancel.show();
				jQuery.TempUploader.error(r.msg || 'undefined');
				break;
		}
	})

	this.msg = $('<div class="TempUploader-msg" style="display:none" />').appendTo(this);
	this.type = $('<input type="hidden" name="t" />').appendTo(this);
	this.file = $('<input type="file" name="f" />').appendTo(this);
	this.bSubmit = $('<button type="submit" style="margin-left:10px;">Загрузить</button>').appendTo(this);
	this.bCancel = $('<button type="button" style="margin-left:10px;">Отмена</button>').appendTo(this)
		.click(function(){
			jQuery.TempUploader.close();
		});

	this.submit(function(){
		jQuery.TempUploader.uploading = true;
		jQuery.TempUploader.attr('action', jQuery.TempUploader.attr('action').replace(/\?(.*)$/, '?'+Math.random()));
		jQuery.TempUploader.info('Файл загружается, подождите...');
		jQuery.TempUploader.file.hide();
		jQuery.TempUploader.bSubmit.hide();
		jQuery.TempUploader.bCancel.hide();
	});
}
jQuery.TempUploader.error = function(html) {
	jQuery.TempUploader.msg.html(html);
	jQuery.TempUploader.msg.removeClass('TempUploader-msg-info');
	jQuery.TempUploader.msg.addClass('TempUploader-msg-error');
	jQuery.TempUploader.msg.show();
	jQuery.screen.updateSize();
}
jQuery.TempUploader.info = function(html) {
	jQuery.TempUploader.msg.html(html);
	jQuery.TempUploader.msg.removeClass('TempUploader-msg-error');
	jQuery.TempUploader.msg.addClass('TempUploader-msg-info');
	jQuery.TempUploader.msg.show();
	jQuery.screen.updateSize();
}
jQuery.TempUploader.open = function(action, type, callback) {
	jQuery.TempUploader.construct();
	jQuery.TempUploader.file.show().val('');
	jQuery.TempUploader.bSubmit.show();
	jQuery.TempUploader.bCancel.show();
	if (callback) jQuery.TempUploader.callback = callback;
	jQuery.TempUploader.attr('action', action + '?');
	jQuery.TempUploader.type.val(type);
	jQuery.TempUploader.msg.hide();
	jQuery.screen.show(this);
}
jQuery.TempUploader.close = function() {
	jQuery.screen.hide();
}

jQuery.fn.extend({
	TempUploader: function(args) {
		if (this.size() > 1) {
			this.each(function(){
				$(this).TempUploader(args);
			})
			return this;
		}
		this.attr('disabled', 'disabled').hide();
		this.field = $('<div class="TempUploader-field">Загрузить файл</div>').insertAfter(this);
		this.input = $('<input type="hidden" name="'+this.attr('name')+'" />').insertAfter(this);
		this.removeAttr('name');
		this.field.mouseover(function(){
			jQuery(this).addClass('TempUploader-field-mouseover');
		});
		this.field.mouseout(function(){
			jQuery(this).removeClass('TempUploader-field-mouseover');
		});

		var _this = this;
		this.field.click(function(){
			jQuery.TempUploader.open(args.action, args.type, function(hash, name){
				_this.field.text(name);
				_this.input.val(hash);
				_this.field.addClass('TempUploader-field-uploaded');
			});
		});
		return this;
	}
});

})();