/* main.html */
$.cookie('lang', 'en');

$(document).ready(function() {
	CookieChecker();
	//ListData_hot();
	//ListData_new();
	//ListData_update();

	// 登入視窗顯示、隱藏
	$('#login_hide').find('span:contains(Login)').click(function() {
		$('.signDIV').show();
		$('input[type="text"]:first', $('form:eq(0)')).focus();
	});
	$('.Xdiv').addClass('tMS02_white').click(function() {
		$('.signDIV').hide();
	});

	// 登入
	$('form').submit(function() {
		$('#msgbox').removeClass().addClass('messagebox').text('Logging in, please wait.').fadeIn(1000);
		$.ajax({
			url: 'php/ajax_login.php',
			type: 'POST',
			cache: false,
			data: {'account': $('#account').val(), 'pwd': hex_sha1($('#pwd').val())},
			success: function(data) {
				if (data == 'DEVELOPER_YES') {
					// 建立cookie，並檢查是否要自動登入
					if ($(':checkbox').attr('checked') == true) {
						$.cookie('account', $('#account').val(), {expires: 30, path: '/', domain: 'camangimarket.com'});
						$.cookie('pwd', hex_sha1($('#pwd').val()), {expires: 30, path: '/', domain: 'camangimarket.com'});
					}
					else {
						$.cookie('account', $('#account').val(), {path: '/', domain: 'camangimarket.com'});
						$.cookie('pwd', hex_sha1($('#pwd').val()),  {path: '/', domain: 'camangimarket.com'});
					}
					$('.signDIV').hide();
					$.cookie('identity', 'developer',{expires: 30, path: '/', domain: 'camangimarket.com'});
					CookieChecker();
				}
				else if (data == 'USER_YES') {
					// 建立cookie，並檢查是否要自動登入
					if ($(':checkbox').attr('checked') == true) {
						$.cookie('account', $('#account').val(), {expires: 30, path: '/', domain: 'camangimarket.com'});
						$.cookie('pwd', hex_sha1($('#pwd').val()), {expires: 30, path: '/', domain: 'camangimarket.com'});
					}
					else {
						$.cookie('account', $('#account').val(), {path: '/', domain: 'camangimarket.com'});
						$.cookie('pwd', hex_sha1($('#pwd').val()),  {path: '/', domain: 'camangimarket.com'});
					}
					$('.signDIV').hide();
					$.cookie('identity', 'user',{expires: 30, path: '/', domain: 'camangimarket.com'});
					CookieChecker();
				}
				else if (data == 'ACCOUNT_ERROR') {
					$("#msgbox").fadeTo(200,0.1, function() {
						$(this).addClass('messageboxerror').text('Please check your account.').fadeTo(900,1);
					});
				}
				else if (data == 'PASSWORD_ERROR') {
					$("#msgbox").fadeTo(200,0.1, function() {
						$(this).addClass('messageboxerror').text('Please check your password.').fadeTo(900,1);
					});
				}
			}
		});

		return false;
	});

});

