var fontSize = 12;
var lineHeight = 20;
var dankan = 24;
var WINDOW_HEIGHT = $(window).height();
var TITLE_MAX_CHAR = 250;
var REST = '…';
var layoutID;
var thickBoxShinbunColArr = new Array();
var thickBoxTitleArr= new Array();
var thickBoxMetaArr= new Array();

var chVerArr = new Array();
chVerArr[0] = '（';
chVerArr[1] = '︵';
chVerArr[2] = '）';
chVerArr[3] = '︶';
chVerArr[4] = '｛';
chVerArr[5] = '︷';
chVerArr[6] = '｝';
chVerArr[7] = '︸';
chVerArr[8] = '「';
chVerArr[9] = '﹁';
chVerArr[10] = '」';
chVerArr[11] = '﹂';
chVerArr[12] = '『';
chVerArr[13] = '﹃';
chVerArr[14] = '』';
chVerArr[15] = '﹄';
chVerArr[18] = 'ー';
chVerArr[19] = '︱';
chVerArr[20] = '…';
chVerArr[21] = '︰';
chVerArr[22] = '(';
chVerArr[23] = '︵';
chVerArr[24] = ')';
chVerArr[25] = '︶';
chVerArr[26] = '{';
chVerArr[27] = '︷';
chVerArr[28] = '}';
chVerArr[29] = '︸';
chVerArr[30] = '[';
chVerArr[31] = '︵';
chVerArr[32] = ']';
chVerArr[33] = '︶';
chVerArr[34] = '【';
chVerArr[35] = '︵';
chVerArr[36] = '】';
chVerArr[37] = '︶';


$(window).resize(function(){

	switch(layoutID) {
		case "portal":			
			var sidebar = $("div#sidebar");
			var bodyHeight = $("#body").height();
			sidebar.height(bodyHeight-60);

			break;
		default:
			WINDOW_HEIGHT = $(window).height();
			$("#TB_ajaxContent").height(WINDOW_HEIGHT*0.85-30);
			$("#TB_window").css({marginTop: '-' + parseInt(($("#TB_window").height() / 2),10) + 'px'});
	
			if(thickBoxShinbunColArr.length >0){
				$(thickBoxShinbunColArr[0]).removeAttr("href").attr({"href":"#TB_inline?height="+(WINDOW_HEIGHT*0.85)+"&width=576&&inlineId=modalColmun"});
			}
			for(var i=0; i<thickBoxTitleArr.length; i++){
				$(thickBoxTitleArr[i]).removeAttr("href").attr({"href":"#TB_inline?height="+(WINDOW_HEIGHT*0.85)+"&width=780&&inlineId=modal"+i});
			}
			thickBoxMetaArr = $('.entryMeta .thickbox');
			for(var i=0; i<thickBoxMetaArr.length; i++){
				$(thickBoxTitleArr[i]).removeAttr("href").attr({"href":"#TB_inline?height="+(WINDOW_HEIGHT*0.85)+"&width=780&&inlineId=modal"+i});
			}
			
			
			
		break;
	}
})

$(document).ready(function(){


	layoutID = $("body").attr("id");
	
	switch(layoutID) {
		case "portal":
			
			var shinbunSelectors = $(".entry");
			var shinbuns = new Array();
			
			var sidebar = $("div#sidebar");
			var bodyHeight = $("#body").height();
			sidebar.height(bodyHeight-60);

			
			buildShinbunFeature();
			
			for(var i=0; i<shinbunSelectors.length; i++){
				shinbuns[i] = new Shinbun(shinbunSelectors[i],i);
				shinbuns[i].relayout();
			}
			
			break;
		
		default:
			thickBoxShinbunColArr = $('.newspapercolumn .thickbox');
			thickBoxTitleArr = $('.entryTitle .thickbox');
			if(thickBoxShinbunColArr.length >0){
				$(thickBoxShinbunColArr[0]).removeAttr("href").attr({"href":"#TB_inline?height="+(WINDOW_HEIGHT*0.85)+"&width=576&&inlineId=modalColmun"});
			}
			for(var i=0; i<thickBoxTitleArr.length; i++){
				$(thickBoxTitleArr[i]).removeAttr("href").attr({"href":"#TB_inline?height="+(WINDOW_HEIGHT*0.85)+"&width=780&&inlineId=modal"+i});
			}

			var articleSelectors = $(".article");
			var articles = new Array();
	
			fontSize = Math.round($(".article").css("fontSize").substring(0,$(".article").css("fontSize").indexOf("px",0)));
			lineHeight = Math.round($(".article").css("lineHeight").substring(0,$(".article").css("lineHeight").indexOf("px",0)));
			if($("body").attr("id").indexOf("e")==0){
				TITLE_MAX_CHAR = 40;
			}
	
			buildShinbunTitle();
	
			for(var i=0; i<articleSelectors.length; i++){
				articles[i] = new Article(articleSelectors[i],i);
				articles[i].relayout();
			}
			break;
	}
	
	
	

});


