// JavaScript Document
var wt = {}

/**
 * 加喜欢
 *
 */
wt.like_add = function(target, id, model) {
	$.post('/ajax/like.php', {"id":id, "model":model}, function(obj) {
		if(obj.state == 1) {
			target.className = target.className.replace("like_cannel", "");
		}else if(obj.state == 2) {
			target.className += " like_cannel";
		}else{
			alert(obj.msg);
		}
		if(obj.count <= 0) {
			document.getElementById("like_" + id).innerHTML = '';
		}else{
			document.getElementById("like_" + id).innerHTML = '<a href="javascript:void(0)" class="nlink" onclick="wt.like_show(\'' + id + '\', \'' + model + '\')">' + obj.count + '人</a>喜欢';
		}
	}, 'json');
}

/**
 * 查看喜欢的人是谁
 *
 */
wt.like_show = function(id, model) {

}

/**
 * 加收藏
 *
 */
wt.reco_add = function(title, summary) {
	var html = '';
	html += '<div style="width:400px; height:200px;">';
	html += '<div style="padding:10px 0px; font-weight:bold;" id="reco_title">' + title + '</div>';
	html += '<div style="padding:0px 0px 10px 0px;" id="reco_summary">' + summary + '</div>';
	html += '<div style="width:390px; margin:0px auto;">';
	html += ' <div style="float:left;"><textarea class="textarea01" id="reco_explains" style="width:280px; height:60px; overflow-y:auto;"></textarea></div>';
	html += ' <div style="float:left; padding:0px 0px 0px 7px;"><input type="button" onclick="wt.reco_do()" value="确 定" class="but01" style="height:67px;"/></div>';
	html += ' <div class="clear"></div>';
	html += '</div>';
	html += '<div style="padding:7px;">不能超过70字哟 ^_^</div>';
	html += '</div>';
	dialog.show(html, '推荐我的主页');
}

/**
 * 加收藏ing
 *
 */
wt.reco_do = function() {
	var title = document.getElementById("reco_title").innerHTML;
	var summary = document.getElementById("reco_summary").innerHTML;
	var explains = document.getElementById("reco_explains").value;
	var url = document.URL;
	
	$.post('/ajax/reco.php', {"title":title, "summary":summary, "explains":explains, "url":url}, function(obj) {
		if(obj.state == 1) {
			dialog.close();
		}else{
			alert(obj.msg);
		}
   },'json');
}

/**
 * 用户头像弹出框
 *
 */
wt.member = function(obj, id) {
	var lastobj = document.getElementById("photo_info_" + id);
	if(lastobj != null) {
		lastobj.style.top = obj.offsetHeight - 10 + obj.offsetTop + "px";
		lastobj.style.left = obj.offsetWidth - 10 + obj.offsetLeft + "px";
		lastobj.title = '';
		$(lastobj).show('fast');
	}else{
		var div = document.createElement('div');	
		div.id = "photo_info_" + id;
		div.style.position = "absolute";
		div.style.border = '1px solid #FF6600';
		div.style.background = '#FFF';
		div.style.display = 'none';
		div.style.padding = '7px';
		div.style.width = '200px';
		div.style.top = obj.offsetHeight - 10 + obj.offsetTop + "px";
		div.style.left = obj.offsetWidth - 10 + obj.offsetLeft + "px";
		div.className = 'radius';
		div.innerHTML = 'loading...';
		$.post('/ajax/member.php', {"action":"photoinfo", "id":id}, function(html) {
			div.innerHTML = html;
		});
		document.body.appendChild(div);
		div.title = '';
		$(div).show('fast');
		div.onmouseover = function() {
			div.title = '';
		}
		div.onmouseout = function() {
			div.title = 'closing...';
			wt.dohmember(id);
		}
	}
}

/**
 * 隐藏用户头像弹出框
 *
 */
wt.hmember = function(obj, id) {
	var lastobj = document.getElementById("photo_info_" + id);
	if(lastobj != null) {
		lastobj.title = 'closing...';
		wt.dohmember(id);
	}
}

