function Article()
{
	this.id = null;
	this.name = "";
	this.scaledWidth = 0;
	this.scaledHeight = 0;
}

function switchArticle(amt, keepScrolling)
{
	if( !keepScrolling )
	{
		clearTimeout(scroller);
	}

	curIndex = (curIndex + amt);
	if( curIndex < 0 )
	{
		curIndex = articleArray.length - 1;
	}
	else
	{
		curIndex = curIndex % articleArray.length;
	}
	displayMainArticle(curIndex);
	return false;
}
function displayMainArticle( curIndex )
{
	var article = articleArray[curIndex];
	var mainArticle = document.getElementById("main_article");
	var mainImg = document.getElementById("mainimg");
	var mainName = document.getElementById("main_name");
	var mainTitle = document.getElementById("main_title");
	var readMore = document.getElementById("read_more");
	var mainImgLink = document.getElementById("mainimg_link");

	var fadeImgDiv = document.createElement("div");
	fadeImgDiv.style.position = "absolute";
	fadeImgDiv.setAttribute("id", "fadeImgDiv");
	fadeImgDiv.style.top = "0px";
	fadeImgDiv.style.left = "0px";
	fadeImgDiv.style.zIndex = 2;

	var fadeImg = document.createElement("img");	
	fadeImg.src = mainImg.src;
	fadeImg.width = mainImg.width ;
	fadeImg.height = mainImg.height;
	
	fadeImg.style.marginLeft = "12px";
	fadeImg.style.marginTop = "12px";
	fadeImg.style.marginRight = "auto";
	fadeImgDiv.appendChild(fadeImg);
	
	mainArticle.appendChild(fadeImgDiv);
	
	mainImg.src = mainImgArray[curIndex].src;
	mainImg.width = article.scaledWidth;
	mainImg.height = article.scaledHeight;
	mainName.innerHTML = adjustMainName( article.name );
	var bestUrl = "view_article.jsp?id="+article.id;
	if( article.seoUrl )
	{
		bestUrl = "/news/"+article.seoUrl;
	}
	mainTitle.onclick = function()
	{
		document.location=bestUrl;
		return false;
	}
	readMore.href = bestUrl;
	mainImgLink.href = bestUrl;

	opacity('fadeImgDiv', 100, 0, 300);
}
function adjustMainName(articleName)
{
	var ruler = document.getElementById("ruler");
	if( ! ruler )
	{
		ruler = document.createElement("div");
		ruler.setAttribute("id", "ruler");
		ruler.style.position = "absolute";
		ruler.style.top = "-1000px";
		ruler.style.visibility = "hidden";
		ruler.style.fontSize = "16pt";
		ruler.style.fontFamily = "Myriad Pro,helvetica,arial,verdana,sans-serif";
		ruler.style.textDecoration = "none";
		ruler.style.fontWeight = "bold";

		document.body.appendChild(ruler);
	}
	var modifiedName = articleName;
	if( ruler )
	{
		ruler.innerHTML = modifiedName;
		var isModified = false;
		while(ruler.offsetWidth > 550 )
		{
			modifiedName = modifiedName.substring(0,modifiedName.length-1);
			ruler.innerHTML = modifiedName;
			isModified = true;
		}
		if( isModified )
		{
			modifiedName += "...";
		}
	}
	return modifiedName;

}
function findPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
		while(1)
		{
			curleft += obj.offsetLeft;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}
