//*****Replace align attribute with class**************************************************//
function replaceAlign() {
  if (!document.getElementsByTagName('img')) return false;
  $('img[align="left"]').addClass('left').removeAttr('align');
  $('img[align="right"]').addClass('right').removeAttr('align');
  $('img[align="middle"]').addClass('middle').removeAttr('align');
}
//*****Replace target attribute with class**************************************************//
function replaceTarget(){
	$('a[target]').not('a[target=_self]').removeAttr('target').addClass('newwindow');
  $('a[target]').removeAttr('target');
}
//*****The following function make it possible to have web standard popups**************************************************//
function strictNewWindow() {
  $('a.newwindow').live('click', function(){
    window.open($(this).attr('href'));
    return false;
  });
}
//*****jQuery clear value from july 21st 2009 comment on http://www.joesak.com/2008/11/19/a-jquery-function-to-auto-fill-input-fields-and-clear-them-on-click*****//
function clearDefaultValue() {
  $(':input').focus(function() {
    if($(this).val() == $(this).attr('title')) {
      $(this).val('');
    }
  }).blur(function() {
    if($(this).val() == '') {
      $(this).val($(this).attr('title'));
    }
  });
  $('#btnNext').mousedown(function() {
	  $(':input').each(function (i) {
      if($(this).val() == $(this).attr('title')) {
        $(this).val('');
      }
    });
  });
}
//*****Extend the footer to the bottom of the page**************************************************//
function extendFooter() {
	var footerHeight = Math.round($('#footer-wrap').height());
	var pageHeight = Math.round($('form#Form').height());
	var windowHeight = Math.round($(window).height());
	var difference = windowHeight - pageHeight;
	
	//Replace 300 with the actual height of your footer in pixles
	if ((footerHeight + difference) > 87) {
		$('#footer-wrap').height(footerHeight + difference);
	}
}
//*****Home Animation******************************************************//
function homeAnimation(){
	$('#banner-list').cycle({ 
		fx: 'fade', 
		speed: 500, 
		timeout: 5000, 
		pager: '#banner-nav',
		pagerAnchorBuilder: function(idx, slide) { 
        // return selector string for existing anchor
        return '#banner-nav li:eq(' + idx + ') a'; 
    }
	});
	$('#banner-hotspot').cycle({ 
		fx: 'fade', 
		speed: 500, 
		timeout: 5000, 
		pager: '#banner-nav',
		pagerAnchorBuilder: function(idx, slide) { 
        // return selector string for existing anchor
        return '#banner-nav li:eq(' + idx + ') a'; 
    }
	});
}
//*****CSS Class functions*************************************************//
function cssClasses(){
  $('.RadPanelBar .rpRootGroup li a.active').each(function(i) {
    $(this).parent().addClass('active');
		if(!$(this).parent().children('ul').length){
			$(this).parent().next('li').addClass('no-border')
		}
  });
}

//**************************************************Toggle Question/Answer**********************************************************//
function toggle() {
  if (!$('ul.toggle').length > 0) return false;
	$('ul.toggle h3').addClass('inactive');
	$('ul.toggle div.toggle').hide();
  $('ul.toggle h3').click(function () {
		$(this).toggleClass('active');
		$(this).toggleClass('inactive');
    $($(this).next('div.toggle')).slideToggle();
  });
}
//*****Split column lists from http://www.madeincima.eu/blog/jquery-plugin-easy-list-splitter/*************************************************//
function splitList(){
  if($('ul.split').length){
    $('ul.split').easyListSplitter({ colNumber: 3 });
  }
  if($('ul.splitTwo').length){
    $('ul.splitTwo').easyListSplitter({ colNumber: 2 });
  }
}
//*****Video Modal Animation and Video Load function group*****************//
function videoModal(){
	$('#video-callout').click(function(){
		
		var x = $('#modal-bg').queue('fx');
		var y = $('#modal-wrap').queue('fx');
		
		if(!x.length && !y.length) {
			$('#modal-bg').animate({
				opacity: ['toggle', 'swing']
			}, 750, function() {
				// Animation complete.
				$('#modal-wrap').animate({
					opacity: ['toggle', 'swing']
				}, 750, function() {
					// Animation complete.
					flowplayer("video-player", "http://builds.flowplayer.netdna-cdn.com/56587/48817/flowplayer.commercial-3.2.7-7.swf",  {
						// player configuration goes here
						clip: 'http://www.monetagroup.com/Portals/_default/Skins/Moneta/images/Moneta-video.mp4'
					}).ipad();
				});
			});
		}
		return false;
	});
	
	$('#modal-close').click(function(){
		closeModal();
		return false;
	});
	
	function closeModal(){
		var x = $('#modal-bg').queue('fx');
		var y = $('#modal-wrap').queue('fx');
		if(!x.length && !y.length) {
			$('#modal-wrap').animate({
				opacity: ['toggle', 'swing']
			}, 750, function() {
				// Animation complete.
				$('#modal-bg').animate({
					opacity: ['toggle', 'swing']
				}, 750, function() {
					// Animation complete.
		
				});
			});
		}
		flowplayer().pause();
	}
	
	$(document).keydown(function(e){
		if($('#modal-bg:visible').length){
			switch(e.keyCode){
				case 27:
				  closeModal();
					break;
			}
		}
  });
	
}
//*****Load all functions**************************************************//
$(document).ready(function(){
  replaceAlign();
  replaceTarget();
  strictNewWindow();
  clearDefaultValue();
	extendFooter();
	cssClasses();
	toggle();
	splitList();
	//Keep the traingle on L1 as you navigate the drop down menu
	$('ul.headerNav ul').mouseover(function(){
	  var c = $(this).attr('class');
	  c = c.replace('m m0 m', '.');
    $(c).addClass('hov');
  }).mouseout(function(){
	  var c = $(this).attr('class');
	  c = c.replace('m m0 m', '.');
    $(c).removeClass('hov');
  });
  //
	if($('.home').length){
		homeAnimation();
		videoModal();
	}
});
$(window).resize(function(){
  extendFooter();
});
