/**
	Projeto - Projeto (CLIENTE) v: 0.1
	Developer(s):
		+ Nome Sobrenome (email@neotix.com.br)
*/


/*CLOSE MODAL*/
function closeModal() {
	/*HIDE MODAL*/
	$('.view_modal').fadeTo('fast', 0, function() {
		$(this).remove();
	});	
	/*HIDE BACKGROUND*/
	$('.bg_modal').fadeTo('fast', 0, function() {
		$(this).remove();
		/*SHOW SELECT*/
		$('select').css('visibility', 'visible');
	});
}

/*MODAL*/
function openModal(url) {
	/*CREATE ELEMENTS*/
	$('body').append($('<img />').attr({src: "/static/img/common/load.gif", width: 88, height: 78, alt: "", className: 'load'})).append($('<div></div>').addClass('bg_modal')).append($('<div></div>').addClass('view_modal'));
	$('.bg_modal').width($('html')[0].scrollWidth).height($('html')[0].scrollHeight);
	/*OPACITY*/
	$('.view_modal').css('opacity', 0);
	$('.bg_modal').css('opacity', 0);
	/*SELECTS HIDE*/
	$('select').css('visibility', 'hidden');
	/*SHOW BACKGROUND*/
	$('.bg_modal').fadeTo('fast', 0.7, function() {
		$('.view_modal').load(url + '?' + (new Date()).getTime(), function() {
			/*REMOVE LOAD*/
			$('.load').remove();
			/*CENTRALIZE MODAL*/
			
			//centralizeElement('.view_modal');
			$('.view_modal').css({marginTop: -$('.view_modal').height()/2 + 20 + 'px', marginLeft: -$('.view_modal').width()/2 + 'px'});
			/*MODAL HIDE*/
			$('.view_modal').fadeTo('fast', 1);
			/*CLOSE MODAL*/
			$("a[rel='modalclose']").click(function() {
				closeModal();
				return false;
			})
		});
	}).click(function(){
		closeModal();
		return false;
	});
}

function initModal() {
	$("a[rel='modal']").click(function() {
		$('html, body').stop().animate({scrollTop: 0}, 500);
		openModal($(this).attr('href'));
		return false;
	})
}	


// centralizeElement
function centralizeElement(obj){

	var viewportWidth =(self.innerWidth || (document.documentElement.clientWidth || (document.body.clientWidth || 0))); // Grava a largura do viewport
	var viewportHeight =(self.innerHeight || (document.documentElement.clientHeight || (document.body.clientHeight || 0))); // Grava a altura do viewport
	
	if (viewportHeight > $(obj).height()) {
		$(obj).css({top: '50%', left: '50%', marginTop: - $(obj).height()/2 + 20 + 'px',  marginLeft: -($(obj).width()/2)});
	}else {
		$(obj).css({top: '0px', left: '0px', marginTop: '0px', marginLeft: '0px'});
	}
}

$(document).ready(function(){
	centralizeElement(".view_modal");
	initModal();
});

$(window).resize(function() {
	centralizeElement(".view_modal");
});