var leadCt=-1;
var leadTimer = 0;
$(document).ready(function(){
	leadStories = $("#market-story .lead-stories");
	//leadSlideImages = $("#lead-slide-show img");
	leadSlideNums = $("#lead-slide-show .nums");
	if(getCookie("LEADSLIDES") != null && !isNaN(getCookie("TEXTSIZE"))){
		$("#lead-slide-show div").html("<a href=\"javascript:startRotate()\">Start Slideshow</a>");
		leadCt=0;
	} else {
		rotateLeadStories();
	}
});


function rotateLeadStories() {
	leadCt++;
	switchLeadStory(leadCt);
	leadTimer = setTimeout("rotateLeadStories()",10000);
}

function switchLeadStory(ct) {
	if (ct >= leadStories.length) {
		leadCt=0;
	} else if (ct < 0) {
		leadCt=leadStories.length-1;
	} else {
		leadCt=ct;
	}
	for(i=0; i<leadStories.length; i++){
		if (leadCt==i) {
			leadStories[i].style.display="block";
			$(leadSlideNums[i]).addClass("numOn");
		} else {
			leadStories[i].style.display="none";
			$(leadSlideNums[i]).removeClass("numOn");
		}
	}
}

function slideshowAction(num){
	$(document).ready(function(){
		stopRotate();
		if(typeof num!='undefined'){
			leadCt=num;
			switchLeadStory(num);
		}
	});
}

function stopRotate() {
	if (leadTimer) clearTimeout(leadTimer);
	setCookie("LEADSLIDES",1,1,'/','.smartmoney.com');
	$("#lead-slide-show div").html("<a href=\"javascript:startRotate()\">Start Slideshow</a>");
}

function startRotate() {
	delCookie("LEADSLIDES",'/','.smartmoney.com');
	$("#lead-slide-show div").html("<a href=\"javascript:stopRotate()\">Pause Slideshow</a>");
	rotateLeadStories();
}

// For setting newsmiuser cookie
document.write('<script type="text/javascript" src="http://m1.smartmoney.net/shared/js/cookiecheck.js"></scr' + 'ipt>');

// Check user logged in
if((getCookie("SMID") != null) && getCookie("SMID") !="" ) userLoggedIn=1; else userLoggedIn=0;

// Portfolio Module
var port_timer="";
function portfolio_track(ga_eAction,ga_eLabel,ga_eValue){
	if(userLoggedIn==1) 
		var loginState="Logged In"; 
	else
		var loginState="Not Logged In"; 
	if(typeof ga_eLabel =='undefined') var ga_eLabel=loginState;
	switch (ga_eAction){
		case 'Click Link':
			if(ga_eLabel=='Portfolio') ga_eLabel+=' - '+loginState;
		break;
	}
	if(typeof pageTracker != 'undefined'){
		if(typeof ga_eValue =='undefined')
			pageTracker._trackEvent('Portfolio Module',ga_eAction,ga_eLabel);
		else
			pageTracker._trackEvent('Portfolio Module',ga_eAction,ga_eLabel,ga_eValue);
	}
}

if(typeof portfolio_listURL=='undefined') portfolio_listURL="http://jtools.smartmoney.com/portfolio2/hp";
$(document).ready(function(){ 
	if(userLoggedIn==1){
		if(getCookie("PORTFOLIO_SHOW") == null || getCookie("PORTFOLIO_SHOW")!=0)
			hpport_get();
		else
			$("#portfolio-list").html('<p><strong>This module has been turned off.</strong></p><p><a href="javascript:void(0);" onclick="portfolio_track(\'Open\');hpport_get();">Click here</a> to see your portfolio.</p>');
	} else {
		$("#portfolio-list").html($("#portfolio-message").html());
	}
	if(getCookie("PORTFOLIO_EXPAND")==null){
		expandPortfolio();
		setCookie("PORTFOLIO_EXPAND",1,1);
		startHidePortfolio(3000);
	}
	$("#portfolio-middle").mouseover(stopHidePortfolio);
	$("#portfolio-middle").mouseout(function(){startHidePortfolio(5000)} );
});

function startHidePortfolio(delay){ port_timer=setTimeout('contractPortfolio()',delay); }
function stopHidePortfolio(){ clearTimeout(port_timer); }

function contractPortfolio() {
	$("#portfolio-middle").slideUp("slow");
	if(userLoggedIn==1)
		$("#portfolio-help").html("<a href=\"javascript:expandPortfolio();\" onclick=\"portfolio_track('Open');\">> Customize Here</a>")
	else
		$("#portfolio-help").html("<a href=\"javascript:expandPortfolio();\" onclick=\"portfolio_track('Open');\">What is this?</a>")
}
function expandPortfolio() {
	$("#portfolio-middle").css("display","block");
	$("#portfolio-help").html("<a href=\"javascript:contractPortfolio();\" onclick=\"portfolio_track('Close');\">x Close</a>")
}

function portfolio_help_display(){
	hpport_clear();
	$("#portfolio-list").html($("#portfolio-message").html());
	expandPortfolio();
}
	
function hpport_get(pid){
	if(pid!=""){
		delCookie("PORTFOLIO_SHOW");
		$("#portfolio-indicator").show();
		$("#portfolio-info").show();
		$.getJSON(portfolio_listURL+"?portfolioID="+pid+"&callback=?", function(func){	if(func.indexOf("hpportv01")!=-1) eval(func);	}); 
	} else {
		setCookie("PORTFOLIO_SHOW",0,14);
		$("#portfolio-info").hide();
		hpport_clear();
	}
}