/**
 * 开始隐藏用户头像弹出框
 *
 */
wt.dohmember = function(id) {
	var lastobj = document.getElementById("photo_info_" + id);
	if(lastobj != null) {
		setTimeout(function(){
			if(lastobj.title == 'closing...') {
				$(lastobj).hide('fast');
			}
		}, 200);
	}
}

/**
 * 小纸条弹出框
 *
 */
wt.pmsg = function(nickname, id) {

	var html = '';
	html += '<div style="width:400px; height:150px;">';
	html += '<div style="padding:10px 0px 10px 0px;"> 写给' + nickname + '的小纸条：</div>';
	html += '<div style="width:390px; margin:0px auto;">';
	html += ' <div style="float:left;"><textarea class="textarea01" id="pmsg_content" style="width:280px; height:60px; overflow-y:auto;"></textarea></div>';
	html += ' <div style="float:left; padding:0px 0px 0px 7px;"><input type="button" onclick="wt.pmsg_do(' + id + ')" value="确 定" class="but01" style="height:67px;"/></div>';
	html += ' <div class="clear"></div>';
	html += '</div>';
	html += '<div style="padding:7px;">不能超过70字哟 ^_^</div>';
	html += '</div>';
	dialog.show(html, '小纸条');

}

/**
 * 发送小纸条
 *
 */
wt.pmsg_do = function(id) {

	var pmsg_content = document.getElementById("pmsg_content").value;

	$.post('/ajax/pmsg.php', {"content":pmsg_content, "id":id}, function(obj) {
																		 
		alert(obj.msg);
		
	}, "json");
	
}

wt.flash = function(src, flashvars, width, height) {

	if(flashvars.indexOf('editor_video_player') != -1) {
		var _src = flashvars.substring(flashvars.indexOf('file=') + 5, flashvars.indexOf('&autostart'));
		if(_src.indexOf('.flv') == -1) {
			src = _src;
		}
	}
	
	var html = '';
	html += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '" align="middle">';
	html += ' <param name="movie" value="' + src + '" />';
	html += ' <param name="quality" value="high" />';
	html += ' <param name="wmode" value="transparent">';
	html += ' <param name="flashVars" value="' + flashvars + '"/>';
	html += ' <embed src="' + src + '" flashVars="' + flashvars + '" wmode="transparent" quality="high" width="' + width + '" height="' + height + '" align="middle"  type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
	html += '</object>';

	if(arguments[4] == undefined) {
		document.writeln(html);
	}else{
		if(typeof(arguments[4]) == 'object') {
			arguments[4].innerHTML = html;
		}else{
			document.getElementById(arguments[4]).innerHTML = html;
		}
	}
}

function check_login(obj) {
	if(obj.email.value == "") {
		alert('请输入邮箱，或者账户。');
		obj.email.focus();
		return false;
	}
	if(obj.password.value == "") {
		alert('请输入密码。');
		obj.password.focus();
		return false;
	}
}

function check_search(obj) {
	alert('暂未开启');
	return false;
}




/******************************************************
 *
 * 表情 Begin
 *
 *
 ******************************************************/
var Mood = function(id) {
	this.id = id;
};