// 檢查 cookie 是否存在
function CookieChecker() {
	if ($.cookie('account') && $.cookie('pwd')) {
		InfoChecker();
		$('#login_hide').hide();
		$('span:contains(My Account)').show();
		$('span:contains(Login)').after('Greeting ' + $.cookie('account') + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="cursor:pointer">Logout</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');

		// 登出
		$('span:contains(Logout)').click(function() {
			$.cookie('account', null, {path: '/', domain: 'camangimarket.com'});
			$.cookie('pwd', null, {path: '/', domain: 'camangimarket.com'});
			location.href = 'index.html';
		});
	}
}

// My Account選單
function AccountMenu() {
	if ($.cookie('identity') == 'developer') {
		if ($('.account_menu:eq(0)').css('display') == 'none') {
			$('.account_menu:eq(0)').css('display','block');
		}
		else {
			$('.account_menu:eq(0)').css('display','none');
		}
	}
	else if ($.cookie('identity') == 'user') {
		if ($('.account_menu:eq(1)').css('display') == 'none') {
			$('.account_menu:eq(1)').css('display','block');
		}
		else {
			$('.account_menu:eq(1)').css('display','none');
		}
	}
}

// 檢查生日、性別是否有填
function InfoChecker() {
	$.ajaxSetup({ cache: false });
	$.post(
		'php/check_data_complete.php',
		{'account': $.cookie('account')},
		function(data) {
			if (data == 'DATA_INCOMPLETE') location.href = 'setting.html';
			else BounsChecker();
		}
	);
}

// 檢查是否通過 Bonus 驗證
function BounsChecker() {
	$.ajaxSetup({ cache: false });
	$.get(
		'php/check_shopping_program.php',
		{'account': $.cookie('account')},
		function(data) {
			if (data == 'PASS') {
				$('#bonus_link').hide();
				if ($.cookie('identity') == 'developer') {
					$('.account_menu:eq(0) a:contains(My Sharing)').after('<br /><a href="bonus_account.php">Shopping bonus</a>');
				}
				else if ($.cookie('identity') == 'user') {
					$('.account_menu:eq(1) a:contains(My Download)').after('<br /><a href="bonus_account.php">Shopping bonus</a>');
				}
			}
			else if (data == 'CHECK') {
				$('#bonus_link').hide();
			}
			else {
				$('#bonus_link').show();
			}
		}
	);
}

//秀出10個熱門APK
function ListData_hot() {
	$('#download_list').empty();

	$.ajaxSetup({ cache: false });
	$.getJSON(
		'php/tab_hot_list.php',
		{ 'account': $.cookie('account'), 'lang': $.cookie('lang')},
		function(data) {
			for(var i=0; i<10; i++) {

				$('#tab_hot_list').append(
					'<div style="padding-top:5px"><div style="background-color:#FFF; float:left; margin-right:5px">' +
					'<a href="' + data[i].link_url + '"><img src="' + data[i].apk_icon + '" width="38" height="38" border="0" /></a></div>' +
					'<div style="float:right; width:140px;padding-top:5px">' +
					'<a href="' + data[i].link_url + '">' + data[i].apk_name + '</a></div></div>' +
					'<div style="clear:both; height:0px">&nbsp;</div>'
				);
			}//end of for
		}
	);
}

//秀出10個最新上傳APK
function ListData_new() {
	$('#download_list').empty();

	$.ajaxSetup({ cache: false });
	$.getJSON(
		'php/tab_new_list.php',
		{ 'account': $.cookie('account'), 'lang': $.cookie('lang')},
		function(data) {
			for(var i=0; i<10; i++) {

				$('#tab_new_list').append(
					'<div style="padding-top:5px"><div style="background-color:#FFF; float:left; margin-right:5px">' +
					'<a href="' + data[i].link_url + '"><img src="' + data[i].apk_icon + '" width="38" height="38" border="0" /></a></div>' +
					'<div style="float:right; width:140px;padding-top:5px">' +
					'<a href="' + data[i].link_url + '">' + data[i].apk_name + '</a></div></div>' +
					'<div style="clear:both; height:0px">&nbsp;</div>'
				);
			}//end of for
		}
	);
}

//秀出10個最近更新APK
function ListData_update() {
	$('#download_list').empty();

	$.ajaxSetup({ cache: false });
	$.getJSON(
		'php/tab_update.php',
		{ 'account': $.cookie('account'), 'lang': $.cookie('lang')},
		function(data) {
			for(var i=0; i<10; i++) {

				$('#tab_update').append(
					'<div style="padding-top:5px"><div style="background-color:#FFF; float:left; margin-right:5px">' +
					'<a href="' + data[i].link_url + '"><img src="' + data[i].apk_icon + '" width="38" height="38" border="0" /></a></div>' +
					'<div style="float:right; width:140px;padding-top:5px">' +
					'<a href="' + data[i].link_url + '">' + data[i].apk_name + '</a></div></div>' +
					'<div style="clear:both; height:0px">&nbsp;</div>'
				);
			}//end of for
		}
	);
}