
var processField = 'keep_value';
var processGroup = -2;
var processOrder = 'down';
var processSearch = '';
var stime = etime = 0;
$(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 : 590, 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 : 430, title : '投资人登记'};
				break;
		}
		modalOption.id = id + 'Modal';
		ajaxFormPost(method, modalOption);
		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');
		$(this).blur();
		processList();
		return false;
	})
	$('a.detail').click(function () {//详情
		detail($(this).attr('value'), $(this).attr('username'));
		return false;
	})
})
function processList() {
	$('#processTbody').html('<tr><td><div id="digCnt"/></td></tr>');
	$.post(
		'/index.php/home/processList/' + processField + '/' + processOrder,
		{ajax : 1, search : processSearch, group_id : processGroup, stime : stime, etime : etime},
		function (data) {
			$('#processTbody').html(data);
			$('a.detail').click(function () {
				detail($(this).attr('value'), $(this).attr('username'));
				return false;
			})
		}
	)
}
function moneyTable(userId) {
	$('#detailContent').html('<div id="digCnt"/>');
	$.post(
		'/index.php/table/money/' + userId,
		{ajax : 1, stime : stime, etime : etime, group_id : processGroup, search : processSearch},
		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 id="digCnt"/>');
	$.post(
		'/index.php/table/rate/' + userId,
		{ajax : 1, stime : stime, etime : etime, group_id : processGroup, search : processSearch},
		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 id="digCnt"/>');
	$.post(
		href,
		{ajax : 1, stime : stime, etime : etime, group_id : processGroup, search : processSearch},
		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 detailModel'>\
		<a href='#' id='moneyTable' class='hover'>净利润</a> \
		<a href='#' id='rateTable'>累计净值</a> \
		<a href='#' id='rawList'>原始数据</a>";
	$.post(
		'/index.php/user/info/' + userId,
		{ajax : 1},
		function (data) {
			if (data.error) {
			} else {
				$('div.detailModel').append(" <a href='#' id='infoUser'>个人介绍</a>");
				$('#infoUser').click(function () {
					infoUser(userId);
					$(this).blur();
					$('div.detailModel a').removeClass();
					$(this).addClass('hover');
					return false;
				})
			}
		},
		'json'
	)
	html += "</div>";
	html += "<div id='detailContent'><div id='digCnt'/></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;			
	})	
	$('#moneyTable,#rateTable,#rawList,#infoUser').click(function () {
		$(this).blur();
		$('div.detailModel a').removeClass();
		$(this).addClass('hover');
	})
}

function infoUser(userId) {
	$('#detailContent').html('<div id="digCnt"/>');
	$.post(
		'/index.php/user/info/' + userId,
		{ajax : 1},
		function (data) {
			if (data.error) $('#detailContent').html('<div style="margin:200px">没有介绍</div>');
			else {
				$('#detailContent').html(data.info);
			}
		},
		'json'
	)
}
