function messages () {
	this.msgQueue = new Array();//存放訊息的陣列
	this.msgInterval = 1;//訊息輪播的間隔
	this.msgCurrent = 0;//目前播放的訊息
	this.msgTarget = "";//顯示訊息的物件id
	this.intervalId = null;//用來存放interval或是timer的id，以便做出暫停的效果
	this.pauseState = 0;//存放播放的狀態，用來判斷是在播放或是暫停
}

messages.prototype.addMsg = function (msg) {//新增訊息到訊息陣列
	this.msgQueue.push(msg);
}

messages.prototype.getMsgInterval = function () {//取得訊息播放的間隔
	return this.msgInterval;
}

messages.prototype.setMsgInterval = function (interval) {//設定訊息播放的間隔
	this.msgInterval = interval;
}

messages.prototype.setMsgTarget = function (id) {//設定訊息播放的目標id
	this.msgTarget = id;
}

messages.prototype.getMsgTarget = function () {//取得訊息播放的目標id
	return this.msgTarget;
}
function dynamicMsg () {
	this.container = document.createElement("div");//訊息將在這個div中顯示
	this.roller1 = document.createElement("div");//用兩個div頭接尾做出不間斷播放的效果
	this.roller2 = document.createElement("div");
	this.currentPos = 0;//以下用來存放訊息移動的位置
	this.rollerTop = 0;
	this.rollerLeft = 0;
	this.rollerRight = 0;
	this.rollerBottom = 0;
}
dynamicMsg.prototype = new messages;//繼承messages物件

dynamicMsg.prototype.display = function (width, height, border, delay, color) {
	try{
		if (this.msgQueue.length == 0) throw "No message in queue!!!";
		if (this.msgTarget == "") throw "You must assign an object id for displaying messages!!!";
		var obj = document.getElementById(this.msgTarget);
		if (obj == null) throw "Object with the assigned id:"+this.msgTarget+" cannot be found!!!";
		var callobj = this;//把this指標存到變數，等一下才有辦法讓網頁的事件來使用
		obj.innerHTML = "";
		obj.appendChild(this.container);
		var img = document.createElement("img");//用一個透明的gif把container撐到指定的大小
		img.src = "images/blank.gif";
		img.height = height;
		img.width = width;
		img.border = "0";
		obj.appendChild(img);
		this.container.style.position = "absolute";//position必須設為absolute，clip才會發生作用
		this.container.style.width = width+"px";
		this.container.style.height = height+"px";
		this.container.style.border = "solid "+border+"px";
		this.container.style.background = color;
		this.container.style.overflow = "hidden";
		this.container.style.clip = "rect(0,"+width+","+height+",0)";
		this.container.innerHTML = "";
		this.container.onmousemove = function(){callobj.pause(1);};
		this.container.onmouseout = function(){callobj.pause(0);};
		var tmp = "";
		for (i=0;i<this.msgQueue.length;i++) {//把訊息陣列中的訊息取出，一則訊息一行來顯示
			tmp += this.msgQueue[i];
			if (i != this.msgQueue.length-1) {
				tmp += "<br>";
			}
		}
		this.roller1.style.top = "0px";
		this.roller1.innerHTML = "";
		this.roller1.innerHTML = tmp;
		this.roller1.style.position = "relative";
		this.container.appendChild(this.roller1);
		this.rollerTop = this.roller1.offsetTop;
		this.rollerLeft = this.roller1.offsetLeft;
		this.rollerRight = this.rollerLeft + this.roller1.offsetWidth;
		this.rollerBottom = this.rollerTop + this.roller1.offsetHeight;
		this.currentPos = 0;
		this.container.appendChild(this.roller2);
		this.roller2.innerHTML = "";
		this.roller2.innerHTML = tmp;
		this.roller2.style.position = "relative";
		this.roller2.style.top = "0px";
		this.msgInterval = delay;
		this.intervalId = window.setInterval(function(){callobj.rotate();},this.msgInterval*100);
	}
	catch (e) {
		alert(e);//顯示例外的訊息
	}
}

