/* sharelist.html */

// 檢查是否為登入狀態，若為"否"則轉去main.php
if (!$.cookie('account') || !$.cookie('pwd')) {
	alert('Please login first');
	location.replace('main.php');
}
else {
	// identity為user就轉頁
	if ($.cookie('identity') == 'user') {
		alert('Sorry! You are not authorized to access.');
		location.replace = 'main.php';
	}
	else {
		var bonus_check = '';
		InfoChecker();
	}
}

$.cookie('lang', 'en');

$(document).ready(function() {
	$('span:contains(Greeting)').text('Greeting ' + $.cookie('account')).ajaxStop(function() {
		if (bonus_check == 'PASS') {
			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>');
			}
		}
	});
	ListData(0);

	// 登出
	$('span:contains(Logout)').click(function() {
		$.cookie('account', null, {path: '/', domain: 'camangimarket.com'});
		$.cookie('pwd', null, {path: '/', domain: 'camangimarket.com'});
		location.replace('main.php');
	});

});

// 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_check = 'PASS';
		}
	);
}

function ListData(page_index) {
	$('#myshare_list').empty();
	var count = page_index * 10 - 10;
	// 列出前10筆資料
	$.ajaxSetup({'cache': false});
	$.getJSON(
		'php/select_sharelist.php',
		{'count': count, 'account': $.cookie('account'), 'lang': $.cookie('lang'), 'type': 'upload'},
		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;';
				}

        if (data[i].apk_category2) {
          show_apk_category2 = ' > ' + data[i].apk_category2;
        }
        else {
          show_apk_category2 = '';
        }

				$('#myshare_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:500px" class="tMS01">' +
					'<div align="center" style="float:right">' + data[i].star + '&nbsp;' + rating + '<br />' + data[i].apk_price +
					'<div><a href="sharemodify.php?sindex=' + data[i].apk_sindex + '&return=1"><img src="images/modify.png" width="63" height="25" border="0" /></a></div></div>' +
					'<div class="tMS06" style="float:right; padding-right:10px">Status: ' + data[i].state + '<br>Downloads: ' + data[i].download_count + '<br>Non-repeated Downloads: ' + data[i].undownload_count + '</div>'+
					'<span class="tMS01"><strong><a href="apps_content.html?sindex=' + data[i].apk_sindex +'">' + data[i].apk_name + '</a></strong><br />' +
					'Developer: ' + data[i].member_name + '<br />' +
					'Category: ' + data[i].apk_category + show_apk_category2 + '<br />' +
					data[i].apk_title + '</span></div>' +
					'<div style="clear:both; height:5px; border-bottom:solid #CCC 1px; margin-bottom:5px">&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); }
}