function pareseChaver(x){
	var temp = '';
	for(var i =0; i<chVerArr.length; i++){
		if(x == chVerArr[i]){
  		temp = chVerArr[i+1];
  		break;
		}
	}
	if(temp =='') temp=x;
	return temp;
}


function buildShinbunTitle(){
	var newspapertitle = $(".newspapertitle .titleLarge");
	var title = newspapertitle.text();
	var fontSizePx = (newspapertitle.height()*1.05/title.length);
	newspapertitle.empty();
	newspapertitle.css({textAlign:"center",fontWeight:"bold",fontSize:fontSizePx,lineHeight:"0.9em"})
	for(i=0; i<title.length; i++){
		var appendDiv = $('<div>'+pareseChaver(title.charAt(i))+'</div>');
		if(i==0){
			appendDiv.css({paddingTop:newspapertitle.height()*0.025})
		}
		newspapertitle.append(appendDiv);
	}
}

function buildShinbunFeature(){
	var TagTable = $('<table></table>');
	var TagTbody = $('<tbody></tbody>');
	var TagTr = $('<tr></tr>');
	var TagTd = $('<td></td>');
	var TagTdImg = $('<td></td>');
	var entryBody = $("#feature_body_inner");
	
	entryBody.find("p").each(function(){
			$(this).removeAttr("style");
			$(this).removeAttr("class");
		});
	entryBody.find("div").each(function(){
			$(this).removeAttr("style");
			$(this).removeAttr("class");
			$(this).css("display","inline");
		});
	entryBody.find("span").each(function(){
			$(this).removeAttr("style");
			$(this).removeAttr("class");
		});
	entryBody.find("blockquote").each(function(){
			$(this).removeAttr("style");
			$(this).removeAttr("class");
		});
		
		
	
	
	var entryBodyContents = entryBody.contents().clone();
	var imgTags = entryBodyContents.find("img");
	var img;
	entryBody.contents().remove();
	
	
	
	TagTd.append(entryBodyContents);
	TagTr.append(TagTd);
	TagTbody.append(TagTr);
	TagTable.append(TagTbody);

	if(imgTags.length>0){
			img = $(imgTags[0]).clone();
			imgTags.each(function(){
				$(this).remove();
			});
			TagTdImg.prepend(img.removeAttr("style").removeAttr("class").removeAttr("width").removeAttr("height"));
			TagTr.append(TagTdImg);
	}
	
	
	entryBody.append(TagTable);
}