Mood.prototype.face = function(limit) {

	var o = this;

	var nums = 94;
	var pageNum = Math.floor(this.width / 88) - 2;

	if(limit - pageNum <= 0) {
		prev_limit = function() {
			alert('到头了');
		}
	}else{
		prev_limit = function() {
			o.face(limit - pageNum - 1);
		}
	}

	if(limit + pageNum >= nums) {
		next_limit = function() {
			alert('到尾了');
		}
	}else{
		next_limit = function() {
			o.face(limit + pageNum + 1);
		}
	}

	var prev = document.createElement('input');
	prev.type = 'button';
	prev.className = 'but01';
	prev.value = '往前翻';
	prev.onclick = prev_limit;

	var next = document.createElement('input');
	next.type = 'button';
	next.className = 'but01';
	next.value = '往后翻';
	next.onclick = next_limit;
	
	this.editFace = document.getElementById(this.id);

	this.editFace.innerHTML = '';

	for(var i=limit; i<=pageNum + limit; i++) {
		var img = document.createElement("div");
		window.ActiveXObject ? img.style.styleFloat = 'left' : img.style.cssFloat = 'left';
		img.style.padding = '10px 0px 0px 0px';
		img.style.margin = '0px 3px';
		var s = i + "";
		if(s.length == 1) s = "0" + i;
		img.innerHTML = '<img src="/images/mood/M' + s + '.GIF" border="0"/>';
		img.name = '[EM' + s + ']';
		img.style.cursor = "pointer";
		img.onclick = function() {
			document.getElementById("content").value += this.name;
		}
		this.editFace.appendChild(img);	
	}

	var buttonDiv = document.createElement("div");
	buttonDiv.style.textAlign = 'center';
	buttonDiv.appendChild(prev);
	buttonDiv.appendChild(next);
	this.editFace.appendChild(buttonDiv);
}

/******************************************************
 *
 * 表情 End
 *
 *
 ******************************************************/
 
 
var editor = {};

editor.size = function(obj, target, size) {
	var target = document.getElementById(target);
	if(obj.value.length > size) {
		obj.value = obj.value.substring(0, size);
		return false;	
	}
	target.innerHTML = "内容长度：" + obj.value.length + "/" + size;
}

editor.table = document.createElement("div");

editor.add = function(target, type) {

	this.target = target;

	var html = '';
	
	if(type == 'url') {
		
		html += '<div style="padding:10px 0px 0px 0px;">';
		html += ' <div style="padding:7px; text-align:left;">标题：<input type="text" id="edit_url_title" class="ipt01"/></div>';
		html += ' <div style="padding:7px; text-align:left;">链接：<input type="text" id="edit_url_url" class="ipt01" style="width:320px;"/></div>';
		html += ' <div style="padding:7px; text-align:center;"><input type="button" onclick="editor.appChild(this)" name="' + target + '|' + type + '" value="添加" class="but01"/></div>';
		html += '</div>';
		
		var title = '添加链接';
	}else if(type == 'img') {

		html += '<div style="padding:10px 0px 0px 0px;">';
		html += ' <div style="padding:7px; text-align:left;"><input type="button" value="网络图片" onclick="editor.change(this, \'edit_img_but_upload\')" id="edit_img_but_src" class="but02_on"/> <input type="button" value="本地上传" onclick="editor.change(this, \'edit_img_but_src\')" id="edit_img_but_upload" class="but02"/></div>';
		html += ' <div style="padding:7px; text-align:left;" id="edit_img_src_div">图片地址：<input type="text" id="edit_img_src" class="ipt01" style="width:300px;"/></div>';
		html += ' <div style="padding:7px; text-align:left; display:none;" id="edit_img_upload_div">本地文件：<input type="file" name="filedata" class="ipt01" onchange="editor.upload(this)" style="width:320px; background-color:#ccc"/></div>';
		html += ' <div style="padding:7px; text-align:center; display:none;" id="edit_img_upload_loading"><img src="/images/loading/01.gif"></div>';
		html += ' <div style="padding:7px; text-align:center;" id="edit_img_submit"><input type="button" onclick="editor.appChild(this)" name="' + target + '|' + type + '" value="添加" class="but01"/></div>';
		html += '</div>';
		
		var title = '添加图片';
	}else if(type == 'video') {

		html += '<div style="padding:10px 0px 0px 0px;">';
		html += ' <div style="padding:7px; text-align:left;">视频地址：<input type="text" id="edit_video_url" class="ipt01" style="width:300px;"/></div>';
		html += ' <div style="padding:7px; text-align:left;">flv, swf才可以播放哟。</div>';
		html += ' <div style="padding:7px; text-align:center;"><input type="button" onclick="editor.appChild(this)" name="' + target + '|' + type + '" value="添加" class="but01"/></div>';
		html += '</div>';
		
		var title = '添加视频';
	}else if(type == 'mp3') {

		html += '<div style="padding:10px 0px 0px 0px;">';
		html += ' <div style="padding:7px; text-align:left;">MP3地址：<input type="text" id="edit_mp3_url" class="ipt01" style="width:300px;"/></div>';
		html += ' <div style="padding:7px; text-align:left;">必须是MP3格式才可以播放哟。</div>';
		html += ' <div style="padding:7px; text-align:center;"><input type="button" onclick="editor.appChild(this)" name="' + target + '|' + type + '" value="添加" class="but01"/></div>';
		html += '</div>';
		
		var title = '添加音乐';
	}
	
	this.table.innerHTML = html;
	this.table.style.width = "400px";
	this.table.style.height = "170px";
	dialog.show(this.table, title);
}

