$(document).ready(function(){

	soundManager.url = '/js/';
	
	$('button, .icon').addClass('ui-state-default ui-corner-all');
	$('.ui-state-default').hover(
		function(){
        	$(this).toggleClass('ui-state-hover');
		},function(){
        	$(this).toggleClass('ui-state-hover');
	});

	$('div.htmltooltip').addClass('ui-corner-all');
	
	$("a[class=popup]").fancybox({
		'titlePosition'  : 'inside'
	});
	$("a[class=gallery]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'inside',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
			return '(' +  (currentIndex + 1) + '/' + currentArray.length + ') ' + title;
		}
	});	
	
	
	$('.toggle_link').click(function(){
		$(this).next('div').toggle();
	});

	$('#online_btn').click(function(){
		$('#online_wnd')
			.load('/user/online')
			.dialog({
				title: 'Online',
				modal: true,
				width: 800,
				height: 600,
				bgiframe: true
			})
			.dialog('open');
		return false;		
	});

	$('.needSignIn').click(function(){
		$("#dialog-message-needSignIn").dialog({
			width: 600,
			modal: true,
		}).dialog('open');
		return false;
	});

	$('.needMoreMoney').click(function(){
		$("#dialog-message-needMoreMoney").dialog({
			width: 600,
			modal: true,
		}).dialog('open');
		return false;
	});
	
	$('#info-message').dialog({
			width: 600,
			modal: true,
			title: $('#info-message').attr('title')
	}).dialog('open');
		
	$('#error-message').dialog({
			width: 600,
			modal: true,
			title: $('#error-message').attr('title')
	}).dialog('open');
	
	
	$('.favorite').append('<span class="icon_fav"></span>');
	
	regJQSimpleDialogs();
	
});

function simpleTabs(id)
{
	$('#'+id+' a').click(function(){
		state = $($(this).attr('href')).is(':visible');
		$('#'+id+' > div').hide();
		$('#'+id+' a').removeClass('bold');

		if (!state)
		{
			$($(this).attr('href')).show();
			$(this).addClass('bold');
		}
	});
}

function simpleDialogs(id)
{
	$('#'+id+' a').click(function(){
		$($(this).attr('href')).dialog({
			width: 'auto',
			modal: true,
			title: $(this).attr('title')
		}).dialog('open');		
		return false;
	});
}

function simpleDialog(id)
{
	$('#'+id).click(function(){
		$($(this).attr('href')).dialog({
			width: 'auto',
			modal: true,
			title: $(this).attr('title')
		}).dialog('open');		
		return false;
	});
}

function regJQSimpleDialogs(in_id)
{
	var id = '.jq-simple-dialog';
	
	if (in_id)
	{
		id = '#' + in_id + ' ' + id;
	}
	
	$(id).click(function(){
		var link = this;
		var div = $(this).attr('href');
		var title = $(div).attr('title');
			
		$(div).dialog('close');
		$(div).dialog({
			width: 'auto',
			height: 'auto',
			minWidth: 100,
			minHeight: 100,
			modal: true,
			title: title,
			open: function(event, ui) {
				if ($(link).attr('rev'))
					setTimeout($(link).attr('rev'), 0);
			},
			beforeclose: function(event, ui) {
				if ($(link).attr('rel') && $(link).attr('rev') )
					setTimeout($(link).attr('rel'), 0);
			}			
		}).dialog('open');					
		
		if ($(link).attr('rel') && !$(link).attr('rev'))
		{
			$(div).html('<img src="/images/loading.gif" />')
			$(div).dialog('close').dialog('open');
			$(div).load($(link).attr('rel'), function(){
				$(div).dialog('close').dialog('open');
			});						
		}		
				
		$(div).attr('title', title);
		
		return false;
	});
}

/*function ajax()
{
	$.post('', {id: Math.random()}, ajax_result, 'json');
}

function ajax_result(data)
{
	alert(data.result);
}*/