$(document).ready(function(){

// for hero buttons

$("a span.btn").removeClass("btn").wrap('<span></span>').parents("a").addClass("btn").wrap('<div class="btnContainer"></div>');
$("a span.btnSmall").removeClass("btn").wrap('<span></span>').parents("a").addClass("btnSmall").wrap('<div class="btnContainer"></div>');
		
	$('input[name="targetUrl"]').attr('style', 'display:none !important');	

      // swap images
   $('#homefundselection img').attr('src', '/ucm/fragments/home_page_2011_theme/images/btn_view.gif');
   $('#logincontent #button_login').attr('src', '/ucm/fragments/home_page_2011_theme/images/btn_login.png');

	var bannercount = 0;
	$('.rotatorSlide').each(function(e){
			bannercount =  bannercount + 1;						
			
	});
	//alert(tempcount);
	
	if(bannercount == "1"){
		//alert("hi");
		$("#nextSlide").attr('style', 'display:none !important');
		$("#prevSlide").attr('style', 'display:none !important');
	
	}
   
 // lightbox  
	$('[target|="lightbox"]').fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type' : 'ajax'
	});
 //Conflicts with pre-existing focus script
 //$('input#search-form-text[type=text]').focus(function() {

  //    $(this).val('');

//  });
	  
//for fund Scriptlist table row
  //$(".customfunddata tr:odd").css("background-color", "#EEEEEE");
 // slect box
 		var options = {
						styleClass: "selectTiaa",
						jScrollPane: 0
						}

		$("#performance_pages").styleSelect(options,false);
		
		
		$('#fundlistopen tr.investmentproductheader td:first-child').css('text-align','left'); 
		$('#fundlistopen table.customfunddataheader td:first-child').css('width','121px').css('text-align','left');
		$('table.customfunddataheader td:nth-child(2)').css('text-align','center');
		$('table.customfunddataheader td:nth-child(3)').css('width','51px').css('text-align','center');
		$('table.customfunddataheader td:nth-child(4)').css('width','51px').css('text-align','center');
		//$('table.customfunddataheader td:nth-child(2)').css('width','27px').css('text-align','center');
		//$('table.customfunddataheader td:nth-child(3)').css('width','47px').css('text-align','center');
		//$('table.customfunddataheader td:nth-child(4)').css('width','49px').css('text-align','center');
		//$('#fundlistopen table.customfunddataheader td:eq(5)').css('width','23px').css('text-align','center');
		//$('#fundlistopen table.customfunddataheader td:eq(6)').css('width','47px').css('text-align','center');
		//$('#fundlistopen table.customfunddataheader td:eq(7)').css('width','49px').css('text-align','center');
});

// TIAA HERO 

var debuger = {
	log : function(){},
	error : function(){},
	info:function(){}
}


if (typeof console !== "undefined") {
	debuger = window.console;
}


//adds a custom ease function to jquery
jQuery.extend( jQuery.easing,
	{
		easeOutExpo: function (x, t, b, c, d) {
			return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
		}
	}
);


	

