/*

 * KIMONO 3.2.0 - New Javascript

 *

 * Copyright (c) 2008 Maximiliano Andrés García (kimono.com.ar)

 * GPL LICENSE

 *

 * $Date: 2009-09-11 00:00:00 -0300

 *

 */



/*

 * 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;

	var img_width;

	var img_height;

	var mensaje = "";



	/*

	 * 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=="") {

			img_width = true;

			obj.width = 600;	

		}

		if(obj.height==null || obj.height=="") {

			img_height = true;

			obj.height = 400;

		}

		if(obj.labelClose==null) {

			obj.labelClose = "Close";

		}

		if(obj.labelLoading==null) {

			obj.labelLoading = "Loading...";

		}

		if(obj.scrolling==null)	{

			obj.scrolling = "auto";

		}

		if(obj.duration==null) {

			obj.duration = 1500;

		}

		if(obj.opacity==null) {

			obj.opacity = 0.40;

		}

	}



	/*

	 * Creo la etiqueta de forma dinamica

	 */

	$("body").append("<div id=\""+ etiqueta +"\"></div>");

	$("#"+etiqueta).fadeTo("fast",obj.opacity, function(){

		$(this).show();

	});



	if(obj.url!=null) {

		exten = (obj.url.substring(obj.url.lastIndexOf("."))).toLowerCase();

	}else{

		exten = ".msg";

		mensaje = "Parameters required: url";

		obj.width = 400;

		obj.height = 50;

	}



	arr_img = [".gif", ".jpg", ".png"];

	arr_flash = [".swf", ".flv"];



	/*

	 * Cargamos la imagen en el contenedor

	 */

	if ($.inArray(exten, arr_img)>=0) {

		img = new Image();

		img.src = obj.url;

		if(img_width) {

			obj.width = img.width;

		}

		if(img_height) {

			obj.height = img.height;

		}

		content = "<img src=\""+obj.url+"\" width=\""+obj.width+"\" height=\""+obj.height+"\" border=\"0\">";

	/*

	 * Cargamos el .swf en el contenedor

	 */

	}else if ($.inArray(exten, arr_flash)>=0) {

		content = "<script type=\"text/javascript\">";

		content = content + "swfobject.embedSWF(\""+obj.url+"\", \"myContent\", \""+obj.width+"\", \""+obj.height+"\", \"9.0.0\", \"#f0f0f0\")";

		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;

	/*

	 * Error de parametros requeridos

	 */

	}else{

		content = "";

	}



	/*

	 * Armado de plantilla en donde se muestran los contenidos

	 */

	var posiscionCerrar = parseInt(obj.width) - 20;

	plantilla = "<div id=\"ki_window\">";

	plantilla = plantilla + "<div id=\"ki_close\" style=\"left: "+posiscionCerrar+"px;\"><img src='images/cerrar.jpg' /></div>";

	plantilla = plantilla + "<div id=\"ki_preload\">"+obj.labelLoading+"</div>";

	plantilla = plantilla + "<div id=\"ki_content\" style=\"display:none\">"+content+"</div>";

	plantilla = plantilla + "<div id=\"ki_message\" style=\"display:none\">"+mensaje+"</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");

				$("#ki_message").css("display", "block");

			});

		}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);

	});



}