function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
		while(1)
		{
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}
function getXMLHTTPRequest()
{
	try
	{
		req = new XMLHttpRequest();
	}
	catch(err1)
	{
		try
		{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (err2)
		{
			try
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (err3)
			{
				req = false;
			}
		}
	}
	return req;
}

function opacity(id, opacStart, opacEnd, millisec)
{
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
	
	setTimeout("hideOverlay()", millisec+100 );
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function getClientWidth() {
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight() {
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

function hideOverlay()
{	
	var fadeImgDiv = document.getElementById('fadeImgDiv');
	fadeImgDiv.parentNode.removeChild(fadeImgDiv);
}
document.getElementsByClassName = function(cl) {
var retnode = [];
var myclass = new RegExp('\\b'+cl+'\\b');
var elem = this.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++) {
var classes = elem[i].className;
if (myclass.test(classes)) retnode.push(elem[i]);
}
return retnode;
}; 

function getScrollPosition()
{
	var ScrollTop = document.body.scrollTop;


	if (ScrollTop == 0)
	{
	    if (window.pageYOffset)
		ScrollTop = window.pageYOffset;
	    else
		ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	return ScrollTop;
}
var http = getXMLHTTPRequest();
var addShown = false;
function showAddDiv()
{
	var addDiv = document.getElementById("add_div");
	var addTxt = document.getElementById("addtxt");
	
	if( addShown )
	{
		addDiv.style.display = "none";
		addTxt.style.display = "block";
		addShown = false;
	}
	else
	{
		addDiv.style.display = "block";
		addTxt.style.display = "none";
		setTimeout( "document.form1.name.focus();", 1000 );
		addShown = true;
	}
	curvyCorners.redraw();
	curvyCorners.redraw();
	
	document.form1.scrollIntoView();
	
	return false;
}
function saveComment()
{
	var name = document.form1.name.value;
	var desc = document.form1.desc.value;
	var article = document.form1.article.value;
	
	if( desc.replace(/^\s+|\s+$/, '').length == 0 )
	{
		cleanAndCloseAdd();
		return false;
	}
	
	var url = "/form/add_comment_action.jsp";
	var params = "name="+name+"&desc="+desc+"&article="+article;
	http.open("POST", url, true);

	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	http.onreadystatechange = function() {//Call a function when the state changes.
		if(http.readyState == 4 && http.status == 200)
		{
		}
	}
	http.send(params);
	
	var now = new Date();
	var allCmms = document.getElementById("all_cmms");
	var newCmm = "";
	newCmm = newCmm + "<div class=\"cmm_box\">";
	newCmm = newCmm + "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"480\">";
	newCmm = newCmm + "	<tr>";
	newCmm = newCmm + "		<th align=\"left\" class=\"v9pxBold\">";
	if(( name == null )||( name.replace(/^\s+|\s+$/, '').length == 0 ))
	{
		newCmm = newCmm + "anonymous";
	}
	else
	{
		newCmm = newCmm + name;
	}
	
	var ampm = "AM"
	var hours = now.getHours();
	var minutes = now.getMinutes();
	var month = now.getMonth()+1;
	
	if( hours > 11 )
	{
		ampm = "PM";
	}
	if( hours > 12 )
	{
		hours = hours - 12;
	}
	
	if( minutes < 10 )
	{
		minutes = "0"+minutes;
	}
	
	if( month < 10 )
	{
		month = "0"+month;
	}
	
	newCmm = newCmm + "</th>";
	newCmm = newCmm + "		<td align=\"right\" width=\"250\" class=\"v8pxBold\">";
	newCmm = newCmm + month+"/"+now.getDate()+"/"+now.getFullYear()+" "+hours+":"+minutes+" "+ampm;
	newCmm = newCmm + "		</td>";
	newCmm = newCmm + "	</tr>";
	newCmm = newCmm + "</table>";
	newCmm = newCmm + "<div style=\"padding:4px\" class=\"v8px\">";
	newCmm = newCmm + desc;
	newCmm = newCmm + "</div>";
	newCmm = newCmm + "</div>";

	allCmms.innerHTML = newCmm + allCmms.innerHTML;
	
	cleanAndCloseAdd();
	return false;
}
function cleanAndCloseAdd()
{
	document.form1.name.value = "";
	document.form1.desc.value = "";
	
	showAddDiv();
}
function loadRelatedArticles(id)
{
	var url = "/get_related_articles.jsp";
	var params = "id="+id;
	http.open("POST", url, true);

	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	http.onreadystatechange = function() {//Call a function when the state changes.
		if(http.readyState == 4 && http.status == 200)
		{
			var relatedArticles = JSON.parse(http.responseText);
			if( relatedArticles.length > 0 )
			{
				var relatedArticlesArea = document.getElementById("rel_art_area");
				relatedArticlesArea.innerHTML = "<h2>Related Articles</h2>";

				for( var i=0; i < relatedArticles.length; i++ )
				{				
					if( relatedArticles[i] )
					{
						var bestRelatedArtUrl = "view_article.jsp?id="+relatedArticles[i].id;
						if( relatedArticles[i].seoUrl )
						{
							bestRelatedArtUrl = "/news/"+relatedArticles[i].seoUrl;
						}
						var maxSize = Math.max( relatedArticles[i].scaledWidth, relatedArticles[i].scaledHeight );

						var outStr = "";
						outStr += "<table style=\"margin:3px 0px 10px 25px;\">";
						outStr += "	<tr>";
						outStr += "		<td valign=\"middle\">";
						outStr += "			<a href=\""+bestRelatedArtUrl+"\"><img src=\""+relatedArticles[i].webPath+"\" width=\""+relatedArticles[i].scaledWidth+"\" height=\""+relatedArticles[i].scaledHeight+"\"></a>";
						outStr += "		</td>";
						outStr += "		<td style=\"text-align:left;\" valign=\"middle\">";
						outStr += "			<div class=\"featarticle\"><a href=\""+bestRelatedArtUrl+"\">"+relatedArticles[i].name+"</a></div>";
						outStr += "			<div class=\"v09\">Article by "+relatedArticles[i].authorName+"</div>";
						outStr += "			<div class=\"v09\">"+relatedArticles[i].simpleLastMod+"</div>";
						outStr += "			<div class=\"v09\">"+relatedArticles[i].numComments+" comments</div>";
						outStr += "		</td>";
						outStr += "	</tr>";
						outStr += "</table>";

						relatedArticlesArea.innerHTML += outStr;
					}
				}
			}
		}
	}
	http.send(params);
}
function ShowRelatedArticlesIfIsInView(artId)
{
	var relatedArticlesArea = document.getElementById("rel_art_area");
	var posBottom = getScrollPosition() + window.getWindowSize().Height;
	var posTop = findPosY(relatedArticlesArea);
	if(( (posBottom + 200) >= posTop )&&( RelatedArticlesNotShown ))
	{
		RelatedArticlesNotShown = false;
		loadRelatedArticles(artId);
	}
}
function getWindowSize()
{
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' )
  {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return {"Width":myWidth,"Height":myHeight};
}
function queryStr(ji)
{
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++)
	{
		ft = gy[i].split("=");
		if (ft[0] == ji)
		{
			return ft[1];
		}
	}
}
function changeArticleImg(index)
{
	curIndex = index;
	var aimg = document.getElementById("aimg");
	var anav = document.getElementById("anav"+index);
	var aimg_title = document.getElementById("aimg_title");
	if( ! aimg )
	{
		populateArticleElementsForImage();
		aimg = document.getElementById("aimg");
		aimg_title = document.getElementById("aimg_title");
	}
	aimg.src = imgArray[index].src;
	aimg.width = aimgArray[index].scaledWidth;
	aimg.height = aimgArray[index].scaledHeight;
	aimg.title = aimgArray[index].desc;
	aimg.index = index;
	aimg_title.innerHTML = aimgArray[index].name;
	if(( aimgArray[index].photographer )
		&&( aimgArray[index].photographer != "" )
		&&( aimgArray[index].photographer != "null" ))
	{
		document.getElementById("photographer").innerHTML = "by: "+aimgArray[index].photographer;
	}
	else
	{
		document.getElementById("photographer").innerHTML = "";
	}
	var elems = document.getElementsByClassName("anavbtn_sel");
	for( var i=0; i < elems.length; i++ )
	{
		elems[i].className = "anavbtn";
	}
	if( anav )
	{
		anav.className = "anavbtn_sel";
	}
}
function populateArticleElementsForImage()
{
	var aElements = document.getElementById("a_elements");
	aElements.innerHTML = "<img id=\"aimg\" onclick=\"displayArtImg()\"/><div id=\"photographer_outer\"><div id=\"photographer\"></div></div>" + 
									"<div id=\"aimg_title_outer\"><div id=\"aimg_title\"></div></div>";

}
var loaderTimeout = undefined;
function displayArtImg()
{
	var aimg = document.getElementById("aimg");
	if( loaderTimeout )
	{
		clearTimeout(loaderTimeout);
		loaderTimeout = undefined;
	}

	var objElems = document.getElementsByTagName("OBJECT");
	if(( objElems )&&( objElems.length > 0 ))
	{
		objElems[0].style.visibility = "hidden";
	}

	var fullimg = document.getElementById("fullimg");
	var fullimgdiv = document.getElementById("fullimgdiv");
	
	var clWidth = getClientWidth();
	var clHeight = getClientHeight();

	fullimgdiv.style.width = (clWidth-10)+"px";
	fullimgdiv.style.height = (clHeight-10)+"px";
	var izmWidth = izmArray[aimg.index].width;
	var izmHeight = izmArray[aimg.index].height;
	

	var thumbWidth = clWidth - 80;
	var thumbHeight = clHeight - 100;	

	if(( izmWidth > thumbWidth )||( izmHeight > thumbHeight ))
	{
		var thumbRatio = thumbWidth / thumbHeight;
		var imageRatio = izmWidth / izmHeight;
		if( thumbRatio < imageRatio )
		{
			thumbHeight = Math.round(thumbWidth / imageRatio);
		}
		else
		{
			thumbWidth = Math.round(thumbHeight * imageRatio);
		}

	}
	else
	{
		thumbWidth = izmWidth;
		thumbHeight = izmHeight;
	}

	fullimgdiv.style.width = (thumbWidth+70)+"px";
	fullimgdiv.style.height = (thumbHeight+90)+"px";
	fullimgdiv.style.top = (((clHeight - (thumbHeight+90))/2) + getScrollPosition()) +"px";
	fullimgdiv.style.left = ((clWidth - (thumbWidth+70))/2)+"px";

	fullimg.width = thumbWidth;
	fullimg.height = thumbHeight;
	fullimg.src = izmArray[aimg.index].src;
	
	fullimg.style.top = (((clHeight - thumbHeight)/2) + getScrollPosition() + 10)+"px";
	fullimg.style.left = ((clWidth - thumbWidth)/2)+"px";
	
	fullimgdiv.style.display = "block";
	
	if( izmArray[aimg.index].loaded )
	{
		fullimg.style.display = "block";
	}
	else
	{
		loaderTimeout = setTimeout(function()
		{
			onImgReady();
		},5000);
		var loading = document.getElementById("loading");
		loading.style.top = ((clHeight - 250)/2) + getScrollPosition();
		loading.style.left = (clWidth - 480)/2;
		loading.style.display = "block";

		fullimg.onload = onImgReady;
	}
}
function onImgReady()
{
	var fullimg = document.getElementById("fullimg");
	var loading = document.getElementById("loading");
	var aimg = document.getElementById("aimg");
	loading.style.display = "none";
	fullimg.style.display = "block";
	
	
	izmArray[aimg.index].loaded = true;
}
