/**
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
*/

loadscript("mdialog");

function search_subject(kwyid, callback) {
	var kw = $('#'+kwyid).val();
	if(kw.trim() == '') {
        alert('未填写关键字。'); return;
	}
	if($('#subject_pid')[0]) {
		pid = $('#subject_pid').val();
	} else {
		pid = 0;
	}
	msgOpen('正在搜索，请稍后...', 180,80);
	$.post(Url('item/ajax/do/subject/op/search/pid/'+pid+'/in_ajax/1'), { 'keyword':kw },
	function(result) {
		msgClose();
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			if(callback) {
				callback(result);
			} else {
				dlgOpen('查找主题', result, 400, 250);
			}
		}
	});
}

function search_myfavorite(callback) {
	$.post(Url('item/ajax/do/subject/op/myfavorite'), { 'in_ajax':1 },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			if(callback) {
				callback(result);
			} else {
				dlgOpen('查找主题', result, 400, 250);
			}
		}
	});
}

function search_myreviewed(callback) {
	$.post(Url('item/ajax/do/subject/op/myreviewed'), { 'in_ajax':1 },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			if(callback) {
				callback(result);
			} else {
				dlgOpen('查找主题', result, 400, 250);
			}
		}
	});
}

function clear_cookie_subject(pid) {
	$.post(getUrl('item','ajax','do=subject&op=clear_cookie_subject&in_ajax=1'),
	{ pid:pid },
	function(result) {});
	$('#cookieitems').css('display','none');
}

function post_log(sid) {
    if (!is_numeric(sid)) {
        alert('无效的ID'); 
		return;
    }
	$.post(getUrl('item','ajax','do=subject&op=post_log&in_ajax=1'), 
	{ sid:sid },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			dlgOpen('补充信息', result, 400, 250);
		}
	});
}

function post_map(sid) {
    if (!is_numeric(sid)) {
        alert('无效的ID'); 
		return;
    }
	$.post(getUrl('item','ajax','do=subject&op=post_map&in_ajax=1'), 
	{ sid:sid },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {

			dlgOpen('地图报错', result, 500, 400);

			$('#mapbtn1').click(
				function() {
					$(document.getElementById('ifupmap_map').contentWindow.document.body).find('#markbtn').click();
				}
			);

			$('#mapbtn2').click(
				function() {
					$("#frm_map").find("[@name=p1]").val($(document.getElementById('ifupmap_map').contentWindow.document.body).find('#point1').val());
					$("#frm_map").find("[@name=p2]").val($(document.getElementById('ifupmap_map').contentWindow.document.body).find('#point2').val());
					if($("#frm_map").find("[@name=p1]").val() == '' || $("#frm_map").find("[@name=p2]").val() == '') {
						alert('您尚未完成标注。');
						return;
					}
					//提交
					ajaxPost('frm_map', '', null);
				}
			);

		}
	});
}

function get_pictures(sid, page) {
    if (!is_numeric(sid)) {
        alert('无效的ID'); 
		return;
    }

	loadscript('lightbox');

	if(!page) page = 1;
	$.post(getUrl('item','ajax','do=picture&op=get&in_ajax=1&page='+page), 
	{ sid:sid },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			$('#itempics').html(result);
			//$('#roll a').lightBox();
		}
	});
}

function select_subject_thumb(sid,page) {
    if (!is_numeric(sid)) {
        alert('无效的SID'); return;
    }
	$.post(Url('item/pic/op/selectpic/sid/'+sid+'/page/'+page), { in_ajax:1}, 
	function(data) {
		if(data) {
			dlgClose();
			dlgOpen('选择图片', data, 400, 280);
		} else {
			alert('没有可用图片！');
		}
	});
}

function insert_subject_thumb(src) {
	$("#thumb").val(src);
	dlgClose();
}

function get_membereffect(sid, effect, member) {
    if (!is_numeric(sid)) {
        alert('无效的ID'); 
		return;
    }

	if(!effect) effect = 'effect1';
	if(!member) member = '0';

	$.post(getUrl('item','ajax','do=subject&op=get_membereffect&in_ajax=1'), 
	{ sid:sid, effect:effect, member:member },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else if (member == 'Y') {
			result += '<div class="clear"></div>';
			$('#effect').html(result);
			$('#effect_floor').css('display','');
		} else {
            var v = result.split('|');
            is_numeric(v[0]) && $('#num_effect1').text(v[0]);
            is_numeric(v[1]) &&  $('#num_effect2').text(v[1]);
        }
	});
}

function post_membereffect(sid, effect) {
    if (!is_numeric(sid)) {
        alert('无效的ID'); 
		return;
    }
	$.post(getUrl('item','ajax','do=subject&op=post_membereffect&in_ajax=1'), 
	{ sid:sid, effect:effect },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
            var v = result.split('|');
            $('#num_effect1').text(v[0]);
            $('#num_effect2').text(v[1]);
        }
	});
}

