$.swim.thinkPod={    
	LOGIN:1,
	CHANGESYMBOL:2,
	// this is a holder object for the call back function to know what to do next. 
	nextSteps:null,
	podApp:null, 
	podDiv:null,
	hOffset:0,
	vOffset:0,
	retry:5,
	//  this is for the thinkPod to call back every time it's loaded, so we can do next steps  
	callbackFunc:function(){
		if(!$.swim.thinkPod.podApp) {
			if($.swim.thinkPod.retry-- > 0)
				setTimeout($.swim.thinkPod.callbackFunc, 1000);
			return;
		}
		if(!$.swim.thinkPod.nextSteps || $.swim.thinkPod.nextSteps["lock"]){ return; }
		else{ $.swim.thinkPod.nextSteps["lock"]=true; }
		
		switch($.swim.thinkPod.nextSteps["todo"]){
	      case $.swim.thinkPod.LOGIN:
			if($.swim.thinkPod.nextSteps["username"] && $.swim.thinkPod.nextSteps["password"]){
				$.swim.thinkPod.papermoneyAutoLogin($.swim.thinkPod.nextSteps["username"], $.swim.thinkPod.nextSteps["password"]);
			}
			$.swim.thinkPod.changeSymbol($.swim.thinkPod.nextSteps['symbol']);
			$.swim.thinkPod.nextSteps={"symbol":$.swim.thinkPod.nextSteps["symbol"], 'todo':$.swim.thinkPod.CHANGESYMBOL   };
			break;
		  case $.swim.thinkPod.CHANGESYMBOL:		  	        				
		  	$.swim.thinkPod.changeSymbol($.swim.thinkPod.nextSteps['symbol']);
		  	$.swim.thinkPod.nextSteps=null;
			break;
		}
	}, 
	// must set first parameter as event for IE because resize, scroll will call this method.
	positionThinkPod: function(event, symbol){
		if(typeof trading_platform=='undefined'){
			return;
		}
		$.swim.thinkPod.podDiv.trigger('reposition');
		if(thinkpod_status=='open'){
			symbol = $.swim.thinkPod.transformOptionSymbol(symbol);
			if($('iframe#thinkPodIFrame').attr("src")== undefined || $('iframe#thinkPodIFrame').attr("src").indexOf("loading")>=0) {
				$('iframe#thinkPodIFrame').attr("src", $.swim.uris.getUrl("tpMediator"));		   			
			    $.swim.thinkPod.nextSteps={"todo":$.swim.thinkPod.CHANGESYMBOL, "symbol":symbol    };
			} 
			if(symbol){  $.swim.thinkPod.changeSymbol(symbol);  }
		}
	},
	showThinkPod:function(symbol){		// slide in
	    if (thinkpod_status != "open") {
    	    if ($("iframe#thinkPodIFrame").attr("src") == undefined ||
    	        $("iframe#thinkPodIFrame").attr("src").indexOf("loading") >= 0) {
    	        $("iframe#thinkPodIFrame").attr("src", $.swim.uris.getUrl("tpMediator"));
    	    }
    	    savePreference("thinkpod_status", "open");
    	    thinkpod_status = "open";
    	    $("img#thinkpod_button").attr("src", $.swim.uris.getImg('tpOpen'));
    	    if (symbol && $.swim.thinkPod.podApp) {
    	        $.swim.thinkPod.podApp.changeSymbol(symbol);
    	    }
   		 } else {
    	    if (symbol) {
    	        $.swim.thinkPod.changeSymbol(symbol);
    	    }
    	}
    	if ($.swim.thinkPod.podApp) {
    	    $.swim.thinkPod.podApp.focus();
    	}
		$.swim.thinkPod.podDiv.trigger('restore');
	},
	closeThinkPod:function(){		// slide out
		$.swim.thinkPod.podDiv.trigger('minimize');
	},
	hideThinkPod:function(){
		$.swim.thinkPod.podDiv.trigger('close');
	},
	realmoneyTradingPlatform:function(symbol){  
		if(trading_platform=='tos_webbased'){
			$.swim.thinkPod.podDiv.trigger('close');//$.swim.thinkPod.hideThinkPod();
	 		window.open(contextPath+'/graphs/thinkOrSwimRedirect.iedu?symbol='+symbol,
		 		'TOSrealmoney', 'left=20, top=20, height=800,width=1060,scrollbars=yes,resizable=yes');
		}else{
			symbol = $.swim.thinkPod.transformOptionSymbol(symbol);
			if($.swim.thinkPod.podApp && $.swim.thinkPod.podApp.isLoaded() && $.swim.thinkPod.podApp.isPapermoney()){
                // in wrong trading platform, need to logout first.
                $.swim.thinkPod.podApp.logout();  
			}
			$.swim.thinkPod.showThinkPod(symbol);
			$.swim.thinkPod.nextSteps={"todo":$.swim.thinkPod.CHANGESYMBOL, "symbol":symbol  };
		}
	},
	papermoneyTradingPlatform:function(symbol, username, password){  
		if(trading_platform=='tos_webbased'){
			$.swim.thinkPod.podDiv.trigger('close');//$.swim.thinkPod.hideThinkPod();
			window.open(contextPath+'/graphs/thinkOrSwimPaperRedirect.iedu?symbol='+symbol,
				'thePaperMoney', 'left=20, top=20, height=800,width=1060,scrollbars=yes,resizable=yes');
		}else{	
			symbol = $.swim.thinkPod.transformOptionSymbol(symbol);		
			$.swim.thinkPod.nextSteps={};
			$.swim.thinkPod.nextSteps["symbol"]=symbol;
			// if not load or not login, need to get the username/password.		
			
			if((!$.swim.thinkPod.podApp || (!$.swim.thinkPod.podApp.isLoaded()||!$.swim.thinkPod.podApp.isPapermoney() )) && 
				(username=='undefined' || username==undefined || username==null || username=='null' || username.length==0
				 || password=='undefined' || password==undefined  || password==null || password=='null' || username.length==0) ){
				 $.ajax(
						{url: $.swim.uris.getUrl('tosPaperCred'),//'/ajaxbridge/graphs/TOSPapermoneyCredential.iedu', 
						 async:false,
						 dataType:'json', 
						 success:function(data) { 
						 	$.swim.thinkPod.nextSteps["username"]=data.username;   
						 	$.swim.thinkPod.nextSteps["password"]=data.password; }
						});	
			}else{
				$.swim.thinkPod.nextSteps["username"]=username;   
				$.swim.thinkPod.nextSteps["password"]=password; 
			}
			// the thinkPod page not loaded yet.	
			if(!$.swim.thinkPod.podApp){			
				$.swim.thinkPod.nextSteps["todo"]=$.swim.thinkPod.LOGIN;   
			
			} // not in papermoney account
			else if($.swim.thinkPod.podApp.isPapermoney()==false){
				// it's logined in real money account
				if($.swim.thinkPod.podApp.isLoaded()){  
					$.swim.thinkPod.podApp.logout();
					$.swim.thinkPod.nextSteps["todo"]=$.swim.thinkPod.LOGIN; 
				}
				else{
					// isLoaded returns false means the user is not signed in; isPapermoney is false means the user login as realmoney.
					//$.swim.thinkPod.papermoneyAutoLogin('wbtester02', 'jimmyrocks');				
				$.swim.thinkPod.papermoneyAutoLogin($.swim.thinkPod.nextSteps["username"],  $.swim.thinkPod.nextSteps["password"]);				
				$.swim.thinkPod.nextSteps["todo"]=$.swim.thinkPod.CHANGESYMBOL; 
				}
			}
			$.swim.thinkPod.showThinkPod(symbol);				
		}
	},
	papermoneyAutoLogin :function(username, password){		
		if($.swim.thinkPod.podApp && !$.swim.thinkPod.podApp.isLoaded()){
			$.swim.thinkPod.podApp.reauthenticate(username, password, true);
		}
	}, 
	changeSymbol:function(symbol){
		if(symbol && $.swim.thinkPod.podApp){ 
			 $.swim.thinkPod.podApp.changeSymbol(symbol);  
		}		
	}, 
	setTradingPlatformContext:function(){
	 if($('span.realmoneyTOSIcon').size()>0){
	    $('span.realmoneyTOSIcon').contextMenu('tradingPlatform', {
	      bindings: {
	        'thinkpod': function(t) {
	        	$('span.wbt').hide();
				$('span.thinkpod').show();			
	        	if(trading_platform!='tos_thinkpod'){
	        		savePreference("trading_platform", "tos_thinkpod");
		        	trading_platform='tos_thinkpod';
		        }
	        	$.swim.thinkPod.realmoneyTradingPlatform($(t).attr('symbol'));		
			},
	        'wbt': function(t) {
	        	$('span.thinkpod').hide();
				$('span.wbt').show();
	        	if(trading_platform!='tos_webbased'){
	        		savePreference("trading_platform", "tos_webbased");
		        	trading_platform='tos_webbased';
		        }
		        $.swim.thinkPod.realmoneyTradingPlatform($(t).attr('symbol'));				
			}
	      }
	    });
	    
	    $('span.paperTOSIcon').contextMenu('tradingPlatform', {
	      bindings: {
	        'thinkpod': function(t) {
	        	$('span.wbt').hide();
				$('span.thinkpod').show();
	        	if(trading_platform!='tos_thinkpod'){
	        		savePreference("trading_platform", "tos_thinkpod");
		        	trading_platform='tos_thinkpod';
		        }	
		        $.swim.thinkPod.papermoneyTradingPlatform($(t).attr('symbol'));			
			},
	        'wbt': function(t){
	        	$('span.thinkpod').hide();
				$('span.wbt').show();
	        	if(trading_platform!='tos_webbased'){
	        		savePreference("trading_platform", "tos_webbased");
		        	trading_platform='tos_webbased';
		        }
		        $.swim.thinkPod.papermoneyTradingPlatform($(t).attr('symbol'));	
			}
	      }
	    });
	 }
	 if($('span.thinkpod').size()>0){
		if(trading_platform=='tos_webbased'){
			$('span.thinkpod').hide();
		}else{
			$('span.wbt').hide();
		}
	}
 },
 transformOptionSymbol:function(symbol){
 	if(!symbol || symbol.indexOf("-")<0){ return symbol;  }
 	else{ return "."+symbol.replace("-", "");   }
 }  
};

