/**
 * @author ted
 */
$(document).ready(function() {	
	
	//weither the popup is open of not
 	var popup = false;
	var _pos;
	//sets the default document height to campare against
	//fixes a bug when you resize to get bigger
	var documentHeight = $(document).height();
	
	//heightOffset gets the difference between the document size and the window size
	//to account for the window being resized
	var heightOffset;
	
	//sets the event function for when the window is resized
	$(window).bind("resize", resizeWindow);
	
	$('#popup .hellmanns_tab').click(function() {
		heightOffset = (documentHeight) <= ($(window).height()) ? 0 : (documentHeight - $(window).height()) * -1
		if (popup == false) {
			popup = true;
			//sets 
			
			var maskHeight;
			var maskWidth;
			 if (!(jQuery.browser.msie && jQuery.browser.version < 7)) {
			maskWidth=  $(window).width()*3;
			maskHeight = $(document).height()*3;
			 }else{
			 maskWidth	 =$(window).width();
				maskHeight = 10;
			 }
			
			$('#popup_mask').css({'width':maskWidth,'height':maskHeight});
			$('#popup_mask').show();
        $('#popup_mask').fadeTo(0, 0);
        $('#popup_mask').fadeTo("slow", 0.5);
			
			$('#footer_container .past_programs').css({'visibility':'visible'});

			//leaves a space under the footer with the same height located in the stylesheet in order to reserve a 
			//place for the tab when it gets positioned absolute
			$('#footer').css({'margin-bottom':'40px'})
			
			var _num;
			
			if(heightOffset <= -432){
				_num = heightOffset+432; 
			}else{
					_num =0; 
			}
			
			$('#popup').css({'position':'absolute', 'bottom':_num})
			$('#popup').animate({
				height: '400px',
				marginTop: '-400px',
				bottom: _num
			}, 800, null, addmaskClick);
		} else {
			popup = false;
			retractPopup();
			
		}
	});
	
	function addmaskClick() {
		$('#popup_mask').click(function () {
		popup = false;
		retractPopup();
	});
	}
	
	function retractPopup() {
		$('#footer').css({'margin-bottom':'0px'})
		$('#popup_mask').fadeOut('slow',clearMask)
		$('#footer_container .past_programs').css({'visibility':'hidden'});
		
			$('#popup').animate({
				height: '40px',
				marginTop: '0px',
				bottom: _pos
			}, 800, null, resetPopupPosition);
	}
	
	function resetPopupPosition() {
		$('#popup').css({'position':'relative', 'bottom':'0'})
	}
	
	function clearMask() {
		$('#popup_mask').hide();
	}
	
	function resizeWindow () {
		if (popup) {
			heightOffset = (documentHeight) <= ($(window).height()) ? 0 : (documentHeight - $(window).height()) * -1
			var _num;
			if(heightOffset <= -432){
				_num = heightOffset+432; 
			}else{
					_num =0; 
			}
		
		_pos = _num;
		
		$('#popup').css({
				'bottom':_num
			})
		}
	}	
	
	
	//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()*3;
		var maskWidth = $(window).width()*3;
	
		//Set height and width to mask to fill up the whole screen
		$('#popup_mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
			$('#boxes').fadeIn(1000);	
			 if ((jQuery.browser.msie && jQuery.browser.version < 8))return;
			$('#popup_mask').show();
			
        $('#popup_mask').fadeTo(0, 0);
        $('#popup_mask').fadeTo("slow", 0.5);
	

		
		//Get the window height and width

	});
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		$('#popup_mask, #boxes').hide();
	});		
	
	//if mask is clicked
	$('#popup_mask').click(function () {
		$(this).hide();
		$('#boxes').hide();
	});		
	
});
		