dynamicMsg.prototype.rotate = function () {//做出向上捲動的效果
	this.roller1.style.top = --this.currentPos + "px";
	this.roller2.style.top = this.currentPos + "px";
	if ((this.currentPos + this.roller1.offsetHeight) == 0) {
		this.currentPos = 0;
	}
}

dynamicMsg.prototype.pause = function (status) {//做出暫停/繼續播放的效果
	if (status == 1) {
		this.pauseState = status;
		clearInterval(this.intervalId);
		return;
	}
	if (status == 0) {
		this.pauseState = status;
		var callobj = this;
		this.intervalId = setInterval(function(){callobj.rotate();},this.msgInterval*100);
		return;
	}
}
//資料參考來源 http://www.ithome.com.tw/plog/index.php?op=ViewArticle&articleId=892&blogId=257


//登入用

//-----------------------Login start -----------------//

function loginUser_action(obj_frm, msg_div, schid , pk_act , page, type)
{
     
     if (gid(msg_div))  gid(msg_div).innerHTML='';

	
	var account_value = StrCode(obj_frm.account.value);
	var passwd_value = StrCode(obj_frm.passwd.value);
      $j.ajax({
		  type: "POST",
		  url: "/cgi/proc_signin_pk.php",
		  data: { 'account':account_value , 'passwd':passwd_value},
		  success: function(msg)
			  {	//alert(msg);
				   if(msg == 'PKOK' || msg == 'OK') {
                       
                       	if(pk_act == 'rankadd' && msg == 'PKOK')  addSchool(schid, '1'  , page , '0', '0') ;
                       	if(pk_act == 'rankadd' && msg == 'OK')  addSchool(schid, '1'  , page , '0', '1') ;
                       	if(pk_act == 'school' && msg == 'PKOK')  addSchool(schid, '1'  , page , '1', '0') ;
                       	if(pk_act == 'school' && msg == 'OK')  addSchool(schid, '1'  , page , '1', '1') ;                        	
                       	if(pk_act == 'sch_info' && msg == 'PKOK')  addSchool(schid, '1'  , page , '3', '0') ;
                       	if(pk_act == 'sch_info' && msg == 'OK')  addSchool(schid, '1'  , page , '3', '1') ;
                       	if(pk_act == 'bid' )  
                       		document.location.href = "/action/2008_schoolpk/bid/?id="+schid
                       	
                       	if(pk_act == 'push') document.location.href = "/action/2008_schoolpk/push/?schid="+schid;
                       	
						if( ! pk_act ) document.location.reload();
					} else if(gid(msg_div)) gid(msg_div).innerHTML="帳號、密碼錯誤";
						    	
		     }
	});	
	return false;
}


