/* apps_content_tw.html */

// sindex為空就轉頁
if (get_sindex() == '') {
	location.replace('main_tw.php');
}
else {
	$.ajax({
		url: 'php/check_auth.php',
		type: 'GET',
		cache: false,
		data: {'sindex': get_sindex()},
		success: function(data) {
			if (data == 'ERROR') {
				location.replace('main_tw.php');
			}
		}
	});
}

$.cookie('lang', 'tc');

var apk_file = '';
var apk_version = '';
var check_price = '';
$(document).ready(function() {
	CookieChecker();
	ListComment(0);

	$('span:contains(English)').html('<a href="apps_content.html?sindex=' + get_sindex() + '">English</a>');

	// 登入視窗顯示、隱藏
	$('#login_hide').find('span:contains(登入)').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('登錄中，請稍候。').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('輸入帳號有誤，請重新輸入。').fadeTo(900,1);
					});
				}
				else if (data == 'PASSWORD_ERROR') {
					$("#msgbox").fadeTo(200,0.1, function() {
						$(this).addClass('messageboxerror').text('輸入密碼有誤，請重新輸入。').fadeTo(900,1);
					});
				}
			}
		});

		return false;
	});

	// 取得apk資訊
	$.ajaxSetup({'cache': false});
	$.getJSON(
		'php/search_apps.php',
		{'sindex': get_sindex(), 'lang': $.cookie('lang')},
   	function(data) {
			var avg_rating = data[0].apk_avgrating;
			var all_rating = data[0].apk_allrate;
			apk_file = data[0].apk_file;
			apk_version = data[0].apk_version;
			apk_versionname = data[0].apk_versionname;
			apk_versioncode = data[0].apk_versioncode;
			check_price = data[0].check_price;

			if (avg_rating == 0) {
				avg_rating = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			}
			if (all_rating == 0) {
				all_rating = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			}

			// 秀出apk資訊
			$('p span:eq(0)').text(data[0].devloper_name);
			if (data[0].apk_url) $('p span:eq(1)').html('<a href="' + data[0].apk_url + '" target="_blank"><img src="images/site.png" width="60" height="20" border="0" style="margin-top:5px"></a>');
			$('p span:eq(2)').text(data[0].devloper_email);
			$('p span:eq(3)').text(data[0].apk_category);
			$('.left2 p:eq(0)').html('最新版本評價<br />' + data[0].current_star + '&nbsp;' + avg_rating);
			$('.left2 p:eq(1)').html('所有版本評價<br />' + data[0].all_star + '&nbsp;' + all_rating);
			$('.left2 p:eq(2)').html(data[0].download_count + '&nbsp;下載次數');
			$('#leo').html('使用者地域分佈<br />' + data[0].chart);
			$('#icons').html('<img src="' + data[0].apk_icon + '" width="48" height="48" />');
			$('.tMS05 strong').html(data[0].apk_price);
			if (data[0].crown && data[0].thumb) $('#crown').html(data[0].crown + "&nbsp;" + data[0].thumb);
			else if (data[0].crown) $('#crown').html(data[0].crown);
			else if (data[0].thumb) $('#crown').html(data[0].thumb);
			$('#apk_name').text(data[0].apk_name);
			$('#apk_version').text('軟體版本: ' + apk_versionname + ', 檔案大小: ' + data[0].apk_filesize);
			$('#android_version').html('支援版本: ' + data[0].sdk_version + '<img src="images/arrow_up.png" border="0" style="vertical-align: text-bottom">');
			$('#apk_date').html('<div style="float:left;width:80px">發佈時間:</div> ' + '<div style="float:left;width:320px">'+data[0].apk_date+'</div>'+'<div style="clear:left"></div>');
			$('#apk_title').html('<div style="float:left;width:80px">簡述:</div> ' + '<div style="float:left;width:320px">'+data[0].apk_title+'</div>'+'<div style="clear:left"></div>');
			$('#dpi_list').html('<div style="float:left;width:80px">支援解析度:</div> ' + '<div style="float:left;width:320px">'+data[0].dpi_list+'</div>'+'<div style="clear:left"></div>');
			$('#language_list').html('<div style="float:left;width:80px">支援語系:</div> ' + '<div style="float:left;width:320px">'+data[0].language_list+'</div>'+'<div style="clear:left"></div>');
			$('#hardware_list').html('<div style="float:left;width:80px">硬體需求:</div> ' + '<div style="float:left;width:320px">'+data[0].hardware_list+'</div>'+'<div style="clear:left"></div>');
			$('#description').html(data[0].apk_description);
			$('#photo').html('<img src="' + data[0].apk_screenshot1 + '"  onLoad="(this.width/this.height > 1)? (this.width > 360? this.width=360:this.width):(this.height > 360? this.height=360:this.height)" />');
			if (data[0].apk_ss_description1) $('#photo_des').text(data[0].apk_ss_description1);
			else $('#photo_des').html('&nbsp;');

			$('#photo1')
				.addClass('album-pic')
				.html('<img src="' + data[0].apk_screenshot1 + '" style="cursor:pointer" />')
				.click(function() {
					$('#photo').html('<img src="' + data[0].apk_screenshot1 + '"  onLoad="(this.width/this.height > 1)? (this.width > 360? this.width=360:this.width):(this.height > 360? this.height=360:this.height)" />');
					if (data[0].apk_ss_description1) $('#photo_des').text(data[0].apk_ss_description1);
					else $('#photo_des').html('&nbsp;');
				});
			$('#photo2')
				.addClass('album-pic')
				.html('<img src="' + data[0].apk_screenshot2 + '" style="cursor:pointer" />')
				.click(function() {
					$('#photo').html('<img src="' + data[0].apk_screenshot2 + '"   onLoad="(this.width/this.height > 1)? (this.width > 360? this.width=360:this.width):(this.height > 360? this.height=360:this.height)" />');
					if (data[0].apk_ss_description2) $('#photo_des').text(data[0].apk_ss_description2);
					else $('#photo_des').html('&nbsp;');
				});
			$('#photo3')
				.addClass('album-pic')
				.html('<img src="' + data[0].apk_screenshot3 + '" style="cursor:pointer" />')
				.click(function() {
					$('#photo').html('<img src="' + data[0].apk_screenshot3 + '"   onLoad="(this.width/this.height > 1)? (this.width > 360? this.width=360:this.width):(this.height > 360? this.height=360:this.height)" />');
					if (data[0].apk_ss_description3) $('#photo_des').text(data[0].apk_ss_description3);
					else $('#photo_des').html('&nbsp;');
				});

			if ($.cookie('account') && $.cookie('pwd')) {
				if(check_price == 'Free') {
				  $('#download_img').html('<a href="' + apk_file + '" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'down\',\'\',\'images/download_tw_o.png\',1)"><img src="images/download_tw.png" name="down" width="100" height="25" border="0" /></a>');
				}
				else {
					$('#download_img').html('<a href="paid_download_tw.html" target="_blank" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'down\',\'\',\'images/download_tw_o.png\',1)"><img src="images/download_tw.png" name="down" width="100" height="25" border="0" /></a>');					
				}
			}
		}
	);

	$('#download_img').click(function() {
		if (!$.cookie('account') || !$.cookie('pwd')) {
			alert('請先登入');
			$('.signDIV').show();
			$('input[type="text"]:first', $('form')).focus();
		}
		else {
  		if(check_price == 'Free') {
  			$.ajax({
  				url: 'php/idu_downloadlist.php',
  				type: 'POST',
  				cache: false,
  				data: {'account': $.cookie('account'), 'sindex': get_sindex(), 'version': apk_version ,
  				'apk_versioncode': apk_versioncode,'apk_versionname':apk_versionname}
  			});
  			
  		}
    }
	});

});

