var isOpen = false;

function positionOverlay() {
	//if box is not set to open then don't do anything
	if( isOpen == false ) return;
	// set the properties of the overlay box, the left and top positions
	$('.overlay-container').css({
		left:( $(window).width() - $('.overlay-container').width() )/2,
		//top:( $(window).height() - $('.overlay-container').height() )/2 -20,
		position:'absolute'
	});
	// set the window background for the overlay. i.e the body becomes darker
	$('.overlay-background_screen').css({
		display:'block',
		width: $(window).width(),
		height:($(window).height() * 2)
	});
}
var isOpen;

function openOverlay() {
	//set status to open
	isOpen = true;
	positionOverlay();
	$('.overlay-container').slideDown();
	$('.overlay-background_screen').css({opacity:0}).animate( {opacity:0.8} );
	// dont follow the link : so return false.
	return false;
}
function closeOverlay() {
	//set status to closed
	isOpen = false;
	//$('.overlay-container').slideUp();
	$('.overlay-container').slideUp();

	// now animate the background to fade out to opacity 0
	// and then hide it after the animation is complete.
	$('.overlay-background_screen').css({
		display:'none',
		width: $(window).width(),
		height:$(window).height()
	});
	return false;
}

function downloadItems() {
	$('#download_form input:checked').each(function(){
	    if ($(this).val() != 'yes' && $(this).val() != 'no')
	    {
		    //window.open($(this).val());
		    var title = $(this).attr("title");
		    //alert(title);
		    window.open('/shop/SigninDownload2.asp?from='+$(this).val()+ '&product=' + $('#product').val() + '&item=' + $(this).next().val() + 
		        '&fname=' + $('#Dfname').val() + '&lname=' + $('#Dlname').val() + '&Dtitle=' + $('#Dtitle').val() + '&email=' + $('#Demail').val() + 
		        '&zip=' + $('#Dzip').val() + '&MDR=' + $('#MDR_PID').val() + '&YesEmail=' + $('send_information').val() + '&dtype='+ title);
		    $(this).attr('checked', '');
		}
	});
	window.focus();
	closeOverlay();
	return false;
}

$(function() {
	// if window is resized then reposition the overlay box
	$(window).bind('resize',positionOverlay);
	// activate when the link with class launch link is clicked
	$('a.overlay-launch_link').click(openOverlay);
	// close it when close link is clicked
	$('a.overlay-close_link').click(closeOverlay);
	$('#download_form').submit(downloadItems);
});