(function($){
	$.fn.think = function(){
		if(typeof trading_platform=='undefined') return;  // if no such variable , shouldn't do anything
		$.swim.thinkPod.podDiv = this;
		return this.each(function(){
				$(this).bgIframeAllIE();
				$(this).bind('close', function(){
	//hideThinkPod:function(){
					$(this).trigger('minimize').hide();
					if($.swim.thinkPod.podApp){  
						$.swim.thinkPod.podApp.unload(); 
					}
				}).bind('minimize', function(){		
	//closeThinkPod:function(){		// slide out
		 			if(false){//$.browser.msie && $.browser.version < 7){
		 				$(this).css('height', 25)
		 					//.css('width', $('body').width( ))
							.trigger('showit').animate({bottom:$.swim.thinkPod.bottom},100);
					}else{
						$(this).css('height', 25).trigger('showit');
						//$('div#thinkPodDivOuter').trigger('showit').animate({bottom:-25},100);
					}
	    			$('div#footerSpacer').css('height', 0);
	    			if(thinkpod_status!='close'){
	    				savePreference("thinkpod_status", "close");	
		    			thinkpod_status='close';
	    				$('img#thinkpod_button').attr('src', $.swim.uris.getImg('tpClose'));///common/images/thinkPod_button_close.png');
	    			}
	    			if($.swim.thinkPod.podApp)  $.swim.thinkPod.podApp.blur();
				}).bind('restore', function(){
				// slide in
		 			if(thinkpod_status!='open'){
						savePreference("thinkpod_status", "open");  
						thinkpod_status='open';	 
						$('img#thinkpod_button').attr('src', $.swim.uris.getImg('tpOpen'));
					}
					if($.swim.thinkPod.podApp) $.swim.thinkPod.podApp.focus();  
					$('div#footerSpacer').css('height', 290);
					 if(false){//$.browser.msie && $.browser.version < 7){
		 				$(this).css('height', 290);
					 }
					 $(this).css('height', 290).trigger('showit');
				}).bind('reposition', function(){
					//positionThinkPod: function(event, symbol){
					if(typeof trading_platform=='undefined') return;  // if no such variable , shouldn't do anything
	    			if(trading_platform=='tos_webbased'){ 
	    				$(this).trigger('close');//hideThinkPod();
	   				}else{  
	   					if(thinkpod_status=='open'){
			   				if(false){//$.browser.msie && $.browser.version < 7){
								$(this).css('height', 290);//.css('width', $('body').width( ));
							} 
					   		//$(this).trigger('showit').css('bottom', $.swim.thinkPod.bottom);		   		 
				   			$('div#footerSpacer').css('height', 290);
				   			if($.swim.thinkPod.hOffset != 0)
								$(this).css('width', $(window).width())
									.css('left', $(window).scrollLeft());//$('body').width( ) + $.swim.thinkPod.hOffset);
							$(this).css('height', 290)//.css('width', $('body').width( ) + $.swim.thinkPod.hOffset)//.trigger('showit');
				   					.css('bottom', $.swim.thinkPod.bottom).trigger('showit');
				   		}else{
				   			//$(this).trigger('minimize');
			   				$('div#footerSpacer').css('height', 0);
				   	    	if(false){//$.browser.msie && $.browser.version < 7){
								$(this).css('height', 25)//.css('width', $('body').width( ))
									.trigger('showit').animate({bottom:$.swim.thinkPod.bottom},100);
							}else{
								if($.swim.thinkPod.hOffset != 0)
									$(this).css('width', $(window).width())
										.css('left', $(window).scrollLeft());//$('body').width( ) + $.swim.thinkPod.hOffset);
								$(this).css('height', 25)//.css('width', $('body').width( ) + $.swim.thinkPod.hOffset)//.trigger('showit');
				   					.css('bottom', $.swim.thinkPod.bottom).trigger('showit');
				   			}
		   				}  	
	   				} 
				}).bind('showit', function(){
					if(typeof trading_platform=='undefined' || 
						trading_platform == 'tos_webbased') return;
					$(this).show(); 
				});
				if($.swim.thinkPod.hOffset != 0)
					$(this).css('width', $(window).width())
						.css('left', $(window).scrollLeft());//$('body').width( ) + $.swim.thinkPod.hOffset);
				$(this).trigger('showit').css('bottom', 0);
				//$('div#footerSpacer').css('height', 290);
			});
	}
})(jQuery);
$(document).ready(function(){
	$('span.paperPodTrade').bind('click', function(){
		$('span.wbt').hide();
		$('span.thinkpod').show();
		if(trading_platform!='tos_thinkpod'){
			savePreference("trading_platform", "tos_thinkpod");
			trading_platform='tos_thinkpod';
		}
		$.swim.thinkPod.papermoneyTradingPlatform($(this).attr('symbol'));
	});
	$('span.paperWebTrade').bind('click', function(){
		$('span.thinkpod').hide();
		$('span.wbt').show();
		if(trading_platform!='tos_webbased'){
			savePreference("trading_platform", "tos_webbased");
			trading_platform='tos_webbased';
		}
		$.swim.thinkPod.papermoneyTradingPlatform($(this).attr('symbol'));	
	});
	$('span.realPodTrade').bind('click', function(){
		$('span.wbt').hide();
		$('span.thinkpod').show();
		if(trading_platform!='tos_thinkpod'){
			savePreference("trading_platform", "tos_thinkpod");
			trading_platform='tos_thinkpod';
		}
	$.swim.thinkPod.realmoneyTradingPlatform($(this).attr('symbol'));
	});
	$('span.realWebTrade').bind('click', function(){
		$('span.thinkpod').hide();
		$('span.wbt').show();
		if(trading_platform!='tos_webbased'){
			savePreference("trading_platform", "tos_webbased");
			trading_platform='tos_webbased';
		}
		$.swim.thinkPod.realmoneyTradingPlatform($(this).attr('symbol'));
	});
	$('span.thinkPodInfo').bind('click', function(){
		showThinkPodPopup();
	});
	$('ul.jd_menu').jdMenu({	//onShow: loadMenu
											//onHideCheck: onHideCheckMenu,
											//onHide: onHideMenu, 
											//onClick: onClickMenu, 
											//onAnimate: onAnimate
											});
	//$('td#tosButtons').hide();
	//setTimeout(function(){$('td#tosButtons').show()}, 100);//.show();
	if(typeof trading_platform=='undefined') return;  // if no such variable , shouldn't do anything
	if($.browser.msie && $.browser.version < 7){ 
		$.swim.thinkPod.bottom = 0;
		$.swim.thinkPod.hOffset = -6;
	}else
		$.swim.thinkPod.bottom = 0;
	$('div#thinkPodDivOuter').think();
	if(typeof thinkPod_symbol=='undefined'){  thinkPod_symbol = 'SWIM';  }
	$.swim.thinkPod.positionThinkPod(null, thinkPod_symbol);
    $.swim.thinkPod.setTradingPlatformContext();
	    
	if($.browser.msie && $.browser.version < 7){ 
		$(window).scroll(
			function(){
				if(typeof trading_platform=='undefined' || 
					trading_platform == 'tos_webbased') return;  // if no such variable , shouldn't do anything
				$($.swim.thinkPod.podDiv)
						.css('left', $(window).scrollLeft())
						.css('bottom', 1).css('bottom', 0);
//						.hide().trigger('showit');
			}
		);
		$($.swim.thinkPod.podDiv).hide().trigger('showit');
		//$('div#thinkPodDivOuter').css('bottom', 0));
			//$('div#thinkPodDivOuter').trigger('showit'));//.hide());//.trigger('showit').css('bottom', -283));
	//		$.swim.thinkPod.positionThinkPod);
	}else if($.browser.msie){
		$(window).scroll(
			function(){
				$($.swim.thinkPod.podDiv)
						.hide().trigger('showit');
			}
		);
		$($.swim.thinkPod.podDiv).hide().trigger('showit');
	}
	$(window).resize($.swim.thinkPod.positionThinkPod);
});
function showThinkPodPopup(){
	//if($("div#thinkPodInfoDIV").length == 0)
		//createThinkPodPopup();
	//hideThinkPodPopup();
	$("div#thinkPodInfoDIV").show();
}
function hideThinkPodPopup(){
	$("div#thinkPodInfoDIV").hide();
}
function tpWalkThrough(){
	var w = window.open('http://education.investools.com/content/website/demos2/thinkPod_04.html', "walkthrough",
               "width=825,height=525,status,scrollbars,resizable,screenX=20,screenY=40,left=20,top=40");
    w.focus();
}
function wbWalkThrough(){
	var w = window.open('http://education.investools.com/content/website/demos2/webBasedTrading.html ', "walkthrough",
               "width=845,height=675,status,scrollbars,resizable,screenX=20,screenY=40,left=20,top=40");
    w.focus();
}
function createThinkPodPopup(){
	var html = new StringBuffer();
	html.append("<div id='thinkPodInfoDIV' class='thinkPodInfoDIV' style='z-index: 19 !important;'>",
		"<div id='thinkPodInfoTop'>",
		"<a href='#' onClick='hideThinkPodPopup();'>",
		"<img src='",$.swim.uris.getImg('spacer'),"' align='right' width='35' height='35' border='0'/></a>",
		"</div>",
		"<div id='thinkPodInfoMid'><div style='margin-left:40px;margin-right:40px;'><br/>",
		"<img align='top' src='",$.swim.uris.getImg('tosLogo'),"'/>",
		"&nbsp;&nbsp;&nbsp;&nbsp;<span  style='color:red;'>NEW!</span>",
		"<p>Now you can trade within the Investools Online envirnonment.  This easy-to-use",
		" interface contains all the essentials for account management and option trading.  ",
		"<a href='#' onclick='tpWalkThrough()'>Take a tour</a>.</p>",
		"<img src='",$.swim.uris.getImg('webLogo'),"'/>",
		"<p>Continue trading with the web version of thinkorswim's award winning software.",
		"  It has all of the analytical tools that are built for traders. <a href='#' onclick='wbWalkThrough()'>Take a tour</a>.</p>",
		"</div></div>",
		"<div id='thinkPodInfoBot'></div>",
		"</div>");
	$("body").before(html.toString());
	$("div#thinkPodInfo").bgiframe();
}