$(document).ready(function()
{
	// Disclaimer Dialog
	if ( $('#disclaimer_dialog').size() )
	{
		$("#disclaimer_dialog").dialog({
			autoOpen: false,
			stack: false,
			modal: true,
			width: 530,
			open: function(){$('#disclaimer_dialog input:first').blur();},
			resizable: false
		});
		$(".disclaimer_link").click(function(){ $('#disclaimer_dialog').dialog('open'); return false; });

		//дисклеймер при клике по фондам
		$(".fullmenu a.funds, .menu a.funds, a.funds_link").click(function(){
			$('#disclaimer_dialog .db_button').attr('href', $(this).attr('href'));
			if(!Get_Cookie('funds-disclaimer-agree')) {
				$('#disclaimer_dialog').dialog('open'); return false;
			}
		});


		$('#disclaimer_dialog .db_button').click(function(){
			var checked = $(this).prev().attr('checked');
			if(!checked) {
				alert('You must check the agree-box')
			} else {
				Set_Cookie('funds-disclaimer-agree', 1, 0, '/', '.'+sDomain, false);
				return true;
			}
			return false;
		});
	}
	
	// Login Dialog
	if ( $('#login_dialog').size() )
	{
		$("#login_dialog").dialog({
			autoOpen: false,
			stack: false,
			modal: true,
			width: 350,
			open: function(){$('#login_dialog input:first').blur();},
			resizable: false
		});
		$(".login_link").click(function(){ $('#login_dialog').dialog('open'); return false; });
		$('#login_dialog .db_button').attr('href', 'javascript:void(0);').click(function(){
			var data = $(this).parents('form').serialize();

			$.getJSON(sHtmlRoot+'_services/userauth/', data, function(Result) {
				if( parseInt(Result.Code)==0 ) {
					$("#login_dialog form:first .error_message").remove();
					$("#login_dialog form:first .txtbox:first").parent().after('<div class="error_message">The E-mail or password your entered is incorrect</div>');
				} else {
					document.location.href='http://' + sDomain + sHtmlRoot+"investor/";
				}
			});

			return false;
		});
		// Forgot Password
		$('#forgot_pass_link').click(function(){ $('#forgot_pass_form').slideDown('slow'); return false; });
		$('#forgot_pass_form .close').click(function(){ $('#forgot_pass_form').slideUp('slow'); return false; });
	}
	
	// Subscribe Dialog
	if ( $('#subscribe_dialog').size() )
	{
		if( $('#subscribe_options').size() ) {
			$("#subscribe_options").selectbox();
		}

		$("#subscribe_dialog").dialog({
			autoOpen: false,
			stack: false,
			modal: true,
			width: 620,
			open: function(){$('#subscribe_dialog input:first').blur();},
			resizable: false
		});
		$(".subscribe_link").click(function(){ $('#subscribe_dialog').dialog('open'); return false; });
		
		$('#subscribe_dialog .db_button').click(function(){
			var form  = $(this).parent('form');
			var data = form.serialize();
			var url = form.attr('action');

			if(form.find("#subscribe_options").val()=="") {
				form.find('i').css('color','red');
				return false;
			} else {
				form.find('i').css('color','');
			}

			$.post(url, data, function(result) {
				if( result.error ) {
					switch(result.error) {
						case 1:
							form.find('.err').html('E-mail doesn\'t specified ');
							break;
						case 2:
							form.find('.err').html('Incorrect email');
							break;
						case 3:
							form.find('.err').html('This address already registered');
							break;
						case 5:
							form.find('.err').html('This address is not registered');
							break;
					}
				} else if( result.ok ) {
					form.html('<h2>Thank you</h2><p>The confirmation code was sent to <b>'+result.ok+'</b>.</p><p>You will receive mail after activation</p>').prev().remove();
				} else if( result.ok1 ) {
					form.html('<h2>Thank you</h2><p>Your address was removed from our mailing list</p>').prev().remove();
				}
			}, 'json');

			return false;
		});
	}
	
	/*********** ERRORS DEMO *************/
	// Login Dialog
	if ( $('#login_dialog_errors').size() )
	{
		$("#login_dialog_errors").dialog({
			autoOpen: false,
			stack: false,
			modal: true,
			width: 350,
			open: function(){$('#login_dialog_errors input:first').blur();},
			resizable: false
		});
		$(".login_errors_link").click(function(){ $('#login_dialog_errors').dialog('open'); return false; });
		$('#login_dialog_errors .db_button').attr('href', 'javascript:void(0);').click(function(){ $(this).parent('form').submit(); return false; });
		// Forgot Password
		$('#forgot_pass_errors_link').click(function(){ $('#forgot_pass_errors_form').slideDown('slow'); return false; });
		$('#forgot_pass_errors_form .close').click(function(){ $('#forgot_pass_errors_form').slideUp('slow'); return false; });
	}
	
	// Subscribe Dialog
	if ( $('#subscribe_dialog_errors').size() )
	{
		if( $('#subscribe_errors_options').size() )
			$("#subscribe_errors_options").selectbox();
		$("#subscribe_dialog_errors").dialog({
			autoOpen: false,
			stack: false,
			modal: true,
			width: 620,
			open: function(){$('#subscribe_dialog_errors input:first').blur();},
			resizable: false
		});
		$(".subscribe_errors_link").click(function(){ $('#subscribe_dialog_errors').dialog('open'); return false; });
		$('#subscribe_dialog_errors .db_button').attr('href', 'javascript:void(0);').click(function(){ $(this).parent('form').submit(); return false; });
	}

	$("#show_image_dialog").dialog({
		autoOpen: false,
		stack: false,
		modal: true,
		width: 660,
		resizable: false
	});
	$('a.zoom').click(function() {
		var img = $(this).attr('href');

		$('#show_image_dialog').html("<div style='background:url("+img+") center center no-repeat; width:600px; height:400px'><!-- --></div>").dialog('open');
		return false;
	});
});
