// JavaScript Document
document.domain = "hinet.net"; 
function getLastNews(){
	$.getJSON("Scripts/lastNews.js",
        function(data){
		  var i =0;
		  var targetDiv = 'newsRow1';
          $.each(data.items, function(i,item){
			var shortTile = item.title.toString().length > 18 ? item.title.toString().substr(0,18) + '...' :item.title.toString();
			
			if(i>4){
				targetDiv = 'newsRow2';
			}
            $('#' + targetDiv + ' ul').append("<li><a href=\"news_detail.jsp?s=" +  item.sn + "\" title=\"" + item.title +  "\">" + shortTile + "</a></li>\n");
          });
        });

}

function getRightNews(){
	$.getJSON("Scripts/lastNews.js",
        function(data){
		  var append = "<ul>\n";
		  var targetDiv = 'rightNewsList';
          $.each(data.items, function(i,item){
			var shortTile = item.title.toString().length > 18 ? item.title.toString().substr(0,18) + '...' :item.title.toString();
			if(i<5){
				append += "<li><a href=\"news_detail.jsp?s=" +  item.sn + "\" title=\"" + item.title +  "\">" + shortTile + "</a></li>\n";
			}
          });
		  append += "</ul>\n";
		  $('#' + targetDiv).html(append);
        });

}

function getIndexNews(){
	$.getJSON("Scripts/lastNews.js",
        function(data){
		  var append = "<ul id=\"newslist\">\n";
		  var targetDiv = 'indexNews';
          $.each(data.items, function(i,item){
			var shortTile = item.title.toString().length > 35 ? item.title.toString().substr(0,35) + '...' :item.title.toString();
			if(i<5){
				append += "<li><div style=\"float:left;\"><a href=\"news_detail.jsp?s=" +  item.sn + "\" title=\"" + item.title +  "\">" + shortTile + "</a></div><div style=\"float:right;\">" +  item.creator + " | " +  item.pubdate + "</div></li>\n";
			}
          });
		  append += "</ul>\n";
		  $('#' + targetDiv).html(append);
        });

}

var clientAns = new Array();

