//IVIE BOX V. 1.0
(function($) 
{
	$.Ivie_box= {
		active : []
	};
	$.fn.Ivie_box = function(settings)
	{
		settings = $.extend({}, $.fn.Ivie_box.defaults, settings);
		var rf = function() { return false; };
		
		var modal_html = '<div class="ivie_box"><div class="header"><!--<div class="title">Modal Title</div>--><div class="close"><a href="#"></a></div></div><div class="content"></div></div><div class="overlay"></div>';

		$.fn.Ivie_box.close();
		
		if($(document).find(".ivie_box").size()==0)
		{
			$(document).find("body").append(modal_html);	
			//alert("exist en el body");
		}
		if($(document).find(".ivie_box").size()>0)
		{
			//alert("testing");
			var ivie_box_modal = $(".ivie_box");
			/*
			if(($.browser.msie && $.browser.version==7) ||($.browser.msie && $.browser.version==8))
			{
				//
			}
			else{
				ivie_box_modal.corner();
				ivie_box_modal.find(".header").corner("top");
			}*/
			presset_ivie_box_modal(ivie_box_modal, settings.title_modal, settings.close_button, settings.loading_img, settings.close_button, settings.on_close_function);
			load_modal_with_ajax(settings.ajax_url, settings.post_data, settings.method, ivie_box_modal, settings.div_show, settings.affter_load_function, settings.loading_img, settings.margin_top, settings.margin_right, settings.show_loading, settings.width, settings.height, settings.move_top_size);
			close_button_key_events(ivie_box_modal, settings.close_button, settings.on_close_function);
			//
		}
		else
			return false;
		
	}
	$.fn.Ivie_box.loading_modal=function(settings)
	{
		var loading_div	= "<div class='loading_modal' style='position:absolute, top:0; right:0; display:none;'></div>";
		if($(document).find("body").find(".loading_modal").size==0)
		{
			//alert("joel");
			show_loading(false,1)
			//$(document).find("body").append(loading_div);
			//loading_modal
			//general_loading(1,'');
		}
		else{
			show_loading(false,0)
			//$(document).find("body").find(".loading_modal").remove();
			//general_loading(0,'');
		}
		set_loading($.fn.Ivie_box.defaults.loading_img, "", "");
	}
	$.fn.Ivie_box.close = function()
	{
		var modal = $(document).find(".ivie_box");
		if(modal.size()>0)
			close_modal_ivie_box(modal, true, $.fn.Ivie_box.defaults.on_close_function);
		else
			return false;
	}
	
	$.fn.Ivie_box.relocate = function()
	{
		var ivie_box_modal = $(".ivie_box");
		if(ivie_box_modal.is(":visible"))
			relocate_modal(ivie_box_modal,0,0,0);
	}
	
	$.fn.Ivie_box.defaults = 
		{
			close_button		:true,
			ajax_load			:true,	//by default for now
			ajax_url			:"",
			post_data			:{},
			method				:"GET",
			title_modal			:"Ivie Box",
			margin_top			:"",
			margin_right		:"",
			div_show			:"",
			affter_load_function:"",
			loading_img			:"<img src='"+BASE_URL+"resources/basic/images/modal_loading.gif' border='0' width='32' />",
			show_loading		:true,
			width				:400,
			height				:"",
			move_top_size		:0,
			on_close_function	: ""
		};
	function close_button_key_events(container_general, close_b, on_close_function)
	{
	/*	if(close_b)
		{*/
			$(document).unbind("keydown").keydown(function(e)
		 	{
		      if (e.keyCode == 27) 
			  {
					close_modal_ivie_box(container_general, close_b, on_close_function);
					return false;
			  }
			});
		//}
	}
	function close_modal_ivie_box(container_general, close_b, on_close_function)
	{
		
			$(".overlay").remove();
			relocate_modal(container_general, "1px", "1px", 0);
			container_general.hide().css({"opacity"	: 0}).find(".content").html("");
			if($(document).find(".loading_modal").size()>0) {
				//$(document).find(".loading_modal").remove();
				//general_loading(0,'');
			}
			if($(document).find(".ivie_box").size()!=0) {
				//$(document).find(".ivie_box").remove();
				//general_loading(0,'');
			}
			$(document).find(".ivie_box").remove();
			//alert(on_close_function);
			if(typeof(on_close_function)=="function")
				on_close_function();
	}
	function presset_ivie_box_modal(container_general, title, close_modal, img, close_b, on_close_function)
	{
		container_general.find(".header .title").html(title);
		if(close_modal)
		{
			container_general.find(".header .close").show();
			events_close_modal(container_general.find(".header .close a"), container_general, close_b, on_close_function);
		}
		else
		{
			container_general.find(".header .close").hide();
			container_general.find(".header .close a").unbind("click");
		}
		if($(document).find(".loading_modal").size()==0)
		{/*
			var loading_div	= "<div class='loading_modal' style='position:absolute, top:0; right:0; display:none;'></div>";
			$(document).find("body").append(loading_div);
			$(document).find(".loading_modal").html(img);*/
			
			//general_loading(1,'');
		}
	}
	function events_close_modal(a, container_general, close_b, on_close_function)
	{
		a.click(function()
		{
			close_modal_ivie_box(container_general, close_b, on_close_function);
			return false;
		});
	}
	function load_modal_with_ajax(url, post_data, method, container_general,alternative_div, on_affter_function, loading, top, right, show_loading, w, h, more_top_size)
	{		
			//alert(url+"/"+Math.random());
			if(url!="")
			{
				$.ajax({
				url: url+"/"+parseInt(Math.random(10)),
				type: method,
				data: post_data,
				beforeSend: function()
				{
					if(show_loading)
						set_loading_system(false,1);
				},
				async: true,
				success: function(res)
				{
					//$.scrollTo('#logo', 800);
					if( on_affter_function != undefined && on_affter_function != '' ) {
						on_affter_function();
					}
					
					if(show_loading)
						set_loading_system(false,0);
					container_general.find(".content").html(res);
					
					show_modal_ivie_box(container_general, top, right, w, h, more_top_size);
										
					//alert(container_general.find("#notesList").html());
					
					/*if(typeof on_affter_function == "function")
						on_affter_function();
					*/
				},
				error:function(x, txt, e)
				{
					overlay_modal_error();
				}
			});
			}
			else
			{
				show_modal_ivie_box(container_general, top, right, w, h, more_top_size);
				container_general.find(".content").html($(alternative_div).html());
				
				if( on_affter_function != undefined && on_affter_function != '' ) {
					on_affter_function();
				}
			}
	}
	function set_loading(img, top, right)
	{/*
		var new_top 	= $(document).height();
		var new_right	= $(document).width();
		var width_box	= $(document).find(".loading_modal img").attr("width");
		var height_box	= $(document).find(".loading_modal").height();	
		
			var new_right_data = (new_right/2) - (width_box/2);
			var new_top_data = (new_top/2) - (height_box/2);
		
		if(top!="", right!='')
			$(document).find(".loading_modal").css({right:right, top:top+"px",position:"absolute"});
		else
			$(document).find(".loading_modal").css({right:new_right_data+"px", top:new_top_data+"px",position:"absolute"});
		$(document).find(".loading_modal").show();
		*/
		//general_loading(1,'');
	}
	function remove_loading()
	{
		//$(document).find(".loading_modal").remove();
		//general_loading(0,'');
	}
	function overlay_modal_error(status){
		//container_general.find(".content").html("error while load this modal");
	}
	
	//main functionality
	function show_modal_ivie_box(div, top, right, w, h, more_top_size)
	{
		if(h!="")
			div.css({width:w+34+"px", height:h+86+"px"});
		else
			div.css({width:w+34+"px", height:"auto"});
		/* end */
		$(".overlay").show();
		if(top!="" && right!="")
		{
			relocate_modal(div, top, right, 0);
		}
		else
		{
			relocate_modal(div,0,0,more_top_size);
			$(window).resize(function()
			{
				if(div.is(":visible"))
					relocate_modal(div,0,0,more_top_size);
			});
		}
	}
	function verify_resize(div, more_top_size)
	{
		var new_top 	= $(window).height();
		var new_right	= $(window).width();
		
		var width_box	= div.width();
		var height_box	= div.height();	
		
		
		
			var new_right_data = (new_right/2) - (width_box/2);
			var new_top_data = ((new_top/2) - (height_box/2))-more_top_size;
			
		var new_ui	= {top_align:new_top_data<0?0:new_top_data+"px", right_align:new_right_data+"px"};
		return new_ui;
		
	}
	function relocate_modal(div, top, right, more_top_size)
	{
		//alert(more_top_size);
		//div.css({top:verify_resize(div).top_align, left:verify_resize(div).left_align});
		/*
		div.animate(
		{
			"right"		: right==0?verify_resize(div).right_align:right,
			"top"		: top==0?verify_resize(div).top_align:top,
			"opacity"	: 1
		},
		{
			queue:false, 
			duration:1000, 
			easing:"bounceEaseOut"
		}).show();
		*/
		//alert("testing...");
/*		var size_top 	= verify_resize(div,more_top_size).top_align;
		var size_right	= verify_resize(div,0).right_align;*/
		div.css({top:top==0?verify_resize(div,more_top_size).top_align:top, right:right==0?verify_resize(div,0).right_align:right, opacity:1}).fadeIn("slow");
	}
	
})(jQuery);
