$(document).ready(function(){
	var $lis=$('.menu li');
	$lis.each(function(i){
		var $li=$(this);
		if($li.attr("name")=='one'){
			if($li.attr("id") == selectMenu){
				$li.addClass("select");
			}
			$li.bind('mouseover',function(){
				$lis.removeClass("this");
				$(this).addClass("this");
				$(this).children('ul').addClass('ermenu');
			});
			$li.mouseout(function(event){
				$(this).removeClass("this");
               	$(this).children('ul').removeClass();
			});
		}
	});	
});

function dialog(message)
{	
	$('#dialog').dialog({					
					autoOpen: false,
					width: 400,
					resizable:false,
					buttons: {
						"确定": function() { 
							$(this).dialog("close"); 
						}
					}
				});
	$('#dialog').html(message);
	$('#dialog').dialog('open');
}

function show(title,url)
{
	$('#dialog').attr("title",title);
	$('#dialog').dialog({					
					autoOpen: false,
					width: 400,
					resizable:false,
					buttons: {}
				});
	$('#dialog').load(url,{"sdf":"sdf"});
	$('#dialog').dialog('open');
	return false;
}

function download(id)
{
	$.getJSON("/movie/download",{"id":id},function(data){
		var html = '<table><tbody>';
		$.each(data,function(i,item){
			html += '<tr><th><input type="checkbox" id="fselect" name="fselect" value="'+item.LINK_ID+'"></th><th><a href="#" title="">'+item.LINK_NAME+'</a></th><td>'+item.LINK_SIZE+'</td><td>'+item.LINK_TYPE+'</td></tr>';
		});
		html += '</tbody><tfoot><tr><th colspan="4"><input type="button" value="下载" title="下载" onclick="dodownload('+id+')"/></th></tr></tfoot></table>';
		$('#dialog').attr("title","下载列表");
		$('#dialog').dialog({					
					autoOpen: false,
					width: 600,
					resizable:false,
					buttons: {}
				});
		$('#dialog').html(html);
		$('#dialog').dialog('open');	
	});
	return false;
}

function dodownload(id)
{
	$.ajax({type:"POST",
			dataType:"json",
			url:"/movie/dodownload",
			data:{"id":id,"items[]":$("#fselect").val()},
			success: function(json){
				if(json.notice == "ok")
				{
					alert(json.message);					
					$('#dialog').dialog('close');	
				}
				else
				{	
					alert(json.message);
					if(json.message == "请先登陆") {
						window.load = "/my"
					} 						
					$('#dialog').dialog('close');	
				}
			}});
	return false;
}
function getDownLinks(id)
{ 
	var inputs  = $("#downLinks input");
	var links = '';
	for(var i=0;i<inputs.length;i++){
		if(inputs[i].checked==true){
			links  += ","+inputs[i].name
		}
	}
	
	if(links){
		Tdodownload(links,id);
	}else{
		dialog("请先选择任务！");
	}
}
function Tdodownload(links,id)
{
	$.ajax({type:"POST",
		dataType:"json",
		url:"/teleplay/dodownload",
		data:{"id":id,"items":links},
		success: function(json){
			if(json.notice == "ok")
			{
				alert(json.message);					
				$('#dialog').dialog('close');	
			}
			else
			{			
				alert(json.message);
				if(json.message == "请先登陆") {
					window.load = "/my"
				} 						
				$('#dialog').dialog('close');
			}
		}});
	
	return false;
}

function send(id)
{
	$.getJSON("/my/friend",{"prepage":100},function(data){
		var html = '<table><tbody><tr><th><select id="fselect" name="fselect" multiple="multiple" style="width:140px;height:160px;">';
		$.each(data,function(i,item){
			html += '<option value="'+item.CLIENT_ID+'">'+item.FRIEND_NAME+'</option>';
		});
		html += '</select></th><td>按住Ctrl多选</td></tr></tbody><tfoot><tr><th colspan="4"><input type="button" value="推送" title="推送" onclick="dosend('+id+')"/></th></tr></tfoot></table>';
		$('#dialog').attr("title","请选择要推送的朋友");
		$('#dialog').dialog({					
					autoOpen: false,
					width: 400,
					resizable:false,
					buttons: {}
				});
		$('#dialog').html(html);
		$('#dialog').dialog('open');	
	});
	return false;
}

function dosend(id)
{
	$.ajax({type:"POST",
			dataType:"json",
			url:"/movie/dosend",
			data:{"id":id,"friends[]":$("#fselect").val()},
			success: function(json){
				if(json.notice == "ok")
				{
					dialog(json.message);
					//window.location = json.url;
				}
				else
				{					
					dialog(json.message);
				}
			}});
	return false;
}