function picture_page(sid, page) {
    if (!is_numeric(sid)) {
        alert('无效的ID'); 
		return;
    }
	if(!page) page = 1;
	$.post(getUrl('item','pic','in_ajax=1&sid='+sid+'&page='+page), 
	{ next:page },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
            $('#pic').html(result);
        }
	});
}

function add_favorite(sid) {
    if (!is_numeric(sid)) {alert('无效的ID'); return;}
	$.post(Url('item/member/ac/favorite/op/add'), 
	{ sid:sid,in_ajax:1 },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		}
	});
}

function open_video(url) {
	var content = "<div id=\"video_dlg\"></div>";
	dlgOpen('播放视频', content, 630, 550);
	var so = new SWFObject(url, 'video2', 610, 498, 7, '#FFF');
	so.addParam("allowScriptAccess", "always");
	so.addParam("allowFullScreen", "true");
	so.addParam("wmode", "transparent");
	so.write("video_dlg");
}

/*************************************************************************************/
/************************************* review ****************************************/
/*************************************************************************************/

function post_review(sid) {
    if (!is_numeric(sid)) {
        alert('无效的ID'); 
		return;
    }
	$.post(Url('item/member/ac/review_add/sid/'+sid), { in_ajax:1 },
	function(result) {
		msgClose();
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			dlgOpen('点评', result, 600, 420);
			var t_height = $('#review_foot').attr('clientHeight') || $('#review_foot').attr('offsetHeight');
			var d_height = $('#'+DLOGID).attr('clientHeight') ||$('#'+DLOGID).attr('offsetHeight');
			$('#'+DLOGID).css('height',460 + t_height - d_height + 'px');
		}
	});
	msgOpen('载入中，请稍后......', 200, 80);
	return false;
}

function post_respond(rid) {
    if (!is_numeric(rid)) {
        alert('无效的ID'); 
		return;
    }
	$.post(Url('item/member/ac/m_respond/op/add/rid/'+rid), 
	{ in_ajax:1 },
	function(result) {
		msgClose();
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			dlgOpen('回复点评', result, 350, 260);
		}
	});
	msgOpen('载入中，请稍后......', 200, 80);
}

var c_page = 1;
function get_respond(data, page) {

	if(data=='') return;

	var ids = data.split('|');
	var rid = ids[0];
	var is_post = ids[1];

	if (!is_numeric(rid)) {
		return;
    }

	if(!page) page = 1;
	if(page == c_page && !is_post) {
		switch ($('#responds_'+rid).attr('show')) {
		case 'YES':
			$('#responds_'+rid).attr('show','NO').slideUp('fast');
			return;
		case 'NO':
			$('#responds_'+rid).attr('show','YES').slideDown('fast');
			return;
		}
	}

	var now_num = parseInt($('#respond_'+rid).text());
	if(!is_post && now_num < 1) {
		post_respond(rid);
		return;
	}

	$.post(getUrl('item','ajax','do=respond&op=get&in_ajax=1&page=' + page), 
	{ rid:rid },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			$('#responds_'+rid).css('display','none').html(result).attr('show','YES').slideDown('fast');
			c_page = page;
		}
	});

	if(is_post) {
		$('#respond_'+rid).html(now_num + 1);
	}
}

function delete_respond(respondid, rid) {
    if (!is_numeric(respondid)) {
        alert('无效的ID'); 
		return;
    }

	if(!confirm('确定要进行删除操作吗？')) return;

	$.post(Url('item/member/ac/m_respond/op/delete'), 
	{ respondids:respondid,in_ajax:1},
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else if (result == 'OK') {
			$('#respond_'+respondid+'_li').remove();
			$('#respond_'+rid).html(parseInt($('#respond_'+rid).text()) - 1);
			//get_respond(rid + '|POST');
		}
	});
}

function add_flower(rid) {
    if (!is_numeric(rid)) {
        alert('无效的ID'); 
		return;
    }
	$.post(getUrl('item','ajax','do=review&op=add_flower&in_ajax=1'), 
	{ rid:rid },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			var num_o = $('#flower_'+rid);
			num_o.html(parseInt(num_o.text())+1);
		}
	});
}

function get_flower(rid, page) {

	if (!is_numeric(rid)) {
		return;
    }

	if(!page) page = 1;
	switch ($('#flowers_'+rid).attr('show')) {
		case 'YES':
			$('#flowers_'+rid).attr('show','NO');
			$('#flowers_'+rid).css('display','none');
			return;
		case 'NO':
			$('#flowers_'+rid).attr('show','YES');
			$('#flowers_'+rid).css('display','');
			return;
	}

	$.post(getUrl('item','ajax','do=review&op=get_flower&in_ajax=1&page=' + page), 
	{ rid:rid },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			$('#flowers_'+rid).html(result);
			$('#flowers_'+rid).attr('show','YES');
		}
	});
}

