
var processField = 'add_value';
var processGroup = 0;
var processOrder = 'down';
var processRange = 240;
var processSearch = '';
$(function () {
	setInterval(function () {
		var firstLi = $('#marquee ul li:first');
		$('#marquee ul').prepend(firstLi.next());
		$('#marquee ul').append(firstLi);
	}, 5000)
	$('#scrollIndex').css('position', 'relative');
	$('#scrollIndex').scrollFollow();
	$('#introduce').click(function () {
		showFormModal({
			modal : $('#introduceModal'),
			title : '网站使用说明',
			confirmFn : function () {}
		})
		return false;
	})
	$('#register, #login, #profile, #password, #investor').click(function () {
		var id = $(this).attr('id');
		var method, modalOption;
		switch (id) {
			case 'register' :
				method = 'user/add';
				modalOption = {width : 620, height : 530, title : '操盘手注册'};
				break;
			case 'login' :
				method = 'user/login';
				modalOption = {width : 450, height : 270, title : '操盘手登录'};
				break;
			case 'profile' :
				method = 'user/update/' + $(this).attr('value');
				modalOption = {width : 600, height : 470, title : '资料修改'};
				break;
			case 'password' :
				method = 'user/password';
				modalOption = {width : 450, height : 270, title : '修改密码'};
				break;
			case 'investor' :
				method = 'investor/add';
				modalOption = {width : 600, height : 420, title : '投资人登记'};
				break;
		}
		modalOption.id = id + 'Modal';
		ajaxFormPost(method, modalOption);
		return false;
	})
	$('#switchProcessList a').click(function () {
		processRange = $(this).attr('value');
		processList();
		$('#switchProcessList a').removeClass('hover');
		$(this).addClass('hover');
		$(this).blur();
		return false;
	})
	$('#processList th').click(function () {
		processField = $(this).attr('value');
		if (typeof processField == 'undefined') return false;
		$('#processList th').removeClass();
		var order = $(this).attr('order');
		if (order == 'down') {
			$(this).addClass('sort_up');
			processOrder = 'up';
		} else {
			$(this).addClass('sort_down');
			processOrder = 'down';
		}
		$(this).attr('order', processOrder);
		processList();
		$(this).blur();
	})
	$('#searchSubmit').click(function () {
		processSearch = $('#search').val();
		processList();
		return false;
	})
	$('#search').keydown(function (e) {//激活enter
		if (e.keyCode == 13) {
			$('#searchSubmit').click();
		}
	})
	$('a.selectGroup').click(function () {
		processGroup = $(this).attr('value');
		$('a.selectGroup').removeClass('hover');
		$(this).addClass('hover');
		processList();
		return false;
	})
	$('a.detail').click(function () {
		detail($(this).attr('value'), $(this).attr('username'));
		return false;
	})
})
function processList() {
	$.post(
		'/index.php/home/processList/' + processRange + '/' + processField + '/' + processOrder,
		{ajax : 1, search : processSearch, group_id : processGroup},
		function (data) {
			$('#processTbody').html(data);
			$('a.detail').click(function () {
				detail($(this).attr('value'), $(this).attr('username'));
				return false;
			})
		}
	)
}
function moneyTable(userId) {
	$('#detailContent').html('<div style="margin:200px">Loading......................................................</div>');
	$.post(
		'/index.php/table/money/' + userId,
		{ajax : 1},
		function (data) {
			if (data.error) $('#detailContent').html('<div style="margin:200px">采集中......................................................</div>');
			else {
				$('#detailContent').flash({
					src: '/static/flash/open-flash-chart.swf',
					width: '100%',
					height: '470px',
					wmode: 'opaque',
					flashvars: data.chart
				})
			}
		},
		'json'
	)
}
function rateTable(userId) {
	$('#detailContent').html('<div style="margin:200px">Loading......................................................</div>');
	$.post(
		'/index.php/table/rate/' + userId,
		{ajax : 1},
		function (data) {
			if (data.error) $('#detailContent').html('<div style="margin:200px">采集中......................................................</div>');
			else {
				$('#detailContent').flash({
					src: '/static/flash/open-flash-chart.swf',
					width: '100%',
					height: '470px',
					wmode: 'opaque',
					flashvars: data.chart
				})
			}
		},
		'json'
	)
}
function rawList(href) {
	$('#detailContent').html('<div style="margin:200px">Loading......................................................</div>');
	$.post(
		href,
		{ajax : 1},
		function (data) {
			$('#detailContent').html(data);
		}
	)
}
function detail(userId, username) {
	moneyTable(userId);
	
	if ($('#detailModal').length > 0) {
		var detailModal = $('#detailModal');
	} else {
		var detailModal = $('<div id="detailModal"></div>');
		$('body').append(detailModal);
	}
	
	detailModal.dialog({
		bgiframe: true,
		minHeight: 10,
		resizable: false,
		title: username,
		modal: true,
		width: 1000,
		height: 562,
		close: function () {
			$(this).dialog('destroy');
		}
	})

	var html = "\
	<div class='button'>\
		<a href='#' id='moneyTable'>净利润</a> \
		<a href='#' id='rateTable'>累计净值</a> \
		<a href='#' id='rawList'>原始数据</a>\
		<a href='/index.php/home/mprint/" + userId + "' target='_blank'>打印图表</a>\
	</div>";
	html += "<div id='detailContent'><div style='margin:200px'>Loading......................................................</div></div>";
	detailModal.html(html);

	$('#moneyTable').click(function () {
		moneyTable(userId);
		return false;
	})
	$('#rateTable').click(function () {
		rateTable(userId);
		return false;
	})
	$('#rawList').click(function () {
		var href = '/index.php/home/rawList/' + userId;
		rawList(href);
		return false;			
	})
}