//ipeen登入顯示頁
function dialogLogin_action(schid , pk_act , page, type)
{
	DialogHide();	//close other dialog

	var def_acc = getCookie('remacc');
	var div_str = '';
div_str = div_str+'   <div id="login_ipeen">   ';
div_str = div_str+'<form method="post" name="logform" id="logform" action="" onsubmit="return loginUser_action(logform , \'div_login_msg\' , '+schid+' , \''+pk_act+'\' , '+page+');">';
div_str = div_str+'	<div class="login_close"><img onclick="DialogHide();" src="/images/v2/action/2008_schoolpk/login/login_close.gif" border="0" style="cursor:pointer;"/></div> ';
div_str = div_str+'         <div class="login_ipeenlogo">  ';
div_str = div_str+'   	請輸入iPeen愛評網帳號、密碼 ';
div_str = div_str+'  </div> ';
div_str = div_str+'    <div class="login_enter"> ';
div_str = div_str+'    	帳號：<input type="text" name="account" id="account" value="'+def_acc+'"  size="30" /> ';
div_str = div_str+'  </div> ';
div_str = div_str+'   <div class="login_enter"> ';
div_str = div_str+'    	密碼： ';
div_str = div_str+'    	<input type="password" name="passwd" id="passwd" value="" size="" /> ';
div_str = div_str+'  <a class="a36" href="http://www.ipeen.com.tw/signin/forget.php">忘記密碼</a>  </div> ';
div_str = div_str+'   <div id="div_login_msg" class="login_error"></div> ';
div_str = div_str+'    <div align="center"> ';
div_str = div_str+'   <input type="submit" value="登入"/>';
//div_str = div_str+'   <a href="#" onmouseout="MM_swapImgRestore()" onclick="return loginUser_action(logform , \'div_login_msg\' , '+schid+' , \''+pk_act+'\' , '+page+');" onmouseover="MM_swapImage(\'Image7\',\'\',\'/images/v2/action/2008_schoolpk/login/login_btn_over.gif\',0)"><img src="/images/v2/action/2008_schoolpk/login/login_btn.gif" name="Image7" width="102" height="36" border="0" id="Image7" /></a>    </div> ';
div_str = div_str+'    <div align="center" style="margin-top:5px;"><a class="a38" href="http://www.ipeen.com.tw/register/register.php">免費註冊</a></div> ';
div_str = div_str+'   </form>';
div_str = div_str+'    </div> ';
var browser = new Browser();
if(browser.isIE && document.readyState != "complete"){	
   setTimeout(function () {dialogLogin_action(schid , pk_act , page, type);},100); return false;}
else{DialogShowByData(div_str, 320,320); }
	document.getElementById('account').focus();
//DialogShowByData(div_str,320,320);
	return false;
}

//-----------------------Login end -----------------//

//連署力挺用