function getQuest(qid){
	var optAns = $('input[@name=ansOpt][@checked]').val();
	if(qid != 1){
		if(!optAns){
			alert('請選取您的答案');
			return false;
		}
		clientAns.push(optAns);
	}
	var per = Math.ceil((parseInt(qid, 10) - 1 )*12.5);
	var barImg = document.getElementById("barImg");
	$('#ansProcess').text(per+"%");
	barImg.width=(parseInt(qid , 10) - 1 )*25;
	if(qid <= 8){
		$.getJSON("Scripts/qaJson.js",
			function(data){
			  $('#questArea').html(data.items[qid-1].title+'？');
			  $('#ansArea').html('');
			  var ansLength = data.items[qid-1].ans.length;
			  
			  $.each(data.items[qid-1].ans, function(i,item){
				$('#ansArea').append('<input type="radio" name="ansOpt" id="ansOpt" value="' + item.opt+  '" />' + "\n" +item.title);
				if(ansLength-1>i){
					$('#ansArea').append('<br/>');
				}
			  });
			}
		);
		$('#questNum').val(parseInt(qid,10) +1);
	}else{
		var appendText = null;
		var ansImg = new Array();
		if(clientAns[3] == "B" && clientAns[4] == "C" && clientAns[5] == "A" && clientAns[6] == "B" && clientAns[7] == "B"){
			ansImg.push("1");
		}else if(clientAns[3] == "B" && clientAns[4] == "C" && clientAns[5] == "B" && clientAns[6] == "A" && clientAns[7] == "B"){
			ansImg.push("2");
		}else if(clientAns[3] == "B" && clientAns[4] == "C" && clientAns[5] == "B" && clientAns[6] == "B" && clientAns[7] == "A"){
			ansImg.push("3");
		}else if(clientAns[3] == "A" && clientAns[4] == "C" && clientAns[6] == "A" && clientAns[7] == "B"){
			ansImg.push("4");
		}else if(clientAns[3] == "A" && clientAns[4] == "C" && clientAns[6] == "B" && clientAns[7] == "A"){
			ansImg.push("5");
		}else if(clientAns[3] == "A" && clientAns[4] == "B" && clientAns[6] == "A"){
			ansImg.push("6");
		}else if(clientAns[3] == "A" && clientAns[4] == "A"){
			ansImg.push("7");
		}else if(clientAns[3] == "B" && clientAns[4] == "C" && clientAns[5] == "A" && clientAns[6] == "A" && clientAns[7] == "B"){
			ansImg.push("1");
			ansImg.push("2");
		}else if(clientAns[3] == "B" && clientAns[4] == "C" && clientAns[5] == "A" && clientAns[6] == "B" && clientAns[7] == "A"){
			ansImg.push("1");
			ansImg.push("3");
		}else if(clientAns[3] == "B" && clientAns[4] == "C" && clientAns[5] == "B" && clientAns[6] == "A" && clientAns[7] == "A"){
			ansImg.push("2");
			ansImg.push("3");
		}else if(clientAns[3] == "A" && clientAns[4] == "C" && clientAns[6] == "A" && clientAns[7] == "A"){
			ansImg.push("4");
			ansImg.push("5");
		}else{
			ansImg.push("1");
			ansImg.push("4");
			appendText = "光世代最新優惠、ADSL最新優惠";
		}
		$.each(ansImg , function(i , item){
								 MM_preloadImages("images/type" + item.toString() + ".gif");
								 $('#sugest'+i).append('<img src="images/type' + item.toString() + '.gif" />');
								 if(appendText==null){
								 	appendText = parseInt(item.toString() , 10) <=3 ? '光世代最新優惠':'ADSL最新優惠';
								 }
								 });
		$('#finalTextDesc').text(appendText);
		$('#finalDiv').show();
		$('#questDiv').hide();
	}
}

function resetOpt(){
	clientAns = new Array();
	$('#questNum').val("1");
	getQuest(1);
	$('#finalDiv').hide();
	$('#questDiv').show();
	$('#sugest0').html('');
	$('#sugest1').html('');
}

MM_preloadImages('images/tag_vote1o.gif','images/tag_vote2o.gif','images/tag_vote1.gif','images/tag_vote2.gif','images/tag_vote1n.gif','images/tag_vote2n.gif');

function changeVoteArea(aid){
	///歷史資料
	if(aid == 1){
		$('#setBtn1').html("<a href=\"javascript:void(0);\" onclick=\"changeVoteArea(0);\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('voteBtn1','','images/tag_vote1o.gif',1)\"><img src=\"images/tag_vote1.gif\" name=\"voteBtn1\" width=\"85\" height=\"22\" border=\"0\" id=\"voteBtn1\" /></a>");
		$('#setBtn2').html('<img src="images/tag_vote2n.gif" width="85" height="22" />');
		$('#vote').hide();
		$('#hisVote').show();
	}else{
		$('#setBtn2').html("<a href=\"javascript:void(0);\" onclick=\"changeVoteArea(1);\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('voteBtn1','','images/tag_vote2o.gif',1)\"><img src=\"images/tag_vote2.gif\" name=\"voteBtn2\" width=\"85\" height=\"22\" border=\"0\" id=\"voteBtn2\" /></a>");
		$('#setBtn1').html('<img src="images/tag_vote1n.gif" width="85" height="22" />');
		$('#hisVote').hide();
		$('#vote').show();
	}
}

function moveMenuUp(){
	$('#menu').css('z-index' , '5');
	$('#menu').css('height' , '105px');
}
	
function moveMenuDown(){
	$('#menu').css('z-index' , '1');
	$('#menu').css('height' , '55px');
}