var Shinbun = function(containor,num){
	this.containor = $(containor);
	this.num = num;
	this.shinbumTitle = this.containor.find(".entry_title");
	this.entryTitle = this.containor.find(".entry_body h3");
	this.entryBody = this.containor.find(".entry_body");
	this.entryMeta = this.containor.find(".entry_meta");
	this.imgTags = this.containor.find("img");
	this.img;

	this.relayout = function(){
		this.entryBody.find(".entry_body_inner p").each(function(){
			$(this).removeAttr("style");
			$(this).removeAttr("class");
		});
		this.entryBody.find(".entry_body_inner div").each(function(){
			$(this).removeAttr("style");
			$(this).removeAttr("class");
			$(this).css("display","inline");
		});
		this.entryBody.find(".entry_body_inner span").each(function(){
			$(this).removeAttr("style");
			$(this).removeAttr("class");
		});
		this.entryBody.find(".entry_body_inner font").each(function(){
			$(this).removeAttr("size");
			$(this).css("fontSize",'inherit');
		});
		this.entryBody.find(".entry_body_inner span").each(function(){
			$(this).css("fontSize",'inherit');
		});
		this.entryBody.find(".entry_body_inner li").each(function(){
			$(this).css("fontSize",'inherit');
		});
		this.entryBody.find(".entry_body_inner em").each(function(){
			$(this).css("fontSize",'inherit');
		});
		
		if(this.imgTags.length>0){
			this.img = $(this.imgTags[0]).clone();
			this.imgTags.each(function(){
				$(this).remove();
			});
			$(this.entryBody.find(".entry_body_inner p")[0]).prepend(this.img.removeAttr("style").removeAttr("class").removeAttr("width").removeAttr("height"));
		}
	}
}


