$().ready(function(){

	var h = $(document).height();
	var w = $(document).width();
	
	$(window).resize(function() {
		h = $(document).height();
		w = $(document).width();
	});
	
	h+=195;
	
	/* Click to photo */
	$('.user-menu .login').click(function() {
		var overlay = document.getElementById("overlay");
		var modal = this.parentNode.parentNode.parentNode.children[1];
		if (overlay && modal){
			if (!document.getElementById("overlay")) $("body").prepend('<div id="overlay"></div>');
			$(overlay).css({'height' : (h+"px"), 'width' : (w+"px")});
			showModal(overlay, modal);
		}
		return false;
	});
/*
	$('.header .cabinet .login a').click(function() {
		var overlay = document.getElementById("overlay");
		var modal = this.parentNode.parentNode.parentNode.children[1];
		if ( $(modal).length == 0 ) {
			if ($(overlay.parentNode.children[0]).hasClass('modal-aff'))
			modal = overlay.parentNode.children[1];
			else modal = overlay.parentNode.children[0];
		}
		if (overlay && modal){
			if (!document.getElementById("overlay")) $("body").prepend('<div id="overlay"></div>');
			$(overlay).css({'height' : (h+"px"), 'width' : (w+"px")});
			$('body').prepend(modal);
			
			showModal(overlay, modal);
		}
		return false;
	});
*/
	$('.affiliates a.current').click(function() {
		var overlay = document.getElementById("overlay");
		var modalaff = this.parentNode.parentNode.children[1];
		$(modalaff).addClass('modal-aff');
		if ( $(modalaff).length == 0 ) {
			if (!$(overlay.parentNode.children[0]).hasClass('modal-aff'))
			modalaff = overlay.parentNode.children[1];
			else modalaff = overlay.parentNode.children[0];
		}
		if (overlay && modalaff){
			if (!document.getElementById("overlay")) $("body").prepend('<div id="overlay"></div>');
			$(overlay).css({'height' : (h+"px"), 'width' : (w+"px")});
			$('body').prepend(modalaff);
			showModal(overlay, modalaff);
		}
		return false;
	});

	
	/* Click to close */
	$('.modal .close').click(function() {
		var overlay = document.getElementById("overlay");
		var modal = this.parentNode.parentNode;

		hideModal(overlay, modal);
		return false;
	});
	
	function showModal(overlay, modal) {
		
		$(modal).css({'visibility' : 'hidden', 'display' : 'block', 'z-index' : '4001'});//, 'left' : '50%', 'margin-left' : '-170px', 'margin-top' : '180px'
		//posModal(modal); 
		$(modal).css({'display' : 'none', 'visibility' : 'visible', 'height' : 'auto'});
		
		$(overlay).css("display","block");
		//alert(modal);
		$(modal).toggle('slow');
	}
	function hideModal(overlay, modal) {
		var k;
		
		$(modal).toggle('slow');
		$(overlay).css("display","none");
		setTimeout(function() { $(modal).css("top",0) },700);
	}
	function posModal(modal) {
		var top;
		var y = findPosY(modal);
		var h = $(window).height();
		var heightModal = $(modal).height();
	
		top = ( h/2 - heightModal/2 - y ) + (document.body.scrollTop || document.documentElement.scrollTop);
		if (top < 0 && Math.abs(top) > y) 
		top = -y;
		$(modal).css("top",(top + "px")); 
		return top;

		function findPosY(obj) {
			var posTop = 0;
			
			while (obj.offsetParent) {posTop += obj.offsetTop; obj = obj.offsetParent;}
			return posTop;
		}
	}
	
	
	


});
