jQuery(function($)
{
  showBenefits();
  demoModals();  
  offerDetailsModal();
  

});

function demoModals()
{	
	var modalLinks = $('a.view');
	var closeLinks = $('#modals .close');
	var theModals = $('#modals .modalBox');
	
	$(theModals).jqm({
		overlayClass: 'overlay2',
		overlay: '1',
		closeClass: 'close',
		modal: false,
		onShow: function(h) {
        	/* callback executed when a trigger click. Show notice */
        	h.w.slideDown();
			setTimeout("try{$('#' + theModals + ' .content').focus();}catch(error){}",100);
        },
		onHide: function(h) {
        	/* callback executed on window hide. Hide notice, overlay. */
        	h.w.slideUp("slow",function() { if(h.o) h.o.remove(); });
		}
	});
	
	modalLinks.click(function()
	{
		var theParent = $(this).parents().get(1).id;
		var theModal = '#' + theParent + 'Modal';
		var theVideo = $(theModal + ' .swfTarget').attr('id');
		
		$(theModals).hide();
		
		$(theModal).jqmShow();		
				
		//videos(theVideo);		
		return false;
	});
	
	/*
	modalLinks.hover(function()
	{ //hover over
		var theParent = $(this).parents().get(1).id;
		var theModal = '#' + theParent + 'Modal';
		var theVideo = $(theModal + ' .swfTarget').attr('id');
		
		$(theModals).hide();
		
		$(theModal).jqmShow();
				
		//videos(theVideo);		
		return false;
	},
	function()
	{
		//hover out
	});
*/
	
	closeLinks.click(function()
	{
		var theModal = $(this).parents().filter('.modalBox').attr('id');
		$('#overlay2').remove();
		return false;
	});	
	
	// If escape key is called, close the top most modal window
	$(document).keyup(function(event){
		if (event.keyCode == 27) {
			$('#fullpayModal,#splitModal,#finishitModal,#trackitModal').jqmHide();
		}
	});
}

function offerDetailsModal()
{
	$('#modals').append('<div id="offerdetailsModal" style="display:none;"></div>');
	
	$('#offerdetailsModal').jqm({
		ajax: '@href',
		trigger: 'a.view-offer',
		overlayClass: 'overlay',
		overlay: '70',
		closeClass: 'close',
		modal: false,
		onShow: function(h) {
        	/* callback executed when a trigger click. Show notice */
        	h.w.fadeIn();
			setTimeout("try{$('#offerdetailsModal .content').focus();}catch(error){}",100);
        },
		onHide: function(h) {
        	/* callback executed on window hide. Hide notice, overlay. */
        	h.w.fadeOut("slow",function() { if(h.o) h.o.remove(); });
		}
	});
	
	// If escape key is called, close the top most modal window
	$(document).keyup(function(event){
		if (event.keyCode == 27) {
			$('#offerdetailsModal').jqmHide();
		}
	});
}

function showBenefits() {
	var benefits = $('#addtl-benefits dl');
	var term = $('#addtl-benefits dt'); //array of all definition titles
	var desc = $('#addtl-benefits dd'); //array of all definition descriptions
	var theInnerWrapper = $('#inner');
	
	// Hovers
	benefits.toggle(
	function()
	{	
		if($(this).children('dt').attr('class') !== 'open')
		{
			descriptionVisible($(this));
		}
		else
		{
			descriptionHidden($(this));
		}
	},
	function()
	{
		if($(this).children('dt').attr('class') !== 'open')
		{
			descriptionVisible($(this));
		}
		else
		{
			descriptionHidden($(this));
		}		
	});

function descriptionVisible(x)
{
	theInnerWrapper.removeClass('innerclosed').addClass('inneropen');
	var theTerm = x.children('dt');
	var theDesc = x.children('dd');
	
	term.removeClass('open');	
	desc.removeClass('open');	
	
	theDesc.queue(function(){
		theTerm.addClass('open');
		$(this).slideDown('slow');
		$(this).dequeue().addClass('open');
	});		
	
	theDesc.queue(function(){
		$('#addtl-benefits dd[class!=open]').each(function()
		{												
			$(this).slideUp('slow');
		});
		$(this).dequeue();
	});
}

function descriptionHidden(y)
{
	theInnerWrapper.removeClass('inneropen').addClass('innerclosed');
	var theTerm = y.children('dt');
	var theDesc = y.children('dd');
	
	theDesc.slideUp('slow');
	theTerm.removeClass('open');
	theDesc.removeClass('open');
}

	// Focus (keyboard navigation)
	/*
benefits.children('a').focus(function(){descriptionVisible($(this).parent())});
	benefits.children('a').blur(function(){descriptionHidden($(this).parent())});
*/

}





