/*
**************************************
* Popup v1.0                         *
* Autor: Wagner B. Soares            *
**************************************
*/
Popup = function(tipo, oRoot, width, height, wTop, wLeft, minX, maxX, minY, maxY)
{
	var popup			= new Object();
	var id				= oRoot;
	var wTop			= parseInt(wTop);
	var wLeft			= parseInt(wLeft);
	
	// seta tamanho da popup
	var width			= (isNumber(width) && width > 200)		? width		: 200;
	var height			= (isNumber(height) && height > 200)	? height	: 200;
	
	// recebe a div que será a popup
	var win				= document.getElementById(id);
	// div que é utilizada para minimizar e maximizar

	var animate			= jQuery.create("div",{},[]);
	// container do conteúdo
	
	var container		= jQuery.create("div",{"class":"JQcontainer"},[]);
	
	// texto do cabeçalho
	var headerText		= null;
	// cria o container dos botões
	var header			= jQuery.create("span",{"class":"JQheader"},[]);
	// cria o container do titulo
	var titleContent	= jQuery.create("span",{"class":""},[]);
	// cria o container do conteúdo

	if (tipo == 1)
	{	
	    var conteudo		= jQuery.create("div",{"class":"JQconteudo"},[]);
    }
    else
    {
        var conteudo		= jQuery.create("div",{"class":"JQVideoteca"},[]);
    }    
	
	//cria e configura o botão [Fechar]
	var buttonClose		= jQuery.create("img",{"id":"close-" + id,"alt":"","title":"","src":"imagens/layout/bt_fechar.gif"},[]);
	
	
	jQuery(buttonClose).css({"cursor":"pointer"});
	buttonClose.onclick = function()
	{
		jQuery(win).css('display','none');
		jQuery(container).empty();
	}
	
	jQuery(header).css({"width":((width - 20) + "px")});
	jQuery(header).append(titleContent);
	jQuery(header).append(buttonClose);
	
	jQuery(win).empty();
	
	if (tipo == 1)
	{
	    jQuery(win).addClass("JQwin");
    }
    else
    {
        jQuery(win).addClass("JQwinVideoteca");
    }
	
	jQuery(win).css({"width":(width + "px")});
	jQuery(win).append(header);
	
	jQuery(container).css({"width":((width) + "px"),"height":((height) + "px")});
	jQuery(container).append(conteudo);
	
	jQuery(animate).append(container);
	jQuery(win).append(animate);
	
	
	// função para configurar o texto do cabeçalho da popup
	this.setTitle = function(t)
	{
		headerText = document.createTextNode(t);
		jQuery(titleContent).empty();
		jQuery(titleContent).append(headerText);
	};
	
	// função inserir o conteúdo da popup
	this.setContent = function(s)
	{
		jQuery(conteudo).empty();
		jQuery(conteudo).html(s);
	};
	
	// função inserir o conteúdo da popup
	this.loading = function(src, w, h)
	{
		var it_width	= parseInt(w);
		var it_height	= parseInt(h);
		var vc_src		= new String(src);
		//cria imagem de loading
		var imgLoading	= jQuery.create("img",{"id":"loading-"+id,"alt":"Carregando...","title":"Carregando...","src":vc_src},[]);
		
		jQuery(imgLoading).css({left: ((width - it_width) / 2), top: ((height - it_height) / 2), position:'absolute'});
		jQuery(conteudo).empty();
		jQuery(conteudo).append(imgLoading);
	};
	
	popup.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;
		}
		
		// 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 = windowWidth;
		} else {
			pageWidth = xScroll;
		}
		
		arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
		return arrayPageSize;
	};
	
	popup.position = function(t, l)
	{
		var ar_pagesize	= this.getPageSize();
		var pageTop		= parseInt(t);
		var pageLeft	= parseInt(l);
		
		// se ñ houver um valor p/ [Left], assume o meio da tela
		if(pageLeft == 0 || !isNumber(pageLeft))
		{
			pageLeft	= parseInt(ar_pagesize[0]);
		}
		
		// se ñ houver um valor p/ [Top], assume o meio da tela
		if(pageTop == 0 || !isNumber(pageTop))
		{
			pageTop		= parseInt(ar_pagesize[1]);
		}
		
		// posiciona janela
		jQuery(win).css({left: ((pageLeft - width)/2), top: ((pageTop - height)/2)});
	};
	
	// exibe a popup
	this.show = function(t, l)
	{
		var pageTop		= parseInt(t);
		var pageLeft	= parseInt(l);
		popup.position(t, l);	// reposiciona janela
		jQuery(win).css('display','block');
	};
	
	var Drag = {
	
	  obj : null,
	
	  init : function(oRoot,head, minX, maxX, minY, maxY)
	  {
	    var o = head;
		
		o.onmousedown  = Drag.start;
	    o.root = oRoot;
	
	    if (isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
	    if (isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
	
	    o.minX  = typeof minX != 'undefined' ? minX : null;
	    o.minY  = typeof minY != 'undefined' ? minY : null;
	    o.maxX  = typeof maxX != 'undefined' ? maxX : null;
	    o.maxY  = typeof maxY != 'undefined' ? maxY : null;
	
	    o.root.onDragStart  = new Function();
	    o.root.onDragEnd  = new Function();
	    o.root.onDrag    = new Function();
	  },
	
	  start : function(e)
	  {
	    var o = Drag.obj = this;
	    e = Drag.fixE(e);
	    o.root.style.zIndex = "200";
	    var y = parseInt(o.root.style.top);
	    var x = parseInt(o.root.style.left);
	    o.root.onDragStart(x, y);
	
	    o.lastMouseX  = e.clientX;
	    o.lastMouseY  = e.clientY;
		
		if (o.minX != null)  o.minMouseX  = e.clientX - x + o.minX;
		if (o.maxX != null)  o.maxMouseX  = o.minMouseX + o.maxX - o.minX;
		
		if (o.minY != null)  o.minMouseY  = e.clientY - y + o.minY;
		if (o.maxY != null)  o.maxMouseY  = o.minMouseY + o.maxY - o.minY;
	
	    document.onmousemove  = Drag.drag;
	    document.onmouseup    = Drag.end;
	
	    return false;
	  },
	
	  drag : function(e)
	  {
	    e = Drag.fixE(e);
	    var o = Drag.obj;
	
	    var ey  = e.clientY;
	    var ex  = e.clientX;
	    var y = parseInt(o.root.style.top);
	    var x = parseInt(o.root.style.left);
	    var nx, ny;
	
	    if (o.minX != null) ex = Math.max(ex, o.minMouseX);
	    if (o.maxX != null) ex = Math.min(ex, o.maxMouseX);
	    if (o.minY != null) ey = Math.max(ey, o.minMouseY);
	    if (o.maxY != null) ey = Math.min(ey, o.maxMouseY);
	
	    nx = x + ((ex - o.lastMouseX) * 1);
	    ny = y + ((ey - o.lastMouseY) * 1);
	
	    Drag.obj.root.style["left"] = nx + "px";
	    Drag.obj.root.style["top"] = ny + "px";
	    Drag.obj.lastMouseX  = ex;
	    Drag.obj.lastMouseY  = ey;
	
	    Drag.obj.root.onDrag(nx, ny);
	    return false;
	  },
	
	  end : function()
	  {
	    document.onmousemove = null;
	    document.onmouseup   = null;
	    Drag.obj.root.onDragEnd(  parseInt(Drag.obj.root.style["left"]), 
	                  parseInt(Drag.obj.root.style["top"]));
	    Drag.obj.root.style.zIndex = "199";
	    Drag.obj = null;
	  },
	
	  fixE : function(e)
	  {
	    if (typeof e == 'undefined') e = window.event;
	    if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
	    if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
	    return e;
	  }
	};
	
	// inicializa a popup
	this.init = function()
	{
		popup.position(wTop, wLeft);
		Drag.init(win, header, minX, maxX, minY, maxY);
	};
};