var page;
var IMG_LOADING = 'http://www.traca.com.br/main/imgs/ajax-loader.gif';
var IMG_CLOSE   = 'http://www.traca.com.br/main/imgs/close.png';
var ID_LOADING  = 'loading';
var URL         = 'http://www.traca.com.br/main/home.php';
$(function() {
  	page = ___getPageSize();
	$('.ver-capa').click(
		function() {
			id = $(this).attr('rel');
			view(id);
		}
	);
});

function view(id) {
	if ($('#'+ID_LOADING).length == 0) {
		
		_top = ___getTop();
		
		div = document.createElement('div');
		$(div).attr('id', ID_LOADING);
		$(div).css(
			{ 'position'   : 'absolute',
			  'background' : '#EEE',
			  'border'     : '2px solid #000',
			  'text-align' : 'center',
			  'display'    : 'none',
			  'z-index'    : 3
			});
		$(div).html('<div style="position:relative;"><div class="fechar" style="position:absolute; right:-15px; top:-25px; z-index:10; display:none; cursor:pointer" onClick="closeLoading()"><span style="color:#FFF; font-size:11px; text-decoration:underline">fechar esta janela</span>&nbsp;<img align="absmiddle" src="'+IMG_CLOSE+'" /></div><div class="mensagem" style="padding:10px;"><div style=""><img src="'+IMG_LOADING+'" /></div></div><div class="retorno" style="display:none; text-align:center;"></div></div>');
		$('body').append($(div));
		$('#'+ID_LOADING).css(
			{ top  : _top + ((page.windowHeight - $('#'+ID_LOADING).height())/2),
			  left : (page.windowWidth  - $('#'+ID_LOADING).width() )/2
			}
		);
		
		
		div2 = document.createElement('div');
		$(div2).attr('id', 'divBlock');
		$(div2).attr('onClick', "closeLoading()");
		$(div2).css(
			{ 'position'   : 'absolute',
			  'background' : '#000',
			  'width'      : page.pageWidth,
			  'height'     : page.pageHeight,
			  'display'    : 'none',
			  'top'        : 0,
			  'left'       : 0,
			  'z-index'    : 2
			});
		$('body').append($(div2));
	}
	
	$('#divBlock').css({opacity: '0.9'}).fadeIn('fast');

	$.ajax(
		{type     : 'post', 
		 url      : URL, 
		 dataType :'html', 
		 timeout  : 90000, 
		 data     : {
		 	modulo  : 'capa',
			idLivro : id
		 }, 

		 beforeSend:
		 	function() {
				$('#'+ID_LOADING).fadeIn();
			},
		 success: 
			function(data){
				$('#'+ID_LOADING+' .retorno').html(data);
				setTimeout("resize()", 800);
			}, 
			
		 error:
			function(){
			}
		}
	);
}

function closeLoading() {
	$('#divBlock').fadeOut();	

	$('#'+ID_LOADING+', #'+ID_LOADING+' .fechar').hide(); 
	$('#'+ID_LOADING+' .retorno').hide(); 
	$('#'+ID_LOADING+' .mensagem').show();
}

function resize() {
	mode = 'width-and-after-height';
	//mode = 'both';
	_top = ___getTop();
	
	switch (mode) {
		default:
		case 'width-and-after-height':

			$('#'+ID_LOADING).animate(
				{ width : $('#'+ID_LOADING+' .retorno').width(),
				  left  : ((page.windowWidth - $('#'+ID_LOADING+' .retorno').width())/2)+'px'
				},
				300,
				'swing',
				function() {
					$('#'+ID_LOADING).animate(
						{ height : $('#'+ID_LOADING+' .retorno').height(),
						  top    : _top + ((page.windowHeight - $('#'+ID_LOADING+' .retorno').height())/2)+'px'
						},
						300,
						'swing',
						function() {
							$('#'+ID_LOADING+' .retorno .ver-capa-livro').height($('#'+ID_LOADING+' .retorno').height());
							$('#'+ID_LOADING+' .retorno, #'+ID_LOADING+' .fechar').fadeIn();
							$('#'+ID_LOADING+' .mensagem').hide();
						}
					);
				}
			);
		break;
		
		case 'both':
			$('#'+ID_LOADING).animate(
				{ width : $('#'+ID_LOADING+' .retorno').width(),
				  left  : ((page.windowWidth - $('#'+ID_LOADING+' .retorno').width())/2)+'px',
				  height : $('#'+ID_LOADING+' .retorno').height(),
				  top    : _top + ((page.windowHeight - $('#'+ID_LOADING+' .retorno').height())/2)+'px'
				},
				300,
				'',
				function() {
					$('#'+ID_LOADING+' .retorno, #'+ID_LOADING+' .fechar').fadeIn();
					$('#'+ID_LOADING+' .mensagem').hide();
				}
			);
		
		break;
	}
	//$('#loading .mensagem').fadeOut();
}

function ___getTop() {
	return (window.pageYOffset)?(window.pageYOffset):document.documentElement.scrollTop;
}

function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	return {pageWidth : pageWidth, pageHeight : pageHeight, windowWidth : windowWidth, windowHeight : windowHeight}
}