function addSchool(schid, islogin, page, intype , intro)
{
	var type = intype ? intype : 0;
	if (islogin=='0')
	{
		if( type == 0) pkact = 'rankadd';
		else if(type == 1 ) pkact = 'school';
		else if(type == 3) pkact = 'sch_info';
		else pkact = '';
		dialogLogin_action(schid , pkact , page);
	}else
	{   
		 $j.ajax({
		  type: "POST",
		  url: "/action/2008_schoolpk/proc_schoolPK_united.php",
		  data: { 'sch_id':schid },
		  success: function(msg)
			  {	  
                  if( msg != '' && msg != 'PKNO' && intro > '0' ) {
                  	  
					    var str = '';
						str = str +'<div id="login_ipeen">';
                        str = str +'<div id="login_cosign">';
	                    str = str +'<div class="login_close"><img onclick="addSchool_close('+schid+', ' + type +');" src="/images/v2/action/2008_schoolpk/login/login_close.gif" border="0" style="cursor:pointer;"/></div>';
	                    str = str +'<div class="login_succes">';
    	                str = str +'恭喜你完成&nbsp;'+msg+'&nbsp;連署囉!!<br/>';
                        str = str +'</div>';
                        str = str +'<div class="login_notice">';
    	                str = str +'<table width="280" border="0" cellspacing="5" cellpadding="0">';
                        str = str +'<tr>';
                        str = str +'<td width="10" align="left" valign="baseline"><img src="/images/v2/action/2008_schoolpk/login/icon.gif" width="10" height="10" /></td>';
                        str = str +'<td width="249">快拉同學來力挺，賺食力金，憑實力競標好禮</td>';
                        str = str +'</tr>';
                        str = str +'<tr>';
                        str = str +'<td width="10" align="left" valign="baseline"><img src="/images/v2/action/2008_schoolpk/login/icon.gif" width="10" height="10" /></td>';
                        str = str +'<td>發表食力文，前100名累積滿30篇送好禮，還<br />';
                        str = str +'有機會當選食力王&nbsp;&nbsp;<a class="a37" href="account.php#a4">點我看更多詳細說明&gt;</a></td>';
                        str = str +'</tr>';
                        str = str +'</table>';
                        str = str +'</div>';
                        str = str +'</div>';
                        str = str +'</div>';
						    	
						DialogShowByData(str,320,320);
						    	
				   } else if( msg != '' && msg != 'PKNO' && intro == '0' ) {
				   	  
					    var str = '';
						str = str +'<div id="login_ipeen">';
                        str = str +'<div id="login_cosign">';
	                    str = str +'<div class="login_close"><img onclick="addSchool_close('+schid+', ' + type +');" src="/images/v2/action/2008_schoolpk/login/login_close.gif" border="0" style="cursor:pointer;"/></div>';
						
	                    str = str +'<div class="login_succes_a">';
    	                str = str +'恭喜你完成&nbsp;'+msg+'&nbsp;連署囉!!<br/>';
                        str = str +'</div>';
						str = str+'<form method="post" name="introform" id="introform" action="" onsubmit="return intro_action(introform , \'div_intro_msg\' , '+schid+', ' + type +');">';
str = str+'    <div class="login_enter"> ';
str = str+'    	介紹人帳號：<input type="text" name="introaccount" id="introaccount" value=""  size="15" /> ';
str = str+'  </div> ';
str = str+'   <div id="div_intro_msg" class="login_error">若無介紹人，可直接關閉視窗</div> ';
str = str+'   <div align="center"><input type="submit" value="輸入"/></div>';
str = str+'   </form>';
                        str = str +'<div class="login_notice">';
    	                str = str +'<table width="280" border="0" cellspacing="5" cellpadding="0">';
                        str = str +'<tr>';
                        str = str +'<td width="10" align="left" valign="baseline"><img src="/images/v2/action/2008_schoolpk/login/icon.gif" width="10" height="10" /></td>';
                        str = str +'<td width="249">快拉同學來力挺，賺食力金，憑實力競標好禮</td>';
                        str = str +'</tr>';

                        str = str +'<tr>';
                        str = str +'<td width="10" align="left" valign="baseline"><img src="/images/v2/action/2008_schoolpk/login/icon.gif" width="10" height="10" /></td>';
                        str = str +'<td>發表食力文，前100名累積滿30篇送好禮，還<br />';
                        str = str +'有機會當選食力王&nbsp;&nbsp;<a class="a37" href="account.php#a4">點我看更多詳細說明&gt;</a></td>';
                        str = str +'</tr>';
                        str = str +'</table>';
                        str = str +'</div>';
                        str = str +'</div>';
                        str = str +'</div>';
						    	
						DialogShowByData(str,320,320);
						    	
				   } 
				   else {

				   var str = '';
						str = str +'<div id="login_ipeen">';
                        str = str +'<div id="login_cosign">';
	                    str = str +'<div class="login_close"><img onclick="addSchool_close('+schid+', ' + type +');" src="/images/v2/action/2008_schoolpk/login/login_close.gif" border="0" style="cursor:pointer;"/></div>';
	                    str = str +'<div class="login_succes">';
    	                str = str +'你已經連署過這間學校囉!!';
                        str = str +'</div>';
                        str = str +'<div class="login_notice">';
    	                str = str +'<table width="280" border="0" cellspacing="5" cellpadding="0">';
                        str = str +'<tr>';
                        str = str +'<td width="10" align="left" valign="baseline"><img src="/images/v2/action/2008_schoolpk/login/icon.gif" width="10" height="10" /></td>';
                        str = str +'<td width="249">快拉同學來力挺，賺食力金，憑實力競標好禮</td>';
                        str = str +'</tr>';
                        str = str +'<tr>';
                        str = str +'<td width="10" align="left" valign="baseline"><img src="/images/v2/action/2008_schoolpk/login/icon.gif" width="10" height="10" /></td>';
                        str = str +'<td>發表食力文，前100名累積滿30篇送好禮，還<br />';
                        str = str +'有機會當選食力王&nbsp;&nbsp;<a class="a37" href="account.php#a4">點我看更多詳細說明&gt;</a></td>';
                        str = str +'</tr>';
                        str = str +'</table>';
                        str = str +'</div>';
                        str = str +'</div>';
                        str = str +'</div>';
						    	
						DialogShowByData(str,320,320);
				   }	

		     }
	    });	
	}
}

