/*
 * KIMONO 3.0.0 - New Javascript
 *
 * Copyright (c) 2008 Rolando Romero (com.ar)
 * GPL LICENSE
 *
 * $Date: 2009-05-29 00:00:00 -0200
 *
 */

/*
 * Function en el cual se carga una ventana fullscreen
 * con el contenido dentro de un iframe, div, img, etc.
 */
getWindow = function(obj)
{
	var plantilla;
	var etiqueta;
	var posi_close;
	var ban;

	/*
	 * Cargo la ventana, tapando el fondo
	 */

	/*
	 * Seteo de etiqueta(DIV) principal en donde se cargara la ventana
	 */
	etiqueta = "ki_screen";

	/*
	 * Borra la etiqueta(DIV) principal y ki_window si ya existe
	 */
	$("#"+etiqueta).remove();
	$("#ki_window").remove();

	/*
	 * Obtenemos el objeto
	 */
	if (obj!=null) {
		$.extend({
			ext: obj
		});
		if(obj.width==null || obj.width=="") {
			obj.width = 600;	
		}
		if(obj.height==null || obj.height=="") {
			obj.height = 400;
		}
		if(obj.labelClose==null || obj.labelClose=="") {
			obj.labelClose = " ";
		}
		if(obj.labelLoading==null || obj.labelLoading=="") {
			obj.labelLoading = " ";
		}
		if(obj.scrolling==null)	{
			obj.scrolling = "auto";
		}
		if(obj.duration==null) {
			obj.duration = 1500;
		}
	}

	/*
	 * Creo la etiqueta de forma dinamica
	 */
	$("body").append("<div id=\""+ etiqueta +"\"></div>");

	if(obj.url!=null) {
		ext = (obj.url.substring(obj.url.lastIndexOf("."))).toLowerCase();
	}else{
		ext = ".msg";
	}

	arr_img = [".gif", ".jpg", ".png"];
	arr_flash = [".swf", ".flv"];

	/*
	 * Cargamos la imagen en el contenedor
	 */
	if ($.inArray(ext, arr_img)>=0) {
		content = "<img src=\""+obj.url+"\" width=\""+obj.width+"\" height=\""+obj.height+"\" border=\"0\">";
	/*
	 * Cargamos el .swf en el contenedor
	 */
	}else if ($.inArray(ext, arr_flash)>=0) {
		content = "<script type=\"text/javascript\">";
		content = content + "swfobject.embedSWF(\""+obj.url+"\", \"myContent\", \""+obj.width+"\", \""+obj.height+"\", \"9.0.0\")";
		content = content + "</script>";
		content = content + "<div id=\"myContent\">Code alt.</div>";
	/*
	 * Cargamos el iframe en el contenedor
	 */
	}else if (obj.url!=null) {
		content = "<iframe src=\""+obj.url+"\" marginwidth=\"0\" marginheight=\"0\" hspace=\"0\" allowtransparency=\"false\" frameborder=\"0\" width=\""+obj.width+"\" height=\""+obj.height+"\" scrolling=\""+obj.scrolling+"\"></iframe>";
	/*
	 * Mensajes personalizado
	 */	
	}else if (obj.message!=null) {
		content = obj.message;
	/*
	 * Mensajes de parametros requeridos
	 */
	}else{
		content = "Parameters required: url - width - height";
	}

	/*
	 * Armado de plantilla en donde se muestran los contenidos
	 */
	plantilla = "<div id=\"ki_window\">";
	plantilla = plantilla + "<div id=\"ki_close\">"+obj.labelClose+"</div>";
	plantilla = plantilla + "<div id=\"ki_preload\">"+obj.labelLoading+"</div>";
	plantilla = plantilla + "<div id=\"ki_content\" style=\"display:none\">"+content+"</div>";
	plantilla = plantilla + "</div>";

	/*
	 * Cargo un div fullscreen, tapando el fondo
	 */
	$("#"+etiqueta).fadeIn("slow");

	/*
	 * Cargo la ventana, sobre el centro de la pagina
	 */
	$("body").append(plantilla);

	/*
	 * Activo la ventana para mover
	 */	
	if(obj.draggable!=null) {
		$("#ki_window").css({"cursor":"move"});
		$("#ki_window").draggable();
	}

	/*
	 * Cierro ventana desde el boton cerrar
	 */
	$("#ki_close").click(function() {
		$("#"+etiqueta).fadeOut("slow");
		$("#"+etiqueta).html("");
		$("#ki_window").fadeOut("slow");
		$("#ki_window").remove();
	});

	/*
	 * Cierro ventana haciendo clic en el fondo
	 */
	$("#"+etiqueta).click(function() {
		$("#"+etiqueta).fadeOut("show");
		$("#"+etiqueta).html("");
		$("#ki_window").fadeOut("slow");
		$("#ki_window").remove();
	});

	/*
	 * Seteo de coordenadas del boton cerrar
	 */
	posi_close = (($("#"+etiqueta).height()/2)-(obj.height/2))-10;

	getWindow.posicionar = function(ini, content)
	{
		/*
	 	 *	Saco el ancho y el alto de la ventana
	 	 */
		var winWidth = $(window).width();
		var winHeight  = $(window).height();

		var docWidth = $(document).width();
		var docHeight  = $(document).height();

		window_x = ((winWidth/2)-(obj.width/2));

		var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement: document.body
		var dsoctop=document.all? iebody.scrollTop: pageYOffset
		window_y = ((winHeight/2)-(obj.height/2)) + dsoctop;

		if (ini==true) {
			/*
		 	 * Configuracion de CSS para XXX
		 	 */
			$("#ki_window").css({"width":"20px","height":"20px"});
			$("#ki_window").css("top",""+(($("#"+etiqueta).height()/2)-20)+"px");
			$("#ki_window").css("left",""+(($("#"+etiqueta).width()/2)-20)+"px");

			/*
		 	 * Controlador de animacion de ventana en inicio
		 	 */
			$("#ki_window").animate({ 
				top: ""+window_y+"px",
				left: ""+window_x+"px",
				width: ""+obj.width+"px",
				height: ""+obj.height+"px"
			}, obj.duration, '', function() {
				$("#ki_preload").fadeOut("show");
				$("#ki_content").fadeIn("show");
			});
		}else{
			/*
		 	 * Controlador de animacion cuando se hace scroll
		 	 */
			$("#ki_window").animate({ 
				top: ""+window_y+"px",
				left: ""+window_x+"px",
				width: ""+obj.width+"px",
				height: ""+obj.height+"px"
			}, 100 );
		}

		$("#"+etiqueta).css("height",""+docHeight+"px");
	}

	/*
	 * Inicia la animacion del contenedor al inicio
	 */
	getWindow.posicionar(true, content);

	/*
	 * Inicia la animacion cuando se usa el  scroll
	 */
	$(window).bind("resize scroll", function(e){
		getWindow.posicionar(false, content);
	});

}