$(document).ready(function(){
			 	
			 	
		$('.story a, #header a.iframe').click(function(e){
			e.preventDefault();
			modalWindow.src = this.href;
			modalWindow.content = '<iframe id="' + modalWindow.windowId + '_iframe' + '" frameborder="0" scrolling="no" allowtransparency="true" src="' + this.href + '"></iframe>';
			modalWindow.open();
		});
		
		var initialBoxLength = 68; // for IE6's sake hardcoded
		var initialBoxLinkLength = 64; // for IE6's sake hardcoded
		
		$('#menu li').not(".current").hover(function() {
		
			var params = {
				width: (initialBoxLength * 2) + 'px' ,
			height: (initialBoxLength * 2) + 'px' 
			};
			
			var linkParams = {
				width: (initialBoxLinkLength  * 2)+ 'px' ,
			height: (initialBoxLinkLength * 2) + 'px',
			fontSize: '16px',
			paddingLeft: '8px'
			};
		
			//var options = { duration: 350, easing: 'easeOutBack'};
			switch( $('#menu li').index( $(this) ) ) {
				case 0:
					params.top = -initialBoxLength + 'px';
					params.left = -initialBoxLength + 'px'; 
					break;
				case 1:
					params.top = -initialBoxLength + 'px';
					params.right = -initialBoxLength + 'px'; 
					break;
				case 2:
					params.bottom = -initialBoxLength + 'px';
					params.left = -initialBoxLength + 'px'; 
					break;
				case 3:
					params.bottom = -initialBoxLength + 'px';
					params.right = -initialBoxLength + 'px'; 
					break;
				
			}
			
			$(this).animate( params , {  queue:false, duration: 350, easing: 'easeOutBack'} );
		//alert($(this).find(":nth-child(1)"));
			$(this).children(":first-child").animate( linkParams ,{  queue:false, duration: 350, easing: 'easeOutBack'} );
		
	
	}, function() {
			
		
		var params = {
				width: initialBoxLength + 'px' ,
			height: initialBoxLength + 'px' 
			};
			
			var linkParams = {
				width: initialBoxLinkLength + 'px' ,
			height: initialBoxLinkLength + 'px',
			fontSize: '10px',
			paddingLeft: '2px'
			};
			
			//var options = { duration: 350, easing: 'easeOutBack'};
			switch( $('#menu li').index( $(this) ) ) {
				case 0:
					params.top = 0;
					params.left = 0; 
					break;
				case 1:
					params.top = 0;
					params.right = 0; 
					break;
				case 2:
					params.bottom = 0;
					params.left = 0; 
					break;
				case 3:
					params.bottom = 0;
					params.right = 0; 	
					break;
				
			}
			$(this).animate( params , {  queue:false, duration: 350, easing: 'easeOutBack'} );
		
		$(this).children(":first-child").animate( linkParams , {  queue:false, duration: 350, easing: 'easeOutBack'} );
		
		
		
	});
			
});


var initialOffsetX = 40;
var initialOffsetY = -10;
var modalWindow = {
	parent:"body",
	windowId:null,
	src:null,
	content:null,
	width:null,
	height:null,
	top: null,
	offsetWidth: null,
	offsetHeight: null,
	close:function() {
	
		$(".modal-window").hide();
		$(".modal-overlay").stop().animate({opacity: 0}, 'fast',function(){
			$(".modal-overlay").hide();
		});
	},
	open:function()	{
		
		if( !$( '#' + this.windowId ).get(0) ){
			var modal = "";
			modal += '<div class="modal-overlay"></div>';
			modal += '<div id="' + this.windowId + '" class="modal-window">';
			//modal += this.content;
			modal += '</div>';	
	
			$(this.parent).append(modal);
			
			$( '#' + this.windowId ).append(this.content);
			$(".modal-window").append('<a class="close-window">Close</a>');
			$(".close-window").click(function(){modalWindow.close();});
			$(".modal-overlay").click(function(){modalWindow.close();});
			var offset =  $( '#' + this.windowId ).offset();
			this.top = offset.top;
			
			this.width = $( '#' + this.windowId ).width();
			this.height = $( '#' + this.windowId ).height();
			this.offsetWidth = ($(document).width()/2 - $(".modal-window").width()/2);
			this.offsetHeight = ($(document.body).height()/2 - $(".modal-window").height()/2);
			$(".modal-overlay").css({opacity: 0}).animate({opacity: 0.95});
				
			$( '#' + this.windowId+'_iframe').width( this.width );
			$( '#' + this.windowId+'_iframe').height( this.height );
			
		} else {
			//alert($( '#' + this.windowId+'_iframe' ).get(0).src = this.src)
			$( '#' + this.windowId+'_iframe' ).get(0).src = this.src;
			
			$(".modal-window").stop().show();
			$(".modal-overlay").stop().show().css({opacity: 0}).animate({opacity: 0.95}, 'fast');
		
		}

		$(".modal-overlay").height($(document).height())
		$(".modal-window").css({left:this.offsetWidth+initialOffsetX+'px', top: this.offsetHeight+initialOffsetY+'px'});
		this.reposition();
		$( window ).scroll( this.reposition );
		$( window ).resize( this.reposition );

	},
	reposition: function () {
		$(".modal-overlay").height($(document).height())
		offsetWidth = ($(document).width()/2 - $(".modal-window").width()/2)+initialOffsetX;
		if( $(window).height() < ($(".modal-window").height() + 40) ) {
			offsetHeight = 50 + $(document).scrollTop(); // If the window is smaller than the box		
		} else {
			offsetHeight = ($(window).height()/2 - $(".modal-window").height()/2)+initialOffsetY + $(document).scrollTop();	
		}
		
		//console.log('window', $(window).height());
		//console.log('.modal-window',$(".modal-window").height());
		//console.log('window scrollTop',$(document).scrollTop());
		//$(".modal-window").vCenter();
		$(".modal-window").stop().animate({left:offsetWidth+'px', top: offsetHeight + 'px'}, 'fast');
		

	}

};



function openNewModalWindow(whereTo){

	modalWindow.src = whereTo;
	modalWindow.content = '<iframe id="' + modalWindow.windowId + '_iframe' + '" frameborder="0" scrolling="no" allowtransparency="true" src="' + whereTo+ '"></iframe>';
	modalWindow.open();
}
