var g_init2 = null;
var g_myBodyInstance2 = null;
var g_myBodyInstanceString2 = null;
var isMinNS42 = null;
var isMinNS52 = null;
var isMinIE42 = null;
var isMinIE52 = null;
var isMacIE2 = null;
var getFFVersion2 = null;
var FFextraHeight2 = null;
var isOpera2 = null;

var encjs = "/js/vote/ps.js";
var dflt_css = "/js/vote/vote.css";
var css_obj = null;
var css_rel = null;
var vote_window = null;
var drag_div2 = null;
var timer2 = null;



function vote_form(aid, surl, ImgId,sc,k,p)
{
	if ( !g_init2 )
	{
		// initialize global variables
		g_myBodyInstance2 = document.body;
		g_myBodyInstanceString2 = "document.body";
		isMinNS42 = (navigator.appName.indexOf("Netscape") >= 0 && parseFloat(navigator.appVersion) >= 4) ? 1 : 0;
		isMinNS52 = (navigator.appName.indexOf("Netscape") >= 0 && parseFloat(navigator.appVersion) >= 5) ? 1 : 0;
		isMinIE42 = (document.all) ? 1 : 0;
		isMinIE52 = (isMinIE42 && navigator.appVersion.indexOf("5.") >= 0) ? 1 : 0;
		isMacIE2 = (isMinIE42 && navigator.userAgent.indexOf("Mac") >= 0) ? 1 : 0;
		getFFVersion2 = navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1];
		FFextraHeight2 = getFFVersion2 < 1.5 ? 16 : 0;
		isOpera2 = (navigator.appName.indexOf("Opera") >= 0) ? 1 : 0;
		
		if ( document.compatMode != "BackCompat" && !isOpera2 )
		{
		    g_myBodyInstance2 = document.documentElement;
		    g_myBodyInstanceString2 = "document.documentElement";
		}
		
		g_init2 = true;
		// initialize done
	}
	

	// get all arguments
	var argv = vote_form.arguments;
	redirect_url = window.location;
	target_win = "_self";
	css_url = dflt_css;
	
	
	if ( !css_obj || (css_rel != css_url) )
	{
		css_obj = document.getElementById('login_css');
		
		if ( !css_obj )
		{
			// add login css
			var login_css = document.createElement('link');
			login_css.id = 'login_css';
			login_css.rel = 'stylesheet';
			login_css.type = 'text/css';
			login_css.media = 'screen';
			login_css.href = css_url;
			document.body.appendChild(login_css);
			
			css_obj = login_css;
		}
		else
		{
			css_obj.href = css_url;
		}
		
		css_rel = css_url;
	}
	
	if ( !vote_window )
	{
		// add encrypt js
		var encScript = document.createElement('script');
		encScript.src = encjs;
		encScript.type = "text/javascript";
		document.body.appendChild(encScript);
		
		// add div element
		var divE = document.createElement('div');
		divE.id = 'login_container';
		divE.style.visibility = 'hidden';
		divE.style.opacity = 0;
		
		var login_HTML = "";
		
		if ( navigator.userAgent.indexOf('MSIE') != -1 )
		{
			login_HTML += "<iframe id=\"mask_iframe\" src=\"\"></iframe>";
		}
		
		login_HTML += "	<div id=\"login_header\">";
		login_HTML += "	<h4>请您投票</h4>";
		login_HTML += "		<span id=\"close\" onclick=\"javascript:setLoginFormVisible2('hide');\"><span class=\"none\">关闭</span></span>";
		login_HTML += "	</div>";
		login_HTML += "	<div id=\"login_form\">";
		login_HTML += "	<form id=\"frmVote\" class=\"ajaxForm\" name=\"user_login\" action=\"" + surl +  "\"method=\"post\" enctype=\"application/x-www-form-urlencoded\" target=\"" + target_win + "\" autocomplete=\"off\" >";
		login_HTML += "		<input type=\"hidden\" id=\"vid\" name=\"vid\" value=\"" + ImgId + "\" />";
		login_HTML += "		<input type=\"hidden\" name=\"c\" value=\"" + sc + "\" />";
		login_HTML += "		<input type=\"hidden\" name=\"FType1\" value=\"" + k + "\" />";
		login_HTML += "		<input type=\"hidden\" name=\"curr_page\" value=\"" + p + "\" />";
		login_HTML += "		<ul>";
		login_HTML += "			<li>";
		login_HTML += "				<span class=\"code_span\">验证图片：</span>";
		login_HTML += "				<img id=\"loginVerifyImg\" src=\"http://ptlogin2.qq.com/getimage?aid=" + aid + "&" + Math.random() + "\" />";
		login_HTML += "				<p><a href=\"javascript:_nchangeImg2(" + aid + ");\" tabindex=\"-1\">看不清,换一张</a></p>";
		login_HTML += "			</li>";
		login_HTML += "			<li>";
		login_HTML += "				<span>附加字符：</span>";
		login_HTML += "				<label accesskey=\"v\" for=\"vrfycode\">";
		login_HTML += "					<input id=\"vrfycode\" name=\"verifycode\"  type=\"text\" class=\"verifycode\" maxlength=\"4\" style=\"ime-mode:disabled;\" />*";
		login_HTML += "				</label>";
		login_HTML += "				<p>请输入上图中字符，大小写忽略</p>";
		login_HTML += "			</li>";
		login_HTML += "			<li>";
		login_HTML += "				<input name=\"imageField\" type=\"submit\" class=\"bt_login\" value=\"投　　票\" border=\"0\">";
		login_HTML += "			</li>";
		login_HTML += "		</ul>";
		login_HTML += "	</form>";
		login_HTML += "	</div>";
		
		divE.innerHTML = login_HTML;
    	
    	document.body.appendChild(divE);
		
		vote_window = document.getElementById('login_container');
		
		timer2 = setTimeout("setLoginFormVisible2('show')", 500);
	}
	else
	{
		_nchangeImg2(aid);
		timer2 = null;
	}	
		
	drag_div2 = true;
		
	if ( !timer2 )
	{
		setLoginFormVisible2('show');
	}
	
	document.user_login.verifycode.value = '';
	document.user_login.imageField.disabled = false;
	
	var l_h = document.getElementById("login_header");
	
	if ( drag_div2 )
	{
		l_h.onmousedown = startMove2;
	}
	else
	{
		l_h.onmousedown = null;
	}
	$("#vid").val(ImgId);
	$("#vrfycode").focus();
	$("#frmVote").ajaxForm(
		{
			dataType:"json",
			beforeSubmit: CheckFormVote2,
			success:showResult
		}	   
	);
}