function doscore(id,score,userid,uri)
{
	$.ajax({type:"POST",
			dataType:"json",
			url:uri+"doscore",
			data:{"id":id,"score":score,"userid":userid},
			success: function(json){
				if(json.notice == "ok")
				{
					dialog(json.message);
					window.location = json.url;
				}
				else
				{					
					dialog(json.message);
				}
			}});
	return false;
}

function message(userid)
{
	$('#dialog').attr("title","发送站内短信");
	$('#dialog').dialog({					
					autoOpen: false,
					width: 400,
					resizable:false,
					buttons: {}
				});
	$('#dialog').html('<form name="addmessage_form" method="post" id="addmessage_form" class="movie_choice"><ul class="clear"><li><textarea name="message" style="width:280px; height:180px;"></textarea></li></ul><p><input type="hidden" name="userid" value="'+userid+'"><input type="button" class="btn_b" value="确定" onclick="domessage()"/></p></form>');
	$('#dialog').dialog('open');
	return false;
}

function domessage()
{
	$.ajax({type:"POST",
			dataType:"json",
			url:"/user/addmessage",
			data:$("#addmessage_form").serialize(),
			success: function(json){
				if(json.notice == "ok")
				{
					$('#dialog').dialog('close');
				}
				else
				{					
					dialog(json.message);
				}
			}});
	return false;
}

function loadComment(url)
{	
	$("#comment_ul").load(url,{time:"111"});
	return false;
}

function submitComment()
{
	if($("#comment_title").val() == "" || $("#comment_title").val() == "请此处输入评论标题" )
	{
		dialog("请输入评论标题。");
		$(this).focus();
		return false;
	}
	if($("#comment_content").val() == "" || $("#comment_content").val() == "请此处输入评论内容")
	{
		dialog("请输入评论内容。");
		$("#common_content").focus();
		return false;
	}
	$.ajax({type:"POST",
			dataType:"json",
			url:"/comment/add",
			data:$("#comment_form").serialize(),
			success: function(json){
				if(json.notice == "ok")
				{
					$("#comment_title").val("");
					$("#comment_content").val("");
					loadComment(comment_url);
					$("#comment_ul").effect("highlight");
				}
				else
				{					
					dialog(json.message);
				}
			}});
	return false;
}
function addfriend(userid)
{
	$('#dialog').attr("title","添加好友");
	$('#dialog').dialog({					
					autoOpen: false,
					width: 400,
					resizable:false,
					buttons: {}
				});
	$('#dialog').html('<form name="addfriend_form" method="post" id="addfriend_form" class="movie_choice"><ul><li><label>好友权限:<label><select name="permit"><option value="1">只读</option><option value="2">可写</option><option value="4">执行权限</option><option value="7">完全控制</option></select></li><li><label>好友分组:<label><select name="group"><option value="-1">默认分组</option></select></li></ul><p><input type="hidden" name="userid" value="'+userid+'"><input type="button" class="btn_b" value="确定" onclick="doaddfriend()"/></p></form>');
	$('#dialog').dialog('open');
	return false;
}

function doaddfriend()
{
	$.ajax({type:"POST",
			dataType:"json",
			url:"/my/addfriend",
			data:$("#addfriend_form").serialize(),
			success: function(json){
				if(json.notice == "ok")
				{
					window.location.reload(1);
				}
				else
				{					
					dialog(json.message);
				}
			}});
	return false;
}

function deletefriend(userid)
{
	$.ajax({type:"POST",
			dataType:"json",
			url:"/my/deletefriend",
			data:"userid="+userid,
			success: function(json){
				if(json.notice == "ok")
				{
					window.location.reload(1);
				}
				else
				{					
					dialog(json.message);
				}
			}});
	return false;
}


function checkallbox()
{
	var button = $("#checkallButton").attr('checked');
	if(button == true){
		$(".checkboxes").attr('checked',true);
	}else{
		$(".checkboxes").attr('checked',"");
	}
}

