/* main.html */

$.cookie('lang', 'en');

var sorting = 'popular';
var category = 'all';

$(document).ready(function() {
	CookieChecker();
	ListData(0);

	// 登入視窗顯示、隱藏
	$('#login_hide').find('span:contains(Login)').click(function() {
		$('.signDIV').show();
		$('input[type="text"]:first', $('form')).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');
					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');
					CookieChecker();
				}
				else if (data == 'SB_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', 'sb_developer');
					CookieChecker();
				}
				else if (data == 'SB_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', 'sb_user');
					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;
	});

	$('#recent').click(function() {
		sorting = 'date';
		$('#recent').html('<img src="images/tab_recent_now.png"/>');
		$('#popular').html('<a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'popular\',\'\',\'images/tab_popular_now.png\',1)"><img src="images/tab_popular.png" name="popular" border="0" /></a>');
		$('#apk_list').empty();
		ListData(0);
	});

	$('#popular').click(function() {
		sorting = 'popular';
		$('#recent').html('<a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'recent\',\'\',\'images/tab_recent_now.png\',1)"><img src="images/tab_recent.png" name="recent" border="0" /></a>');
		$('#popular').html('<img src="images/tab_popular_now.png"/>');
		$('#apk_list').empty();
		ListData(0);
	});

});

// 檢查 cookie 是否存在
function CookieChecker() {
	if ($.cookie('account') && $.cookie('pwd')) {
		$('#login_hide').hide();
		$('span:contains(My Account)').show();
		$('span:contains(Login)').after('Greeting ' + $.cookie('account') + '&nbsp;&nbsp;&nbsp;&nbsp;<span style="cursor:pointer">Logout</span>&nbsp;&nbsp;&nbsp;&nbsp;');

		if ($.cookie('identity') == 'sb_developer') {
			$('.account_menu:eq(0) a:contains(My Sharing)').after('<br /><a href="bonus_account.php">Shopping bonus</a>');
		}
		else if ($.cookie('identity') == 'sb_user') {
			$('.account_menu:eq(1) a:contains(My Download)').after('<br /><a href="bonus_account.php">Shopping bonus</a>');
		}

		if ($.cookie('identity') == 'developer' || $.cookie('identity') == 'user') {
			$('#bonus_link').show();
		}
		else {
			$('#bonus_link').hide();
		}

		// 登出
		$('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' || $.cookie('identity') == 'sb_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' || $.cookie('identity') == 'sb_user') {
		if ($('.account_menu:eq(1)').css('display') == 'none') {
			$('.account_menu:eq(1)').css('display','block');
		}
		else {
			$('.account_menu:eq(1)').css('display','none');
		}
	}
}

// Game分類子選單
function GameMenu() {
	if ($('#gamemenu').css('display') == 'none') {
		$('#gamemenu').css('display','block');
	}
	else {
		$('#gamemenu').css('display','none');
	}
}

// 分類選單
function changeSelect(h) {
	$('#td'+h).addClass('t01_selected');
	for(var i=1; i<=16; i++) {
		if (i != h) {
			$('#td'+i).removeClass('t01_selected').addClass('t01');
		}
	}

	if (h == 1) { h = 'all'; }
	else if (h == 2) { h = 'communication';	}
	else if (h == 3) { h = 'entertainment';	}
	else if (h == 4) { h = 'finance';	}
	else if (h == 5) { h = 'life'; }
	else if (h == 6) { h = 'game_all'; }
	else if (h == 7) { h = 'arcade'; }
	else if (h == 8) { h = 'brain'; }
	else if (h == 9) { h = 'cards'; }
	else if (h == 10) { h = 'casual'; }
	else if (h == 11) { h = 'multimedia'; }
	else if (h == 12) { h = 'reference'; }
	else if (h == 13) { h = 'shopping'; }
	else if (h == 14) { h = 'social'; }
	else if (h == 15) { h = 'tools'; }
	else if (h == 16) { h = 'other'; }

	category = h;
	$('#apk_list').empty();
	ListData(0);
}

// 條列apk資料
function ListData(page_index) {
	$('#apk_list').empty();
	var count = page_index * 10 - 10;
	// 列出前10筆資料
	$.ajaxSetup({ cache: false });
	$.getJSON(
		'php/select_appslist.php',
		{'count': count, 'category': category, 'sorting': sorting, 'lang': $.cookie('lang')},
		function(data) {
			var service_count = data[0].service_count;
			var total_count = data[0].total_count;

			for(var i=0; i<service_count; i++) {
				var rating = data[i].apk_avgrating;
				if (rating == 0) {
					rating = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
				}

				$('#apk_list').append(
					'<div style="float:left; border:solid #CCC 1px; margin-top:5px">' +
					'<a href="apps_content.html?sindex=' + data[i].apk_sindex + '">' +
					'<img src="' + data[i].apk_icon + '" width="48" height="48" border="0" /></a></div>' +
					'<div style="float:right;width:440px" id="title_link">' +
					'<div align="center" style="float:right;">' + data[i].star + '&nbsp;' +
					'<span class="tMS01">' + rating + '<br />' + data[i].apk_price + '</span></div>' +
					'<span class="tMS01"><a href="apps_content.html?sindex=' + data[i].apk_sindex + '" class="tMS01">' + data[i].apk_name + '</a><br />' +
					'Developer: ' + data[i].member_name + '<br />' +
					'Category: ' + data[i].apk_category + '<br  />' +
					data[i].apk_title + '</span></div>'+
					'<div style="clear:both; height:10px; border-bottom:#CCC solid 1px">&nbsp;</div>' 
				);
			}

			if (service_count == 'ZERO') {
				$('#Pagination').hide();
			}
			else {
				$('#Pagination').show();
				if (page_index == 0) {
					$('#Pagination').pagination(total_count, {
						num_edge_entries: 2,
						items_per_page: 10,
						num_display_entries: 5,
						callback: pageselectCallback
					});
				}
			}
		}
	);
}

function pageselectCallback(page_index) {
	page_index = page_index + 1;
	if (page_index > 0) { ListData(page_index); }
}

