/***********************************************************
			Alguns códigos retirados do lightbox
************************************************************/

var IcoreDivopen = {
	url:'',
	__construct: function() {
		var me = this;
		me.geraDivopen();
	},
	getPageScroll: function() {
		var yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {
			yScroll = document.body.scrollTop;
		}
		arrayPageScroll = new Array('',yScroll) 
		return arrayPageScroll;
	},

	getPageSize: function() {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			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
			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;
		}	
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}
		if(xScroll < windowWidth){
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
		return arrayPageSize;
	},
	
	dataCallBack: function() { //callback para DOM gerado no divopen
		$("#emoticons li a").hover(function() {
			$(this).css("backgroundPosition","center -32px");																																		
		},
			function() {
				if (!$(this).data('clicked'))
				$(this).css("backgroundPosition","center top");		
			}
		).click(function() {
			$("#emoticons li a").removeData('clicked').css("backgroundPosition","center top");
			$(this).css("backgroundPosition","center -32px").data('clicked',true);
			$('#form_emoticon').val($(this).attr('id'));
		});
	},

	geraDivopen: function() {
		var me = this;
		var zIndex = 1501;
		var behavior = null;
		$('a').click(function(e) {
			if ($(this).attr('rel') == 'divopen') {
				e.preventDefault();
				var nome = $(this).attr('name');
				if($(document).data(nome) != "" && typeof $(document).data(nome) != "undefined") {
					return false;
				}
				$obj = $('<div class="divopen" name="'+nome+'"></div>').hide();
				me.posicionarDivopen($obj);
				$('body').prepend($obj);
				$obj.addClass(nome);
				$obj.addClass('carregando-divopen');
				$obj.show();
				me.posicionarDivopen($obj);
				$(document).data(nome,"true");
				$content = $('<div class="headContent"></div><div class="divopen-head-left"><div class="divopen-head-right"><div class="divopen-head"></div></div></div><div class="divopen-body-left"><div class="divopen-body-right"><div class="divopen-body-content"></div></div></div><div class="divopen-footer-left"><div class="divopen-footer-right"><div class="divopen-footer"></div></div></div>');
				$obj.prepend($content);
				$obj.css("zIndex",zIndex+1);
				zIndex++;
				$.get($(this).attr('href'), function(data){
					//alert(me.url);																	 
					$obj.find('div').css("behavior","url("+me.url+"/js/iepngfix.htc)");
					$obj.removeClass('carregando-divopen');
					$data = $(data);
					/**
					 * Topo
					 */
					$($data.find('.top').parent()).appendTo($obj.find(".headContent")).hide().fadeIn();

					me.posicionarDivopen($obj);
					var timer = setTimeout(function () {
						me.posicionarDivopen($obj);
					}, 1);
					/**
					 * Corpo
					 */
					$($data.find('.content').parent()).appendTo($obj.find(".divopen-body-content"));/*.hide().slideDown().queue(function () {
						clearInterval(timer);
					});*/

					me.dragger($obj);
					me.fechar();

					me.dataCallBack();
					$.Geral.comentario();
				 });
				$('.divopen').mousedown(function() { $(this).css("zIndex",zIndex+1); zIndex++;})
				return false;
			}
		});
	},

	posicionarDivopen: function($obj) {

		var me = this;

		//var $obj = $(".divopen");
		var arrayPageSize = me.getPageSize();
		var arrayPageScroll = me.getPageScroll();

		var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - $obj.height()) / 2);
		var lightboxLeft = ((arrayPageSize[0] - 20 - $obj.width()) / 2);

		$obj.css({top:(lightboxTop < 0) ? 0 : lightboxTop, left:(lightboxLeft < 0) ? 0 : lightboxLeft})
	},

	dragger: function($obj) {
		$obj.draggable().mouseover(
			function() { $(this).find('.headContent').css("cursor","move"); }).mouseout(
			function() { $(this).find('.headContent').css("cursor","default"); })
	},

	fechar: function() {
		var me = this;
		$('.fechar-divopen').click(function() {
			me.zIndex = 1501;
			$(this).parents(".divopen").remove();
			$(document).removeData($(this).parents(".divopen").attr('name'));
			return false;
		})
	}

}

$(function() {
	IcoreDivopen.__construct();
})