function intro_action(obj_frm, msg_div, p, intype)
{
     
     if (gid(msg_div))  gid(msg_div).innerHTML='';

	var account_value = StrCode(obj_frm.introaccount.value);
      $j.ajax({
		  type: "POST",
		  url: "/action/2008_schoolpk/proc_schoolPK_intro.php",
		  data: { 'introaccount':account_value },
		  success: function(msg)
			  {	//alert(msg);
				   if(msg == 'OK') { addSchool_close(p, intype );}
				   else if(gid(msg_div)) gid(msg_div).innerHTML="介紹人帳號錯誤";
						    	
		     }
	});	
	return false;
}


function addSchool_close(schid, intype) {
	
    DialogHide();
	var loader = new I_PageLoader({'target_id':'#container'});
	if( intype == 0 )
	{
		document.location.href = '/action/2008_schoolpk/school/?schid='+schid;
		//loader.load('/action/2008_schoolpk/rank_school.php?p='+p);
	}
	else if( intype == 1)
	{
		document.location.href = '/action/2008_schoolpk/school/?schid='+schid;
	}
	else document.location.reload();
} 

function addPush(schid, islogin) {

	if (islogin=='0')  dialogLogin_action(schid , 'push');
	else
	{ 
		$j.get('/', function() {
		document.location.href = "/action/2008_schoolpk/push/?schid="+schid; } );	
	}
}

function addBid(schid, islogin) {
	if (islogin=='0')  dialogLogin_action(schid , 'bid');
	else document.location.href = "/action/2008_schoolpk/bid/?schid="+schid;

}

//投票用

function vote_shop(sid, store_title, store_subname , p , show , bys , schid)
{
	var str = '';
	var def_content="";
	if (store_subname!='')
	{
		def_content = store_title +"("+store_subname+")";
	}else
	{
		def_content = store_title;
	}
    str = str +'<div id="login_ipeen">';
    str = str +'<form method="post" name="voteform" id="voteform" >';
	str = str +'<div class="login_close"><img src="/images/v2/action/2008_schoolpk/login/login_close.gif" border="0"  onClick="DialogHide();" style="cursor:pointer;" /></div>';
    str = str +'<div class="wanteat_shop">';
   	str = str +'我最想吃<label class="allschool_cosignnum">'+def_content+'</label>';
    str = str +'</div>';
	str = str +'<div class="login_enter">';
    str = str +'真實姓名：';
    str = str +'<input class="login_enterframe" name="v_name" type="text"  style="width:150px;" />';
  	str = str +'</div>';
    str = str +'<div class="login_enter">';
    str = str +'手機電話：';
    str = str +'<input class="login_enterframe" name="v_tel" type="text"  style="width:150px;"/>';
    str = str +'</div>';
    str = str +'<div class="login_enter">';
    str = str +'E-mail：';
    str = str +'<input class="login_enterframe" type="text"  style="width:200px;" name="v_e_pre" />';
    str = str +'</div>';
    str = str +'<div align="center">';
    str = str +'<input type="hidden" name="s_id" id="s_id" value="'+sid+'"/>';
    str = str +'<input type="hidden" name="p" id="p" value="'+p+'"/>';
    str = str +'<a href="javascript:;" onclick="return chk_frm_vote(voteform , \''+show+'\' , \''+bys+'\' , \''+schid+'\');" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'Image7\',\'\',\'/images/v2/action/2008_schoolpk/login/login_btn_over.gif\',0)"><img src="/images/v2/action/2008_schoolpk/login/login_btn.gif" name="Image7" width="102" height="36" border="0" id="Image7" /></a>';
    str = str +' </div>';
    str = str +'<div class="wanteat_notice">';
    str = str +'參與投票者皆可參與抽獎，請留下真實姓名及可以聯絡到的電話和E-MAIL，以便我們日後將獲獎項目告知您，若因填寫錯誤或不實資料而導致無法取得聯繫者，主辦單位不負任何責任。';
    str = str +'</div>';
    str = str +'</form>';
    str = str +'</div>';



	return DialogShowByData(str,320,320);
}

