
function G(id){
	return document.getElementById(id);
}

function menuRegist(){
	if(G("service") && G("menu")){
		var menuBtn = G("service");
		var menu = G("menu");
		menuBtn.onmouseover = menuBtn.onmouseout = menu.onmouseover = menu.onmouseout = function(){
		    G("menu").style.display = G("menu").style.display == "block"?"none":"block";
		}
	}
}

var iconnum = 0;
var iflogin = 0;
var pspurl = "http://passport.baidu.com/";

function setIcon(){
	
	if(G("voteWrap")){
		var icons = G("voteWrap").getElementsByTagName("div");
		var temp = [];
		for(var i=0;i<icons.length;i++){
			if(icons[i].className == "icon"){
				temp.push(icons[i]);
			}
		}
		
		for(var j=0;j<temp.length;j++){
			temp[j].onclick = function(e){
				
				var e = window.event || e;
				var target = e.target || e.srcElement;
				
				if(target.tagName == "SPAN"){
					target = target.parentNode;
				}
				
				
				var endDate = new Date();
				//endDate.setFullYear(2009,7,28);
				endDate.setFullYear(2009,8,4);
				var today = new Date();
				
				// 先判断是否截止
				if(today > endDate){
					alert("该活动已截至，感谢您的支持");
					return;
				}
				
				// 再判断是否登录
				if(iflogin == 0){
					window.location = pspurl + '?login&tpl=hi&u=' + window.encodeURIComponent('http://im.baidu.com/super/index.php');
					return;
				}
				
				// 判断是否已挂了三个
				if(iconnum >= 3){
					alert("您已经挂了3个选手的图标了，只有取消一个才能继续挂其他图标");
					
					return;
				}
				
				var starname = target.getAttribute("starname");
				
				var username = "";
				
				if(G("sid")){
					username = G("sid").innerHTML;
				}
				
				startBaiduHiEx({ 
					'type' : 'prefix', 
					'sid' : username,
					'prefix' : window.encodeURIComponent(window.encodeURIComponent('[' + starname + ']'))
				});
				
				//alert("操作完毕！可在百度Hi上查看挂图标的状态，并保持在线为你喜爱的选手投票吧！（必须要登录百度Hi才能成功挂图标）");
			
				window.setTimeout(function(){
					// 刷新本页
					location.href = "http://im.baidu.com/super/index.php";
				}, 2000);	
					
			}
		}
	}
}

function cancleIcon(){
	if(G("voteWrap")){
		var icons = G("voteWrap").getElementsByTagName("div");
		var temp = [];
		for(var i=0;i<icons.length;i++){
			if(icons[i].className == "cancleIcon"){
				temp.push(icons[i]);
			}
		}
		
		iconnum = temp.length;
		
		for(var j=0;j<temp.length;j++){
			temp[j].onclick = function(e){
				
				
				
				var e = window.event || e;
				var target = e.target || e.srcElement;
				
				if(target.tagName == "SPAN"){
					target = target.parentNode;
				}
				
				var starname = target.getAttribute("starname");
				
				var endDate = new Date();
				//endDate.setFullYear(2009,7,28);
				endDate.setFullYear(2009,8,4);
				var today = new Date();
				
				// 先判断是否截止
				if(today > endDate){
					alert("该活动已截至，感谢您的支持");
					return;
				}
				
				// 再判断是否登录
				if(iflogin == 0){
					window.location = 'http://passport.baidu.com/?login&tpl=hi&u=' + window.encodeURIComponent('http://im.baidu.com/super/index.php');
					return;
				}
				var username = "";
				
				if(G("sid")){
					username = G("sid").innerHTML;
				}
							
				startBaiduHiEx({ 
					'type' : 'delsegment', 
					'sid' : username,
					'starname' : starname,
					'segment' : window.encodeURIComponent(window.encodeURIComponent('[' + starname + ']'))
				});
				
				window.setTimeout(function(){
					// 刷新本页
					location.href = "http://im.baidu.com/super/index.php";
				}, 2000);	
			}
		}
	}
}


function copyLink(){
	var myContent = '百度Hi正在举办“点亮百度Hi，为快乐女声加油”活动，为喜爱的选手投票赢取百度广告位展示机会，同时还有可能获得魅族M8手机哦！http://im.baidu.com/super/';
	var myAlert = "已复制本活动链接，可以粘贴发送给QQ、MSN、百度Hi的好友";
	if(window.clipboardData){
		try{
			window.clipboardData.clearData();
			window.clipboardData.setData("Text",decodeURIComponent(myContent));
			alert(myAlert);
		}catch(e){
			alert("复制不成功，可能是您浏览器设置的安全级别过高。")
			return;
		}
	}else{
		alert("您使用的浏览器不支持此复制功能，请使用Ctrl+C或鼠标右键复制链接发送给好友。");
		return;
	}
	return false;
}

window.onload = function(){
	
	if(G("iflogin")){
		iflogin = G("iflogin").innerHTML;
	}
	
	menuRegist();
	setIcon();
	cancleIcon();
	
	
	if(G("invite")){
		G("invite").onclick = function(){
			copyLink();
			return false;
		}
	}
	
}