var tiaaHero = function(){
	var _startSlide, _duration, _autoPlay, 
		_sides,  _currentSlide, _prevSlide, 
		_scrollerDiv,_timer, _inHover,_intransition;
	
	_startSlide = 0;
	_duration = 5000;
	_autoPlay = true;
	_inHover = false;
	_intransition = false;
	
	_slides = [];
	
	function getSlideWidth(){
		return 1280;
	}
	
	/**
	 * Configures a slide
	 * @param {jquery slide div} slideDiv
	 */
	function creatNewSlide(index){
		
		
		var newSlide = {};
		newSlide.index = index;
		newSlide.slideDiv = $('#rotatorScroller>div:eq('+ index +')');
		
		
		var slideDuration = newSlide.slideDiv.attr('duration');
		var slideSwfSrc = newSlide.slideDiv.attr('swfSrc');
		var customCallback = newSlide.slideDiv.attr('callBack');
		var customResetCallback = newSlide.slideDiv.attr('resetCallback');
		
		newSlide.duration = (slideDuration)? slideDuration : _duration;
		
		
		if (customCallback !== undefined) {			
			var callBackObj = window;
			
			//this is a custom HTML slide.
			newSlide.onInCallback = callBackObj[customCallback];
			newSlide.onOutCallback = callBackObj[customResetCallback];			
		}
		
		//if this is still undefined its a standard slide
		if(newSlide.onInCallback === undefined){
			
			//now lets reset the inner contents
			newSlide.cta = newSlide.slideDiv.find('.rotatorCTA:eq(0)');
			//debuger.log(newSlide.cta);
			newSlide.ctaLeft = newSlide.cta.css('left').split('px').join('');
			
			
			
			//this is a standard slide
			newSlide.onInCallback = function(){
				//debuger.log('standard slide in',newSlide)
				
				newSlide.cta.stop().animate({
					"opacity": 1,
					"left" :newSlide.ctaLeft
					}, 
					{
						duration: 250 ,
						specialEasing: {
						},
						complete : function() {
						}
					}
				);
				
			};
			
			newSlide.onOutCallback = function(){
				newSlide.cta.css({
					'opacity': 0,
					'left':newSlide.ctaLeft-15
					});
			};
			
		}
				
		
		//reset the slide
		newSlide.onOutCallback();
			
		
		return newSlide;
	}
	
	function setupBtns(){
	  
    $("#nextSlide").click(function (evt){
      if(_currentSlide != null){
        var nextIndex = _currentSlide.index +1;
        if(nextIndex >= _slides.length){
          nextIndex = 0;
        }
        gotoSlideInternal(nextIndex,"next");
      }
      evt.preventDefault();
    })
    
    
    $("#prevSlide").click(function (evt){
      if(_currentSlide != null){
        var nextIndex = _currentSlide.index -1;
        if(nextIndex < 0){
          nextIndex = _slides.length-1;
        }
        gotoSlideInternal(nextIndex,"prev");
      }
      evt.preventDefault();
    })    
	}
	
	function resizeOverflow(){
		var winWidth = $(window).width();
		
		var offset = 0;
		var rInnerW = 980;
		var negOffset = -150;
		var bannerbtnwidth = $("#nextSlide").width();
		var loginboxwidth = $("#heroForm").innerWidth();
		//alert(loginboxwidth+"loginboxwidth");
		var bannerbtnpadding = 24;
		var bannerbtninnerwidth = bannerbtnwidth + bannerbtnpadding;
		var herooverlaywidth = $("#heroOverlay").width();
		var loginboxleft = herooverlaywidth - loginboxwidth - bannerbtninnerwidth;
		var herooverlayoffset = (winWidth - herooverlaywidth)/2;
		//console.log(herooverlaywidth);
		//console.log($("#heroOverlay").width() + "heroOverlay");
		
		
		
		if(winWidth > 980 && winWidth <= 1280){
			rInnerW = winWidth;
			negOffset = (rInnerW-1280)/2;
		}else if(winWidth > 1280){
			rInnerW = 1280;
			negOffset = 0;
		}
		//alert(winWidth);
		//$("#heroForm").css('left',678);
		
		if(winWidth <= (980 + bannerbtninnerwidth)){			
			$("#heroForm").css('left',690);
		}
		else if(winWidth > (980 + bannerbtninnerwidth) && winWidth <= 1250){
			var screenWidthPercentage = (winWidth/980) - 1;
			var variableleft = Math.round(710 + (731 * screenWidthPercentage));
			//variableleft = (variableleft > 731) ? 731 : variableleft;
			//var variablePadding = 24 + (121 * screenWidthPercentage);
			//var newloginboxleft = Math.round(winWidth - herooverlayoffset - loginboxwidth - bannerbtnwidth - variablePadding);
			//alert(newloginboxleft+ "newloginboxleft");
			
			 var oldrangeloginpos = (1280 - (980+bannerbtninnerwidth));
			 var newrangeloginpos = (731 - 710);
			 var newloginpos = (((winWidth - (980+bannerbtninnerwidth)) * newrangeloginpos) / oldrangeloginpos) + 710;	
			
			
			$("#heroForm").css('left',newloginpos);
		}
		else{
			//bannerbtnpadding = 121;
			//bannerbtninnerwidth = bannerbtnwidth + bannerbtnpadding;
			//loginboxleft = rInnerW - herooverlayoffset - loginboxwidth - bannerbtninnerwidth;
			//newloginboxleft = Math.round(winWidth - herooverlayoffset - loginboxwidth - bannerbtninnerwidth);
			//loginboxleft = rInnerW - herooverlayoffset - loginboxwidth - bannerbtninnerwidth;
			//731px
			$("#heroForm").css('left',731);
		}
	//alert(rInnerW+"rInnerW");
	//var loginboxleft =  rInnerW - bannerbtnwidth - loginboxwidth - bannerbtnpadding;
	
	
	$('#rotator').css('width',rInnerW);
	$('#rotatorInner').css('left',negOffset);
    $('#nextSlide').css('right',-150-negOffset);	
    $('#prevSlide').css('left',-150-negOffset);
	
	
	/*var deviceAgent = navigator.userAgent.toLowerCase();	
	var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);	
	if (agentID) { 
		var heroformright = $("#heroForm").css('left');	
		var heroformrightval = heroformright.replace(/px/i, '');			
			if(heroformrightval >= 732){
				heroformrightval = heroformrightval - 30 +"px";			
				$("#heroForm").css('left',heroformrightval);
			}				
			$('#nextSlide').css('right',15);
			//$('#login_section').css('right',5);
	}*/
		
	}
	

	
	
	if($.browser.msie){	   
	   userAgent = $.browser.version;
	   userAgent = userAgent.substring(0,userAgent.indexOf('.'));
	   var version = userAgent;
	   
	
		
		if(version == "7"){
			$("#fundscriptlist").css("padding","18px 0 6px 0 !important");
			$("#logincontent #userId").css("padding","5px 0 6px 0 !important");
		}
		if(version == "6"){
			$("#logincontent #userId").css("padding","5px 0 4px 0 !important");			
		}
		if(version == "8"){
			$("#fundscriptlist").css("padding","35px 0 6px 0 !important");
			$("#logincontent #userId").css("padding","5px 0 5px 0 !important");
			
		}
	}
	
	
	
	function setup(){
		
		
		//lets first deal with the window size
		$(window).resize(function() {
				resizeOverflow();
		});
		resizeOverflow();
		
		
		var rotatorDiv = $('#rotator');
		
		//lets grab the default values
		if (rotatorDiv.attr('duration') !== undefined) {
			_duration = rotatorDiv.attr('duration');
		}
		
		if (rotatorDiv.attr('isAutoPlay') !== undefined) {
			_autoPlay = (rotatorDiv.attr('isAutoPlay') == "true");
		}
		
		if (rotatorDiv.attr('startSlide') !== undefined) {
			_startSlide = rotatorDiv.attr('startSlide');
		}
		
		//lets create the 4 slides
		for(var i = 0; i < $('#rotatorScroller>div').length; i++){
			_slides.push(creatNewSlide(i));
		} 
		
		//grab a reference to the scroller div
		_scrollerDiv = $('#rotatorScroller');
		
		//now lets deal with the hover detection
		$('#nextSlide').add('#prevSlide').add('#rotator').hover(function(evt){
			//debuger.log('hover in');
			_inHover = true;
		}, function(evt){
			//debuger.log('hover out');
			_inHover = false;
		})
    setupBtns();
	}

	function gotoSlideInternal(num,direction){
	  try{
	  
    if(_intransition){
      return;
    }
		
		//lets clear the last timeout
		if (_timer !== undefined) {
			window.clearTimeout(_timer)
		}
		
				
		var targetLeft = -1* num * getSlideWidth();
		
		//make sure we've called reset on the previous slide
		if(_prevSlide !== undefined){
			_prevSlide.onOutCallback();
		}
		
		if (_currentSlide === undefined) {
			//this is the first rotation
			
			//we want to go directly
		//	_scrollerDiv.css('left', targetLeft);
			_currentSlide = _slides[num];

			if (_currentSlide === undefined) {
				return;
			}
			

			_currentSlide.onOutCallback();
			_currentSlide.onInCallback();
			
    $("#rotatorScroller>div").css('left',1280);
    /*
			//move all other slides off
			for(var sl = 0; sl<_slides.length; sl++){
			  if(sl != _currentSlide.index){
		    _slides[num].slideDiv.css("left",1280);
        }			  
			}
			*/
			_currentSlide.slideDiv.css("left",0);
			
			
			if(_autoPlay){
				//we need to make a timer
				var nextIndex = _currentSlide.index +1;
				if(nextIndex >= _slides.length){
					nextIndex = 0;
				}
				_timer = window.setTimeout('tiaaHero.timoutComplete('+nextIndex + ')',_currentSlide.duration);
			}
			
			return;
		}else{
			if(_currentSlide.index === num){
				//debuger.log('we are already on that slide', num);
				return;
			}
     _prevSlide = _currentSlide;
		}
		
    var newSlideFromX = 1280;
    var oldSlideToX = -1280;
    
    if(direction == "prev"){
      //we need to switch it up
      newSlideFromX = -1280;
      oldSlideToX = 1280;
    }
    
    _currentSlide = _slides[num];   
    $(_currentSlide.slideDiv).css("left",newSlideFromX)    
    		
		
		targetTime = 850;
		_intransition = true;
		$(_prevSlide.slideDiv).stop().animate({
			left: oldSlideToX
			},
			{
				duration: targetTime ,
				specialEasing: {
					left : "easeOutExpo"
				},
				complete : function() {
					//debuger.log('in complete');
					
					_currentSlide.onInCallback();
					_prevSlide.onOutCallback();
					_intransition = false;
					
					if(_autoPlay){
						//we need to make a timer
						var nextIndex = _currentSlide.index +1;
						if(nextIndex >= _slides.length){
							nextIndex = 0;
						}
						_timer = window.setTimeout('tiaaHero.timoutComplete('+nextIndex + ')',_currentSlide.duration);
						//debuger.log(_currentSlide.duration);
					}
				}
			}
		);
		
		$(_currentSlide.slideDiv).stop().animate({
      left: 0
      },
      {
        duration: targetTime ,
        specialEasing: {
          left : "easeOutExpo"
        }
       }
     );
		
		}catch(e){
	  }
	}
	
	return {
		start : function(obj){
			//debuger.log('starting tiaa',_slides);			
			
			setup();
			
			//now lets activate first slide
			gotoSlideInternal(_startSlide);
			
		},
		gotoSlide: function(num,direction){
			gotoSlideInternal(num,direction);
		},
		timoutComplete : function(num){
			//we need to see if we're in hover.
			if (!_inHover) {
				gotoSlideInternal(num);
			}else{
				_timer = window.setTimeout('tiaaHero.timoutComplete('+num + ')',_currentSlide.duration);				
			}
		}
	};
}();


$(document).ready(function(){
   $("#heroOuter").css("visibility", "visible");
	if($("#heroOuter").length){
		tiaaHero.start();
	};	
});


/*$(window).load(function(){
	$("#heroOuter").css("visibility", "visible");
	if($("#heroOuter").length){
		tiaaHero.start();
	};	
});*/