// 檢查 cookie 是否存在
function CookieChecker() {
	if ($.cookie('account') && $.cookie('pwd')) {
		InfoChecker();
		$('#login_hide').hide();
		$('span:contains(我的帳戶)').show();
		$('span:contains(登入)').after('歡迎 ' + $.cookie('account') + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="cursor:pointer">登出</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
		//alert(check_price)
		if(check_price == 'Free') {
			$('#download_img').html('<a href="' + apk_file + '" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'down\',\'\',\'images/download_tw_o.png\',1)"><img src="images/download_tw.png" name="down" width="100" height="25" border="0" /></a>');
		}
		else {
			$('#download_img').html('<a href="paid_download_tw.html" target="_blank" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'down\',\'\',\'images/download_tw_o.png\',1)"><img src="images/download_tw.png" name="down" width="100" height="25" border="0" /></a>');					
		}

		// 登出
		$('span:contains(登出)').click(function() {
			$.cookie('account', null, {path: '/', domain: 'camangimarket.com'});
			$.cookie('pwd', null, {path: '/', domain: 'camangimarket.com'});
			location.href = 'apps_content_tw.html?sindex=' + get_sindex();
		});
	}
}

// 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_tw.html';
		}
	);
}

// 取得URL上的sindex值
function get_sindex() {
	var str = window.location.search.replace('?sindex=', '');
	return str;
}

function ListComment(page_index) {
	$('#comments_list').empty();
	var count = page_index * 5 - 5;
	// 列出前5筆留言
	$.ajaxSetup({ cache: false });
	$.getJSON(
		'php/select_comment.php',
		{'count': count, 'sindex': get_sindex()},
		function(data) {
			var service_count = data[0].service_count;
			var total_count = data[0].total_count;

			for(var i=0; i<service_count; i++) {
				$('#comments_list').append(					
					'<div style="width:700px; border-bottom:dotted #CCC 1px; padding:5px;">' +
					'<div class="tMS01" style="width:620px; float:left;">' +
					'<strong>' + data[i].account + '</strong>&nbsp;&nbsp;' +
					'<span class="tMS01">' + data[i].ctime + '</span><br />' +
					'<span class="tMS01">' + data[i].comments + '</span></div>' +
					'<div style="float:right; width:60px;" align="right">' +
					'<span class="tMS02">軟體版本<br />' + data[i].version + '</span><br />' +
					'</div><div style="clear:both"></div></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: 5,
						num_display_entries: 5,
						callback: pageselectCallback
					});
				}
			}
		}
	);
}

function pageselectCallback(page_index) {
	page_index = page_index + 1;
	if (page_index > 0) { ListComment(page_index); }
}