function showResult(data)
{
	alert(data.message);	
	setLoginFormVisible2('hide');
	if(data.code == 0&&data.fid)
	{
		$("#"+data.fid+"_vote").text(parseInt($("#"+data.fid+"_vote").text())+1);	
	}
	
}


function _nchangeImg2(aid)
{
	var img = document.getElementById("loginVerifyImg");
	img.src = "http://ptlogin2.qq.com/getimage?aid=" + aid + "&" + Math.random();
	var ctrl = document.getElementById("code");
	if (ctrl != null)
	{
		ctrl.focus();
	}
}


function CheckFormVote2()
{
	var vcode = document.getElementById('vrfycode');
	if (vcode.value.length != 4)
	{
		alert("请正确填写验证字符！");
		vcode.focus();
		vcode.select();
		return false;
	}
	return true;
}

function process2(form)
{
	if (!CheckFormVote2())
	{
		return false;
	}
	document.user_login.imageField.disabled = true;
	return true;
}

function setLoginFormVisible2(vis)
{
	if (vis != 'hide')
	{
		vote_window.style.display = '';
		vote_window.style.visibility = 'visible';
		vote_window.style.opacity = 1;
		
		if ( getWindowWidth2() != getWidth2(vote_window) )
		{
			moveLayerTo2(vote_window,(getWindowWidth2()-getWidth2(vote_window))/2,getPageScrollY2()+(getWindowHeight2()-getHeight2(vote_window))/2);
		}
		
	}
	else
	{
		vote_window.style.display = 'none';

	}
}

// drag functions
function startMove2(evt)
{
	evt = getCurrentEvent2(evt);
		
	if ( !isLeftKeyDown2(evt) )
		return;
	
	var alertDlg = document.getElementById("login_header");
	var loginCtnr = document.getElementById("login_container");
	setEventCapture2(alertDlg);
	stopEventTransfer2(evt);
	loginCtnr.$clientX = evt.clientX;
	loginCtnr.$clientY = evt.clientY;
	alertDlg.onmousemove = MoveIt2;
	alertDlg.onmouseup = stopMove2;
}

function MoveIt2(evt)
{
	evt = getCurrentEvent2(evt);

	if ( !isLeftKeyDown2(evt) )
		return;
	
	var alertDlg = document.getElementById("login_container");//login_container
	
	var w_wid = document.body.scrollWidth;
	var w_hgt = document.body.scrollHeight;
	var xoffset = alertDlg.offsetLeft + (evt.clientX - alertDlg.$clientX);
	var yoffset = alertDlg.offsetTop + (evt.clientY - alertDlg.$clientY);
	
	if ( xoffset + getWidth2(alertDlg) > w_wid )
	{
		alertDlg.style.left = w_wid - getWidth2(alertDlg) - 4 + 'px';
		return;
	}
	else if ( yoffset + getHeight2(alertDlg) > w_hgt )
	{
		alertDlg.style.top = w_hgt - getHeight2(alertDlg) - 4 + 'px';
		return;
	}
	
	alertDlg.style.left = xoffset + "px";
	alertDlg.style.top = yoffset + "px";
	alertDlg.$clientX = evt.clientX;
	alertDlg.$clientY = evt.clientY;
}