function getDownloads(type,page,prepage)
{
//	alert(type + "....." +  page+ "........" + prepage);
	var url = "/my/" + type;
	$.ajax({
		type:"post",
		dataType:"json",
		url:url,
		data:{"type":type,"page":page,"prepage":prepage},
		success:function(json){	
			var html = '';
			if(json != ""){
				for(var i=0;i<json.length;i++){
					html += "<tr class='task" + json[i].TASK_ID + "'><th><a  class='taskTitle' href='/"+ checkDownType(json[i].RESOURCE_TYPE)+ "/show/id/" + json[i].RESOURCE_ID + "' target='_blank' title='" + json[i].TASK_NAME + "'>" + json[i].TASK_NAME + "</a></th>" +
							"<td>" + checkDownType(json[i].TOTAL_SIZE) + "</td><td>" + checkDownType(json[i].DOWNLOAD_SPEED) + "</td><td><input type='submit' value='" 
							+ checkDownType(json[i].TASK_STATUS+"s")+ "' title='" + json[i].TASK_ID + "' class='grn'/></td>"+
							"<td><input type='button' value='x' onclick='delDownload(" + json[i].TASK_ID + ")' title='删除' class='org'/></td></tr>"
				}
			} else {
				html = "<tr><td colspan='6' style='text-align:center'>无相关下载任务！</td></tr>"
			}
			$("#getAjax").html(html);
		}
		
	})
	return false;
}

function delDownload(taskid,url)
{
	if(url == '/my/delComment') {
		url ='/my/delComment';
	} else {
		url ='/my/delTask';
	}
	var r = confirm("确定要删除吗？");
	if (r == true){
		$.ajax({
			type:"post",
			dataType:"json",
			url:url,
			data:"taskid="+taskid,
			success:function(json){	
				dialog("删除成功！");
				if(url == '/my/delTask'){
					$(".task" + taskid).hide();
				} else {
					$(".comment" + taskid).hide();	
				}
			}
		})
	} else {
		
	}
	return false;
}
function checkDownType(type){
	var r;
	switch (type)
	{
		case '7'   : r = 'movie';break;
		case '16'  : r = 'education';break;
		case '1s'  : r = "任务未确认";break;
		case '2s'  : r = "任务等待";break;
		case '4s'  : r = "任务运行";break;
		case '8s'  : r = "任务完成";break;
		case '64s' : r = "任务暂停";break;
		case '128s': r = "任务删除";break;
		case "16s" : r = "任务出错";break;
		default    : r = "";
	}
	return r;
}


function uploadPic() {
	$.ajax({
		type:"post",
		dataType:"json",
		url:'/my/upload',
		data:$("#upfileForm").serialize(),
		success:function(json){
//			alert(json);
		}
	})
	return false;
}
function checkComment() {
	$("#comment_title").focus(function(){
		var r = $(this).val();
		if(r == "请此处输入评论标题"){
			$(this).val("");
		}
	});
	$("#comment_title").blur(function(){
		var r = $(this).val();
		if(r == ""){
			$(this).val("请此处输入评论标题");
		}
		
	})
	$("#comment_content").focus(function(){
		var r = $(this).val();
		if(r == "请此处输入评论内容"){
			$(this).val("");
		}
	});
	$("#comment_content").blur(function(){
		var r = $(this).val();
		if(r == ""){
			$(this).val("请此处输入评论内容");
		}
		
	})
}

function getxmlhttp() {
	var xmlhttp = false;
	try {
		xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
	} catch(e) {
		try {
			xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
		} catch(E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function createPhoto(img) {
	$("#photo").css({
		overflow: 'hidden',
		width: '100px',
		height: '100px'
		});
	$("#photo").empty();
	$("#photo").html(img);
}

function processajax(obj,serverPage) {
	var theimg;
	xmlhttp = getxmlhttp();
	xmlhttp.open("GET",serverPage);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4  && xmlhttp.status == 200 ) {
			document.getElementById(obj).innerHTML = xmlhttp.responseText;
			createPhoto(xmlhttp.responseText);
		}	
	}
	xmlhttp.send(null);
}	
function uploadimg(theform) {
	$("#showimg").empty;
	var r = $(".upfile").val();
	if(r == ""){
		setStatus("请选择上传文件...","showimg");
		return false;
	}
	var types = new Array('jpeg','jpg','png','gif');
	var type = r.split('.')[1];
	if(type == types[0] || type == types[1] || type == types[2] || type == types[3]) {
		theform.submit();
		setStatus("上传中，请稍后...","showimg");
	} else {
		setStatus("上传文件格式不对...","showimg");
	}
	
}

function setStatus(theStatus,theObj) {
	obj = document.getElementById(theObj);
	if(obj){
		obj.innerHTML = "<div class=\"bold\">" + theStatus + "</div>"
	}
}
function doneloading(theframe,thefile) {
	var theloc  = "/my/showphoto/thefile/'" + thefile + "'";
	theframe.processajax("showimg",theloc);
}