var Article = function(containor,num){
	this.maxChar;
	this.containor = $(containor);
	this.modalContainor = $(containor).clone();
	this.num = num;
	this.entryId = this.containor.attr("id");
	this.entryTitle = this.containor.find(".entryTitle");
	this.entryMain = this.containor.find(".entryTitle");
	this.entryBody = this.containor.find(".entryBody");
	this.entryFullBody = this.entryBody.clone();
	this.entryMeta = this.containor.find(".entryMeta");
	this.entryAlign = 'left';
	this.entryPermalink = this.containor.find(".NPpermalink a");
	this.entryPermalinkURL = this.entryPermalink.attr("href");
	if(this.containor.attr("rel")!=undefined){
		this.entryAlign = this.containor.attr("rel");
	}
	this.imgTags = this.entryBody.find("img");
	this.height = this.containor.height();
	this.width = this.containor.width();
	
	if(this.num==0){
		this.width -=dankan/2;
	}
	else if(this.num ==2 || this.num==4){
		this.width -=dankan;
	}
	this.entryTitleHeight = this.entryTitle.height();
	this.entryTitleWidth = this.entryTitle.width();
	this.img = '';
	this.imgWidth;
	this.imgHeight;
	this.fixImgWidth;
	this.fixImgHeight;
	this.photoAreaWidth;
	this.photoAreaHeight;
	this.modalContainor.removeAttr("rel").removeAttr("class").removeAttr("style").removeAttr("id").attr({"class":"entryModal"});
	
	this.entryBody.find("div").each(function(){
			$(this).removeAttr("style");
			$(this).removeAttr("class");
	});
	this.entryBody.find("blockquote").each(function(){
			$(this).removeAttr("style");
			$(this).removeAttr("class");
	});
	this.entryBody.find("font").each(function(){
			$(this).removeAttr("size");
			$(this).css("fontSize",fontSize);
	});
	this.entryBody.find("span").each(function(){
			$(this).css("fontSize",fontSize);
	});
	this.entryBody.find("li").each(function(){
			$(this).css("fontSize",fontSize);
	});
	this.entryBody.find("em").each(function(){
			$(this).css("fontSize",fontSize);
	});
	
	
	
	if(this.imgTags.length>0){
		this.imgWidth = $(this.imgTags[0]).width();
		this.imgHeight = $(this.imgTags[0]).height();
		
		if(this.imgWidth>this.imgHeight){
			this.photoAreaWidth = (this.width-dankan)/2;
			this.photoAreaHeight = (Math.ceil(this.photoAreaWidth/4*3/lineHeight))*lineHeight-(lineHeight-fontSize);
			this.fixImgHeight = this.photoAreaHeight;
			this.fixImgWidth = this.imgWidth*this.fixImgHeight/this.imgHeight;
			this.img = $(this.imgTags[0]).removeAttr("width").removeAttr("height").removeAttr("style").attr({"style":"height:"+this.fixImgHeight+"px;"});
		}
		else if(this.width<=288 && this.imgWidth<this.imgHeight){
			this.photoAreaWidth = (this.width-dankan)/2;
			this.photoAreaHeight = (Math.ceil(this.photoAreaWidth/3*4/lineHeight))*lineHeight-(lineHeight-fontSize);
			this.fixImgWidth = this.photoAreaWidth;
			this.fixImgHeight = this.imgHeight*this.fixImgWidth/this.imgWidth;
			this.img = $(this.imgTags[0]).removeAttr("width").removeAttr("height").removeAttr("style").attr({"style":"height:"+this.fixImgHeight+"px;"});		
		}
		else{
			this.photoAreaWidth = (Math.ceil((this.height - this.entryTitleHeight)/4*3/fontSize))*fontSize;
			this.photoAreaHeight = this.height - this.entryTitleHeight;
			this.fixImgWidth = this.photoAreaWidth;
			this.fixImgHeight = this.imgHeight*this.fixImgWidth/this.imgWidth;
			this.img = $(this.imgTags[0]).removeAttr("width").removeAttr("height").removeAttr("style").attr({"style":"height:"+this.fixImgHeight+"px;"});
		}	
	} 
	
	else{
		this.photoAreaWidth = (this.width-dankan)/2;
	}
	
	this.relayout = function(){
		
		var TagEntryWrapper = $('<div class="entryWrapper"></div>');
		var TagEntryMain = $('<div class="entryMain"></div>');
		var TagEntryLeft = $('<div class="left"></div>');
		var TagEntryRight = $('<div class="right"></div>');
		var TagEntryExpand  = $('<div class="expand"></div>');
		var TagEntryTemp  = $('<div class="temp"></div>');
		var TagDivClear = $('<div class="clear"></div>');
		var TagEntryModal = $('<div id="modal'+this.num+'" class="entryModal" style="display:none; overflow:auto;"></div>');
		var TagEntryModalLeft = $('<div class="modalLeft"></div>');
		var TagEntryModalRight = $('<div class="modalRight"></div>');
		var TagEntryMeta = $('<div class="meta"></div>');
		
		
		
		TagEntryMeta.append(this.entryMeta);
		
		setTitleSize(this);
		
		if(this.imgTags.length>0){
			if(this.entryAlign=="right"){
			
				
				TagEntryRight.attr({"style":"width:"+this.photoAreaWidth+"px;"});
				TagEntryLeft.attr({"style":"width:"+(this.width-this.photoAreaWidth-dankan)+"px;"});
				TagEntryLeft.append(this.entryBody);
				var TagEntryPhoto = $('<div class="entryPhoto"></div>').append($('<span class="photoFrame"></span>').append(this.img).attr({"style":"overflow:hidden;height:"+this.photoAreaHeight+"px;"}));
				TagEntryRight.append(TagEntryPhoto);
		
				TagEntryMain.append(TagEntryLeft);
				TagEntryMain.append(TagEntryRight);
				TagEntryMain.append(TagDivClear);
				
				//trim text to fit left
				this.entryTitle.after(TagEntryMain);
				var TagTemp = $('<div></div>');
				TagTemp.append(TagEntryLeft.clone());
				
				var leftColTextHeight = this.height - this.entryTitleHeight;
				var leftColTextWidth = this.width-this.photoAreaWidth-dankan;
				var leftColTextChars = Math.ceil(leftColTextHeight / lineHeight) * Math.floor(leftColTextWidth / fontSize) ;
				var rightColTextHeight = this.height - this.entryTitleHeight - this.photoAreaHeight -8;
				var rightColTextWidth = this.photoAreaWidth;
				var rightColTextChars = Math.ceil(rightColTextHeight / lineHeight) * Math.floor(rightColTextWidth / fontSize) +leftColTextChars - Math.floor(rightColTextWidth / fontSize); 
				trimText(TagTemp,rightColTextChars);
				
				TagEntryExpand.append(TagTemp.attr({"style":"margin-top:-"+(this.height - this.entryTitleHeight-8)+"px;"}));
				TagEntryRight.append(TagEntryExpand.attr({"style":"overflow:hidden; margin-top:"+(lineHeight-fontSize)+"px; height:"+(this.height - this.entryTitleHeight-this.photoAreaHeight)+"px;"}));
				
				//trim text to fit right
				trimText(TagEntryLeft,leftColTextChars);
				
			}
		
			else{
				TagEntryLeft.attr({"style":"width:"+this.photoAreaWidth+"px;"});
				TagEntryRight.attr({"style":"width:"+(this.width-this.photoAreaWidth-dankan)+"px;"});
				TagEntryTemp.append(this.entryBody);
				TagEntryTemp.attr({"style":"overflow:hidden; width:"+this.photoAreaWidth+"px; height:"+(this.height - this.entryTitleHeight-this.photoAreaHeight)+"px;"});
				var TagEntryPhoto = $('<div class="entryPhoto"></div>').append($('<span class="photoFrame"></span>').append(this.img).attr({"style":"overflow:hidden;height:"+this.photoAreaHeight+"px;"}));
				TagEntryLeft.append(TagEntryTemp);
				TagEntryLeft.append(TagEntryPhoto);
				TagEntryMain.append(TagEntryLeft);
				TagEntryMain.append(TagEntryRight);
				TagEntryMain.append(TagDivClear);
				this.entryTitle.after(TagEntryMain);
	
				/* temporary adjust */
				/* for A4 */
				if(layoutID == 't2_100' || layoutID == 't2_101' || layoutID == 't2_102' ){
					if(this.num>0)var tempAdj = 0;
					else var tempAdj = 4;
				}
				/* for A3 */
				else{
					if(this.num>0)var tempAdj = lineHeight - fontSize;
					else var tempAdj = 0;
				
				}
			
				var marginTop
			
				if(this.height-this.entryTitleHeight-this.photoAreaHeight<=0) {
				 marginTop = 0;
				}
				else{
					marginTop = this.height-this.entryTitleHeight-this.photoAreaHeight+tempAdj;
					TagEntryPhoto.css("margin-top",(lineHeight - fontSize)+"px")
				}
			
				TagEntryExpand.append(TagEntryTemp.clone().attr({"style":"margin-top:-"+marginTop+"px;"}));
				TagEntryRight.append(TagEntryExpand);
				
				//trim text to fit
				var leftColTextHeight = TagEntryTemp.height();
				var leftColTextWidth = TagEntryTemp.width();
				var leftColTextChars = Math.ceil(leftColTextHeight / lineHeight) * Math.floor(leftColTextWidth / fontSize);
				var rightColTextHeight = TagEntryLeft.height()-12;
				var rightColTextWidth = TagEntryRight.width();
				var rightColTextChars = Math.ceil(rightColTextHeight / lineHeight) * Math.floor(rightColTextWidth / fontSize) +leftColTextChars - Math.floor(rightColTextWidth / fontSize); 
				trimText(TagEntryTemp,leftColTextChars);
				trimText(TagEntryExpand,rightColTextChars);
				
			}
		}
		else{
			TagEntryRight.attr({"style":"width:"+this.photoAreaWidth+"px;"});
			TagEntryLeft.attr({"style":"width:"+(this.width-this.photoAreaWidth-dankan)+"px;"});
			TagEntryLeft.append(this.entryBody);
/* 			TagEntryLeft.append(this.entryMore); */

	
			TagEntryMain.append(TagEntryLeft);
			TagEntryMain.append(TagEntryRight);
			TagEntryMain.append(TagDivClear);
			this.entryTitle.after(TagEntryMain);
			TagEntryExpand.append(TagEntryLeft.clone().attr({"style":"margin-top:-"+(this.height - this.entryTitleHeight-lineHeight+fontSize)+"px;"}));
			TagEntryRight.append(TagEntryExpand.attr({"style":"overflow:hidden; height:"+(this.height - this.entryTitleHeight)+"px;"}));
		}
		
		
		
		
		TagEntryWrapper.append(this.containor.children());
		TagEntryWrapper.attr({"style":"overflow:hidden; height:"+this.height+"px;"});
			
		this.containor.append(TagEntryWrapper);
		TagEntryWrapper.after(TagEntryMeta);
		TagEntryMeta.attr({"style":"margin-top:-"+TagEntryMeta.height()+"px; width:"+TagEntryRight.width()+"px;"});
		
		var entryMetaMarginRight =0;
		if(this.num==0){
		entryMetaMarginRight =dankan/2;
		}
		else if(this.num ==2 || this.num==4){
			entryMetaMarginRight =dankan;
		}
		
		TagEntryMeta.css({'paddingRight':entryMetaMarginRight});
		

		TagEntryMeta.find(".thickbox").removeAttr("href").attr({"href":"#TB_inline?height="+(WINDOW_HEIGHT*0.85)+"&width=780&&inlineId=modal"+this.num});
		
		var modalContainorImgTags = this.modalContainor.find("img");
		var modalContainorImg ='';
		if(modalContainorImgTags.length>3){ 
			modalContainorImg = $(modalContainorImgTags[0]).removeAttr("width").removeAttr("height").removeAttr("style").clone();
			$(modalContainorImgTags[0]).attr({"style":"display:none"});
		}
		
		var modalContainorEntryBody = this.modalContainor.find(".entryBody");
		modalContainorEntryBody.find("div").each(function(){
			$(this).removeAttr("style");
			$(this).removeAttr("class");
		});
		modalContainorEntryBody.find("blockquote").each(function(){
			$(this).removeAttr("style");
			$(this).removeAttr("class");
		});
	
		TagEntryModalLeft.append(this.modalContainor).append(TagDivClear.clone());
		TagEntryModalRight.append(modalContainorImg).append(TagDivClear.clone());
		TagEntryModal.append(TagEntryModalLeft);
		TagEntryModal.append(TagEntryModalRight);
		TagEntryModal.find('.entryTitle a').removeAttr('href').attr({'href':this.entryPermalinkURL});

		$('body').append(TagEntryModal);
	}
	
	
	function setTitleSize(arcl){
		var title = arcl.entryTitle.text();
		var length = title.length;
		var size;
		var lineHeight;
		if(length>TITLE_MAX_CHAR){
			arcl.entryTitle.find("h2 a").text(title.substring(0,TITLE_MAX_CHAR-1)+REST);
			length = TITLE_MAX_CHAR;
		}
		size = Math.ceil((arcl.entryTitleWidth)/length);
		
		jQuery.each(jQuery.browser, function(i, val) {
			if(i=='msie' && val==true){
				size = Math.ceil((arcl.entryTitleWidth)/length+2);
			}
		});
		
		if($("body").attr("id").indexOf("e")==0){
			size = Math.ceil((arcl.entryTitleWidth)/length+6);
		}
		
		lineHeight = arcl.entryTitleHeight +2;
		if(size>arcl.entryTitleHeight){
			size=arcl.entryTitleHeight-4;
		}
		
		arcl.entryTitle.find("h2").attr({"style":"font-size:"+size+"px; line-height:"+lineHeight+"px;"});
		
	}
	
	function trimText(jObject,trimChars){
		parseNode(jObject);
		trimNode(jObject,trimChars);
		function parseNode(jobj){
			if(jobj.children().length>0){
				jobj.contents().not("[nodeType=1]").wrap("<span/>");
				var jobjArr = jobj.children(); 
				var len = jobjArr.length;
				for(var i=0; i<len; i++){
					parseNode($(jobjArr[i]));
				}
			}
			else{
				return jobj;
			}
		}
		function trimNode(jobj,trimNum){
			if(jobj.children().length>0){
				var jobjArr = jobj.children(); 
				var len = jobjArr.length;
				for(var i=0; i<len; i++){
					if($(jobjArr[i]).text().length <trimNum){
						trimNum -= $(jobjArr[i]).text().length;
					}
					else{
						trimNode($(jobjArr[i]),trimNum);
						trimNum = 0;
					}
				}
			}
			else{
				if(trimNum==0){
					jobj.empty();
					jobj.remove();
				}
				else{
					var trimtext = jobj.text().substring(0,trimNum);
					jobj.text(trimtext);
					trimNum = 0;
				}
			}
		}
	}
	
	
	
}