function chk_frm_vote(obj_frm , show , bys , schid)
{

	var result = true;
	var err_msg = "";

  if (obj_frm.v_name.value == ""){
   err_msg = err_msg + " - 請填寫聯絡姓名!\n\n";
   }

  if (obj_frm.v_tel.value == ""){
   err_msg = err_msg + " - 請填寫手機電話!\n\n";
   }
   
	if (obj_frm.v_e_pre.value =="" )
	{
			err_msg = err_msg + " - 請填寫Email!\n\n";
	}else
	{
		var email = obj_frm.v_e_pre.value;
		if (!IsEmail(email))
		{
			err_msg = err_msg + " - 請填寫正確的Email!\n\n";
		}
	}
	


	if (err_msg !='')
	{
		alert(err_msg);
	}else
	{
		SendAddVote(obj_frm , show , bys , schid);
	}
	
	return false;
}

function SendAddVote(obj_frm , show , bys , schid) {
	
	var s_id = obj_frm.s_id.value;
	var v_name = obj_frm.v_name.value;
	var v_tel = obj_frm.v_tel.value;
	var v_e_pre = obj_frm.v_e_pre.value;
	var p = obj_frm.p.value;
      $j.ajax({
				type: "POST",
				        url: "/action/2008_schoolpk/proc_add_vote.php",
				        data: { "v_name":v_name, "v_tel":v_tel , "v_e_pre":v_e_pre, "sid":s_id },
				        success: function(msg)
						{	
						    if(msg == 'OK') {
						    	 var str = '';
						    	str = str +'<div id="login_ipeen">';
	                            str = str +'<div class="login_close"><img onclick="vote_store('+p+' , \''+show+'\' , \''+bys+'\' , \''+schid+'\');" src="/images/v2/action/2008_schoolpk/login/login_close.gif" border="0"style="cursor:pointer;"/></div>';
                                str = str +'<div class="wanteat_message">';
    	                        str = str +'<label class="allschool_cosignnum">恭喜你完成投票囉!</label>';
                                str = str +'<br/>快再看有沒有其他想吃的好料吧!';
                                str = str +'</div>';
                                str = str +'</div>';
						    	
						    	DialogShowByData(str,320,320);
						    	
						    	
						    }								
						    if(msg == 'TIMEOUT') {
						    	 var str = '';
						    	str = str +'<div id="login_ipeen">';
	                            str = str +'<div class="login_close"><img onclick="vote_store('+p+' , \''+show+'\' , \''+bys+'\' , \''+schid+'\');" src="/images/v2/action/2008_schoolpk/login/login_close.gif" border="0" style="cursor:pointer;"/></div>';
                                str = str +'<div class="wanteat_message">';
    	                        str = str +'<label class="allschool_cosignnum">Ooops~您剛剛已經投過這間店囉~</label>';
                                str = str +'<br/>快再看有沒有其他想吃的好料吧!';
                                str = str +'</div>';
                                str = str +'</div>';
						    	
						    	DialogShowByData(str,320,320);
						    	
						    	
						    }	
						}
			});	
}

function vote_store(p , show, bys, schid) {
	rschid = schid ? schid : 0;
    DialogHide();
	loader = new I_PageLoader({'target_id':'#container'});
	if(rschid == '0') loader.load('/action/2008_schoolpk/rank_shop.php?p='+p);
	else loader.load('/action/2008_schoolpk/school/?act=list&schid='+rschid+'&p='+p+'&by='+bys+'&show='+show);
} 

