$(document).ready(function(){
	//Fixes IE6 PNG opacity
	var isIE6 = !$.support.opacity && !window.XMLHttpRequest	
	$.fn.fixPNG = function() {
		return this.each(function () {
			var image = $(this).css('backgroundImage');

			if (image.match(/^url\(["']?(.*\.png)["']?\)$/i)) {
				image = RegExp.$1;
				$(this).css({
					'backgroundImage': 'none',
					'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=" + ($(this).css('backgroundRepeat') == 'no-repeat' ? 'crop' : 'scale') + ", src='" + image + "')"
				}).each(function () {
					var position = $(this).css('position');
					if (position != 'absolute' && position != 'relative')
						$(this).css('position', 'relative');
				}).css('zoom', 1);
			}
		});
	};
	
	//Launch Modal Window
	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();		
		//Get the A tag
		var id = $(this).attr('href');	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});		
		//hides select boxes not contained inside of pop up so they don't show through pop up
		if (isIE6) {
				$('select:not(#popupBoxes select)').filter(function() {
					return this.style.visibility !== 'hidden';
				}).css({'visibility':'hidden'});
				// fixes opacity on drop shadows
				$('.popShadow').fixPNG();
			}
			
		//transition effect	mask
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
		//transition effect wrapper
		$(id).fadeIn(2000);	
		
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
		var idH = $(id).height()
		var idW = $(id).width()
		var idH2 = $(id).innerHeight()
		var idW2 = $(id).innerWidth()
		//width/height of inner div
		var innerH = $('.popupInner', id).height();		
		var innerW = $('.popupInner', id).width();
		//and position of inner div
		var position = $('.popupInner', id).position();
		
		var innerBH = innerH - $('.popupInnerTop', id).innerHeight() - ($('.popupInnerTop', id).innerHeight()-$('.popupInnerTop', id).height());
              
		/*now determine difference between width/height of inner div and wrap, 
		add position*2 to give equal margin all around inner div
		*/
		var idnewW = (innerW-idW)+idW+(position.top*2);
		var idnewH = (innerH-idH)+idH+(position.top*2);
		
		//Set the popup window to center and give new height/width
		$('.popupInnerBottom', id).css({'height':innerBH});
		$('.popupInner', id).css({'width':innerW,'height':innerH});
		$(id).css({'width':idnewW,'height':idnewH});	
		if($(document).scrollTop() > 0) {
			$(id).css({'top': $(document).scrollTop()+(winH/2-idnewH/2),'left': winW/2-idnewW/2});
		} else {
			$(id).css({'top': winH/2-idnewH/2,'left': winW/2-idnewW/2});
		}
		
	});	
	
	
	//if close button is clicked
	$('.popupWrap .closePopup').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.popupWrap').hide();
		$('#siteContainer').removeClass('noprint');
		$('#mask').removeClass('noprint');
		$('body').removeClass('noprintBKG');
		
		//show select boxes again
		if (isIE6) {
				$('select:not(#popupBoxes select)').filter(function() {
					return this.style.visibility !== 'visible';
				}).css({'visibility':'visible'});
			}
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.popupWrap').hide();
		$('#siteContainer').removeClass('noprint');
		$('#mask').removeClass('noprint');
		$('body').removeClass('noprintBKG');
		//show select boxes again
		if (isIE6) {
				$('select:not(#popupBoxes select)').filter(function() {
					return this.style.visibility !== 'visible';
				}).css({'visibility':'visible'});
			}
	});			
	
});
