/// <reference path="jquery-1.2.6-vsdoc.js"/>
g_TopicCount = 0;
LoadTopicData();
	
function RotatateTopic() {
    if (g_TopicCount < g_Data.length)
        g_TopicCount++;
    else if (g_TopicCount > g_Data.length - 1)
        g_TopicCount = 1;
	//alert($("#topic" + g_TopicCount.toString()).html());
	ClickTopic(0);
}

callId = setInterval(RotatateTopic, 5000);

function LoadTopicData() {
    $.getJSON("GetTop3Articles.aspx", function(data) {
        g_Data = data;
        var htmlContent = ""; var clickClass = "Clicked";
        for (var i = 1; i <= g_Data.length; i++) {
            if (i == 1) {
                clickClass = "Clicked";
                document.getElementById("newsPic").src = g_Data[0].ImagePath;
                document.getElementById("newsTitle").innerHTML = g_Data[0].Title;
                document.getElementById("newsTitle").href = document.getElementById("newsPicLink").href = g_Data[0].Url;
                document.getElementById("newsContent").innerHTML = g_Data[0].Content;
            }
            else {
                clickClass = "";
            }

            htmlContent += "<div class=\"newsImage" + clickClass + "Link\" id=\"topic" + i + "\">";
            htmlContent += "<div class=\"newsImage" + clickClass + "Box\" id=\"topicImg" + i + "\" onmouseover=\"ClickTopic(" + i + ");clearInterval(callId);\">";
            htmlContent += "<a href=\"javascript:ClickTopic(" + i + ")\"><img src=\"" + data[i - 1].Thumbnail + "\" border=\"0\" /></a></div></div>";
        }
        document.getElementById("ThumbnailImgs").innerHTML = htmlContent;
    });
}

function LoadDownJone30Data() {
    hoverMenu("navMenu");
	if (document.all)
		document.getElementById("dataCanvas").style.height = "680px";
		
    $.ajax({
        url: "GetDownJones30.aspx",
        datatype: "json",
        type: "get",
        success: function(data) {
            var dj30 = eval(data);
            var tableStartTag = "<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" border=\"0\">";
            var tableEndTag = "</table>";
            var htmlContent = "";
            for (var idx = 0; idx < dj30.length; idx++) {
                if (idx % 5 == 0) {
                    htmlContent += tableStartTag;
                }

                var rowHtml = "";
                if ((idx + 1) % 2 == 0) {
                    rowHtml += "<tr class=\"whiteLine\">";
                }
                else {
                    rowHtml += "<tr class=\"greenLine\">";
                }
                rowHtml += "<td width=\"36%\" align=\"left\"><strong>" + dj30[idx].Symbol + "</strong></td>";
//                var imgKeyword = "up";
//                if (dj30[idx].DayChangePercent.indexOf("-") > -1)
//                    imgKeyword = "down";
//                else
//                    imgKeyword = "up";
//                rowHtml += "<td width=\"9%\" align=\"left\"><img src=\"images/stock_arrow_" + imgKeyword + ".gif\" width=\"7\" height=\"4\" /></td>";
                rowHtml += "<td width=\"27%\" align=\"left\">" + dj30[idx].LastPrice + "</td>";
                //rowHtml += "<td width=\"34%\" align=\"left\">(" + dj30[idx].DayChangePercent + ")</td>";
                var colorStyle = "up";
                if (dj30[idx].DayChangePercent.indexOf("-") > -1)
                    colorStyle = "#fe0000";//green
                else
                    colorStyle = "#42b442";//red
                rowHtml += "<td width=\"28%\" align=\"left\"><font color='" + colorStyle + "'>" + dj30[idx].DayChange + "</font></td>";
                rowHtml += "</tr>";
                htmlContent += rowHtml;

                if (idx % 5 == 4) {
                    htmlContent += tableEndTag;
                }
            }
            //alert(htmlContent);
            document.getElementById("indexTableList").innerHTML = htmlContent;
            init_Scroll();
        }
    });
}

function ClickTopic(selectIdx)
{
	if(selectIdx != 0)
		g_TopicCount = selectIdx;
    for (var idx = 1; idx <= g_Data.length; idx++)
	{
		if (idx == g_TopicCount) {
		    if ($("#topic" + idx).hasClass("newsImageLink"))
		        $("#topic" + idx).removeClass("newsImageLink");
				
			if ($("#topic" + idx).hasClass("newsImageClickedLink") == false)
				$("#topic" + idx).addClass("newsImageClickedLink");

			if ($("#topicImg" + idx, $("#topic" + idx)).hasClass("newsImageBox"))
				$("#topicImg" + idx, $("#topic" + idx)).removeClass("newsImageBox");

			if ($("#topicImg" + idx, $("#topic" + idx)).hasClass("newsImageClickedBox") == false)
				$("#topicImg" + idx, $("#topic" + idx)).addClass("newsImageClickedBox");

			document.getElementById("newsPic").src = g_Data[idx - 1].ImagePath;
			document.getElementById("newsTitle").href = document.getElementById("newsPicLink").href = g_Data[idx - 1].Url;
			
			$("#newsTitle").html(g_Data[idx - 1].Title);
			$("#newsContent").html(g_Data[idx - 1].Content);
		}
		else {
			if ($("#topic" + idx).hasClass("newsImageClickedLink"))
				$("#topic" + idx).removeClass("newsImageClickedLink");

			if ($("#topic" + idx).hasClass("newsImageLink") == false)
				$("#topic" + idx).addClass("newsImageLink");

			if ($("#topicImg" + idx, $("#topic" + idx)).hasClass("newsImageClickedBox"))
				$("#topicImg" + idx, $("#topic" + idx)).removeClass("newsImageClickedBox");

			if ($("#topicImg" + idx, $("#topic" + idx)).hasClass("newsImageBox") == false)
				$("#topicImg" + idx, $("#topic" + idx)).addClass("newsImageBox");
		}
	}
}

function init_Scroll(){
	 var scrollPics = new scrollVertical('container','indexTableList','table');
		 scrollPics.speed = 4000;
		 scrollPics.isPause = true;
 
	 var timer_start = setTimeout(function(){clearTimeout(timer_start);scrollPics.isPause = false;},2000);

	 Event.addEvent(scrollPics.scrollArea,"mouseover",function(){scrollPics.isPause = true;});
	 Event.addEvent(scrollPics.scrollArea,"mouseout",function(){scrollPics.isPause = false;});

	 for (var i = 1; i <= g_Data.length; i++)
	{
		Event.addEvent(document.getElementById("topicImg" + i),"mouseout",function(){callId = setInterval(RotatateTopic, 5000);});
		//Event.addEvent(document.getElementById("topicImg" + i),"mouseover",function(){});
	}
}

function hoverMenu(id) {
 var root = document.getElementById(id);
 for (var i=0;i<root.childNodes.length;i++ ) {
  node = root.childNodes[i];
  if (node.nodeName=="LI") {
	   node.onmouseover = function ()
	   {
			this.className = "hover"; 
	   }
	}
	  node.onmouseout = function (){this.className = this.className.replace("hover", "");}
 }
}
Event.addEvent(window,'load',LoadDownJone30Data);