var defUrl = "../news/"; 
var description;
/*	var param = location.search;
	param = param.substring(1);
	var pair = param.split("&");
	var i = "";
	var tmp = "";
	var key = new Array();
	for (i = 0; i < pair.length; i++) {
		tmp = pair[i].split("=");
		keyName = tmp[0];
		keyValue = tmp[1];
		key[keyName] = keyValue;
	}*/
var filesplit = location.pathname.split("/");
var htmlname = filesplit[(filesplit.length-1)];
var htmlsplit = htmlname.split(".");
var idtemp = htmlsplit[0];
$(function(){
	var param = {
		"newsid" : idtemp
	};
	$.postJSON("../PHP/json.php/News/selectNewsDetail", param,function(json) {
		description = json.description;
		$.postJSON("../PHP/json.php/Index/selectPlayer",function(json) {
			var testsearch = [];
			var testparam = {};
			for(var i=0; i < json.length; i++){
				testparam[i] = {
					"keyword": json[i].playernamekana,
					"url": "../player/" + json[i].playerid + ".html"
				}
				testsearch[i] = testparam[i];
			}
//			alert(testsearch["keyword"]);
			
/*			for(var i=0; i < json.length; i++){
				alert(json[i].playernamekana);
				testsearch['keyword'] = json[i].playernamekana;
				testsearch['url'] = "../news/news.html?newsid=" + json[i].playerid;
			}*/
			var html ="";
				html += ""+description.linkKeywords(testsearch)+""
			$("#newsdescription").html(html);
		});
	
		var html ="";
			html += "<img class='mainarticleimage' src='../assets/images/app/news/"+json.news_id+"-"+json.time_id+".jpg'  alt='' width='510px' height='250px' />"
		$("#mainImg").html(html);
		
/*		var html ="";
			html += ""+json.title+""
		$("#newsTitle").html(html);
		document.title = ""+json.title+"";
*/		$("#breadNews").text(json.title)		

		var html ="";
			html += ""+json.img_caption+""
		$("#caption-no-border").html(html);
		sdate = json.news_date.split("-");
		yyyy = sdate[0];
		var html ="";
			html += ""+yyyy+""
		$("#newsyear").html(html);

		mm = sdate[1];
		var html ="";
			html += ""+mm+""
		$("#newsmonth").html(html);

		dd = sdate[2];
		var html ="";
			html += ""+dd+""
		$("#newsdate").html(html);
		
		if(json.match_id != null&&json.match_id != 0){
			selectNewsMatch(json.match_id)
		}else{
//			selectNewsMatch();
		}
		$(".caption").slideDown(400)
		selectNews();
	});
});

function selectNews(){
	var param = {
		"newsid" : idtemp
	};
	$.postJSON("../PHP/json.php/News/selectNews",param, function(json) {
		var html = "";
		maxpage = Math.ceil(json.length/20);
		for(var i = 0; i < json.length; i++) {
			html += "<div class='news-item'>";
			html += "<p class='news-date'>"+json[i].news_date+"</li>";
			html += "<p class='news-title'><a href='"+defUrl + json[i].newsid+".html'>"+json[i].title+"</a></li>";
			html += "</div>";
		}
		$(".newsList").html(html);
	});
}
function selectNewsMatch(id){
	var param = {
		"matchid" : id
	}
	$.postJSON("../PHP/json.php/News/selectNewsMatch",param, function(json) {
		var html = "";
		matchDate = json.match_date.split("-");
		matchY = parseInt(matchDate[0],10);
		matchM = parseInt(matchDate[1],10);
		matchD = matchDate[2];
		var matchTime 	= json.start_time.split(":");
			matchHou 	= matchTime[0]
			matchMin 	= matchTime[1]
		mm = parseInt(matchM,10);
		matchD = matchDate[2];
			html +="<div class='mainarticletext-internal'>"
			html += "<img id='competition' src='../assets/images/app/league/"+json.league_id+".jpg' height='45' width='90' />";
			html += "<img id='homeside' src='../assets/images/crests/arsenal.png' height='45' width='45' alt='Arsenal' title='Arsenal' />";
			html += "<a href='../match/"+id+".html'><img class='matchmenubutton' src='../assets/images/matchinfo/matchmenu.gif' height='45' width='90' alt='Match Menu' /></a>";
			html += "<img id='awayside' src='../assets/images/app/teamlogo/"+json.team_id+".jpg' height='45' width='45'/>";
			html += "<div class='naClearFix' >";
			html += "<p id='title-w-caption'>アーセナル "+json.get_point+" - "+json.lost_point+"  "+json.team_name+" </p>";
			html += "<p>"+json.league_name+"</p>";
			html += "<p>"+matchY+"年"+mm+"月"+matchD+"日"+matchHou+":"+matchMin+"</p>";
			html += "</div>";
			html += "</div>";
	$("#newsMatch").html(html);
	$("#newsMatch").slideDown(600);
	});
}