function post_report(rid) {
    if (!is_numeric(rid)) {
        alert('无效的ID'); 
		return;
    }
	$.post(getUrl('item','ajax','do=review&op=post_report&in_ajax=1'), 
	{ rid:rid },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			dlgOpen('举报点评', result, 350, 260);
		}
	});
}

function check_form_report() {
	var form = document.frm_report;
	if(form.username && form.username.value.length == 0){
		msgOpen('请填写您的昵称。');
		return false;
	}
	if(form.email && form.email.value.length == 0){
		msgOpen('请输入您的电子邮件地址。');
		return false;
	} else if(form.email && !is_email(form.email.value)) {
		msgOpen('您输入的电子邮件地址格式不正确。');
		return false;
	}
	var sort = getRadio(form, 'sort');
	if(form.sort.value == '') {
		msgOpen('您选择举报类型。');
		return false;
	}
	if(form.reportcontent.value.length > 200){
		msgOpen('您填写的举报说明文字过多(200字以内)，请精简一下您的举报说明。');
		return false;
	}
	if(!is_numeric(form.rid.value) || !is_numeric(form.modelid.value)) {
		msgOpen('无法确定举报对象。');
		return false;
	}

	return true;
}

function get_review(sid, order, page) {
	if (!is_numeric(sid)) {
		return;
    }
	if(!order) order = 'posttime';
	if(!page) page = 1;

	var json = { 'sid' : sid };
	
	if(order) {
		json.order = order;
	}

	$.post(getUrl('item','ajax','do=review&op=get&in_ajax=1&page=' + page), json,
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			$('#display').html(result);
			$("div[@class=subrail] > span").each(function(i) {
				$(this).removeClass('selected');
			});
			$('#review_order_'+order).toggleClass('selected');
		}
	});

	return false;
}

/****************************************************************************************/
/************************************* guestbook ****************************************/
/****************************************************************************************/

function get_guestbook(sid,page) {
    if(!is_numeric(sid)) { alert('无效的ID'); return; }
	if(!page) page = 1;
	$.post(getUrl('item','ajax','do=guestbook&op=get&in_ajax=1&sid='+sid+'&page='+page), 
	{ empty:getRandom() },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			$('#display').html(result);
		}
	});
	//取消a标记的href跳转
	return false;
}

function post_guestbook(sid) {
    if(!is_numeric(sid)) { alert('无效的ID'); return; }
	$.post(Url('item/member/ac/m_guestbook/op/add/sid/'+sid), 
	{ in_ajax:1, empty:getRandom() },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			dlgOpen('留言', result, 350, 260);
		}
	});
}

function edit_guestbook(guestbookid) {
    if(!is_numeric(guestbookid)) { alert('无效的ID'); return; }
	$.post(Url('item/member/ac/m_guestbook/op/edit/guestbookid/'+guestbookid), 
	{ in_ajax:1, empty:getRandom() },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			dlgOpen('编辑留言', result, 350, 260);
		}
	});
}

function reply_guestbook(guestbookid) {
	if(!is_numeric(guestbookid)) { alert('无效的ID'); return; }
	$.post(Url('item/member/ac/g_guestbook/op/reply/guestbookid/'+guestbookid), 
	{ in_ajax:1,empty:getRandom() },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			dlgOpen('回复留言', result, 350, 260);
		}
	});
}

function insert_reply(guestbookid) {
	if(!is_numeric(guestbookid)) { alert('无效的ID'); return; }
	$.post(Url('item/member/ac/g_guestbook/op/insert/guestbookid/'+guestbookid), 
	{ in_ajax:1,empty:getRandom() },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			var reply = $('#guestbook_reply_'+guestbookid);
			result = '回复：' + result;
			if(reply[0]) {
				reply.html(result);
			} else {
				reply = $('<div>'+result+'</div>').attr('id','guestbook_reply_'+guestbookid).toggleClass('reply');
				$('#guestbook_content_'+guestbookid).after(reply);
			}
		}
	});
}

function delete_guestbook(guestbookid, no_cfm) {
	if(!is_numeric(guestbookid)) { alert('无效的ID'); return; }
	if(!no_cfm && !confirm('您确定要删除这条信息吗?')) return;
	$.post(Url('item/member/ac/m_guestbook/op/delete'), 
	{ guestbookids:guestbookid,in_ajax:1,empty:getRandom() },
	function(result) {
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else if(result=='OK') {
			msgOpen('留言删除完毕！', 200, 60);
			$('#guestbook_'+guestbookid).remove();
		}
	});
}
