function getArgs() { 
	var args = new Object(); 
	var query = location.search.substring(1); 
	var pairs = query.split("&");
	
	for(var i = 0; i < pairs.length; i++) { 
		var pos = pairs[i].indexOf('='); 
		if (pos == -1) continue;
		
		var argname = pairs[i].substring(0,pos); 
		var value = pairs[i].substring(pos+1); 
		args[argname] = unescape(value); 
	} 
	return args; 
}

var args = getArgs(); 
if (args.section) section = parseFloat(args.section);

$('document').ready( function(){
	$('div.askAnExpert a.expand').click( function(){
		$('div.askAnExpert').toggleClass('askAnExpertExpanded');
		return false;
	});
	$('a.newsletter').click( function(){
		$('div.newsletterSignUp').toggle();
		return false;
	});
	$('div.newsletterSignUp a.closeBtn').click( function(){
		$('div.newsletterSignUp').toggle();
		return false;
	});
	$('div.newsletterSignUp input.email').focus( function(){
		if($(this).val() == "Enter Email Address") {
			$(this).val("");
		}
	});
	$('div.newsletterSignUp input.email').blur( function(){
		if($(this).val() == "") {
			$(this).val("Enter Email Address");
		}
	});
	$('div.newsletterSignUp input.firstName').focus( function(){
		if($(this).val() == "Enter First Name") {
			$(this).val("");
		}
	});
	$('div.newsletterSignUp input.firstName').blur( function(){
		if($(this).val() == "") {
			$(this).val("Enter First Name");
		}
	});
	$('div.newsletterSignUp input.lastName').focus( function(){
		if($(this).val() == "Enter Last Name") {
			$(this).val("");
		}
	});
	$('div.newsletterSignUp input.lastName').blur( function(){
		if($(this).val() == "") {
			$(this).val("Enter Last Name");
		}
	});
	$('div.companiesWith a.redButton').mouseover( function(){
		var allCompanyButtons = $('div.companiesWith a.redButton');
		var currentPosition = $(allCompanyButtons).index(this);
		var calloutBoxes = $('div.companiesWith div.callout');
		$(calloutBoxes).addClass('hide');
		$(calloutBoxes[currentPosition]).removeClass('hide');
	});
	
	// Highlight Navigation
	var pageTitle = $('h1').html();
	var rightNavItems = $('ul.rightNav a');
	for(var i=0; i<rightNavItems.length; i++){
		if(pageTitle == $(rightNavItems[i]).html()){
			$(rightNavItems[i]).addClass('selected');
		}
	}
	
	$('div.popUp').append('<a href="#" class="closeBtn">Close</a>');
	
	$('a.popUp').click( function(){
		var allPopUpLink = $('a.popUp');
		var allPopUpDiv = $('div.popUp');
		var popUpPosition = $(allPopUpLink).index(this);
		$(allPopUpDiv).addClass('hide');
		$(allPopUpDiv[popUpPosition]).removeClass('hide');
		return false;
	});
	
	$('div.popUp a.closeBtn').click( function(){
		$(this).parent().addClass('hide');
		return false;
	});
	
	$("a.emailPage").attr("href", "mailto:?subject=Commonwealth Cash Management&body=" + location.href);
	
	$('div.expander h3, div.expander a.collapseBtn').click( function(){
		$('div.expander').removeClass('expanded');
		$(this).parent().parent().addClass('expanded');
		return false;
	});
	
	if(args.section != undefined){
		var allCallouts = $('div.expander');
		$('div.expander').removeClass('expanded');
		$(allCallouts[args.section]).addClass('expanded');
	}
	
	$('div.popUpWrapper a.closeBtn').click(function(){
    	$('div.popUpWrapper iframe').attr('src', "/blank.html");
        $('div.popUpWrapper').css('visibility','hidden');
        $('div#coverLayer').css('visibility','hidden');
	});
	
	
	
	$('a.popUpDemo').click( function(){
    	$('div.popUpWrapper iframe').attr('src', $(this).attr('href'));
		$('div.popUpWrapper h3').html("Product Demo: " + $(this).html());
		$('div#coverLayer').css('visibility','visible');
		$('div.popUpWrapper').css('visibility','visible');
		setWindowSize();
		return false;
	});

});

// Opacity Focused Layer
function setWindowSize() {
    var popUpWidth = $('div.popUpWrapper').width();
    var popUpHeight = $('div.popUpWrapper').height();
    var docWidth = 0;
    var docHeight = 0;
    var marginWidth = 0;
    var marginTop = 0;
    var currentScrollPos = document.body.scrollTop != 0 ? document.body.scrollTop : document.documentElement.scrollTop;
    
	// returns width of window $('body').width();
	// returns width of contents minimum and widow width max $(document).width();

	if($('body').width() > $('#header').width()){
	    docWidth = $('body').width();
	} else {
	    docWidth = $('#header').width();
	}

    // get height of the document whichever is taller
	if($(window).height() > ($('#header').height()+$('#content').height()+$('#footer').height()+120)){
	    docHeight = $(window).height()
	} else {
	    docHeight = $('#header').height()+$('#content').height()+$('#footer').height()+120;
	}
    
    $('div#coverLayer').width(docWidth);
    $('div#coverLayer').height(docHeight);

	// Center window horizontally
	if(docWidth > popUpWidth){
		marginWidth = Math.round((docWidth - popUpWidth)/2);
	}
	
	// Center window vertically
	if(($(window).height()-popUpHeight)>0){
		marginTop = ($(window).height()-popUpHeight)/2 + currentScrollPos;
	} else {
		marginTop = currentScrollPos;
	}
	
	$('div.popUpWrapper').css("margin-left", marginWidth);
	$('div.popUpWrapper').css("margin-top", marginTop);
    
    var totalHeight = $('div.popUpWrapper').height() + marginTop;
    
	if(totalHeight > docHeight){
	    docHeight = totalHeight;
        $('div#coverLayer').height(docHeight);
	}
}

var resizeTimer = null;
$(window).bind('resize', function() {
    if (resizeTimer) clearTimeout(resizeTimer);    
    resizeTimer = setTimeout(setWindowSize, 50);
});