function stopMove2(evt)
{
	evt = getCurrentEvent2(evt);

	if ( !isLeftKeyDown2(evt) )
		return;

	var alertDlg = document.getElementById("login_header");
	alertDlg.onmousemove = null;
	alertDlg.onmouseup = null;
	releaseEventCapture2(alertDlg);
}

function getElementPageOffset2(x,y)
{
	return {x: x + document.body.scrollLeft - 2, y: y + document.body.scrollTop - 2};
}

function getPageOffset2(element)
{
	var left = 0,top = 0;

	do {
		top += element.offsetTop || 0;
		left += element.offsetLeft || 0;
		element = element.offsetParent;
	} while(element);

	return {x: left, y: top};
}

function getCurrentEvent2(evt)
{
	return (!evt) ? window.event : evt;
}

function getEventTarget2(evt)
{
	evt = ( (!evt) ? window.event : evt );
	return evt.target || event.srcElement;
}

function isLeftKeyDown2(evt)
{
	return ( ( (evt.which) && (evt.which==1) ) || ( (evt.button) && (evt.button==1) ) );
}

function stopEventTransfer2(evt)
{
	if (evt.preventDefault)
	{
		evt.preventDefault();
		evt.stopPropagation();
	}
	else
	{
		evt.returnValue = false;
		evt.cancelBubble = true;
	}
}

function setEventCapture2(target)
{
	if (target.setCapture)
		target.setCapture();
	else if (window.captureEvents)
		window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP|Event.MOUSEDOWN);
}

function releaseEventCapture2(target)
{
	if (target.releaseCapture)
		target.releaseCapture();
	else if (window.releaseEvents)
		window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP|Event.MOUSEDOWN);
}

function isNULL2(e)
{
	if (e==undefined || e==null)
		return true;

	return false;
}


function moveLayerTo2(layer, x, y)
{
	if (isMinIE42)
	{
		layer.style.left = x;
		layer.style.top  = y;
	}
	else if (isMinNS52)
	{
		layer.style.left = x+'px';
		layer.style.top  = y+'px';
	}
	else if (isMinNS42)
    	layer.moveTo(x, y);
}

function getPageLeft2(layer)
{
	if (isMinIE42||isMinNS52)
		return (layer.offsetLeft);
	else if (isMinNS42)
		return (layer.pageX);
	
	return (-1);
}

function getPageTop2(layer)
{
	if (isMinIE42||isMinNS52)
		return (layer.offsetTop);
	else if (isMinNS42)
		return (layer.pageY);
	
	return (-1);
}

function getPageScrollX2()
{
	if (isMinIE42||isMinNS52)
		return (g_myBodyInstance2.scrollLeft);
	else if (isMinNS42)
		return (window.pageXOffset);
	
	return (-1);
}

function getPageScrollY2()
{
	if (isMinIE42||isMinNS52)
		return (g_myBodyInstance2.scrollTop);
	else if (isMinNS42)
		return (window.pageYOffset);
	
	return (-1);
}

function getWidth2(layer)
{
	if (isMinIE42||isMinNS52)
	{
		if (layer.style.pixelWidth)
			return (layer.style.pixelWidth);
		else
			return (layer.clientWidth);
	}
	else if (isMinNS42)
	{
		if (layer.document.width)
			return (layer.document.width);
		else
			return (layer.clip.right - layer.clip.left);
	}
	
	return (-1);
}

function getHeight2(layer)
{
	if (isMinIE42||isMinNS52)
	{
		if (false && layer.style.pixelHeight)
			return (layer.style.pixelHeight);
		else
			return (layer.clientHeight);
	}
	else if (isMinNS42)
	{
		if (layer.document.height)
			return (layer.document.height);
		else
			return (layer.clip.bottom - layer.clip.top);
	}
	
	return(-1);
}

function getWindowWidth2()
{
	if (isMinIE42||isMinNS52)
		return (g_myBodyInstance2.clientWidth);
	else if (isMinNS42)
		return (window.innerWidth);
	
	return (-1);
}

function getWindowHeight2()
{
	if (isMinIE42||isMinNS52)
		return (g_myBodyInstance2.clientHeight);
	else if (isMinNS42)
		return(window.innerHeight);
	
	return(-1);
}
