
// Sets some image vars for cleanliness
var green_down = '/OldSecond/media/graphics/bg-arrow_dark_green_down.gif';
var blue_down = '/OldSecond/media/graphics/bg-arrow_blue_down.gif';


(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);

$(function(){
	//$("video").play(function() { alert('failed') });
	// For styling purposes only
	$('#tab-secure_dropdown li:first-child').addClass('first');
	$('#sub_nav ul li:last-child, #secure_dropdown li:last-child, #tab-secure_dropdown li:last-child').addClass('last');
	
	// Count off sections so that we can control the BLUE wealth management section
	var count = 0;
	$('#main_nav li:not(li ul li)').each(function(){
		$(this).addClass('section-'+count);
		count ++;
	});
	
	// Sub nav control
	//$('#main_nav ul ul').parent().find('a:first').after('<a class="sub_toggle" href="#"></a>');
	$('#main_nav ul ul').each(function(){
		// Counts li's that aren't hidden. If greater than 0, display an arrow to toggle dropdown.
		var x_count = $(this).find('li:not(.hidden)').length;
		if (x_count > 0) {
			$(this).parent().find('a:first').after('<a class="sub_toggle" href="#"></a>');
		}
	});
	$('.section-2 .sub_toggle').addClass('sub_toggle_blue');
	$('.sub_toggle').live('click',function(){
		$(this).toggleClass('open').next().animate({
			height: "toggle",
			opacity:"toggle"
		}, 400);
		if ($(this).hasClass('open')) {
			if ($(this).parents('li').hasClass('section-2')) {
				$(this).css('background-image','url("'+blue_down+'")');
			} else {
				$(this).css('background-image','url("'+green_down+'")');
			}
		} else {
			$(this).removeAttr('style');
		}
		return false;
	});
	// Open Sub Nav automatically on parent pages.
	$('li.open').find('.sub_toggle:eq(0)').addClass('open').css('background-image','url("'+green_down+'")');;
	// Use JS to toggle sub pages
	var subsub = $('#main_nav li ul li ul li.active');
	$(subsub).parent().show();
	$(subsub).parent().parent().find('a:eq(1)').addClass('open').css('background-image','url("'+green_down+'")');
	
	// slideshow rotation
	$('#slideshow:not(.EditMode #slideshow,.DesignMode #slideshow)').cycle({
		timeout: 6000, 
		pager:  '#slideshow_nav',
		pauseOnPagerHover: true
	});
	$('#slideshow_nav a').text('');
	// slideshow play/pause button
	$('#play_pause').live('click',function(){
		if ($(this).attr('rel') == 'pause') {
			$('#slideshow').cycle('pause');
			$(this).attr('rel','resume').css('backgroundPosition','0 -16px');
		} else if ($(this).attr('rel') == 'resume') {
			$('#slideshow').cycle('resume');
			$(this).attr('rel','pause').css('backgroundPosition','0 0');
		}
		return false;
	});
	
	// Secure Trigger
	$('#secure_trigger').click(function(){
		$(this).next().slideToggle();
		return false;
	});
	
	// TAB - Secure Trigger
	$('#tab-secure_login_trigger').click(function(){
		$(this).next().slideToggle();
		return false;
	});
	
	// Close Table
	$('#comparison_table .close').click(function(){
		Shadowbox.close();
		return false;
	});
	
	// Open Info Box
	$('.info_trigger').click(function(){
		var position = $(this).position();
		$(this).parents('li').find('.info_box').css({'left':position.left+40+'px','top':position.top+8+'px'}).fadeIn();
		return false;
	});
	
	// Open Info Box FREE
	$('.info_trigger_free').click(function(){
		var number = $('.info_trigger_free').index($(this));
		var position = $(this).position();
		$('.info_box:eq('+number+')').css({'left':position.left+40+'px','top':position.top+8+'px'}).fadeIn();
		return false;
	});
	
	// Open Info Hover
	$('.info_hover').mouseover(function(){
		var box = $(this).next();
		var position = $(this).position();
		$(box).css({'left':position.left+$(this).width()+40+'px','top':position.top+8+'px'}).fadeIn();
		return false;
	});

	// Close Info Box
	$('.info_box .close').click(function(){
		$(this).parent().fadeOut();
		return false;
	});

	// Submit button hover
	$('#btn-submit').hover(function(){
		$(this).attr('src','/OldSecond/media/graphics/btn-submit_over.png');
	},function(){
		$(this).attr('src','/OldSecond/media/graphics/btn-submit.png');
	});
	
	// Extra toggling
	$('.extra').hide();
	$('.extra_trigger').click(function(){
		$(this).parent().parent().toggleClass('opened').find('.extra').slideToggle();
		return false;
	});
	
	// Anchor Tag Helper
	var currentURL = window.location;
	$(currentURL.hash).find('.extra').show();
	
	// News lister download links
	$('#news_lister a[href$=pdf]').addClass('pdf');
	$('#news_lister a[href$=doc]').addClass('doc');
	$('#news_lister a[href$=ppt]').addClass('ppt');
	$('#news_lister a[href$=xls]').addClass('xls');
	
	// Preload the toggle image
	$.preLoadImages("/OldSecond/media/graphics/bg-plus_list_arrow_down.png");
	
	// Poster images - toggle hover
	var filename,
		ext;
	$('#feature_poster,input[type=image].FormButton').hover(function(){
		// Is this a parent with a child image or a form submit button?
		var img = 0;
		if ($(this).find('img').length > 0) {img = 1;}

		// Get filename
		if (img) {
			filename = $(this).find('img').attr('src');
		} else {
			filename = $(this).attr('src');
		}
		
		// Trim filename to get ext and filename sans ext
		var dot = filename.lastIndexOf(".");
		ext = filename.substr(dot,filename.length);
		var n = filename.slice(0, -4);
		
		// Apply modified source name to element
		if (img) {
			$(this).find('img').attr('src', n+'_over'+ext);
		} else {
			$(this).attr('src', n+'_over'+ext);
		}
	},function(){
		// Is this a parent with a child image or a form submit button?
		var img = 0;
		if ($(this).find('img').length > 0) {img = 1;}
		
		// Apply original filename to element
		if (img) {
			$(this).find('img').attr('src', filename);
		} else {
			$(this).attr('src', filename);
		}
	});



	// Allow Shadowbox to take charge of videos when running in IE
	if ($.browser.msie) {
		$('a.shadowbox-trigger').attr('rel', 'shadowbox;width=640;height=360;');
	} else {

				// HTML5 Video
				$('#feature_poster').click(function(){
					$('#breadcrumbs,.feature_content,#feature_poster').fadeOut();
					$(this).parent().animate({'min-height':'288px'}, 'slow', function() {
						$('#feature_player_wrap').fadeIn();
						$('#feature_player_wrap video').trigger('play');
					});
					return false;
				});
				// ===================
				$('#feature_poster1').click(function(){
					$('#breadcrumbs,.feature_content,#feature_poster1,#feature_poster4,,#feature_poster3,#feature_poster2').fadeOut();
					$(this).parent().animate({'min-height':'288px'}, 'slow', function() {
						$('#feature_player_wrap1').fadeIn();
						$('#feature_player_wrap1 video').trigger('play');
					});
					return false;
				});
				$('#feature_poster2').click(function(){
					$('#breadcrumbs,.feature_content,#feature_poster2,#feature_poster4,#feature_poster3,#feature_poster1').fadeOut();
					$(this).parent().animate({'min-height':'288px'}, 'slow', function() {
						$('#feature_player_wrap2').fadeIn();
						$('#feature_player_wrap2 video').trigger('play');
					});
					return false;
				});
				$('#feature_poster3').click(function(){
					$('#breadcrumbs,.feature_content,#feature_poster3,#feature_poster4,#feature_poster2,#feature_poster1').fadeOut();
					$(this).parent().animate({'min-height':'288px'}, 'slow', function() {
						$('#feature_player_wrap3').fadeIn();
						$('#feature_player_wrap3 video').trigger('play');
					});
					return false;
				});
				$('#feature_poster4').click(function(){
					$('#breadcrumbs,.feature_content,#feature_poster4,#feature_poster3,#feature_poster2,#feature_poster1').fadeOut();
					$(this).parent().animate({'min-height':'288px'}, 'slow', function() {
						$('#feature_player_wrap4').fadeIn();
						$('#feature_player_wrap4 video').trigger('play');
					});
					return false;
				});
				
				$('#feature_player_wrap .close').click(function(){
					$('#feature_player_wrap video').trigger('pause');
					$('#feature_player_wrap').fadeOut();
					$('#feature').animate({'min-height':'201px'}, 'slow', function() {
						$('#breadcrumbs,.feature_content,#feature_poster').fadeIn();
						$('#feature').removeAttr('style');
					});
					return false;
				});
				// ===================
				$('#feature_player_wrap1 .close').click(function(){
					$('#feature_player_wrap1 video').trigger('pause');
					$('#feature_player_wrap1').fadeOut();
					$('#feature').animate({'min-height':'201px'}, 'slow', function() {
						$('#breadcrumbs,.feature_content,#feature_poster1,#feature_poster4,#feature_poster3,#feature_poster2').fadeIn();
						$('#feature').removeAttr('style');
					});
					return false;
				});
				$('#feature_player_wrap2 .close').click(function(){
					$('#feature_player_wrap2 video').trigger('pause');
					$('#feature_player_wrap2').fadeOut();
					$('#feature').animate({'min-height':'201px'}, 'slow', function() {
						$('#breadcrumbs,.feature_content,#feature_poster2,#feature_poster4,#feature_poster3,#feature_poster1').fadeIn();
						$('#feature').removeAttr('style');
					});
					return false;
				});
				$('#feature_player_wrap3 .close').click(function(){
					$('#feature_player_wrap3 video').trigger('pause');
					$('#feature_player_wrap3').fadeOut();
					$('#feature').animate({'min-height':'201px'}, 'slow', function() {
						$('#breadcrumbs,.feature_content,#feature_poster3,#feature_poster4,#feature_poster2,#feature_poster1').fadeIn();
						$('#feature').removeAttr('style');
					});
					return false;
				});
				$('#feature_player_wrap4 .close').click(function(){
					$('#feature_player_wrap4 video').trigger('pause');
					$('#feature_player_wrap4').fadeOut();
					$('#feature').animate({'min-height':'201px'}, 'slow', function() {
						$('#breadcrumbs,.feature_content,#feature_poster4,#feature_poster3,#feature_poster2,#feature_poster1').fadeIn();
						$('#feature').removeAttr('style');
					});
					return false;
				});
	} // if msie	


	// PHP error
	$('.EditingFormErrorLabel').prev().find('input,textarea').addClass('js_applied_error');	
	
	// Fix Live Chat
	if ($('#text_chat_link').is(':hidden')) {
		$('#text_chat_link').parent().hide();
	}
	
	// Dynamic Zebra striping
	$('.comparison_container .even').removeClass('even');
	$('.comparison_container tr:even').addClass('even');


	Shadowbox.init({
		player: "html",
		overlayOpacity: 0.8,
		viewportPadding: 0
	});


}); // $document.ready