function hpport_clear(){
	hpport_list(1);
	$("#portfolio-title").html("PORTFOLIO");
	$("#portfolio-details").hide();
	$("#portfolio-indicator").hide();
	$("#portfolio-date").html('');
	$("span[smport=name]").html('');
	$("span[smport=nchg]").html('');
	$("span[smport=pchg]").html('');
	$("span[smport=icon]").html('');
}

function hpportv01(js){
	if(typeof js.PORTFOLIO!="undefined"){
		portfolio_current=js.PORTFOLIO;
		for(i=0; i<portfolio_current.length; i++){
			var port=portfolio_current[i];
			var pName=port.NAME;
			if(typeof port.VALUE!='undefined'){	// Default portfolio is the one that has value defined
				var nchg=port.NETCHANGE.replace(/\$|,/g,'');
				$("#portfolio-indicator").hide();
				if(port.NAME.length > 23) pName=port.NAME.substring(0,23)+"...";
				$("#portfolio-title").html("PORTFOLIO:");
				//$("#portfolio-help").html("Today's Chg.");
				$("span[smport=name]").html(pName);
				$("span[smport=value]").html(port.VALUE);
				$("span[smport=nchg]").html(port.NETCHANGE.replace(/\$/g,''));
				$("span[smport=pchg]").html(port.PERCENTCHANGE);
				$("#portfolio-date").html('(last update: '+port.LASTTRADE+')');
				if(nchg > 0){
					chgClass="up";
					$("span[smport=icon]").html('<img src="http://m1.smartmoney.net/framework/images/arrow-portfolio-up.gif" width="11" height="7" alt="Up" />');
				}	else if (nchg < 0) {
					chgClass="down";
					$("span[smport=icon]").html('<img src="http://m1.smartmoney.net/framework/images/arrow-portfolio-down.gif" width="11" height="7" alt="Down" />');
				} else {
					chgClass="";
					$("span[smport=icon]").html('');
				}
				$("span[smport=nchg]").attr("class",chgClass);
				$("span[smport=pchg]").attr("class",chgClass);
				$("a[smport=link]").attr("href","/portfolio?portfolioID="+port.ID);
				$("#portfolio-details").show();
			}
		}
		hpport_list(0);
	} else if (typeof js.ERROR!="undefined") {
		hpport_error(js.ERROR);
	}
}

function hpport_error(val){
	var msg="";
	hpport_clear();
	switch(val){
		case "no portfolio":
			msg='<p><strong>No portfolio found.</strong></p><p><a href="/portfolio/" onclick="portfolio_track(\'Click Link\',\'Portfolio\');">Create a portfolio</a> and view the performance here.</p>';
		break;
	}
	$("#portfolio-list").html(msg);
}

function hpport_list(nodisplay){
	if(typeof nodisplay=='undefined') nodisplay=0;
	if(typeof portfolio_current != "undefined"){
		var strPortfolioList="";
		for(i=0; i<portfolio_current.length; i++){
			var port=portfolio_current[i];
			var pName=port.NAME;
			if(typeof port.VALUE!='undefined' && nodisplay==0){
				strPortfolioList+='<label for="port_option'+i+'"><input type="radio" id="port_option'+i+'" name="pid" value="'+port.ID+'" style="vertical-align: text-bottom;" checked="checked" /> '+eval(i+1)+'. <strong>'+pName+'</strong></label><br />';
			} else {
				strPortfolioList+='<label for="port_option'+i+'"><input type="radio" id="port_option'+i+'" name="pid" value="'+port.ID+'" style="vertical-align: text-bottom;" /> '+eval(i+1)+'. '+pName+'</label><br />';
			}
		}
		if(portfolio_current.length >=1){
			strPortfolioList='<strong>Choose a Portfolio to be displayed here:</strong><br /><form name="portfolio-form" id="portfolio-form">'+strPortfolioList;
			if(nodisplay==1) strPortfolioList+="<strong>";
			strPortfolioList+='<label for="port_nodisplay"><input type="radio" id="port_nodisplay" name="pid" value="" style="vertical-align: text-bottom;" ';
			if(nodisplay==1) strPortfolioList+='checked ';
			strPortfolioList+= '/> Do Not Display</label><br />';
			if(nodisplay==1) strPortfolioList+="</strong>";
			strPortfolioList+='<input type="button" name="portfolioselect" value="Change Portfolio" onclick="portfolio_track(\'Change Portfolio\');hpport_get($(\'input[name=pid]:checked\').val());" style="font-size:12px;" /></form>';
		}
		$("#portfolio-list").html(strPortfolioList);
	}
}
// End portfolio module
var chartsym="$I.DJI";
function changeImage(sym){
	var img_location = "http://charts.smartmoney.com/gifbuilder/images/smartmoney/";
	var img_locationEnd="";
	var chrt_counter = Math.round(new Date().getTime()/60000);
	chartsym=sym;
	document.chart.src=img_location+sym+img_locationEnd+"?u=u"+(chrt_counter++);
}
function chartLink(){
	parent.location.href="/stock-quote/?story=charting&symbol="+chartsym;
}
// refresh the chart image immediately with random param
// in case the browser shows the cached version of the image instead.
$(document).ready(function(){
	changeImage(chartsym);
});
//-->