editor.appChild = function(obj) {
	var msg = obj.name.split("|");
	
	var target = document.getElementById(msg[0]);
	var type = msg[1];
	
	if(type == 'url') {

		var title = document.getElementById("edit_url_title").value;
		var url = document.getElementById("edit_url_url").value;
		target.value += "\n[a href=\"" + url + "\"]" + title + "[/a]";

	}else if(type == 'img') {

		if(document.getElementById('edit_img_src_div').style.display == 'none') {

			document.getElementById('edit_img_upload_loading').style.display = 'block';
			obj.onclick = '';
			return false;

		}else{
			var src = document.getElementById("edit_img_src").value;
			target.value += "\n[img]" + src + "[/img]";
		}
		
	}else if(type == 'video') {

		var url = document.getElementById("edit_video_url").value;
		target.value += "\n[video]" + url + "[/video]";
		
	}else if(type == 'mp3') {
		var url = document.getElementById("edit_mp3_url").value;
		target.value += "\n[mp3]" + url + "[/mp3]";
	}
	
	dialog.close();

}

editor.change = function(obj, target) {
	obj.className = 'but02_on';
	target = document.getElementById(target);
	target.className = 'but02';
	if(target.id == 'edit_img_but_upload') {
		document.getElementById('edit_img_src_div').style.display = 'block';
		document.getElementById('edit_img_upload_div').style.display = 'none';
		document.getElementById('edit_img_submit').style.display = 'block';
		
	}else{
		document.getElementById('edit_img_src_div').style.display = 'none';
		document.getElementById('edit_img_upload_div').style.display = 'block';
		document.getElementById('edit_img_submit').style.display = 'none';
	}
}

editor.upload = function(input) {

	var iframe = null;

	if(window.ActiveXObject){
		iframe = document.createElement('<iframe name="upload_iframe">'); 
	}else if(window.XMLHttpRequest){
		iframe = document.createElement('iframe');
	}
	
	iframe.name = 'upload_iframe';

	iframe.id = iframe.name;
	iframe.src = 'about:blank';
	iframe.style.display = 'none';
	document.body.appendChild(iframe);
	
	var form = document.createElement('form');
	document.body.appendChild(form);
	
	this.table.innerHTML = "<div style='text-align:center; padding:30px 0px 10px 0px;'>正在上传...</div><div style='text-align:center;'><img src='/images/loading/01.gif'/></div>";
	
	form.id = iframe.name + '_form';
	form.style.display = 'none';
	form.target	 = iframe.name;
	form.action	 = '/ajax/editor.upload.php';
	form.encoding = 'multipart/form-data';
	form.method  = 'POST';
	form.appendChild(input);

	setTimeout(function() {
		form.submit();
	}, 100);

	return false;
}

editor_upload_complete = function(src, state, msg) {
	document.body.removeChild(document.getElementById("upload_iframe"));
	document.body.removeChild(document.getElementById("upload_iframe_form"));
	dialog.close();
	if(state == 1) {
		document.getElementById(editor.target).value += "\n[img]" + src + "[/img]";
	}else{
		alert(msg);
	}
}
